-
Notifications
You must be signed in to change notification settings - Fork 1
/
CloudWatchToHipchat.js
58 lines (48 loc) · 1.7 KB
/
CloudWatchToHipchat.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
var http = require('https');
// update with your hipchat data
var hipchatToken = 'xxxxxxxxxxxxxxxxxxx';
var hipchatRoom = '1111111';
exports.handler = function(event, context) {
var alarm = JSON.parse(event.Records[0].Sns.Message);
//console.log(alarm);
var snsMessage = '(' + alarm.AlarmName + '){' + alarm.NewStateValue + '} ' + alarm.NewStateReason;
var hipchatColor = alarm.NewStateValue === 'ALARM' ? 'red' : 'green';
var hipchatMessage = JSON.stringify({
color: hipchatColor,
message: snsMessage,
notify: true,
message_format: 'text',
});
console.log('hipchat message: ' + hipchatMessage);
var httpOptions = {
host: 'api.hipchat.com',
port: 443,
method: 'POST',
path: '/v2/room/' + hipchatRoom + '/notification?auth_token=' + hipchatToken,
headers: {
'Content-Type': 'application/json',
}
};
var req = http.request(httpOptions, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
res.on('end', function () {
console.log(res.statusCode);
if (res.statusCode === 204) {
console.log('success');
context.succeed('message delivered to hipchat');
} else {
console.log('failed');
context.fail('hipchat API returned an error');
}
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
context.fail('failed to deliver message to hipchat');
});
req.write(hipchatMessage);
req.end();
};