Get Disk Space Email Alerts from your Linux Servers

2497

Headless and unmonitored servers sometimes becomes a pain when your server stops responding and later you figure out it was due to 100% disk usage. This simple php script can periodicly check for disk usage and send you email alert when the disk usage is above 90%.

Step 1) You will need php-cli and come other packages too achive this. In ubuntu or debian you can do this by

apt-get update
apt-get install php-mail php-net-smtp php-net-socket php-pear php5-cli

Step 2) Create /usr/local/bin/diskspacecheck.php with the following content using your favourite editor, mine is nano 🙂

#!/usr/bin/php
<?php

require_once “Mail.php”;

/*
* FUNCTION TO SEND MAIL
*/
function SendMail($msg){
$date = date(‘r’);
$bodymsg = $msg;
$hostname = gethostname();
$hostname = preg_replace(“/[^a-zA-Z0-9_ -]/s”,”_”,$hostname);
$from = ”
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
“;
$to = ”
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
“;
$subject = “$bodymsg”;
$body = “This is an automatic alert. Please do not reply.nn$bodymsg – $date”;
$port = “465”;
$host = “ssl://smtp.gmail.com”;
$username = ”
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
“;
$password = “MyPassword”;
/*
* REPLACE
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
ANS MyPassword WITH AN ACTIVE GMAIL ACCOUNT.
*/
$headers = array (‘From’ => $from,
‘To’ => $to,
‘Subject’ => $subject);
$smtp = Mail::factory(‘smtp’,
array (‘host’ => $host,
‘port’ => $port,
‘auth’ => true,
‘username’ => $username,
‘password’ => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
echo(“<p>” . $mail->getMessage() . “</p>”);
} else {
echo(“<p>Message successfully sent!</p>”);
}
}

/*
* FUNCTION TO DISPLAY SIZE IN HUMAN READABLE FORMAT
*/

function formatSize( $bytes )
{
$types = array( ‘B’, ‘KB’, ‘MB’, ‘GB’, ‘TB’ );
for( $i = 0; $bytes >= 1024 && $i < ( count( $types ) -1 ); $bytes /= 1024, $i++ );
return( round( $bytes, 2 ) . ” ” . $types[$i] );
}

/*
* CHECK DISKSPACE FOR / PARTITION
*/

$dt = disk_total_space(“/”);
$df = disk_free_space(“/”);
$du = $dt – $df;
$dp = sprintf(‘%.2f’,($du / $dt) * 100);

$df = formatSize($df);
$du = formatSize($du);
$dt = formatSize($dt);

if ($dp > 90){
SendMail(“disk usage of / is $dp%nFree Space – $dfnDisk Usage – $dun”);
}

/*
* CHECK DISKSPACE FOR /home PARTITION
*/

$dt = disk_total_space(“/home”);
$df = disk_free_space(“/home”);
$du = $dt – $df;
$dp = sprintf(‘%.2f’,($du / $dt) * 100);

$df = formatSize($df);
$du = formatSize($du);
$dt = formatSize($dt);

/*
* THIS WILL SEND AN EMAIL ALERT WHEN DISKUSAGE IS ABOVE 90%, YOU CAN CHANGE THIS VALUE THAT SUITES YOUR REQUIREMENT
*/

if ($dp > 90){
SendMail(“disk usage of /home is $dp%nFree Space – $dfnDisk Usage – $dun”);
}

?>

Step 3) Create a cron job which will run this script daily and alert when the disk space goes low

nano /etc/cron.d/diskspacecheck30 10 * * * root /usr/bin/php -f /usr/local/bin/diskspacecheck.php

This cronjob will run this php script every day 10:30am. Change it to suit your need.

 

Swapnil Jain |
This e-mail address is being protected from spambots. You need JavaScript enabled to view it