Last active
September 13, 2019 09:14
-
-
Save ishideo/9385de77a9b673b6d60b297e8d10df0b to your computer and use it in GitHub Desktop.
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
'use strict'; | |
if ((typeof GasTap)==='undefined') { | |
eval(UrlFetchApp.fetch('https://raw.githubusercontent.com/zixia/gast/master/src/gas-tap-lib.js').getContentText()) | |
} | |
var test = new GasTap() | |
function testServerWatchAlert() { | |
test('Got alive response code', function (t) { | |
var response = checkHttpResponse('https://www.github.com') | |
t.equal(response, 200, 'got 200') | |
}) | |
test('Got dead response code', function (t) { | |
var response = checkHttpResponse('http://xxx.xxx.com') | |
t.equal(response, 999, 'got 999.') | |
}) | |
test("Got dead server's URL.", function (t) { | |
var url = ['https://www.github.com', 'https://gist.github.com', 'http://x.x.x.x'] | |
var url_expected = ['http://x.x.x.x'] | |
var url_dead = getDeadUrl(url) | |
t.equal(JSON.stringify(url_dead), JSON.stringify(url_expected), "got dead URL") | |
}) | |
test("Got no dead server's URL", function (t) { | |
var url = ['https://www.github.com', 'https://gist.github.com'] | |
var url_expected = [] | |
var url_dead = getDeadUrl(url) | |
t.equal(JSON.stringify(url_dead), JSON.stringify(url_expected), "got no URL") | |
}) | |
test('Got messages from dead servers', function (t) { | |
var url = ['http://xxx.xxx.xxx', 'http://x.x.x.x'] | |
var msgs_expected = ['This server is dead: ' + url[0], 'This server is dead: ' + url[1]] | |
var msgs_dead = url.map(sendMessage) | |
t.equal(JSON.stringify(msgs_dead), JSON.stringify(msgs_expected), 'These messages equal expected messages') | |
}) | |
test.finish() | |
} |
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
'use strict'; | |
if ((typeof SlackApp)==='undefined') { | |
eval(UrlFetchApp.fetch('https://raw.githubusercontent.com/soundTricker/SlackApp/master/bin/Code.gs').getContentText()) | |
} | |
function serverWatchAlert() { | |
var urls = ['http://www.google.com', | |
'http://www.yahoo.com']; | |
var urls_dead = getDeadUrls(urls); | |
if (urls_dead != []) { Logger.log(urls_dead.map(sendMessage)) }; | |
} | |
function getDeadUrls(urls){ | |
var urls_dead = urls.filter( function(s) { if (checkHttpResponse(s)!=200) return s; }); | |
return urls_dead; | |
} | |
function checkHttpResponse(url){ | |
var options = { | |
"muteHttpExceptions" : true, | |
"validateHttpsCertificates" : false, | |
"followRedirects" : false, | |
} | |
try{ | |
var response = UrlFetchApp.fetch(url, options); | |
return response.getResponseCode(); | |
} catch(e){ | |
return 999; | |
} | |
} | |
function sendMessage(url){ | |
var token = PropertiesService.getScriptProperties().getProperty('SLACK_ACCESS_TOKEN'); | |
var slackApp = SlackApp.create(token); | |
var options = { | |
channelId: "#general", | |
userName: "bot", | |
iconUrl: "https://www.gstatic.com/images/icons/material/product/2x/apps_script_64dp.png", | |
}; | |
var message = 'this www server is dead' + url; | |
slackApp.postMessage(options.channelId, message, { | |
username: options.userName, | |
icon_url: options.iconUrl, | |
}); | |
return message; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment