-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlxc_monitoring.js
More file actions
153 lines (143 loc) · 5.84 KB
/
lxc_monitoring.js
File metadata and controls
153 lines (143 loc) · 5.84 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
'use strict';
const { opendir, readFile } = require('node:fs/promises');
const PATH_SEP = require('node:path').sep;
let meshserver;
let monitoring;
let prometheus;
let cgroupRootPath;
let metricNamePrefix;
let collectorName;
const metrics ={};
const LXC_PAYLOAD_PREFIX = 'lxc.payload.';
const LXC_PAYLOAD_PREFIX_LENGTH = LXC_PAYLOAD_PREFIX.length;
const SERVICE_SUFFIX = '.service';
const SERVICE_SUFFIX_LENGTH = SERVICE_SUFFIX.length;
const PSI_SUFFIX = '.pressure';
const MEMORY_CURRENT = 'memory.current';
const MEMORY_SWAP_CURRENT = 'memory.swap.current';
const SYSTEM_SLICE = 'system.slice';
const psiControllerNames = [ 'cpu', 'io' ];
const psiScopeNames = [ 'some', 'full' ];
const psiValueNames = [ 'avg10', 'avg60', 'avg300', 'total' ];
const metricLabelNames = [ 'container_name', 'service_name' ];
async function collectCgroupMetrics(cgroupPath, containerName, serviceName) {
const labels = { container_name: containerName, service_name: serviceName };
const readFileOptions = { encoding: 'ascii' };
let value;
value = +( await readFile(cgroupPath + PATH_SEP + MEMORY_CURRENT, readFileOptions));
metrics.memoryCurrent.set(labels, value);
value = +( await readFile(cgroupPath + PATH_SEP + MEMORY_SWAP_CURRENT, readFileOptions));
metrics.memorySwapCurrent.set(labels, value);
for (let controllerName of psiControllerNames) {
const controllerMetrics = metrics[controllerName];
const lines = (await readFile(cgroupPath + PATH_SEP + controllerName + PSI_SUFFIX, readFileOptions)).split('\n');
lines.pop(); // remove the trailing empty line
for (let line of lines) {
const parts = line.split(' ');
const scope = parts.shift();
const scope_metrics = controllerMetrics[scope];
for (let part of parts) {
let [ valueName, value ] = part.split('=');
value = +value;
scope_metrics[valueName].set(labels, value);
}
};
}
}
async function collectLxcContainerMetrics() {
const startTs = Date.now();
const cgroupRootDir = await opendir(cgroupRootPath).catch((error) => {
console.log('Cannot open cgroup root directory.', error);
});
for await (const containerDirent of cgroupRootDir) {
if (!containerDirent.isDirectory() || !containerDirent.name.startsWith(LXC_PAYLOAD_PREFIX)) {
continue;
}
const containerPath = cgroupRootPath + PATH_SEP + containerDirent.name;
const containerName = containerDirent.name.slice(LXC_PAYLOAD_PREFIX_LENGTH);
collectCgroupMetrics(containerPath, containerName, '');
const slicePath = containerPath + PATH_SEP + SYSTEM_SLICE;
const sliceDir = await opendir(slicePath);
for await (const sliceDirent of sliceDir) {
if (!sliceDirent.isDirectory() || !sliceDirent.name.endsWith(SERVICE_SUFFIX)) {
continue;
}
const servicePath = slicePath + PATH_SEP + sliceDirent.name;
const serviceName = sliceDirent.name.slice(0, sliceDirent.name.length - SERVICE_SUFFIX_LENGTH);
collectCgroupMetrics(servicePath, containerName, serviceName);
}
}
const endTs = Date.now();
const duration = (endTs - startTs) / 1000;
metrics.nodeCollectorDuration.labels(collectorName).set(duration);
metrics.nodeCollectorSuccess.labels(collectorName).set(1);
metrics.duration.labels(collectorName).set(duration);
}
function setupLxcContainerMetrics() {
metrics.nodeCollectorDuration = new prometheus.Gauge({
name: 'node_scrape_collector_duration_seconds',
help: 'foobar',
labelNames: [ 'collector' ],
});
metrics.nodeCollectorSuccess = new prometheus.Gauge({
name: 'node_scrape_collector_success',
help: 'foobar',
labelNames: [ 'collector' ],
});
metrics.duration = new prometheus.Gauge({
name: metricNamePrefix + '_scrape_collector_duration_seconds',
help: 'foobar',
labelNames: [ 'collector' ],
});
metrics.memoryCurrent = new prometheus.Gauge({
name: metricNamePrefix + '_memory_current_bytes',
help: 'Currently used memory',
labelNames: metricLabelNames,
});
metrics.memorySwapCurrent = new prometheus.Gauge({
name: metricNamePrefix + '_memory_swap_current_bytes',
help: 'Currently used swap memory',
labelNames: metricLabelNames,
});
for (let controllerName of psiControllerNames) {
const obj1 = metrics[controllerName] ??= {};
for (let scopeName of psiScopeNames) {
const obj2 = obj1[scopeName] ??= {};
for (let valueName of psiValueNames) {
obj2[valueName] = new prometheus.Gauge({
name: metricNamePrefix + `_${controllerName}_pressure_${scopeName}_${valueName}`,
help: `Pressure (${scopeName}) ${valueName}`,
labelNames: metricLabelNames
});
}
}
}
}
module.exports.lxc_monitoring = function (parent) {
meshserver = parent.parent;
var obj = {};
obj.server_startup = async function() {
let config;
config = await readFile(__filename.replace(/\.js$/, '.conf.json'), 'utf-8').catch((error) => {
console.log('Error while reading configuration file.', error);
});
try {
config = JSON.parse(config);
} catch (error) {
console.log('Error while parsing configuration.', error);
return;
}
({ collectorName, metricNamePrefix, cgroupRootPath } = config );
const mounts = await readFile('/proc/mounts', 'ascii');
const fstypes = Object.fromEntries(mounts.split('\n').map((entry) => (entry.split(' '))).map(([_, mountpoint, fstype])=>([mountpoint, fstype])));
if (fstypes[cgroupRootPath] !== 'cgroup2') {
console.log(new Error('The configured cgroup-root is not a cgroup2 filesystem.'));
return;
}
monitoring = meshserver.monitoring;
prometheus = monitoring.prometheus;
setupLxcContainerMetrics();
monitoring.collectors.push(collectLxcContainerMetrics);
}
return obj;
}