Last active
June 13, 2017 20:57
-
-
Save filipenevola/52ce17ef73b271a02ca261b93177c55c to your computer and use it in GitHub Desktop.
Google Script to Monitor Websites (like pingdom) and maybe restart the server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// spreadsheet attached as csv just for example | |
var sheet = SpreadsheetApp.getActiveSheet(), | |
startRow = 2, // skips header line | |
lastRow = sheet.getLastRow() -1, // get last line with data | |
dataRange = sheet.getRange(startRow, 1, lastRow, 6), // first value is the row, second is the column in number | |
data = dataRange.getValues(); // get data of every cell within the range | |
var FOOTER = "<br/><br/><a href=\"https://script.google.com/macros/d/MlurQVKrLcdi-mMWhhh3KoQL0bBO3PCUx/edit?uiv=2&mid=ACjPJvGlhxliD17ZP1feshkqGxfbplgytbuk6ZUR4bCV3Wt2ChtUk_rI3XArfC1JTrz4lhUmJpwICfja09S1tR1YRkS03LIgjPNmk7d7sjI1PUibdnPY_BNZr6E5Z_mJ1LWVam3p0lX9xps\">editar script</a>"; | |
function checkAllSites() { | |
var nao = "NÃO"; | |
var sim = "SIM"; | |
for (var i = 0; i < data.length; i++) { | |
var row = data[i], | |
siteUrl = row[0], | |
dropletId = row[1], | |
notificationEmail = row[2], | |
currentRow = (startRow+i), | |
wasItAvailable = sheet.getRange(currentRow, 4), //getRange is 1-based instead of 0-based | |
lastCheckDate = sheet.getRange(currentRow, 5), | |
lastDownDate = sheet.getRange(currentRow, 6), | |
lastUpDate = sheet.getRange(currentRow, 7), | |
countingDowns = sheet.getRange(currentRow, 8), | |
reason = sheet.getRange(currentRow, 9), | |
timestamp = Utilities.formatDate(new Date(), "America/Sao_Paulo", "dd/MM/yyyy HH:mm:ss"); | |
if(!pingSite(siteUrl)) { // Let's try to open the site and check if we get a success response | |
countingDowns.setValue(parseInt(countingDowns.getValue()) + 1); | |
reason.setValue(getResponseCode(siteUrl)); | |
lastDownDate.setValue(timestamp); | |
if((wasItAvailable.getValue() == "" || wasItAvailable.getValue() == sim) && parseInt(countingDowns.getValue()) > 2) { | |
var text = ""; | |
var restarted = restart(siteUrl, dropletId); | |
if(restarted) { | |
text = "restarted - "; | |
} | |
send(notificationEmail, text + "DOWN - Site Indisponível - " + siteUrl, "Site Indisponível " + siteUrl + " em " + timestamp + "!" + FOOTER); | |
wasItAvailable.setValue(nao); | |
} | |
} else { | |
countingDowns.setValue("0"); | |
lastUpDate.setValue(timestamp); | |
if(wasItAvailable.getValue() == "" || wasItAvailable.getValue() == nao){ | |
// UP | |
send(notificationEmail, "UP - Site Disponível - " + siteUrl, "Site Disponível " + siteUrl + " em " + timestamp + "!" + FOOTER); | |
wasItAvailable.setValue(sim); | |
} | |
} | |
lastCheckDate.setValue(timestamp); | |
} | |
}; | |
function restart(siteUrl, dropletId) { | |
if(!dropletId) { | |
return false; | |
} | |
var url = 'https://api.digitalocean.com/v2/droplets/' + dropletId + '/actions'; | |
var options = | |
{ | |
"muteHttpExceptions": true, | |
"method" : "post", | |
"payload" : { "type": "reboot" }, | |
"headers": { "contentType": "application/json", "Authorization": "Bearer XXXXXXXXXXXXXXXXXXXx" } | |
}; | |
Logger.log("post " + url); | |
try { | |
var status = UrlFetchApp.fetch(url, options).getResponseCode(); | |
return success(status); | |
} catch(error) { | |
Logger.log(error); | |
return false; | |
} | |
}; | |
function getResponseCode(url) { | |
return UrlFetchApp.fetch(url, {"muteHttpExceptions": true}).getResponseCode(); | |
} | |
function pingSite(url) { | |
Logger.log("get " + url); | |
try { | |
var status = getResponseCode(url); | |
return success(status); | |
} catch(error) { | |
Logger.log(error); | |
return false; | |
} | |
}; | |
function success(status) { | |
Logger.log(status); | |
if (status >= 200 && status <= 206) { | |
return true; | |
} | |
return false; | |
} | |
function send(to, subject, body) { | |
MailApp.sendEmail({ | |
to: to, | |
subject: subject, | |
htmlBody: body | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Site URL | Droplet Id | Notificar | Rodando? | Último Teste | Último Down | Último Up | Contagem Downs | Reason | |
---|---|---|---|---|---|---|---|---|---|
http://agendatenis.com | [email protected] | NÃO | 16/12/2016 19:40:56 | 16/12/2016 19:40:56 | 27/09/2016 02:45:59 | 22973 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment