forked from hisener/opsgenie-notifier
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
78 lines (69 loc) · 1.79 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
const path = require('path')
const opsgenie = require('opsgenie-sdk')
const notifier = require('node-notifier')
require('dotenv').config()
opsgenie.configure({
'host': process.env.OG_HOST,
'api_key': process.env.OG_API_KEY
})
let actions = [
{
name: 'Acknowledge Alert',
callback: function (alertId) {
opsgenie.alertV2.acknowledge({ identifier: alertId }, {}, console.log)
}
},
{
name: 'Close Alert',
callback: function (alertId) {
opsgenie.alertV2.close({ identifier: alertId }, {}, console.log)
}
}
]
let knownAlerts = []
setInterval(function () {
getOpenAlerts()
.then(function (alerts) {
alerts.forEach(function (alert) {
if (!knownAlerts.includes(alert.id)) {
console.log(new Date().toISOString(), 'New Alert', alert.id)
knownAlerts.push(alert.id)
notify(alert)
}
})
})
.catch(function (err) {
console.error(new Date().toISOString(), err)
})
}, process.env.INTERVAL || 15000)
function getOpenAlerts () {
return new Promise(function (resolve, reject) {
opsgenie.alertV2.list({
query: 'status: open', // TODO: AND createdAt Date.now()
limit: 3
}, function (err, res) {
if (err) {
return reject(err)
}
resolve(JSON.parse(res).data)
})
})
}
function notify (alert) {
notifier.notify({
title: 'New Alert',
message: alert.message,
icon: path.join(__dirname, 'opsgenie-logo.png'),
wait: true,
closeLabel: 'Ignore',
actions: actions.map(action => action.name),
alertId: alert.id
}, function (err, response, metadata) {
if (err) {
console.error(err)
}
})
}
notifier.on('click', function (notifierObject, options, metadata) {
actions[metadata.activationValueIndex].callback(options.alertId)
})