Repository navigation
Expand file tree
/
Copy pathcheck_gcode_monitor.sh
More file actions
79 lines (66 loc) · 2.27 KB
/
Copy pathcheck_gcode_monitor.sh
File metadata and controls
79 lines (66 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# Health check for gcode-monitor systemd service.
# Usage: ./check_gcode_monitor.sh
# Optional cron entry (runs every 15 minutes):
# */15 * * * * /home/your_username/Claude_Code/Send_To_Printer/check_gcode_monitor.sh
set -euo pipefail
SERVICE_NAME="gcode-monitor.service"
LOG_FILE="${HOME}/.gcode_sync.log"
MAX_LOG_MINUTES=30
timestamp() {
date +"%Y-%m-%d %H:%M:%S"
}
print_header() {
echo "[$(timestamp)] $1"
}
send_alert() {
local title="$1"
local body="$2"
local urgency="${3:-critical}"
# Desktop notification (works if DISPLAY/DBUS available)
if command -v notify-send &>/dev/null; then
notify-send --urgency="$urgency" "$title" "$body" 2>/dev/null || true
fi
print_header "ALERT: $title - $body"
}
check_service_status() {
if systemctl is-active --quiet "${SERVICE_NAME}"; then
print_header "Service ${SERVICE_NAME} is active."
else
print_header "ERROR: Service ${SERVICE_NAME} is not active."
send_alert "GCode Monitor Down" "Service ${SERVICE_NAME} is not active"
systemctl status "${SERVICE_NAME}" --no-pager
exit 1
fi
}
check_recent_logs() {
if [[ ! -f "${LOG_FILE}" ]]; then
print_header "WARNING: Log file ${LOG_FILE} not found."
return
fi
log_mtime=$(stat -c %Y "${LOG_FILE}")
now=$(date +%s)
diff_minutes=$(( (now - log_mtime) / 60 ))
if (( diff_minutes > MAX_LOG_MINUTES )); then
print_header "WARNING: Log file has not been updated in ${diff_minutes} minutes."
else
print_header "Log file updated ${diff_minutes} minutes ago."
fi
print_header "Last five log entries:"
tail -n 5 "${LOG_FILE}"
}
check_recent_errors() {
print_header "Scanning journal for recent errors..."
if journalctl -u "${SERVICE_NAME}" --since "30 minutes ago" | grep -qi "error"; then
print_header "ERROR: Recent errors detected in journal."
send_alert "GCode Monitor Errors" "Recent errors detected in journal for ${SERVICE_NAME}"
journalctl -u "${SERVICE_NAME}" --since "30 minutes ago"
exit 1
else
print_header "No errors detected in journal over the last 30 minutes."
fi
}
check_service_status
check_recent_logs
check_recent_errors
print_header "Health check completed successfully."