Tuesday, October 16, 2012

Monitor server down and get email notification



Reference:
http://techawakening.org/website-downtime-free-sms-alerts-google-docs/988/ 

Using Google document to monitor web server down. The notification  can be sent by an email and posted in your Google calendar.
1) Click here to make copy of the Google Spreadsheet, assuming you have login your Google account.
2) Modify the websites and email address to yours in F column in your copy of Google document.
3)  Now select Tools-> Script editor  in your copy of Google document.
(note there is a bug in the script:
line 71 change
 var email = sheet.getRange("F5").getValue();
to
 var email = sheet.getRange("F8").getValue();
)
you can see:
 /**   Get Free SMS alerts for your Site's Uptime and Downtime  **/

/**   Original Script by Amit Agarwal 26/03/2012  **/
/**   http://labnol.org/?p=33232  **/

/*    Modified by Shunmugha Sundaram to get SMS alerts
      and to support multiple site monitoring- 31/03/2012 */
/*    Instructions at http://techawakening.org/?p=988 */
function isMySiteDown()
{
var i=3; 
// Tweak the loop variable if you want to monitor more then 4 sites.
for (i=3;i<=6;i++)
   {   
  // In cell E3 enter the URL of the site you want to monitor  
  var url = SpreadsheetApp.getActiveSheet().getRange("F"+i).getValue();
        if(url.length)
         {
           var scriptvalue=ScriptProperties.getProperty("status"+ i);
  if (!scriptvalue) {
     ScriptProperties.setProperty("status" + i, 200);
   
  }
            
  var response, error;

  try {   
    response = UrlFetchApp.fetch(url);
  
  } catch(error)
   
  {  
   insertData(error, -1, url, i, "Website down");
   continue;
  }
 
  var code = response.getResponseCode();
 
  if (code == 200)
  {
    {
    insertData("Up", code, url, i, "Website up"); }
   
  }
  else
    insertData(response.getContent()[0], code,url, i,"Website down");
     //end if
          }
  else
  {
    //Don't poll it
  }
     }
}
function insertData(error, code, url, i, msg) { 
 
  
  if (ScriptProperties.getProperty("status"+i) == code)
  {
   return;
  }
    
  var sheet = SpreadsheetApp.getActiveSheet();
  var email = sheet.getRange("F8").getValue();
  var row   = sheet.getLastRow() + 1;
  var now = new Date().getTime();
 
  sheet.getRange(row,1).setValue(new Date());
  sheet.getRange(row,2).setValue(url);
  sheet.getRange(row,3).setValue(error);
  sheet.getRange(row,4).setValue(code);
 
  ScriptProperties.setProperty("status"+i, code);
 
  if(code!=200)
   
  CalendarApp.createEvent( url + ' is DOWN' ,
                            new Date(now+40000),
                            new Date(now+40000)).addSmsReminder(0);
  else
   
  CalendarApp.createEvent( url + ' is UP' ,
                            new Date(now+40000),
                            new Date(now+40000)).addSmsReminder(0);
 
 // Uncomment the below line if you want to get Email alerts too.
 
  //MailApp.sendEmail(email, msg, error);
}

4. If you want to monitor more than 4 sites (max 4 sites are current setting), you can change the loop
for (i=3;i<=6;i++) in the script.
If you want to receive email notification, uncomment the last line:
 MailApp.sendEmail(email, msg, error);
5)  Important step: set your  trigger.
 Go to Tools – > Script Editor – > Resources – > Current Script’s Triggers,  Under Run select “isMySiteDown” then in set  a Time-Driven trigger for every 15 minutes or others .
6) Now you can check your servers down information in your copy of Google document and email.



No comments:

Post a Comment