feat: add CUPS watchdog timers

This commit is contained in:
2026-06-09 14:58:13 +08:00
parent 2a688d2514
commit 00d512f9d9
11 changed files with 668 additions and 0 deletions

View File

@@ -1109,6 +1109,109 @@ EOF
DRIVER_MANAGER_PASSWORD="$admin_pass"
}
# 安装网络和打印服务守护脚本
install_watchdogs() {
info "安装 CUPS 守护脚本..."
apt install -y iproute2 iputils-ping curl 2>/dev/null || warn "守护脚本依赖安装失败,继续尝试使用系统已有命令"
local install_dir="/opt/cups-watchdog"
local config_dir="/etc/cups-watchdog"
local log_dir="/var/log/cups-watchdog"
local source_dir=""
local tmp_dir=""
mkdir -p "$install_dir" "$config_dir" "$log_dir"
SCRIPT_DIR="$(cd "$(dirname "$0")" 2>/dev/null && pwd)" || SCRIPT_DIR=""
if [ -n "$SCRIPT_DIR" ] && [ -d "$SCRIPT_DIR/watchdog" ]; then
source_dir="$SCRIPT_DIR/watchdog"
info "从本地复制守护脚本"
else
info "从仓库下载守护脚本..."
tmp_dir=$(mktemp -d)
if download_repo_archive "$tmp_dir/repo.zip"; then
unzip -q "$tmp_dir/repo.zip" -d "$tmp_dir" 2>/dev/null
source_dir=$(find "$tmp_dir" -type d -name "watchdog" 2>/dev/null | head -1)
fi
fi
if [ -z "$source_dir" ] || [ ! -d "$source_dir" ]; then
[ -n "$tmp_dir" ] && rm -rf "$tmp_dir"
warn "未找到守护脚本目录,跳过安装"
return 1
fi
cp -f "$source_dir/network-watchdog.sh" "$install_dir/"
cp -f "$source_dir/print-watchdog.sh" "$install_dir/"
chmod +x "$install_dir/network-watchdog.sh" "$install_dir/print-watchdog.sh"
cp -f "$source_dir/cups-network-watchdog.service" /etc/systemd/system/
cp -f "$source_dir/cups-network-watchdog.timer" /etc/systemd/system/
cp -f "$source_dir/cups-print-watchdog.service" /etc/systemd/system/
cp -f "$source_dir/cups-print-watchdog.timer" /etc/systemd/system/
[ -n "$tmp_dir" ] && rm -rf "$tmp_dir"
local current_ip=$(get_ip)
local current_gateway=$(get_gateway)
local current_interface=$(get_interface)
local current_netmask=$(get_netmask)
local current_dns=$(get_dns)
current_dns="${current_dns:-114.114.114.114 223.5.5.5}"
local ping_targets="223.5.5.5 114.114.114.114"
if [ -n "$current_gateway" ]; then
ping_targets="$current_gateway $ping_targets"
fi
if [ ! -f "$config_dir/network-watchdog.conf" ]; then
cat > "$config_dir/network-watchdog.conf" << EOF
# CUPS network watchdog config.
# Edit these values when you want to manually switch back to a fixed IP:
# /opt/cups-watchdog/network-watchdog.sh static
INTERFACE="$current_interface"
STATIC_IP="$current_ip"
STATIC_PREFIX="$current_netmask"
STATIC_GATEWAY="$current_gateway"
STATIC_DNS="$current_dns"
PING_TARGETS="$ping_targets"
FAIL_THRESHOLD=3
DHCP_AFTER_FAILURE=1
LOG_FILE="/var/log/cups-watchdog/network.log"
EOF
chmod 600 "$config_dir/network-watchdog.conf"
else
info "保留现有网络守护配置: $config_dir/network-watchdog.conf"
fi
if [ ! -f "$config_dir/print-watchdog.conf" ]; then
cat > "$config_dir/print-watchdog.conf" << EOF
# CUPS print watchdog config.
SERVICES="cups avahi-daemon cups-driver-manager"
CUPS_URL="http://127.0.0.1:631/"
CHECK_CUPS_HTTP=1
CHECK_LPSTAT=1
COMMAND_TIMEOUT=8
FAIL_THRESHOLD=2
RESTART_COOLDOWN=60
LOG_FILE="/var/log/cups-watchdog/print.log"
EOF
chmod 600 "$config_dir/print-watchdog.conf"
else
info "保留现有打印守护配置: $config_dir/print-watchdog.conf"
fi
systemctl daemon-reload
systemctl enable --now cups-network-watchdog.timer
systemctl enable --now cups-print-watchdog.timer
success "守护脚本安装完成"
info "网络守护配置: $config_dir/network-watchdog.conf"
info "打印守护配置: $config_dir/print-watchdog.conf"
}
# 安装虚拟PDF打印机用于测试连接
install_pdf_printer() {
info "安装虚拟 PDF 打印机..."
@@ -1333,6 +1436,8 @@ main() {
((step++))
fi
echo " $step. 启动并设置开机自启"
((step++))
echo " $step. 安装网络和打印服务守护脚本"
echo ""
read -p "是否继续? [Y/n]: " confirm < /dev/tty
confirm=${confirm:-Y}
@@ -1425,6 +1530,9 @@ main() {
systemctl enable avahi-daemon
success "Avahi 服务已启动并设置开机自启"
# 8. 安装守护脚本
install_watchdogs
# 更新IP地址如果配置了静态IP
LOCAL_IP=$(get_ip)
@@ -1463,6 +1571,13 @@ main() {
echo -e " ${GREEN} ${NC} PDF 输出目录: /var/spool/cups-pdf/"
echo ""
fi
if systemctl is-enabled --quiet cups-network-watchdog.timer 2>/dev/null && \
systemctl is-enabled --quiet cups-print-watchdog.timer 2>/dev/null; then
echo -e " ${GREEN}[守护脚本]${NC} 已启用网络和打印服务自动恢复"
echo -e " ${GREEN} ${NC} 配置目录: /etc/cups-watchdog/"
echo -e " ${GREEN} ${NC} 日志目录: /var/log/cups-watchdog/"
echo ""
fi
echo -e " ${YELLOW}下一步:${NC}"
echo " 1. 用浏览器打开上面的地址"
if lpstat -p PDF 2>/dev/null | grep -q "PDF"; then
@@ -1508,6 +1623,7 @@ uninstall() {
echo " - CUPS-PDF 虚拟打印机"
echo " - 中文界面模板"
echo " - 驱动管理器"
echo " - 网络和打印服务守护脚本"
echo " - 所有打印任务和配置"
echo ""
read -p "确定要卸载吗? [y/N]: " confirm < /dev/tty
@@ -1526,9 +1642,13 @@ uninstall() {
systemctl stop cups 2>/dev/null || true
systemctl stop avahi-daemon 2>/dev/null || true
systemctl stop cups-driver-manager 2>/dev/null || true
systemctl stop cups-network-watchdog.timer cups-network-watchdog.service 2>/dev/null || true
systemctl stop cups-print-watchdog.timer cups-print-watchdog.service 2>/dev/null || true
systemctl disable cups 2>/dev/null || true
systemctl disable avahi-daemon 2>/dev/null || true
systemctl disable cups-driver-manager 2>/dev/null || true
systemctl disable cups-network-watchdog.timer 2>/dev/null || true
systemctl disable cups-print-watchdog.timer 2>/dev/null || true
# 卸载软件包
info "卸载软件包..."
@@ -1546,7 +1666,14 @@ uninstall() {
rm -rf /usr/share/cups/templates-zh
rm -rf /usr/share/cups/templates-en-backup
rm -rf /opt/cups-driver-manager
rm -rf /opt/cups-watchdog
rm -rf /etc/cups-watchdog
rm -rf /var/log/cups-watchdog
rm -f /etc/systemd/system/cups-driver-manager.service
rm -f /etc/systemd/system/cups-network-watchdog.service
rm -f /etc/systemd/system/cups-network-watchdog.timer
rm -f /etc/systemd/system/cups-print-watchdog.service
rm -f /etc/systemd/system/cups-print-watchdog.timer
systemctl daemon-reload 2>/dev/null || true
# 清理依赖