DEV Community

Cover image for Simple disk space monitor for linux servers
sentadoensilla
sentadoensilla

Posted on

Simple disk space monitor for linux servers

For you, new in linux, new admin server

Is very important know when disk space is very low because linux servers stop when / reach 0% free disk space, so you need a warning, an automatic warning, an email automatic warning.

For this (simple) guide go to use:

  1. Linux server runing (begin and end of all)

  2. Postfix email service runing on this server (send you and warning email, if not have read an guide, for example http://www.postfix.org/STANDARD_CONFIGURATION_README.html)

  3. crontab (come include into reputable linux distributions)


Before starting, view postfix service status, restarting it or install and configure

for view status open a terminal and type:

sudo systemctl status postfix
Enter fullscreen mode Exit fullscreen mode

When Postfix not installed, show something like this:

Image description

When Postfix is installed, but not running show something like that:

Image description

Maybe typing "sudo systemctl start postfix" fix the issue.

When Postfix is installed and running, show you:

Image description

Check all with your system administrator but if you are the sysadmin, God help us!!! God = Google or StackOverflow or https://www.cyberciti.biz/


FIRST STEP: Create a bash script monitor

We create a bash script with code to check hard drive space:

open terminal and type:

nano /usr/diskSpace.sh
Enter fullscreen mode Exit fullscreen mode

then paste this code (first read it and understand it, is very simple, but usefull)

#!/bin/bash
#a simple script that warn me about low disk space for partition in linux
#this script send a email when disk partition reach a DISKLIMIT
#to sysadmin ninja guy use a jutsu and save the world
#usign command df -h grep and contrab you get an email for low hard disk space in partitions
#You need and email server running like postfix (maybe sendmail)
#here and practical example to reach a postfix mail server, you choose: http://www.postfix.org/STANDARD_CONFIGURATION_README.html

PATH=$PATH:/usr/sbin:/usr/bin:/sbin:/bin
export $PATH

#ADDRESS FOR SERVER
DIRIP=$(/sbin/ifconfig | awk -F':' '/inet/&&!/127.0.0.1/{split($2,_," ");print _[1]}')
#EMAIL ACCOUNTS SENDER AND RECEIBERS
SENDERS="sender@myserver.com"
RECEIBERS="sysadminguy@myserver.com internguy@myserver.com developer@myserver.com theboss@myserver.com"
#RED LINE FLAG TO SEND AN EMAIL
DISKLIMIT=80

df -h | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $6 }' | while read output;
do
    usep=$(echo $output | awk '{print $1}' | cut -d'%' -f1  )
    partition=$(echo $output | awk '{ print $2 }' )
    if [ "$usep" -ge "$DISKLIMIT" ]; then
        echo -e $DIRIP"\n The disk space for some partitions is very low \n\n Please read the following report and do something: \n\nFor partition $partition   space used is $usep % \n\n This report was generated at: $(date)" |
        mail -s "[SOME SERVER] VERY LOW DISK SPACE "${DIRIP}" "  -a "From: "${SENDERS}" "${RECEIBERS}
    fi
done
service postfix restart
Enter fullscreen mode Exit fullscreen mode

You can modify DISKLIMIT to run and get an email, for testing proposes


SECOND STEP: Include a cron job each minute

A cron job let me run a script in minutes, hours, days, weeks... server run my script /usr/diskSpace each minute then a partition reach DISKLIMIT percent postfix send an email. Please calculate DISKLIMIT for notice in time includin week end or modify for vacations.

Open a terminal and type:

sudo nano /etc/crontab
Enter fullscreen mode Exit fullscreen mode

This is the crontab template, add to bottom:

* * * * * root sh /usr/diskSpace.sh
Enter fullscreen mode Exit fullscreen mode

Then each minute the server run /usr/diskSpace.sh and maybe send email for partitions with low hard disk space.

This simple guide was writing reading ciberciti.biz tuts and testing in Debian, freeBSD, Ubuntu and CentOS for reach good results.

Thanks for reading.

Top comments (0)