A few months ago I came across the article Monitor your Website's Uptime with Google Docs by Amit Agarwal. His idea to use Google Apps Script for monitoring Websites is compelling, it's free and downtimes of your monitoring script are very unlikely.
I've used Amit's solution for a few sites, but I didn't like to have one copy of the script per site. So I created the simple monitoring script below, that retrieves all URLs to check from one Spreadsheet. While this solution is more convenient to use and maintain, especially if you monitor many sites, it sacrifices flexibility.
I think the code is pretty easy to follow, it basically gets the rows from the current spreadsheet, performs an HTTP request for each URL, and in case of an exception, adds the status code and the URL to the
UrlFetchApp.fetch throws an exception, instead of returning an object, that allows to retrieve the HTTP status code, therefore the gross exception message parsing.
This has been a known issue since 2010, and the ugly design design was supposed to be fixed according to a reply from a Google employee. Meanwhile, the Google Apps Script group was closed and support moved to Stack Overflow. So be it.
Updated the code to include the
muteHttpExceptions parameter to
UrlFetchApp.fetch() as pointed out by Eric Koleda in the comments below.
At the end of the code, an email will be sent to the owner of the document, if errors were encountered during script execution.
Script Editor. When you do this the first time for a document, you'll see a popup like the one below, simply close it.
Current script's triggers....
No triggers set up. Click here to add one now.link.
checkURLsis already selected, since it's the only function.
Time-driven, and from the next dropdowns the interval you deem appropriate. If you set it to run every 6 hours, it will look like the following:
savebutton. Doing this the first time, you'll be prompted to authorize the script, to do so click on the
Even though this is a straightforward process, I'm sure you get, why I don't want to repeat it for every site. Now, whenever I add a URL it will be monitored from the next execution of the script. To stop monitoring a certain site, the corresponding row simply needs to be deleted in the spreadsheet.
You may want to set different intervals or specific times for checking sites, or different email recipients, which doesn't work with this solution. Of course, you can have several copies with sets of URLs, but this would defeat the purpose of having something more maintainable.
For more sophisticated stuff, you will need to extend the script's functionality. That being said, Google Apps Script offers a rich set of APIs and an extensive developer documentation.
One more thing to keep in mind are quota limits for the different services used in scripts, in this case HTTP requests and sending emails. For more information see the table on the Apps Script Dashboard's Quota Limits tab.
Ramiro is a developer who likes open source, data mining, visualization, and writing. To learn more about him and this site, see the about page.google apps script web development code