-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_monitoring.js
More file actions
352 lines (335 loc) · 13 KB
/
agent_monitoring.js
File metadata and controls
352 lines (335 loc) · 13 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
'use strict';
const { opendir, mkdir, readdir, rmdir, rm, readFile, writeFile, stat } = require('node:fs/promises');
const { sep: PATH_SEP, normalize: normalizePath } = require('node:path');
const { hostname } = require('os');
const {
nop,
createReadyPromise,
aquireCacheWriteLock,
aquireCacheReadLock,
CounterMap,
convertObjectToPrometheusLabels,
startHeartbeat,
} = require('./utils')({
CACHE_HEARTBEAT_INTERVAL: 30_000,
CACHE_HEARTBEAT_OFFSET: 500,
});
const kDataSymbol = Symbol('AgentMonitoringData');
const kCacheSymbol = Symbol('AgentMonitoringCache');
const CONNECT_TYPE_MESHAGENT = 1;
let MeshServer_NotifyUserOfDeviceStateChange;
let meshserver;
let webserver;
let monitoring;
let cacheDir;
let heartbeatFilePath;
let prometheusJobName;
const [ready, resolveReady] = createReadyPromise();
const eventTarget = new EventTarget();
const stableAgents = new Map();
const startupTime = Date.now();
async function collectMetricFromCache(write, name, type, help, dirname, caches, clearCache) {
let counters, countersZero;
if (name === 'up') {
counters = new CounterMap();
countersZero = new CounterMap();
}
write(
'# TYPE ' + name + ' ' + type + '\n' +
(help ? '# HELP ' + name + ' ' + help + '\n' : '')
);
for (const [encodedMeshName, _agentName, agentDir, labels] of caches) {
const metricDir = agentDir + dirname + PATH_SEP;
const meshName = decodeURIComponent(encodedMeshName);
let cacheContent;
if (!(cacheContent = await readdir(metricDir).catch(nop))) {
continue;
}
cacheContent = cacheContent
.map((fileName) => ([fileName, fileName.split('#')]))
.map((data) => (data[1][0] = +data[1][0], data))
.sort((a, b) => (a[1][0] - b[1][0]));
for (const [_, [timestamp, value]] of cacheContent) {
if (name === 'up' && timestamp != startupTime) {
counters.inc(meshName);
if (+value === 0) {
countersZero.inc(meshName);
}
}
const line = name + labels + ' ' + value + ' ' + timestamp + '\n';
write(line);
}
if (clearCache) {
await Promise.all(cacheContent.map(([name]) => (rm(metricDir + name))));
await rmdir(metricDir);
}
}
return [counters, countersZero];
}
async function collectMetrics(req, res) {
const now = Date.now();
const clearCache = req.headers['user-agent'].startsWith('Prometheus/') && req.query['clear-cache'] === 'yes';
const lastScrapeTime = parseInt((await stat(cacheDir + 'last_scrape').catch(() => ({}))).mtimeMs);
const timeElapsedSinceLastScrape = now - lastScrapeTime;
const releaseCacheReadLock = await aquireCacheReadLock(kCacheSymbol);
const encodedMeshNames = (await readdir(cacheDir, { withFileTypes: true }))
.filter((dirent) => (dirent.isDirectory()))
.map((dirent) => (dirent.name));
const caches = [];
const quietMeshes = [];
for (const encodedMeshName of encodedMeshNames) {
const meshDir = cacheDir + encodedMeshName + PATH_SEP;
const dirents = (await readdir(meshDir, { withFileTypes: true }))
.filter((dirent) => (dirent.isDirectory()));
if (dirents.length === 0) {
quietMeshes.push([decodeURIComponent(encodedMeshName), encodedMeshName]);
continue
}
for (const dirent of dirents) {
caches.push([encodedMeshName, dirent.name])
}
}
for (const cache of caches) {
const caheDirWithPathSep = cacheDir + cache[0] + PATH_SEP + cache[1] + PATH_SEP;
const labels = await readFile(caheDirWithPathSep + 'labels', { encoding: 'utf-8' });
cache.push(caheDirWithPathSep);
cache.push(labels);
}
const write = (text) => (/*console.log(text),*/ res.write(text));
const [stateChangeCounters, stateChangeDownCounters] = await collectMetricFromCache(write, 'up', 'untyped', false, 'up', caches, clearCache);
if (stateChangeCounters.size) {
write('###\n');
}
for (const [_, { [kDataSymbol]: { labels } }] of stableAgents) {
write('up' + labels + ' 1 ' + now + '\n');
}
write('\n');
await collectMetricFromCache(write, 'meshcentral_agent_warmup_seconds', 'gauge', 'Time until agent became stable', 'warmup', caches, clearCache);
write('\n');
await collectMetricFromCache(write, 'meshcentral_agent_stable_seconds', 'gauge', 'Time agent was up and stable', 'stable', caches, clearCache);
write('\n');
write([
'# TYPE meshcentral_agent_state_changes_per_scrape gauge',
'# HELP meshcentral_agent_state_changes_per_scrape MeshAgent state changes per scrape interval',
''
].join('\n'));
const tray = [
'# TYPE meshcentral_agent_state_changes_per_second gauge',
'# HELP meshcentral_agent_state_changes_per_second MeshAgent state changes per second',
];
function writeStateChangeMetrics(meshName, upCounter, downCounter) {
write([
'meshcentral_agent_state_changes_per_scrape{to_state="down",mesh_name="' + meshName + '",job="' + prometheusJobName + '"} ' + downCounter + ' ' + lastScrapeTime,
'meshcentral_agent_state_changes_per_scrape{to_state="up",mesh_name="' + meshName + '",job="' + prometheusJobName + '"} ' + upCounter + ' ' + lastScrapeTime,
'',
].join('\n'));
tray.push(
'meshcentral_agent_state_changes_per_second{to_state="down",mesh_name="' + meshName + '",job="' + prometheusJobName + '"} ' + (downCounter * 1000 / timeElapsedSinceLastScrape) + ' ' + lastScrapeTime,
'meshcentral_agent_state_changes_per_second{to_state="up",mesh_name="' + meshName + '",job="' + prometheusJobName + '"} ' + (upCounter * 1000 / timeElapsedSinceLastScrape) + ' ' + lastScrapeTime,
);
}
for (const [meshName, downCounter] of stateChangeDownCounters) {
const upCounter = (stateChangeCounters.get(meshName) - downCounter);
writeStateChangeMetrics(meshName, upCounter, downCounter);
stateChangeCounters.delete(meshName)
}
for (const [meshName, upCounter] of stateChangeCounters) {
writeStateChangeMetrics(meshName, upCounter, 0);
}
for (const [meshName, encodedMeshName] of quietMeshes) {
writeStateChangeMetrics(meshName, 0, 0);
if (clearCache) {
await rmdir(cacheDir + encodedMeshName).catch(nop);
}
}
write('\n');
if (tray.length > 2) {
tray.push('');
}
write(tray.join('\n'));
write('\n');
if (clearCache) {
for (const [_1, _2, agentDir] of caches) {
await rm(agentDir + 'labels');
await rmdir(agentDir);
}
await writeFile(cacheDir + 'last_scrape', '');
}
releaseCacheReadLock();
}
function _hook_deviceStateChange(meshid, nodeid, connectTime, connectType, powerState, serverid, stateSet, extraInfo) {
if ((connectType & CONNECT_TYPE_MESHAGENT) !== 0) {
if (stateSet) {
webserver.wsagents[nodeid][kDataSymbol] = { connectTime };
} else {
const meshagent = stableAgents.get(nodeid);
if (meshagent) {
meshagent[kDataSymbol].disconnectTime = connectTime;
hook_agentDisconnect(meshagent);
}
}
}
MeshServer_NotifyUserOfDeviceStateChange.call(meshid, nodeid, connectTime, connectType, powerState, serverid, stateSet, extraInfo);
}
function hook_agentCoreIsStable(meshagent) {
const data = meshagent[kDataSymbol];
const now = data.stableTime = Date.now();
data.warmupDuration = now - data.connectTime;
const nodeKey = meshagent.dbNodeKey;
const encodedNodeKey = encodeURIComponent(nodeKey);
const meshKey = meshagent.dbMeshKey;
const meshName = webserver.meshes[meshKey].name;
const encodedMeshName = encodeURIComponent(meshName);
const agentName = meshagent.agentName || meshagent.name || meshagent.agentInfo?.computerName || '';
const agentDir = cacheDir + encodedMeshName + PATH_SEP+ encodedNodeKey + PATH_SEP;
const upDir = agentDir + 'up' + PATH_SEP;
const warmupDir = agentDir + 'warmup' + PATH_SEP;
const labels = data.labels = convertObjectToPrometheusLabels({
instance: 'meshagent' + meshKey.slice(meshKey.lastIndexOf('/')) + nodeKey.slice(nodeKey.lastIndexOf('/')),
agent_name: agentName || '',
mesh_name: meshName || '',
job: prometheusJobName,
});
stableAgents.set(nodeKey, meshagent);
(async function () {
const releaseCacheWriteLock = await aquireCacheWriteLock(kCacheSymbol);
await mkdir(agentDir, { recursive: true });
await Promise.all([
// Don't fail if exists.
mkdir(upDir).catch(nop),
mkdir(warmupDir).catch(nop),
]);
await Promise.all([
writeFile(agentDir + 'labels', labels),
// Make sure the agent was down at startup. If the agent came up before
// the last scrape there is no "down" event on unclean shutdown.
writeFile(upDir + startupTime + '#0', ''),
writeFile(upDir + now + '#1', ''),
writeFile(warmupDir + now + '#' + (data.warmupDuration / 1000), ''),
]);
releaseCacheWriteLock();
eventTarget.dispatchEvent(new CustomEvent('agenCoreIsStable', { detail: { meshagent } }));
})();
}
function hook_agentDisconnect(meshagent) {
const data = meshagent[kDataSymbol];
const now = data.disconnectTime;
data.stableDuration = now - data.stableTime;
const nodeKey = meshagent.dbNodeKey;
const encodedNodeKey = encodeURIComponent(nodeKey);
const meshKey = meshagent.dbMeshKey;
const meshName = webserver.meshes[meshKey].name;
const encodedMeshName = encodeURIComponent(meshName);
const agentDir = cacheDir + encodedMeshName + PATH_SEP + encodedNodeKey + PATH_SEP;
const upDir = agentDir + 'up' + PATH_SEP;
const stableDir = agentDir + 'stable' + PATH_SEP;
const labels = data.labels;
stableAgents.delete(nodeKey);
(async function () {
const releaseCacheWriteLock = await aquireCacheWriteLock(kCacheSymbol);
await mkdir(agentDir, { recursive: true });
await Promise.all([
// Don't fail if exists.
mkdir(upDir).catch(nop),
mkdir(stableDir).catch(nop),
]);
await Promise.all([
writeFile(agentDir + 'labels', labels),
writeFile(upDir + now + '#0', ''),
writeFile(stableDir + now + '#' + (data.stableDuration / 1000), ''),
]);
releaseCacheWriteLock();
eventTarget.dispatchEvent(new CustomEvent('disconnect', { detail: { meshagent } }));
})();
}
async function startupCache() {
await mkdir(cacheDir, { recursive: true });
heartbeatFilePath = cacheDir + 'heartbeat';
const lastHeartbeat = parseInt((await stat(heartbeatFilePath).catch(() => ({}))).mtimeMs);
// create missing "down" events
for await (const dirent of await opendir(cacheDir)) {
if (!dirent.isDirectory()) {
continue;
}
const upDir = cacheDir + dirent.name + PATH_SEP + 'up';
const upDirContent = await readdir(upDir).catch(nop);
if (!upDirContent) {
continue;
}
const [lastTimestamp, lastValue] = (upDirContent)
.map((name) => (name.split('#').map((val) => (+val))))
.sort((a, b) => (a[0] - b[0]))
.pop();
if (lastValue === 1) {
const stableDir = cacheDir + dirent.name + PATH_SEP + 'stable';
if (lastHeartbeat > lastTimestamp) {
// last "up" event was before last heartbeat
await Promise.all([
writeFile(upDir + PATH_SEP + lastHeartbeat + '#0', ''),
writeFile(stableDir + PATH_SEP + lastHeartbeat + '#' + (lastHeartbeat - lastTimestamp), ''),
]);
} else {
// last "up" event was after last heartbeat
const lastDown = lastTimestamp + 1000;
await Promise.all([
writeFile(upDir + PATH_SEP + lastDown + '#0', ''),
writeFile(stableDir + PATH_SEP + lastDown + '#1', ''),
]);
}
}
}
await Promise.all([
rm(heartbeatFilePath).catch(nop),
writeFile(cacheDir + 'last_scrape', ''),
]);
startHeartbeat(heartbeatFilePath);
}
function startup() {
let config = Object.assign({
cachedirpath: '.cache',
prometheusjobname: 'meshcentral@' + hostname(),
}, meshserver.config.settings.plugins.pluginsettings?.agent_monitoring);
webserver = meshserver.webserver;
monitoring = meshserver.monitoring;
MeshServer_NotifyUserOfDeviceStateChange = meshserver.NotifyUserOfDeviceStateChange;
meshserver.NotifyUserOfDeviceStateChange = _hook_deviceStateChange;
prometheusJobName = config.prometheusjobname;
if (process.platform === 'win32') {
throw new Error('SUPPORT FOR WIN32 IS NOT TESTED YET!');
const tmp = config.cachedirpath;
// either UNC or absolute on current drive or absolute with drive letter
if (tmp[0] === '\\' || tmp.slice(1, 3) === ':\\' || tmp.slice(1, 3) === ':/') {
cacheDir = '';
} else {
cacheDir = __dirname + PATH_SEP;
}
} else {
cacheDir = config.cachedirpath[0] === '/' ? '' : __dirname + PATH_SEP;
}
cacheDir = normalizePath( cacheDir + config.cachedirpath );
if (!cacheDir.endsWith(PATH_SEP)) {
cacheDir += PATH_SEP;
}
(async function () {
await startupCache();
monitoring.collectors.push(collectMetrics);
resolveReady();
})();
}
module.exports = {
agent_monitoring: function (pluginHandler) {
meshserver = pluginHandler.parent;
return {
hook_agentCoreIsStable: hook_agentCoreIsStable,
addEventListener: eventTarget.addEventListener.bind(eventTarget),
server_startup: startup,
};
},
ready,
kDataSymbol,
nop,
createReadyPromise,
aquireCacheReadLock,
aquireCacheWriteLock,
CounterMap,
};