Simple disk space monitor for linux servers
Tags: linux, bash, sysadmin
Published: 2022-04-14 18:28:31

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:

0. Linux server runing (_begin and end of all_)

1. 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_)

2. 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
```
When Postfix not installed, show something like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4y5wujq10pw34qsn67ae.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7sxyqdyb3ctalh77nhk6.png)

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

When Postfix is installed and running, show you:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0b2s055fr4tbtluaqn9g.png)

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
```
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
```

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
```

This is the crontab template, add to bottom:

```
* * * * * root sh /usr/diskSpace.sh
```
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.