From 8c603d6ebd1e78829f2e9547dea056d6ab62aafe Mon Sep 17 00:00:00 2001 From: Peter Leurs Date: Fri, 15 Aug 2025 23:09:48 +0200 Subject: [PATCH] add monitoring scripts --- appinfo.yml | 29 +++++++++++++++++++++++++++++ monitor-test.sh.j2 | 16 ++++++++++++++++ monitor.sh.j2 | 18 ++++++++++++++++++ monitor.socket.j2 | 9 +++++++++ monitor@.service.j2 | 15 +++++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 monitor-test.sh.j2 create mode 100644 monitor.sh.j2 create mode 100644 monitor.socket.j2 create mode 100644 monitor@.service.j2 diff --git a/appinfo.yml b/appinfo.yml index 6eb491a..d16b1be 100644 --- a/appinfo.yml +++ b/appinfo.yml @@ -6,6 +6,16 @@ templatefiles: - src: backupmanager.j2 dest: ~/backupmanager mode: "0755" + - src: monitor@.service.j2 + dest: ~/.config/systemd/user/monitor@.service + - src: monitor.socket.j2 + dest: ~/.config/systemd/user/monitor.socket + - src: monitor.sh.j2 + dest: ~/monitor.sh + mode: "0755" + - src: monitor-test.sh.j2 + dest: ~/monitor-test.sh + mode: "0755" backuptype: none configdefinition: "$id": "backup manager config" @@ -72,3 +82,22 @@ configdefinition: description: Repositories where the latest backup is older then timeout days will give a warning that there are no recent backups required: - repos +exports: + monitoring: + checks: + - name: main + message: Backup manager activated + interval: 3600 + type: string + okvalue: active + - name: lastrun + message: Errors last run + interval: 3600 + type: string + okvalue: "" + - name: lastrunrecent + message: Backup manager did not run recently + interval: 3600 + type: string + okvalue: "OK" + diff --git a/monitor-test.sh.j2 b/monitor-test.sh.j2 new file mode 100644 index 0000000..a1a125e --- /dev/null +++ b/monitor-test.sh.j2 @@ -0,0 +1,16 @@ +#!/bin/bash + +# Usage: ./execute_command_client.sh + +if [ $# -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi + +command="$1" + +# Connect to socket and send command +output=$(nc -U {{ statedir }}/{{ name }}.monitoring <<< "$command") + +# Print output +echo "$output" diff --git a/monitor.sh.j2 b/monitor.sh.j2 new file mode 100644 index 0000000..0e8e932 --- /dev/null +++ b/monitor.sh.j2 @@ -0,0 +1,18 @@ +#!/bin/bash +read -r command +case $command in + main) + systemctl is-active --user backup-manager.timer + ;; + lastrun) + cat {{ homedir }}/.backupmanager.errors + ;; + lastrunrecent) + [ -f {{ homedir }}/.backupmanager.errors ] && [ $(find {{ homedir }}/.backupmanager.errors -mtime -2) ] && echo "OK" || echo "outdated" + ;; + *) + echo "Parameter unknown" + ;; +esac + + diff --git a/monitor.socket.j2 b/monitor.socket.j2 new file mode 100644 index 0000000..17163d1 --- /dev/null +++ b/monitor.socket.j2 @@ -0,0 +1,9 @@ +[Unit] +Description=Execute Command Socket + +[Socket] +ListenStream={{ statedir }}/{{ name }}.monitoring +Accept=yes + +[Install] +WantedBy=sockets.target diff --git a/monitor@.service.j2 b/monitor@.service.j2 new file mode 100644 index 0000000..55ee896 --- /dev/null +++ b/monitor@.service.j2 @@ -0,0 +1,15 @@ +[Unit] +Description=Execute Command Service +After=network.target +Requires=monitor.socket + +[Service] +Type=simple +ExecStart={{ homedir }}/monitor.sh +StandardInput=socket +StandardOutput=socket +TimeoutStopSec=5 +RuntimeMaxSec=10 + +[Install] +WantedBy=sockets.target \ No newline at end of file