In this short tutorial we will see how to built an application that can be used as a (simple) Nagios but, without the need to set up a server and maintain a lot of code. It’s not a replacement to a ‘full’ monitoring systems that give you rich set of abilities but more ‘an idea’ to show you the power of Google cloud with Apps script. The main three components of our system will be:
- Google Spreadsheet – We will use two sheets. One to allow us to set configuration data like: urls to monitor, email to get notifications etc’. The other will be our ‘log’. We will write to it all the alerts so we could analyze them later.
- Google Form – This will be a quick and easy way to acknowledge that we are aware for the situation and we wish to mute the emails.
- Apps Script – All the logic for the checks, alerts and everything in between will be done with a short script with one trigger.
For the first two components, we will use a regular google sheet that looks similar to this:
and a form that will enable us to mute the alert:
Let’s see some code snippets. First we will create the main function that will fetch our site and analyze the content in order to see that we present the user a valid page.
Now that we have the ‘fetcher’ we can add a logic to alert only if we have 2 (or more) failures in X minutes. We are using the triggers in order to run the checks every 5 minutes and later a time based trigger to do another 2nd check. This will minimize the false-positive cases of alerts. You can also think on a mechanism that will stop the alerts after Y emails. For the brave ones, you can send SMS (or voice!) with the integration of Apps script with Twilio!
Other things to note in the script above are the usage of the spreadsheet a our simple database. You can do something better with ScriptDb and have more powerful with this object database. In case you do use ScriptDB, it will be great to checkout ScriptDB common patterns as it gives you the functions you will need. Happy hacking.