-
Notifications
You must be signed in to change notification settings - Fork 2
/
LambdaWebCheck.js
69 lines (50 loc) · 1.71 KB
/
LambdaWebCheck.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Check if a url is responding and logs an event to AWS CloudWatch
// (C) ActValue 2016 - pmosconi
"use strict";
// external dependencies
const AWS = require('aws-sdk');
const request = require('request');
// PUT YOUR URL HERE
const url = 'https://mywebsite.com/';
const timeout = 10000; // ms to wait for response
// get reference to cloudwatch
const cloudwatch = new AWS.CloudWatch();
exports.handler = (event, context, callback) => {
// ignore invalid SSL certificate
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
// call url and if response code is not 200 something is not working
request.get(url, {timeout: timeout},
(err, response, body) => {
let value = 0;
if (err) {
console.log('Error: ' + err);
value = 1;
}
else if (response.statusCode !== 200) {
console.log('Status Code: ' + response.statusCode);
value = 1;
}
else console.log('Status Code: 200');
let params = {
MetricData: [ /* required */
{
MetricName: 'WebSiteNotResponding', /* required */
Dimensions: [
{
Name: 'url', /* required */
Value: url /* required */
}
],
Timestamp: new Date,
Unit: 'Count',
Value: value
}
],
Namespace: 'ActValue' /* required */
};
cloudwatch.putMetricData(params, (err, data) => {
if (err) callback(err, 'KO');
else callback(null, data);
});
});
};