-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbutler-sos-influxdb.js
229 lines (181 loc) · 7.81 KB
/
butler-sos-influxdb.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
// Add dependencies
var request = require('request');
// Load code from sub modules
var globals = require('./globals');
// Set specific log level (if/when needed)
// Possible values are { error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5 }
// globals.logger.transports.console.level = 'info';
globals.logger.transports.console.level = 'verbose';
// globals.logger.transports.console.level = 'debug';
globals.logger.info('Starting Butler SOS/Influxdb');
function postToInfluxdb(host, serverName, body) {
// Calculate server uptime
var dateTime = Date.now();
var timestamp = Math.floor(dateTime);
var str = body.started;
var year = str.substring(0, 4);
var month = str.substring(4, 6);
var day = str.substring(6, 8);
var hour = str.substring(9, 11);
var minute = str.substring(11, 13);
var second = str.substring(13, 15);
var dateTimeStarted = new Date(year, month - 1, day, hour, minute, second);
var timestampStarted = Math.floor(dateTimeStarted);
var diff = timestamp - timestampStarted;
// Create a new JavaScript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds.
var date = new Date(diff);
var days = Math.trunc((diff) / (1000 * 60 * 60 * 24))
// Hours part from the timestamp
var hours = date.getHours();
// Minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// Seconds part from the timestamp
var seconds = "0" + date.getSeconds();
// Will display time in 10:30:23 format
var formattedTime = days + ' days, ' + hours + 'h ' + minutes.substr(-2) + 'm ' + seconds.substr(-2) + 's';
// Write the whole reading to Influxdb
globals.influx.writePoints([{
measurement: 'sense_server',
tags: {
host: serverName
},
fields: {
version: body.version,
started: body.started,
uptime: formattedTime
}
},
{
measurement: 'mem',
tags: {
host: serverName
},
fields: {
comitted: body.mem.comitted,
allocated: body.mem.allocated,
free: body.mem.free
}
},
{
measurement: 'apps',
tags: {
host: serverName
},
fields: {
active_docs_count: body.apps.active_docs.length,
loaded_docs_count: body.apps.loaded_docs.length,
calls: body.apps.calls,
selections: body.apps.selections
}
},
{
measurement: 'cpu',
tags: {
host: serverName
},
fields: {
total: body.cpu.total
}
},
{
measurement: 'session',
tags: {
host: serverName
},
fields: {
active: body.session.active,
total: body.session.total
}
},
{
measurement: 'users',
tags: {
host: serverName
},
fields: {
active: body.users.active,
total: body.users.total
}
},
{
measurement: 'cache',
tags: {
host: serverName
},
fields: {
hits: body.cache.hits,
lookups: body.cache.lookups,
added: body.cache.added,
replaced: body.cache.replaced,
bytes_added: body.cache.bytes_added
}
}
])
.then(err => {
globals.logger.verbose('Sent data to Influxdb');
})
.catch(err => {
console.error(`Error saving data to InfluxDB! ${err.stack}`)
})
}
function postToMQTT(host, serverName, body) {
// Get base MQT topic
var baseTopic = globals.config.get('Butler-SOS-Influxdb.mqttConfig.baseTopic');
// Send to MQTT
globals.mqttClient.publish(baseTopic + serverName + '/version', body.version);
globals.mqttClient.publish(baseTopic + serverName + '/started', body.started);
globals.mqttClient.publish(baseTopic + serverName + '/mem/comitted', body.mem.comitted.toString());
globals.mqttClient.publish(baseTopic + serverName + '/mem/allocated', body.mem.allocated.toString());
globals.mqttClient.publish(baseTopic + serverName + '/mem/free', body.mem.free.toString());
globals.mqttClient.publish(baseTopic + serverName + '/cpu/total', body.cpu.total.toString());
globals.mqttClient.publish(baseTopic + serverName + '/session/active', body.session.active.toString());
globals.mqttClient.publish(baseTopic + serverName + '/session/total', body.session.total.toString());
globals.mqttClient.publish(baseTopic + serverName + '/apps/active_docs', body.apps.active_docs.toString());
globals.mqttClient.publish(baseTopic + serverName + '/apps/loaded_docs', body.apps.loaded_docs.toString());
globals.mqttClient.publish(baseTopic + serverName + '/apps/calls', body.apps.calls.toString());
globals.mqttClient.publish(baseTopic + serverName + '/apps/selections', body.apps.selections.toString());
globals.mqttClient.publish(baseTopic + serverName + '/users/active', body.users.active.toString());
globals.mqttClient.publish(baseTopic + serverName + '/users/total', body.users.total.toString());
globals.mqttClient.publish(baseTopic + serverName + '/cache/hits', body.cache.hits.toString());
globals.mqttClient.publish(baseTopic + serverName + '/cache/lookups', body.cache.lookups.toString());
globals.mqttClient.publish(baseTopic + serverName + '/cache/added', body.cache.added.toString());
globals.mqttClient.publish(baseTopic + serverName + '/cache/replaced', body.cache.replaced.toString());
globals.mqttClient.publish(baseTopic + serverName + '/cache/bytes_added', body.cache.bytes_added.toString());
if (body.cache.lookups > 0) {
globals.mqttClient.publish(baseTopic + serverName + '/cache/hit_ratio', Math.floor(body.cache.hits / body.cache.lookups * 100).toString());
// console.log(Math.floor(body.cache.hits / body.cache.lookups * 100).toString());
}
}
function getStatsFromSense(host, serverName) {
request({
followAllRedirects: true,
url: 'https://' + host + '/healthcheck/engine/healthcheck/',
headers: {
'Cache-Control': 'no-cache'
},
json: true
}, function (error, response, body) {
// Check for error
if (error) {
return globals.logger.error('Error:', error);
}
if (!error && response.statusCode === 200) {
globals.logger.verbose('Received ok response from ' + serverName);
globals.logger.debug(body);
// globals.logger.info(body.apps.loaded_docs.length);
// Post to MQTT
postToMQTT(host, serverName, body);
// Post to Influxdb
postToInfluxdb(host, serverName, body);
}
})
}
setInterval(function () {
globals.logger.verbose('Event started: Statistics collection');
var serverList = globals.config.get('Butler-SOS-Influxdb.serversToMonitor.servers');
serverList.forEach(function (server) {
globals.logger.verbose('Getting stats for server: ' + server.serverName);
getStatsFromSense(server.host, server.serverName);
});
}, globals.config.get('Butler-SOS-Influxdb.pollingInterval'));