-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcherbot.js
148 lines (130 loc) · 5.42 KB
/
watcherbot.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
var version = '0.3.0',
sys = require('sys'),
os = require('os'),
fs = require('fs'),
net = require('net'),
util = require('util'),
moment = require('moment'),
xmpp = require('simple-xmpp'),
humanize = require('humanize'),
exec = require('child_process').exec,
//ping = require('ping-wrapper2'),
hostnamearray = os.hostname().split('.'),
hostname = hostnamearray[0];
var config = require('./config.json');
var pretext = config.nick + '> ';
xmpp.on('online', function() {
util.log('Connected to ' + config.host );
xmpp.join(config.muc + '/' + config.nick );
util.log('Joined ' + config.muc );
});
xmpp.on('chat', function(from, message) {
if (message.indexOf('reload') !== -1) {
xmpp.send(from, pretext + 'Exiting on ' + os.hostname());
process.exit();
}
if (message.indexOf('hello') !== -1) {
xmpp.send(from, pretext + 'Hello from ' + os.hostname());
}
if (message.indexOf('version') !== -1) {
xmpp.send(from, pretext + 'WatcherBot ' + version);
}
if (message.indexOf('uptime') !== -1) {
var loadavg = os.loadavg(),
uptime = os.uptime(),
upstring = '';
var years = moment.duration(uptime,'seconds').years(),
months = moment.duration(uptime,'seconds').months(),
days = moment.duration(uptime,'seconds').days(),
hours = moment.duration(uptime,'seconds').hours(),
minutes = moment.duration(uptime,'seconds').minutes();
if (years !== 0) upstring += years + ' years ';
if (months !== 0) upstring += months + ' months ';
if (days !== 0) upstring += days + ' days ';
if (hours !== 0) upstring += hours + ' hours ';
if (minutes !== 0) upstring += minutes + ' minutes ';
xmpp.send(from, pretext + hostname + " " + upstring +
'\n' + pretext + "loadavg: " + loadavg[0].toFixed(2) + " " + loadavg[1].toFixed(2) + " " + loadavg[2].toFixed(2) +
'\n' + pretext + "mem: " + humanize.filesize(os.totalmem()) + "/" + humanize.filesize(os.freemem())
);
}
if (message.indexOf('status') !== -1) {
exec(config.statscmd + ' | grep -e "Services Ok" -e "Hosts Up" | sed "s/ / /g"', function(error, stdout, stderr) {
var array1 = stdout.split("\n");
for ( i = 0; i < array1.length; i++) {
array1[i] = pretext + array1[i];
}
stdout = array1.join("\n");
xmpp.send(from, stdout + config.baseurl + "/icinga/");
});
}
if (message.indexOf('url') !== -1) {
xmpp.send(from, pretext + config.baseurl);
}
});
fs.stat(config.botsock, function (err) {
if (!err) fs.unlinkSync(config.botsock);
var localsocket = net.createServer(function(connection) {
util.log('Socket created at s'+config.botsock);
connection.on('end', function() {
util.log('socket disconnected');
});
connection.on('data', function(chunk) {
xmpp.send(config.muc, pretext + chunk , true);
});
});
localsocket.listen(config.botsock, function() {
util.log('socket bound on '+config.botsock);
});
});
xmpp.on('groupchat', function(conference, from, message, stamp) {
if(from != config.nick)
if (message.indexOf('!hello') !== -1) {
xmpp.send(conference, pretext + 'Hello from ' + os.hostname() , true);
}
if (message.indexOf('!version') !== -1) {
xmpp.send(conference, pretext + 'WatcherBot ' + version , true);
}
if (message.indexOf('!uptime') !== -1) {
var loadavg = os.loadavg(),
uptime = os.uptime(),
upstring = '';
var years = moment.duration(uptime,'seconds').years(),
months = moment.duration(uptime,'seconds').months(),
days = moment.duration(uptime,'seconds').days(),
hours = moment.duration(uptime,'seconds').hours(),
minutes = moment.duration(uptime,'seconds').minutes();
if (years !== 0) upstring += years + ' years ';
if (months !== 0) upstring += months + ' months ';
if (days !== 0) upstring += days + ' days ';
if (hours !== 0) upstring += hours + ' hours ';
if (minutes !== 0) upstring += minutes + ' minutes ';
xmpp.send(conference,
pretext + hostname + " " + upstring +
'\n' + pretext + "loadavg: " + loadavg[0].toFixed(2) + " " + loadavg[1].toFixed(2) + " " + loadavg[2].toFixed(2) +
'\n' + pretext + "mem: " + humanize.filesize(os.totalmem()) + "/" + humanize.filesize(os.freemem()) , true
);
}
if (message.indexOf('!status') !== -1) {
exec(config.statscmd + ' | grep -e "Services Ok" -e "Hosts Up" | sed "s/ / /g"', function(error, stdout, stderr) {
var array1 = stdout.split("\n");
for ( i = 0; i < array1.length; i++) {
array1[i] = pretext + array1[i];
}
stdout = array1.join("\n");
xmpp.send(conference, stdout + config.baseurl + "/icinga/" , true);
});
}
if (message.indexOf('!url') !== -1) {
xmpp.send(conference, pretext + config.baseurl, true);
}
});
xmpp.on('error', function(err) {
console.error(err);
});
xmpp.connect({
jid : config.jid,
password : config.password,
host : config.host,
port : config.port
});