From d4569055778711d02609306d0f92b4d9930b4846 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 15 Apr 2023 14:31:18 +0200 Subject: [PATCH] Implemented systemctl error checker --- .../native-journalctl-health-check/README.md | 2 ++ .../files/journalctl-health-check.sh | 10 ++++++++++ .../files/journalctl-health-check.timer | 8 ++++++++ .../handlers/main.yml | 6 ++++++ .../meta/main.yml | 2 ++ .../tasks/main.yml | 19 +++++++++++++++++++ .../journalctl-health-check.service.j2 | 7 +++++++ .../vars/main.yml | 1 + .../{readme.md => README.md} | 2 +- roles/native-journalctl/meta/main.yml | 2 ++ 10 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 roles/native-journalctl-health-check/README.md create mode 100644 roles/native-journalctl-health-check/files/journalctl-health-check.sh create mode 100644 roles/native-journalctl-health-check/files/journalctl-health-check.timer create mode 100644 roles/native-journalctl-health-check/handlers/main.yml create mode 100644 roles/native-journalctl-health-check/meta/main.yml create mode 100644 roles/native-journalctl-health-check/tasks/main.yml create mode 100644 roles/native-journalctl-health-check/templates/journalctl-health-check.service.j2 create mode 100644 roles/native-journalctl-health-check/vars/main.yml rename roles/native-journalctl/{readme.md => README.md} (55%) create mode 100644 roles/native-journalctl/meta/main.yml diff --git a/roles/native-journalctl-health-check/README.md b/roles/native-journalctl-health-check/README.md new file mode 100644 index 00000000..8a91c4ac --- /dev/null +++ b/roles/native-journalctl-health-check/README.md @@ -0,0 +1,2 @@ +# journalctl-health-check +Checks journalctl for error messages \ No newline at end of file diff --git a/roles/native-journalctl-health-check/files/journalctl-health-check.sh b/roles/native-journalctl-health-check/files/journalctl-health-check.sh new file mode 100644 index 00000000..15e5df8e --- /dev/null +++ b/roles/native-journalctl-health-check/files/journalctl-health-check.sh @@ -0,0 +1,10 @@ +#!/bin/sh +echo "Checking journalctl for error messages..." +journalctl_errors="$(journalctl --since '1 day ago' --no-pager | grep -i 'error')" +if [ ! -z "$journalctl_errors" ] +then + echo "Some errors where found: $journalctl_errors" + exit 1 +fi +echo "All docker containers are healthy." +exit 0 \ No newline at end of file diff --git a/roles/native-journalctl-health-check/files/journalctl-health-check.timer b/roles/native-journalctl-health-check/files/journalctl-health-check.timer new file mode 100644 index 00000000..ec27e6c7 --- /dev/null +++ b/roles/native-journalctl-health-check/files/journalctl-health-check.timer @@ -0,0 +1,8 @@ +[Unit] +Description=starts journalctl-health-check.service + +[Timer] +OnCalendar=00:00 + +[Install] +WantedBy=timers.target diff --git a/roles/native-journalctl-health-check/handlers/main.yml b/roles/native-journalctl-health-check/handlers/main.yml new file mode 100644 index 00000000..c934f249 --- /dev/null +++ b/roles/native-journalctl-health-check/handlers/main.yml @@ -0,0 +1,6 @@ +- name: "restart journalctl-health-check.timer" + systemd: + name: journalctl-health-check.timer + state: restarted + enabled: yes + daemon_reload: yes diff --git a/roles/native-journalctl-health-check/meta/main.yml b/roles/native-journalctl-health-check/meta/main.yml new file mode 100644 index 00000000..9d9d34e2 --- /dev/null +++ b/roles/native-journalctl-health-check/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - native-systemd-email diff --git a/roles/native-journalctl-health-check/tasks/main.yml b/roles/native-journalctl-health-check/tasks/main.yml new file mode 100644 index 00000000..9b151914 --- /dev/null +++ b/roles/native-journalctl-health-check/tasks/main.yml @@ -0,0 +1,19 @@ +- name: "create {{journalctl_health_check_folder}}" + file: + path: "{{journalctl_health_check_folder}}" + state: directory + mode: 0755 + +- name: create journalctl-health-check.sh + copy: + src: journalctl-health-check.sh + dest: "{{journalctl_health_check_folder}}journalctl-health-check.sh" + +- name: create journalctl-health-check.service + template: src=journalctl-health-check.service.j2 dest=/etc/systemd/system/journalctl-health-check.service + +- name: create journalctl-health-check.timer + copy: + src: journalctl-health-check.timer + dest: "/etc/systemd/system/journalctl-health-check.timer" + notify: restart journalctl-health-check.timer diff --git a/roles/native-journalctl-health-check/templates/journalctl-health-check.service.j2 b/roles/native-journalctl-health-check/templates/journalctl-health-check.service.j2 new file mode 100644 index 00000000..a18459a0 --- /dev/null +++ b/roles/native-journalctl-health-check/templates/journalctl-health-check.service.j2 @@ -0,0 +1,7 @@ +[Unit] +Description=checking journalctl health +OnFailure=systemd-email@%n.service + +[Service] +Type=oneshot +ExecStart=/bin/bash {{journalctl_health_check_folder}}journalctl-health-check.sh diff --git a/roles/native-journalctl-health-check/vars/main.yml b/roles/native-journalctl-health-check/vars/main.yml new file mode 100644 index 00000000..40a83e86 --- /dev/null +++ b/roles/native-journalctl-health-check/vars/main.yml @@ -0,0 +1 @@ +journalctl_health_check_folder: "/home/administrator/scripts/journalctl-health-check/" \ No newline at end of file diff --git a/roles/native-journalctl/readme.md b/roles/native-journalctl/README.md similarity index 55% rename from roles/native-journalctl/readme.md rename to roles/native-journalctl/README.md index 928c28ca..cd9cdfcb 100644 --- a/roles/native-journalctl/readme.md +++ b/roles/native-journalctl/README.md @@ -3,5 +3,5 @@ # live monitoring ```bash - watch -n 0.5 'journalctl -n 20 --no-pager' + 'journalctl -f' ``` diff --git a/roles/native-journalctl/meta/main.yml b/roles/native-journalctl/meta/main.yml new file mode 100644 index 00000000..75444eba --- /dev/null +++ b/roles/native-journalctl/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - native-journalctl-health-check