📜 ⬆️ ⬇️

Website monitoring with Google Docs



Digital Inspirations blog author Amid Agarwal (Amit Agarwal) has published a script for Google Docs, which polls the status of the site and puts the answer in a table cell. Thus, you can make a monitoring service yourself. Google Docs allows you to run the script at one minute intervals and receive notifications by email.

The instruction is as follows: copy this document to yourself, enter the monitoring URL in cell E3 and your email address in cell E5, then set the minute trigger to execute the script in Tools → Script Editor → Resources → Current Script's Triggers.


')
When you try to save a trigger, a large red warning will appear with a request for authorization.

Script code

/** Monitor your Site's Uptime **/ function isMySiteDown() { // Get the URL of the Website to monitor var url = SpreadsheetApp.getActiveSheet().getRange("E3").getValue(); // HTTP Response Code of the last server request if (!ScriptProperties.getProperty("status")) { ScriptProperties.setProperty("status", 200); } var response, error; try { // Fetch the web page using UrlFetchApp response = UrlFetchApp.fetch(url); } catch(error) { insertData(error, -1, "Website down"); return; } var code = response.getResponseCode(); // code = 200 means the fetch request was successful if (code == 200) insertData("Up", code, "Website up"); else insertData(response.getContent()[0], code, "Website down"); } function insertData(error, code, msg) { // Ignore if the error message is logged already if (ScriptProperties.getProperty("status") == code) return; // Log the server error in a Google Sheet var sheet = SpreadsheetApp.getActiveSheet(); var email = sheet.getRange("E5").getValue(); var row = sheet.getLastRow() + 1; sheet.getRange(row,1).setValue(new Date()); sheet.getRange(row,2).setValue(error); sheet.getRange(row,3).setValue(code); // Send an email alert for the downtime ScriptProperties.setProperty("status", code); MailApp.sendEmail(email, msg, error); } 

Source: https://habr.com/ru/post/141686/


All Articles