Monday, April 7, 2014

Bash: disk space alert

There are millions scripts and utilities for that.. Here is my input:
[root@gsm-gateway ~]# cat /usr/local/sbin/diskspace_alert

#!/bin/bash

ALERT=0
MAX_VALUE=$1
for i in `df -h | tr -s ' '| cut -d' ' -f5 | grep -oP "\d{1,3}?(?=%)"`
do
        [ "$i" -ge "$MAX_VALUE" ] && ALERT=$((ALERT+1)) && DF_OUTPUT=$(df -h)
done

[ "$ALERT" -gt 0 ] && /usr/local/sbin/php_mailer "The disk space is nearly full" "$DF_OUTPUT"


Replace /usr/local/sbin/php_mailer to your favorite Linux mailer

add to cron:

* */1 * * * /usr/local/sbin/diskspace_alert 80

where 80 - is the max percentage value

No comments:

Post a Comment