Skip to content

Instantly share code, notes, and snippets.

@boutell
Created October 15, 2012 22:22
Show Gist options
  • Save boutell/3896011 to your computer and use it in GitHub Desktop.
Save boutell/3896011 to your computer and use it in GitHub Desktop.
CPU load watcher: sends email when the load average changes by at least one full point
<?php
// Install me via cron:
// [email protected]
// * * * * * /usr/local/bin/php /home/someaccount/loadwatch.php
$fields = explode(' ', file_get_contents('/proc/loadavg'));
$avg = $fields[0];
$last = @file_get_contents("/home/gps/cpulast.txt");
if (!strlen($last))
{
$last = -1;
}
if (abs(floor($last) - floor($avg)) >= 1.0)
{
echo("CPU load is now $avg");
file_put_contents("/home/gps/cpulast.txt", $avg);
}
$out = fopen("/home/gps/cpu.csv", "a");
fwrite($out, date('Y-m-d H:i:s') . ',' . $avg . "\n");
fclose($out);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment