From 01231cab6b2548267c9799b052b370bf49cc54a8 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sun, 16 Apr 2023 09:59:54 +0200 Subject: [PATCH] Added disc-space-checker --- group_vars/all | 7 +++++- playbook.yml | 1 + .../templates/backups-cleanup.py.j2 | 2 +- roles/native-disc-space-check/README.md | 2 ++ .../native-disc-space-check/handlers/main.yml | 12 ++++++++++ roles/native-disc-space-check/meta/main.yml | 2 ++ roles/native-disc-space-check/tasks/main.yml | 22 +++++++++++++++++++ .../templates/disc-space-check.service.j2 | 7 ++++++ .../templates/disc-space-check.sh.j2 | 13 +++++++++++ .../templates/disc-space-check.timer.j2 | 8 +++++++ roles/native-disc-space-check/vars/main.yml | 1 + 11 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 roles/native-disc-space-check/README.md create mode 100644 roles/native-disc-space-check/handlers/main.yml create mode 100644 roles/native-disc-space-check/meta/main.yml create mode 100644 roles/native-disc-space-check/tasks/main.yml create mode 100644 roles/native-disc-space-check/templates/disc-space-check.service.j2 create mode 100644 roles/native-disc-space-check/templates/disc-space-check.sh.j2 create mode 100644 roles/native-disc-space-check/templates/disc-space-check.timer.j2 create mode 100644 roles/native-disc-space-check/vars/main.yml diff --git a/group_vars/all b/group_vars/all index d8f9875f..1a235fad 100644 --- a/group_vars/all +++ b/group_vars/all @@ -3,8 +3,13 @@ on_calendar_btrfs_health_check: "*-*-* 00:00:00" on_calendar_journalctl_health_check: "*-*-* 00:00:00" +on_calendar_disc_space_check: "*-*-* 06,12,18,00:00:00" on_calendar_docker_health_check: "*-*-* 09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,00,01,02:00:00" on_calendar_backups_cleanup: "*-*-* 06,12,18,00:30:00" on_calendar_docker_volume_backup: "*-*-* 03:30:00" on_calendar_docker_compose_restart_unhealthy: "*-*-* 09,10,11,12,13,14,15,16,17,18,19,20,21,22,23,00,01:30:00" -on_calendar_pull_primary_backups: "*-*-* 21:30:00" \ No newline at end of file +on_calendar_pull_primary_backups: "*-*-* 21:30:00" + +# Space Variables +size_percent_maximum_backup: 75 +size_percent_disc_space_warning: 85 \ No newline at end of file diff --git a/playbook.yml b/playbook.yml index 2d03f440..c917d156 100644 --- a/playbook.yml +++ b/playbook.yml @@ -6,6 +6,7 @@ - system-security - system-update - native-journalctl + - native-disc-space-check #- native-hostname - name: setup btrfs health check hosts: btrfs_health_check_hosts diff --git a/roles/native-backups-cleanup/templates/backups-cleanup.py.j2 b/roles/native-backups-cleanup/templates/backups-cleanup.py.j2 index fb5c27ec..7a51c32f 100644 --- a/roles/native-backups-cleanup/templates/backups-cleanup.py.j2 +++ b/roles/native-backups-cleanup/templates/backups-cleanup.py.j2 @@ -5,7 +5,7 @@ import os backup_disk_path = "{{backup_disk_path}}" backups_folder_path = os.path.join(backup_disk_path, "Backups/") deleted = True -while psutil.disk_usage(backup_disk_path).percent > int({{backup_space_percent}}) and deleted: +while psutil.disk_usage(backup_disk_path).percent > int({{size_percent_maximum_backup}}) and deleted: deleted = False print("%d %% of disk %s are used. Freeing space..." % (psutil.disk_usage(backup_disk_path).percent,backup_disk_path)) for primary_directory in os.listdir(backups_folder_path): diff --git a/roles/native-disc-space-check/README.md b/roles/native-disc-space-check/README.md new file mode 100644 index 00000000..3e6a5c17 --- /dev/null +++ b/roles/native-disc-space-check/README.md @@ -0,0 +1,2 @@ +# disc-space-check +Checks if enough disc space is free \ No newline at end of file diff --git a/roles/native-disc-space-check/handlers/main.yml b/roles/native-disc-space-check/handlers/main.yml new file mode 100644 index 00000000..ad1c55a9 --- /dev/null +++ b/roles/native-disc-space-check/handlers/main.yml @@ -0,0 +1,12 @@ +- name: "reload disc-space-check.service" + systemd: + name: disc-space-check.service + state: reloaded + enabled: yes + daemon_reload: yes +- name: "restart disc-space-check.timer" + systemd: + name: disc-space-check.timer + state: restarted + enabled: yes + daemon_reload: yes diff --git a/roles/native-disc-space-check/meta/main.yml b/roles/native-disc-space-check/meta/main.yml new file mode 100644 index 00000000..9d9d34e2 --- /dev/null +++ b/roles/native-disc-space-check/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - native-systemd-email diff --git a/roles/native-disc-space-check/tasks/main.yml b/roles/native-disc-space-check/tasks/main.yml new file mode 100644 index 00000000..cfb37098 --- /dev/null +++ b/roles/native-disc-space-check/tasks/main.yml @@ -0,0 +1,22 @@ +- name: "create {{disc_space_check_folder}}" + file: + path: "{{disc_space_check_folder}}" + state: directory + mode: 0755 + +- name: create disc-space-check.sh + template: + src: disc-space-check.sh.j2 + dest: "{{disc_space_check_folder}}disc-space-check.sh" + +- name: create disc-space-check.service + template: + src: disc-space-check.service.j2 + dest: /etc/systemd/system/disc-space-check.service + notify: reload disc-space-check.service + +- name: create disc-space-check.timer + template: + src: disc-space-check.timer.j2 + dest: /etc/systemd/system/disc-space-check.timer + notify: restart disc-space-check.timer diff --git a/roles/native-disc-space-check/templates/disc-space-check.service.j2 b/roles/native-disc-space-check/templates/disc-space-check.service.j2 new file mode 100644 index 00000000..ec7d8a5a --- /dev/null +++ b/roles/native-disc-space-check/templates/disc-space-check.service.j2 @@ -0,0 +1,7 @@ +[Unit] +Description=checking disc space +OnFailure=systemd-email@%n.service + +[Service] +Type=oneshot +ExecStart=/bin/bash {{disc_space_check_folder}}disc-space-check.sh diff --git a/roles/native-disc-space-check/templates/disc-space-check.sh.j2 b/roles/native-disc-space-check/templates/disc-space-check.sh.j2 new file mode 100644 index 00000000..2cec0f36 --- /dev/null +++ b/roles/native-disc-space-check/templates/disc-space-check.sh.j2 @@ -0,0 +1,13 @@ +#!/bin/sh +errors=0 +echo "checking disc space use..." +df +for disc_use_percent in $(df --output=pcent | sed 1d) +do + disc_use_percent_number=$(echo "$disc_use_percent" | sed "s/%//") + if [ "$disc_use_percent_number" -gt "{{size_percent_disc_space_warning}}" ]; then + echo "WARNING: $disc_use_percent_number exceeds the limit of {{size_percent_disc_space_warning}}%." + errors+=1; + fi +done +exit $errors; \ No newline at end of file diff --git a/roles/native-disc-space-check/templates/disc-space-check.timer.j2 b/roles/native-disc-space-check/templates/disc-space-check.timer.j2 new file mode 100644 index 00000000..3ac6bda7 --- /dev/null +++ b/roles/native-disc-space-check/templates/disc-space-check.timer.j2 @@ -0,0 +1,8 @@ +[Unit] +Description=starts disc-space-check.service + +[Timer] +OnCalendar={{on_calendar_disc_space_check}} + +[Install] +WantedBy=timers.target diff --git a/roles/native-disc-space-check/vars/main.yml b/roles/native-disc-space-check/vars/main.yml new file mode 100644 index 00000000..b545053a --- /dev/null +++ b/roles/native-disc-space-check/vars/main.yml @@ -0,0 +1 @@ +disc_space_check_folder: "/home/administrator/scripts/disc-space-check/" \ No newline at end of file