Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I modified Amit Agarwal's Script which checks the uptime of one website so that it now checks several sites. The script runs every 5 minutes, however, I kept receiving mails that a site was down and then 5 mins later that it was up again.

I adapted my code to only log a change if it persisted for two checks, like this:

function siteUptimeCheck(url, row) {
    var response, error;
    var status = "status"+row.toString();         // status1, status2..
    if (!ScriptProperties.getProperty(status))
      ScriptProperties.setProperty(status, 200);
    try {                                         // try, catch, else
        var success = true;
        response = UrlFetchApp.fetch(url);
    } catch(error) {
        success = false;    
        insertData(status, url, row, error, response.getResponseCode(), "Site down: ");
        return;
    } if(success) {
        insertData(status, url, row, "Up", response.getResponseCode(), "Site up: ");
    }
}

function insertData(status, url, row, error, code, msg) {
      if (ScriptProperties.getProperty(status) == code) {    
          return;                                  // Return if there are no changes
      } else if (ScriptProperties.getProperty(status) == 1) {
          sheet.getRange(row,2).setValue(code);
          if (code != 200) {
            //create newdate log entry
            sheet.getRange(row,3).setValue(newdate +" "+ error);
          }
          ScriptProperties.setProperty(status, code);
          MailApp.sendEmail(email, msg, error);
     } else {
          ScriptProperties.setProperty(status, 1);  // Marker for "something changed"
  }
}

I'm fairly new to programming, are there better ways to solve this?

share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.