Last active
May 13, 2023 13:59
-
-
Save Bouni/f92647895955a31e71b446b5baa3014a to your computer and use it in GitHub Desktop.
Use Uptime Kuma for disk space monitoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
BASE_URL="https://uptime.bouni.de/api/push/" | |
disks=("/dev/sda1") | |
alerts=(85) | |
urls=("pCDwxLPOoI") | |
check () { | |
DISK=$1 | |
ALERT_LEVEL=$2 | |
URL_HASH=$3 | |
disk_usage=`/usr/bin/df -h $DISK | /usr/bin/tail -n +2 | /usr/bin/awk 'END {print $5}' | /usr/bin/tr -d "%"` | |
# Get disk space in GB | |
disk_space=`/usr/bin/df -h $DISK | /usr/bin/tail -n +2 | /usr/bin/awk 'END {print $2}'` | |
# get used disk space in GB | |
used_space=`/usr/bin/df -h $DISK | /usr/bin/tail -n +2 | /usr/bin/awk 'END {print $3}'` | |
# get free disk space in GB | |
free_space=`/usr/bin/df -h $DISK | /usr/bin/tail -n +2 | /usr/bin/awk 'END {print $4}'` | |
# get mount point | |
mount_point=`/usr/bin/df -h $DISK | /usr/bin/tail -n +2 | /usr/bin/awk 'END {print $6}'` | |
# Check if disk usage is higher than 85% | |
if [ $disk_usage -gt $ALERT_LEVEL ]; then | |
# Send push notification | |
/usr/bin/curl -k --get \ | |
--data-urlencode "msg=Disk usage on ${mount_point} is high: ${disk_usage}% (${free_space}/${used_space}/${disk_space})" \ | |
--data-urlencode "status=down" \ | |
"${BASE_URL}${URL_HASH}" | |
else | |
/usr/bin/curl -k --get \ | |
--data-urlencode "msg=Disk usage on ${mount_point} is OK: ${disk_usage}% (${free_space}/${used_space}/${disk_space})" \ | |
--data-urlencode "status=up" \ | |
"${BASE_URL}${URL_HASH}" | |
fi | |
} | |
for (( i=0; i<${#disks[@]}; i++ )); do | |
check ${disks[$i]} ${alerts[$i]} ${urls[$i]} | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Sends disk space stats to Uptime Kuma | |
[Service] | |
Type=oneshot | |
ExecStart=/opt/scripts/diskspace.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Unit] | |
Description=Sends disk space stats to Uptime Kuma | |
[Timer] | |
OnCalendar=*:0/10 | |
[Install] | |
WantedBy=timers.target |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment