Added disc-space-checker

This commit is contained in:
Kevin Veen-Birkenbach 2023-04-16 09:59:54 +02:00
parent 3508baebac
commit 01231cab6b
11 changed files with 75 additions and 2 deletions

View File

@ -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"
on_calendar_pull_primary_backups: "*-*-* 21:30:00"
# Space Variables
size_percent_maximum_backup: 75
size_percent_disc_space_warning: 85

View File

@ -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

View File

@ -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):

View File

@ -0,0 +1,2 @@
# disc-space-check
Checks if enough disc space is free

View File

@ -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

View File

@ -0,0 +1,2 @@
dependencies:
- native-systemd-email

View File

@ -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

View File

@ -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

View File

@ -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;

View File

@ -0,0 +1,8 @@
[Unit]
Description=starts disc-space-check.service
[Timer]
OnCalendar={{on_calendar_disc_space_check}}
[Install]
WantedBy=timers.target

View File

@ -0,0 +1 @@
disc_space_check_folder: "/home/administrator/scripts/disc-space-check/"