Website Monitor with Google Scripts

W
Published in: Google Apps Script

Use Google Apps Script to monitors website domains and send SMS text alerts when any of these sites are down or inaccessible.


function init() {
  if (ScriptApp.getScriptTriggers().length == 0) {
    // Set up a monitor that triggers every 5 minutes
    ScriptApp.newTrigger("websiteMonitor")
      .timeBased().everyMinutes(5).create();
  }
}

function websiteMonitor()
{
  var response, error, code, urls;

  // The script can monitor multiple website URLs (comma separated)
  urls = SpreadsheetApp.getActiveSheet().getRange("B2").getValue();
  urls = urls.replace(/\s/g, "").split(",");

  for (var i=0; i<urls.length; i++) {

    var url = urls[i];

    if (!ScriptProperties.getProperty(url)) {
        ScriptProperties.setProperty(url, 200);
    }

    // Trying to connect to the website URL
    try {
      response = UrlFetchApp.fetch(url);
    } catch(error) {
      // If URLFetchApp fails, the site is probably down
      updateLog(url, -1);
      continue;
    }

    code = response.getResponseCode();
    updateLog(url, code);

  }
}

function updateLog(url, code) {

  if (ScriptProperties.getProperty(url) == code)
    return;

  ScriptProperties.setProperty(url, code);

  var sheet = SpreadsheetApp.getActiveSheet();

  var row   = sheet.getLastRow() + 1;
  var time  = new Date();
  var msg = "Down";

  if (code == 200)
    msg = "Up";

  msg = "Website is " + msg + "  " + url;

  sheet.getRange(row,1).setValue(time);
  sheet.getRange(row,2).setValue(msg);

  // Send an email notification when the site status changes
  var email = sheet.getRange("B3").getValue();
  MailApp.sendEmail(email, msg, url);

  var now   = new Date(time.getTime() + 10000);

  // Create an event in Google Calendar with an SMS reminder
  if (sheet.getRange("B4").getValue().toLowerCase() == "yes")
    CalendarApp.createEvent(msg, now, now).addSmsReminder(0);
}
📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Google Apps Script

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube