#!/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 first part of this script checks for given minimum space on a mounted volume, and alerts ntfy in case the limit is surpassed. ## 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 }') 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 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 if (btrfs fi show | grep -wic "Some devices missing"); then echo Bad: - Btrfs is missing devices, some disk is broken echo Sending alert... curl -k --retry 3 \ -d "Bad - Some devices are missing in btrfs: maybe a broken disk?" \ -H "Title: Missing disks in btrfs on $(hostname)" \ -H "Priority: high" \ -H "Tags: warning,boar" \ $topicurl 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}')" for i in ${mounted_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) if [ ${check_disk_errors} -eq 1 ] ; then : # do nothing if output is a single "1" else echo Bad: - Btrfs is detecting errors on partition $i, some disk is about to be broken echo Sending alert... curl -k --retry 3 \ -d "Bad - Errors on btrfs mounted device $i: you will have to replace a disk. Check the status of mounted disks with: btrfs device stats $i" \ -H "Title: btrfs errors on partition $i of $(hostname)" \ -H "Priority: high" \ -H "Tags: warning,boar" \ $topicurl fi done