Categories
Linux PHP

Free SMS Notification for high server load

I’ve found in foros.ovh this interesting script that notifies high server load to you using an SMS.

All of this works thanks Google Calendar and his API.

Steps:

  • First of all you need a google account and then sign in google calendar.
  • Then go to your google calendar -> settings (right top bar) -> mobile setup
  • Follow the instructions to add you phone number.
  • After this Settings->Calendar Settings and in any of them click on Notifications.
  • Create an Event reminders, SMS 0 Minutes.

With this, now every time we create a new event in google calendar just on the time (because of that 0 minutes) we will receive an SMS to our phone.

Try it, come on!

The Script:

To make this script work you need to have Zend Gdata (http://framework.zend.com/download/gdata/) downloaded or you can download the script + Zend Gdata here.

// Config
define('EMAIL','xxxx@gmail.com');//gmail account used in google calendar
define('PASS','xxx');//your gmail password
define('SERVER_NAME','your_servername');//just as reference server name
//level of avg load http://php.net/manual/en/function.sys-getloadavg.php
define('LEVEL_EMAIL',3);//load average to notify by email
define('LEVEL_SMS',5);//load average to notify by sms
// End Config

//Loading Zend_Gdata libraries
ini_set('include_path', 'ZendGdata/library');
require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata');
Zend_Loader::loadClass('Zend_Gdata_AuthSub');
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
Zend_Loader::loadClass('Zend_Gdata_Calendar');
//End Loading Zend_Gdata libraries

// Function to create a new Google Calendar event
function createQuickAddEvent ($client, $quickAddText) {
  $gdataCal = new Zend_Gdata_Calendar($client);
  $event = $gdataCal->newEventEntry();
  $event->content = $gdataCal->newContent($quickAddText);
  $event->quickAdd = $gdataCal->newQuickAdd('true');
  $newEvent = $gdataCal->insertEvent($event);
}
// End Function to create a new Google Calendar event


//load average
if (function_exists(sys_getloadavg)){
	$load=sys_getloadavg();
	$load=$load[0];
}
else{
	$content = file_get_contents("/proc/loadavg");
	$loadavg = explode(" ", $content);
	$load = $loadavg[0] + 0;
}
//end load average


//NOTIFICATIONS

// Email - Send email notification
if($load >= LEVEL_EMAIL){
    mail(EMAIL, "Alarm high server load (".$load.") ".SERVER_NAME, "");
}

//SMS - Add Event in Google Calendar
if($load >= LEVEL_SMS){
    $text = "Alarm high server load (".$load.") ".SERVER_NAME;
    $service = Zend_Gdata_Calendar::AUTH_SERVICE_NAME; // service name Google Calendar
    $client = Zend_Gdata_ClientLogin::getHttpClient(EMAIL,PASS,$service);

    // Hour and minute with 2 minutes delayment
    $hour= date("H");
    $minute= date("i")+2;

    // Creates the event in Google Calendar
    createQuickAddEvent($client, $text." ".$hour.":".$minute);
}
//END NOTIFICATIONS

Now just put the script in you cron for example every 15 minutes,

*/15 * * * * /root/scripts/SMS_High_Server_Load/sms-load.php >/dev/null 2>&1

Other ideas, using this system is to send SMS when your site is down with this script.