-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.js
247 lines (228 loc) · 8.08 KB
/
index.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const https = require("https")
const http = require("http")
const net = require("net")
const {hrtime} = process
const AWS = require("aws-sdk")
const cloudwatch = new AWS.CloudWatch()
const hrToMs = (timing) => Math.round(timing[0] * 1000 + timing[1] / 1000000)
const hrDiff = (start, end) => hrToMs(end) - hrToMs(start)
const timingsDiff = (timings, key1, key2) =>
(timings[key1] && timings[key2] && hrDiff(timings[key1], timings[key2])) || -1
const defaultTimeout = 2000
const processTimings = function(timings) {
return {
lookup: timingsDiff(timings, "start", "lookup"),
connect: timingsDiff(timings, "lookup", "connect"),
secureConnect: timingsDiff(timings, "connect", "secureConnect"),
readable: timingsDiff(timings, "secureConnect", "readable") || timingsDiff(timings, "connect", "readable"),
close: timingsDiff(timings, "readable", "close"),
total: timingsDiff(timings, "start", "close")
}
}
const createRequest = function(url, callback) {
const handler = url.startsWith("http://") ? http : https
return handler.get(url, callback)
}
const sendData = (data, event) => Promise.all(
data
.reduce((acc, metric) => {
let arr = acc[acc.length - 1]
if (!arr || arr.length >= 10) {
acc.push([metric])
} else {
arr.push(metric)
}
return acc
}, [])
.map(metricData => cloudwatch.putMetricData({
Namespace: event.namespace || "Watchtower",
MetricData: metricData
}).promise())
)
const handlers = {}
/**
* Query HTTP(S) Endpoints and log timings and HTTP status with CloudWatch
*
* @param {Object} event - Requested checks
* @param {Object[]} event.targets - Endpoints to be checked
* @param {string} [event.targets[].url] - Endpoint URL - use for http(s) endpoints
* @param {string} [event.targets[].hostname] - Endpoint Hostname - use for non-http(s) endpoints
* @param {string} [event.targets[].name] - Endpoint Name
* @param {string} [event.targets[].type] - Check type - can be "http(s)" or "port". Defaults to "http(s)"
* @param {string[]} [event.logTimings=["readable", "total"]] - Determine which timings are logged.
* @param {string} [event.namespace="Watchtower"] - CloudWatch namespace
* @param {number} [event.timeout=2000] - Time in ms before requests are aborted.
* @param {function} callback - Lambda callback function
*
* @returns {Promise} - Promise that resolves if all checks were successful and data was stored in CloudWatch
*/
exports.handler = function(event, context, callback) {
const targets = event.targets
if (!targets) callback("No targets given")
const requests = targets.map(target => new Promise((resolve, reject) => {
const data = {
name: target.name || target.url,
timings: {
start: hrtime()
}
}
switch (target.type) {
case "smtp":
handlers.smtp(target, data, event, resolve, reject)
break
case "port":
handlers.port(target, data, event, resolve, reject)
break
default:
handlers.http(target, data, event, resolve, reject)
}
}))
return Promise.all(requests).then(results => {
const timestamp = new Date()
const includedTimings = event.logTimings || ["readable", "total"]
const metricData = results.map(result => {
const timingMetrics = includedTimings.map(timing => {
return {
MetricName: `timing-${timing}`,
Dimensions: [{Name: result.name, Value: `Timing: ${timing}`}],
Value: result.durations[timing],
Unit: "Milliseconds",
Timestamp: timestamp
}
})
return [{
MetricName: "status",
Dimensions: [{Name: result.name, Value: "HTTP Status"}],
Value: result.statusCode,
Timestamp: timestamp
}, ...timingMetrics]
}).reduce((acc, val) => [...acc, ...val], [])
return sendData(metricData, event)
.then(data => {
callback(null, data)
})
.catch(error => {
callback(error, null)
})
}).catch(error => {
callback(error)
})
}
/*
Check handler for HTTP(S)
*/
handlers.http = (target, data, event, resolve, reject) => {
const request = createRequest(target.url, response => {
data.statusCode = response.statusCode
response.once("readable", () => data.timings.readable = hrtime())
response.once("end", () => data.timings.end = hrtime())
})
request.setTimeout(1)
const timeout = setTimeout(() => request.abort(), event.timeout || defaultTimeout)
request.on("socket", socket => {
socket.on("lookup", () => data.timings.lookup = hrtime())
socket.on("connect", () => data.timings.connect = hrtime())
socket.on("secureConnect", () => data.timings.secureConnect = hrtime())
})
request.on("close", () => {
data.timings.close = hrtime()
data.durations = processTimings(data.timings)
clearTimeout(timeout)
resolve(data)
})
request.on("error", () => {
data.timings.close = hrtime()
data.durations = processTimings(data.timings)
data.statusCode = typeof data.statusCode !== "undefined" ? data.statusCode : 0
clearTimeout(timeout)
resolve(data)
})
}
/*
Check handler for ports
*/
handlers.port = (target, data, event, resolve, reject) => {
const socket = new net.Socket()
socket.setTimeout(event.timeout || defaultTimeout)
socket.on("connect",() => {
data.timings.connect = hrtime()
})
socket.on("lookup",() => {
data.timings.lookup = hrtime()
})
socket.on("data",() => {
data.timings.readable = hrtime()
socket.end()
})
socket.on("end",() => {
data.timings.end = hrtime()
})
socket.on("error",() => {
data.timings.close = hrtime()
data.durations = processTimings(data.timings)
data.statusCode = -1
socket.destroy()
resolve(data)
})
socket.on("timeout", () => {
data.timings.close = hrtime()
data.durations = processTimings(data.timings)
data.statusCode = -1
socket.destroy()
resolve(data)
})
socket.on("close", () => {
data.timings.close = hrtime()
data.durations = processTimings(data.timings)
data.statusCode = 0
socket.destroy()
resolve(data)
})
socket.connect(target.port, target.hostname, () => {})
}
handlers.smtp = (target, data, event, resolve, reject) => {
const socket = new net.Socket()
const smtpFlags = {}
socket.setTimeout(event.timeout || defaultTimeout)
socket.setEncoding("utf8")
socket.on("connect",() => {
data.timings.connect = hrtime()
})
socket.on("lookup",() => {
data.timings.lookup = hrtime()
})
socket.on("data",(smtpdata) => {
if(smtpdata.match(/^220/) && smtpFlags.greeting !== true) {
socket.write("EHLO lambda-watchtower.test\r\n","utf8")
smtpFlags.greeting = true
} else if(smtpdata.match(/^250/)) {
data.timings.readable = hrtime()
socket.end()
}
})
socket.on("end",() => {
data.timings.end = hrtime()
})
socket.on("error",() => {
data.timings.close = hrtime()
data.durations = processTimings(data.timings)
data.statusCode = -1
socket.destroy()
resolve(data)
})
socket.on("timeout", () => {
data.timings.close = hrtime()
data.durations = processTimings(data.timings)
data.statusCode = -1
socket.destroy()
resolve(data)
})
socket.on("close", () => {
data.timings.close = hrtime()
data.durations = processTimings(data.timings)
data.statusCode = 0
socket.destroy()
resolve(data)
})
socket.connect(target.port, target.hostname, () => {})
}