From 850d16c160f6043e7fe58b6b773a6ea221e18e76 Mon Sep 17 00:00:00 2001 From: "andrea.noni" Date: Wed, 30 Oct 2024 18:39:06 +0100 Subject: [PATCH] rewritten disk usage script part --- disks_checks.sh | 67 ++++++++++++++++++++++++++++--------------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/disks_checks.sh b/disks_checks.sh index 18af807..f89f3c9 100755 --- a/disks_checks.sh +++ b/disks_checks.sh @@ -1,38 +1,50 @@ #!/bin/bash -### I lost the link where I copied the first part of this script from, sorry, i'll update as soon as I find it back! The btrfs raid1 check part (second) and the btrfs device errors part (third) are written by tech_at_hangar.org +### the used parts of this scrits have been written by tech_at_hangar.org -## The first part of this script checks for given minimum space on a mounted volume, and alerts ntfy in case the limit is surpassed. +## This unused part of this script checks for given minimum space on a mounted volume, and alerts ntfy in case the limit is surpassed. it stays here as a reference only. I copied the script from internet but I lost the reference, sorry! + +## root volume +#mingigs=10 +#avail=$(df | awk '$6 == "/" && $4 < '$mingigs' * 1024*1024 { print $4/1024/1024 }') + +#if [ -n "$avail" ]; then +# curl -k --retry 3 \ +# -d "Only $avail GB available on the root disk. Better clean that up." \ +# -H "Title: Low disk space alert on $(hostname)" \ +# -H "Priority: default" \ +# -H "Tags: warning,cd" \ +# $topicurl +#fi + +## The first part of this script checks for disk usage and alerts if it exceeds 94% ## The second part of this script checks for missing disks on btrfs raid1 devices and alerts ntfy in case of missing devices ## The third part of this script checks for read/write errors on disks in mounted partitions and alerts ntfy in case of errors -# root volume -mingigs=10 -avail=$(df | awk '$6 == "/" && $4 < '$mingigs' * 1024*1024 { print $4/1024/1024 }') +# the ntfy instance where you are senfing alerts to: topicurl=https://NTFY_SERVER_IP/disk_alerts -if [ -n "$avail" ]; then - curl -k --retry 3 \ - -d "Only $avail GB available on the root disk. Better clean that up." \ - -H "Title: Low disk space alert on $(hostname)" \ - -H "Priority: default" \ - -H "Tags: warning,cd" \ - $topicurl -fi -# OTHER VOLUME NAME -mingigs=500 -avail=$(df | awk '$6 == "/mnt/OTHER_VOLUME" && $4 < '$mingigs' * 1024*1024 { print $4/1024/1024 }') -topicurl=https://NTFY_SERVER_IP/disk_alerts +### check disk usage status and notify if more then 95% is in use + +# print every /dev/ disk mounted, write all results in a single line, separated by commas and remove last comma +mounted_partitions="$(df | awk '{print $1}' | grep '/dev/' | tr '\n' ',' | awk '{sub(/,$/,""); print}')" + +for i in ${mounted_partitions//,/ } +do + # df on every mounted disk detected, take the 5th column, remove the % symbol, and output only the second line + disk_occupation=$(df $i | awk '{print $5}' | tail -n+2 | sed 's/.$//') + + if [ ${disk_occupation} -gt 95 ] ; then + curl -k --retry 3 \ + -d "Less then 5% space available on the $i partition. Better clean that up." \ + -H "Title: Low disk space alert on $(hostname)" \ + -H "Priority: default" \ + -H "Tags: warning,cd" \ + $topicurl + fi +done -if [ -n "$avail" ]; then - curl -k --retry 3 \ - -d "Only $avail GB available on the /mnt/OTHER_VOLUME disk. Better clean that up." \ - -H "Title: Low disk space alert on $(hostname)" \ - -H "Priority: default" \ - -H "Tags: warning,cd" \ - $topicurl -fi ### check btrfs raid status on btrfs devices @@ -49,11 +61,10 @@ fi ### check errors on btrfs devices -topicurl=https://NTFY_SERVER_IP/disk_alerts # get list of btrfs mounted partition, put all results in a single line separated by a comma and remove the last comma -mounted_partitions="$(mount | grep btrfs | awk '{print $3}' | tr '\n' ',' | awk '{sub(/,$/,""); print}')" +mounted_btrfs_partitions="$(mount | grep btrfs | awk '{print $3}' | tr '\n' ',' | awk '{sub(/,$/,""); print}')" -for i in ${mounted_partitions//,/ } +for i in ${mounted_btrfs_partitions//,/ } do # check btrfs errors on mounted device, if all output is 0 then return a single "1" check_disk_errors=$(btrfs device stats $i | awk '{print $2}' | grep -c 0 -v)