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','[email protected]');//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.
BTW I was having some troubles with the cron, doesn’t work the php execution,what I did then is to create in one of my sites a protected folder, and use cron with curl:
curl http://nyoursite.com/protected_folder/sms-load.php
Thanks. An excellent script and a very handy use of the calendar SMS.
Welcome!
Hi there,
I am using your script and it works, it creates an event in my google calander but it creates an event without SMS notification, i enabled it in google and when i manually create an event whe SMS remember option is there.
do you know why this happen?
greetings,
Stefan van der Ploeg
Hello,
You need that for the calendars that you create by default they should have the SMS notification, otherwise won’t work ;)
It is standing on default, when i make an event my self the sms notification is on, when the script does sms notification isnt, i u want it more clearly i can send u some screenshots
But belongs to the same calendar? cuz then I don’t know. Maybe google changed the way it works.
oke thanks anyway
Amazing! I just tried this and it did work perfectly to me! Thanks man!