Hi,
In this blog post where I am going to show you how to watch disk usage and when disk usage become greater than ?% and then send mail to user that the user will be aware that the pfsense disk usage at critical point and user needs to work on it to make disk works again before system get crashed.
I’m using pfssense 2.4.4 version and amd64 bit architecture pfsense.
My Github Address
https://github.com/lifeoverlinux/
Disk Usage Shell Script Download
SSH to your pfsense with the following command. After you connected your pfsense with ssh, press “8” and enter to shell.
lifeoverlinux# ssh root@my_pfsense_ip_address
Under my github account there is a folder “pfsense”. In this folder copy&paste code into a text file and save it or you can download the file from my github and send to pfsense with scp command. It’s both okay.
https://github.com/lifeoverlinux/My-Shell-Scripts/tree/master/pfsense
pfsense/root:# nano diskscript.sh #!/bin/sh # Author: Ibrahim Ucar # Written in: pfSense 2.4.4 # Date: 23.10.2018 # Mail: [email protected] # This script watches the disk given by user and when disk being greater than "90%" that I send mail to user. # Well, before system crash, Admin will be aware that he should work to make available disk space. # You can set this script to CRON to make this script works every minute, hour, day, etc. # ------------IMPORTANT------------- # # Please write the name of the disk which disk or partition has been mounted to ( "/" ). # Use "df -h" command to see which disk mounted to ( "/" ) and then write disk name into the variable.. # For example, for me I will write "/dev/sda1" into the variable. # EXAMPLE: Filesystem Size Used Avail Use% Mounted on # EXAMPLE: /dev/sda1 20G 3.2G 15G 18% / diskname="/dev/sda1" # ------------IMPORTANT------------- # # Necessary file for this script get to work. touch /tmp/email_diskusage.txt MAILFILE=/tmp/email_diskusage.txt # Rest of the shell script commands. df -H | grep "$diskname" | awk '{ print $5}' > /tmp/diskuse.txt usage=$(cat /tmp/diskuse.txt | cut -f1 -d\%) if [ $usage -ge 90 ]; then echo "WARNING! Your disk running out of space. The usage greater than 90%!. I sent an email to user." php -f /root/mailsender.php fi
Please do not forget to read “IMPORTANT” field in shell script!!!
After you do that make sure that “diskscript.sh” has executable permission to root user.
pfsense/root# chmod u+x diskscript.sh
Now about this shell script is done.
Mail Sender PHP Script Download
Under the same /root/ directory, create a file with exact same name “mailsender.php”. Then copy&paste the code of mailsender.php and save it.
https://github.com/lifeoverlinux/My-Shell-Scripts/tree/master/pfsense
NOTE : Do not forget to fill up necessary fields in php codes! You have to fill up correctly ($to, $from, $user $pass) forms.
pfsense/root:# nano mailsender.php // Taken from Stackoverflow! I couldn't find the same link address that I found php codes on it. // If you get error after run mailsender.php : https://serverfault.com/questions/635139/how-to-fix-send-mail-authorization-failed-534-5-7-14 <?php $usage = exec("cat /tmp/diskuse.txt | cut -f1 -d\%"); $hostname = exec(hostname); $date = exec("date '+%Y/%m/%d at %H:%M:%S'"); require "Mail.php"; $to = "email_address"; // to email address $from = "email_address"; // the email address $subject = "WARNING: Disk Capaticy 90%"; $body = "Disk running out of space. Currently disk usage ".$usage."% on ".$hostname." as on ".$date.". Do not forget to check disk space before system crash."; $host = "smtp.gmail.com"; $port = "587"; $user = "username"; // write your mail address $pass = "password"; // write your mail password $headers = array("From"=> $from, "To"=>$to, "Subject"=>$subject); $smtp = @Mail::factory("smtp", array("host"=>$host, "port"=>$port, "auth"=> true, "username"=>$user, "password"=>$pass)); $mail = @$smtp->send($to, $headers, $body); if (PEAR::isError($mail)){ echo "error: {$mail->getMessage()}"; } else { echo "Message sent"; } ?>
After you do that make sure that “mailsender.php” has executable permission to root user.
pfsense/root:# chmod u+x mailsender.php
Now about this php script is done.
Test Time!
Now edit the “diskscript.sh” file and find the following sentence and changed it from “90” to “1” and save it. You’ll need to write back “90” after we have tested. 😉
Original: if [ $usage -ge 90 ]; then
Changed: if [ $usage -ge 1 ]; then
And then I’m guessing that you have filled up the necessary fields in php code “mailsender.php”. If you didn’t fill up, then go back to do that.
Run the following command and we will see if it’s everything okay, mail will be sent to user if disk usage greater than 1%.
pfsense/root:# sh diskscript.sh
error: authentication failure [SMTP: Invalid response code received from server (code: 534 ......)
Upps! Something wrong, let’s take a look the following link. In this link that find the line that I wrote down.
Link : https://serverfault.com/questions/635139/how-to-fix-send-mail-authorization-failed-534-5-7-14
Find Line : https://www.google.com/settings/security/lesssecureapps
After I enabled this feature on my gmail account, I tested again and as you can see it’s worked.
pfsense/root:# sh diskscript.sh Message sent
Example email which sent by script.
About CRON
You can set “diskscript.sh” on crontab and make this script works every 1 week, 1 month, 2 month, periods. It’s up to you. It’s so easy to add crontab on cron. First of all, you need to install Cron package from Package Manager. And then you will figure out how to add crontab.
As a root user, php -f /root/diskscript.sh
That’s all you need to do, now wait until your pfsense disk usage become %90 and pfsense will send you mail about disk usage. Do not add “mailsender.php” into crontab. Just add “diskscript.sh” to crontab.
Have fun!
Hi İbrahim,
many thanks for that great script, it was exactly what i needed for my project.
I have just one question, could you show me your cron entry?
When I execute the script all works fine and I get a email, when it gets executed by cron, the script runs but I guess it’s not executing php -f /root/mailsender.php.
My cron looks like this: 20 * * * * root /root/testip.sh
regards,
Jan
Hi Jan,
Your welcome, I’m happy to that this helped you. Well, you will not add “mailsender.php” into crontab. You will have to add “diskscript.sh” into crontab.
E.g crontab:
1. login to shell.
2. crontab -e
#add line, every 1 hour it will work.
3. * */1 * * * php -f /root/diskscript.sh
#save and exit, then restart service
4. service cron onerestart (for pfsense) service cron restart (debian/ubuntu)
Example screenshot below here.
If you have a problem about that, let me know.
Also you can find another my shell scripts at my github : https://github.com/lifeoverlinux
Merhaba İbrahim Bey,
Verdiğiniz bilgiler doğrultusunda işlemleri yaptım.
Çok teşekkür ederim.