Alert when disk usage is greater than the allowable

How to alert when disk usage is greater than the allowable threshold easily?
You can set a cron job to check disk usage then send to you alert-API when it is greater than the allowable threshold.

Here is an example, hourly periodic check. If the system has used more than 10% of the disk space, send an alert to the API using JSON.

Step 1: Create a bash script “disk-alert.sh”

#!/bin/bash
alertApi='https://www.domain.com/alert-api'
diskUsage=$(df -h / | awk 'NR==2{print $5}' | sed 's/%//')
if [ $diskUsage -ge 10 ]; then
	# diskRemaining=$(df --output=avail / | awk 'NR==2{print $1/(1024*1024)}')
	diskRemaining=$(df -h --output=avail / | awk 'NR==2{print $1}')
	ip=$(/sbin/ifconfig | grep 'inet ' | awk '{print $2}' | head -n 1)
	curl $alertApi -X POST -H "Content-Type: application/json" -d "{\"diskUsage\": $diskUsage, \"diskRemaining\": \"$diskRemaining\", \"ip\": \"$ip\"}"
fi

Step 2: Set cron job

vi /var/spool/cron/root

Write this to end of file then save:

0 * * * * /bin/sh /home/disk-alert.sh 2>&1 >> /home/cron.log

Now, you can modify the allowable threshold to 90 or up to you.

One paste script

Just edit parameters like threshold and API and paste in terminal (you can repeat to modify):

touch  /var/spool/cron/root
sed -i '/disk-alert\.sh/d' /var/spool/cron/root && echo '0 * * * * /bin/sh /home/disk-alert.sh' >> /var/spool/cron/root
vi -c ":%d" -c "startinsert" -c ":set timeoutlen=1000 ttimeoutlen=0" -c ":au CursorHoldI * stopinsert" -c 'autocmd InsertLeave * silent! write | silent! quit' /home/disk-alert.sh
#!/bin/bash
alertApi='https://www.domain.com/alert-api'
ips=""
if [ -z "$ips" ]; then
	publicIp=$(curl -s https://api.ipify.org)
	if [ -z "$publicIp" ]; then
	  publicIp=$(curl -s https://ifconfig.me)
	fi
	ips=$(/sbin/ip addr show | grep 'inet ' | grep -v ' lo' | awk '{print $2}' | cut -d'/' -f1 | awk '{ips=ips", "$1}END{gsub(/^, /, "", ips); print ips}')	
	if [ -z "$ips" ]; then		
		ips=$(/sbin/ifconfig | grep 'inet ' | grep -v ' lo' | awk '{print $2}' | awk '{ips=ips", "$1}END{gsub(/^, /, "", ips); print ips}')
	fi
	ips="$publicIp, $ips"
	sed -i "s/ips=\"\"/ips=\"$ips\"/" $0
fi

diskUsage=$(df -h / | awk 'NR==2{print $5}' | sed 's/%//')
lastDiskUsage=0
threshhold=10
# diskRemaining=$(df --output=avail / | awk 'NR==2{print $1/(1024*1024)}')
diskRemaining=$(df -h --output=avail / | awk 'NR==2{print $1}')
if [ $diskUsage -ge $threshhold	] &&	[ $diskUsage -lt 94 ]; then
	
	newThreshhold=$((diskUsage + 5))
	curl $alertApi -X POST -H "Content-Type: application/json" -d "{\"diskUsage\": $diskUsage, \"nextAlert\": $newThreshhold, \"diskRemaining\": \"$diskRemaining\", \"ip\": \"$ips\"}"
	sed -i "s/threshhold=$threshhold/threshhold=$newThreshhold/" $0
elif 	[ $diskUsage -ge $threshhold	] && [	$diskUsage -ge 94 ]; then
	sed -i '/disk-alert\.sh/d' /var/spool/cron/root && echo '*/5 * * * * /bin/sh /home/disk-alert.sh' >> /var/spool/cron/root
	curl $alertApi -X POST -H "Content-Type: application/json" -d "{\"diskUsage\": $diskUsage, \"nextAlert\": $newThreshhold, \"diskRemaining\": \"$diskRemaining\", \"ip\": \"$ips\"}"	
elif	[ $diskUsage -lt $threshhold	] && [	$diskUsage -lt $lastDiskUsage ]; then
	newThreshhold=$((threshhold - (lastDiskUsage - diskUsage)))
	sed -i "s/threshhold=$threshhold/threshhold=$newThreshhold/" $0
	sed -i '/disk-alert\.sh/d' /var/spool/cron/root && echo '0 * * * * /bin/sh /home/disk-alert.sh' >> /var/spool/cron/root
	curl $alertApi -X POST -H "Content-Type: application/json" -d "{\"diskUsage\": $diskUsage, \"nextAlert\": $newThreshhold, \"diskRemaining\": \"$diskRemaining\", \"ip\": \"$ips\"}"	
fi
sed -i "s/lastDiskUsage=$lastDiskUsage/lastDiskUsage=$diskUsage/" $0

Leave a Reply

Your email address will not be published. Required fields are marked *