-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
208 lines (192 loc) · 6.7 KB
/
main.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
var mongoClient = require('mongodb').MongoClient,
config = require('./conf/config'),
Hipchatter = require('hipchatter'),
redis = require('redis');
var hipchatter = new Hipchatter(process.env.HIPCHAT_TOKEN);
var ignored = [];
var notified = [];
mongoClient.connect('mongodb://' + config.mongo_host + ':' + config.mongo_port + '/' + config.mongo_database, function(err, conn) {
if (err) {
console.log(err.message);
throw new Error(err);
} else {
main(conn);
}
});
function main(db) {
var opts;
var subscriber = redis.createClient();
subscriber.subscribe('events');
subscriber.on('message', function (channel, event) {
console.log(event);
event = JSON.parse(event);
if (event.type == 'trigger' && (event.level == 'alarmed' || event.level == 'fired') && ignored.indexOf(event.hostname) === -1) {
if (processEvent(event)) {
opts = {
message: 'Server ' + event.hostname + ' ' + event.level + ' with ' + event.value + ' ' + event.sensor,
color: 'red',
token: process.env.HIPCHAT_TOKEN_ROOM
};
hipchatter.notify(process.env.HIPCHAT_ROOM, opts, function(err) {});
notified.push({
'hostname': event.hostname,
'time': new Date().getTime() / 1000
});
}
} else if (event.type == 'feed') {
opts = {
message: 'Feed ' + event.feed + ' reporting ' + event.url,
color: 'red',
token: process.env.HIPCHAT_TOKEN_ROOM
};
hipchatter.notify(process.env.HIPCHAT_ROOM, opts, function(err) {});
} else if (event.type == 'plugins') {
opts = {
message: event.message,
color: 'yellow',
token: process.env.HIPCHAT_TOKEN_ROOM
};
hipchatter.notify(process.env.HIPCHAT_ROOM, opts, function(err) {});
}
});
var last = new Date().getTime();
setInterval(function() {
hipchatter.history(process.env.HIPCHAT_ROOM, function(err, history) {
if (history && history.items && history.items.length > 0) {
var aux = history.items;
for (var i = 0; i < aux.length; i++) {
if (aux[i].message.indexOf('@jarvis') === 0 && new Date(aux[i].date).getTime() > last) {
console.log(aux[i]);
last = new Date(aux[i].date).getTime();
processMsg(aux[i]);
}
}
}
});
}, 15000);
function processEvent(event) {
var notified = findNotified(event.hostname);
var now = new Date().getTime() / 1000;
if (!notified) {
return true;
} else if (notified && now - notified.time > 300) {
removeNotified(event.hostname);
return true;
}
return false;
}
function findNotified(hostname) {
for (var i = 0; i < notified.length; i++) {
if (notified[i].hostname === hostname) {
return notified[i];
}
}
}
function removeNotified(hostname) {
for (var i = 0; i < notified.length; i++) {
if (notified[i].hostname === hostname) {
notified.splice(i, 1);
}
}
}
function processMsg(msg) {
var publisher = redis.createClient();
var msgd = msg.message.trim().replace(/ /g, ' ').split(' ');
if (msgd.length == 2 && msgd[1].toLowerCase() === 'help') {
var opts = {
message: '@jarvis suspend/unsuspend server domain -- @jarvis lock server ip reason -- @jarvis unlock server ip -- @jarvis ping -- @jarvis feeds 10 -- @jarvis muted -- @jarvis mute/unmute server',
color: 'green',
token: process.env.HIPCHAT_TOKEN_ROOM
};
hipchatter.notify(process.env.HIPCHAT_ROOM, opts, function(err) {});
} else if (msgd.length == 4 && msgd[1].toLowerCase() === 'suspend') {
publisher.publish('suspend', JSON.stringify({
type: 'suspend',
hostname: msgd[2],
domain: msgd[3]
}));
} else if (msgd.length == 4 && msgd[1].toLowerCase() === 'unsuspend') {
publisher.publish('suspend', JSON.stringify({
type: 'unsuspend',
hostname: msgd[2],
domain: msgd[3]
}));
} else if (msgd.length == 4 && msgd[1].toLowerCase() === 'unlock') {
publisher.publish('csf', JSON.stringify({
type: 'unlock',
hostname: msgd[2],
ip: msgd[3]
}));
} else if (msgd.length >= 5 && msgd[1].toLowerCase() === 'lock') {
var reason = '';
for (var i = 4; i < msgd.length; i++) {
reason = reason + ' ' + msgd[i];
}
reason.trim();
publisher.publish('csf', JSON.stringify({
type: 'lock',
hostname: msgd[2],
ip: msgd[3],
reason: reason
}));
} else if (msgd.length == 2 && msgd[1].toLowerCase() === 'ping') {
var opts = {
message: 'Pong...',
color: 'green',
token: process.env.HIPCHAT_TOKEN_ROOM
};
hipchatter.notify(process.env.HIPCHAT_ROOM, opts, function(err) {});
} else if (msgd.length == 3 && msgd[1].toLowerCase() === 'mute') {
ignored.push(msgd[2]);
var opts = {
message: msgd[2] + ' was muted.',
color: 'green',
token: process.env.HIPCHAT_TOKEN_ROOM
};
hipchatter.notify(process.env.HIPCHAT_ROOM, opts, function(err) {});
} else if (msgd.length == 3 && msgd[1].toLowerCase() === 'unmute') {
var ind = ignored.indexOf(msgd[2]);
if (ind != -1) {
ignored.splice(ind, 1);
var opts = {
message: msgd[2] + ' was unmuted.',
color: 'green',
token: process.env.HIPCHAT_TOKEN_ROOM
};
hipchatter.notify(process.env.HIPCHAT_ROOM, opts, function(err) {});
} else {
var opts = {
message: msgd[2] + ' is not muted.',
color: 'yellow',
token: process.env.HIPCHAT_TOKEN_ROOM
};
hipchatter.notify(process.env.HIPCHAT_ROOM, opts, function(err) {});
}
} else if ((msgd.length == 3 || msgd.length == 2) && msgd[1].toLowerCase() === 'feeds') {
var limit = parseInt(msgd[2]) || 3;
db.collection('feeds').find({}).sort({
date: -1
}).limit(limit).toArray(function(err, feeds) {
var aux = '';
for (var i = 0; i < feeds.length; i++) {
aux += feeds[i].url + ' --- ';
}
aux = aux.substring(0, aux.length - 5);
var opts = {
message: 'Last ' + limit + ' feeds: ' + aux,
color: 'green',
token: process.env.HIPCHAT_TOKEN_ROOM
};
hipchatter.notify(process.env.HIPCHAT_ROOM, opts, function(err) {});
});
} else if (msgd.length == 2 && msgd[1].toLowerCase() === 'muted') {
var opts = {
message: ignored.toString(),
color: 'green',
token: process.env.HIPCHAT_TOKEN_ROOM
};
hipchatter.notify(process.env.HIPCHAT_ROOM, opts, function(err) {});
}
}
console.log('Notifier started.');
}