Skip to content

Instantly share code, notes, and snippets.

@betapcode
Created February 5, 2015 15:44
Show Gist options
  • Save betapcode/7c4b8e386675a04dad3c to your computer and use it in GitHub Desktop.
Save betapcode/7c4b8e386675a04dad3c to your computer and use it in GitHub Desktop.
file check disk space on server linux
#!/bin/bash
# Created by @betapcode - time 05/12/2014
# Check Out of disk space on server
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# admin email account - eg: minh.pham@tamtay.vn betapcode@gmail.com pnminh.it@gmail.com
ADMIN="minh.pham@tamtay.vn"
# set usage alert threshold - default is 90%
THRESHOLD=90
#hostname
HOSTNAME=$(hostname)
#ipaddress
CMD_GET_IP="$(which ip)"
IPADDR=`$CMD_GET_IP addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'`
#mail client
MAIL="$(which mail)"
# store all disk info here
EMAIL=""
for line in $(df -hP | egrep '^/dev/' | awk '{ print $6 "_:_" $5 }')
do
part=$(echo "$line" | awk -F"_:_" '{ print $1 }')
part_usage=$(echo "$line" | awk -F"_:_" '{ print $2 }' | cut -d'%' -f1 )
if [ $part_usage -ge $THRESHOLD -a -z "$EMAIL" ];
then
EMAIL="$(date): Running out of diskspace on $HOSTNAME\n"
EMAIL="$EMAIL\n$part ($part_usage%) >= (Threshold = $THRESHOLD%)"
elif [ $part_usage -ge $THRESHOLD ];
then
EMAIL="$EMAIL\n$part ($part_usage%) >= (Threshold = $THRESHOLD%)"
fi
done
if [ -n "$EMAIL" ];
then
echo -e "$EMAIL" | $MAIL -s "[TT Alert]: In $IPADDR diskspace on $HOSTNAME" "$ADMIN"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment