2022-09-14 13:03:14 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2022-09-15 11:28:41 +02:00
|
|
|
getSystem,
|
2022-09-14 13:03:14 +02:00
|
|
|
getByAppId
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-15 11:28:41 +02:00
|
|
|
const apps = require('./apps.js'),
|
|
|
|
|
assert = require('assert'),
|
2022-09-14 13:03:14 +02:00
|
|
|
BoxError = require('./boxerror.js'),
|
2022-08-08 13:06:14 +02:00
|
|
|
fs = require('fs'),
|
2022-09-14 13:03:14 +02:00
|
|
|
safe = require('safetydance'),
|
2022-08-08 13:06:14 +02:00
|
|
|
superagent = require('superagent');
|
2022-09-14 13:03:14 +02:00
|
|
|
|
2022-09-15 11:28:41 +02:00
|
|
|
// for testing locally: curl 'http://127.0.0.1:8417/graphite-web/render?format=json&from=-1min&target=absolute(collectd.localhost.du-docker.capacity-usage)'
|
|
|
|
|
// the datapoint is (value, timestamp) https://buildmedia.readthedocs.org/media/pdf/graphite/0.9.16/graphite.pdf
|
2022-09-14 13:03:14 +02:00
|
|
|
const GRAPHITE_RENDER_URL = 'http://127.0.0.1:8417/graphite-web/render';
|
|
|
|
|
|
2022-08-08 13:06:14 +02:00
|
|
|
// https://rootlesscontaine.rs/getting-started/common/cgroup2/#checking-whether-cgroup-v2-is-already-enabled
|
|
|
|
|
const CGROUP_VERSION = fs.existsSync('/sys/fs/cgroup/cgroup.controllers') ? '2' : '1';
|
|
|
|
|
|
2022-09-14 13:03:14 +02:00
|
|
|
async function getByAppId(appId, fromMinutes, noNullPoints) {
|
|
|
|
|
assert.strictEqual(typeof appId, 'string');
|
|
|
|
|
assert.strictEqual(typeof fromMinutes, 'number');
|
|
|
|
|
assert.strictEqual(typeof noNullPoints, 'boolean');
|
|
|
|
|
|
|
|
|
|
const timeBucketSize = fromMinutes > (24 * 60) ? (6*60) : 5;
|
|
|
|
|
|
|
|
|
|
const memoryQuery = {
|
2022-08-08 13:06:14 +02:00
|
|
|
target: null, // filled below
|
2022-09-14 13:03:14 +02:00
|
|
|
format: 'json',
|
|
|
|
|
from: `-${fromMinutes}min`,
|
|
|
|
|
until: 'now'
|
|
|
|
|
};
|
2022-08-08 13:06:14 +02:00
|
|
|
if (CGROUP_VERSION === '1') {
|
|
|
|
|
memoryQuery.target = `summarize(collectd.localhost.table-${appId}-memory.gauge-memsw_usage_in_bytes, "${timeBucketSize}min", "avg")`;
|
|
|
|
|
} else {
|
|
|
|
|
memoryQuery.target = `summarize(sum(collectd.localhost.table-${appId}-memory.gauge-memory_current, collectd.localhost.table-${appId}-memory.gauge-memory_swap_current), "${timeBucketSize}min", "avg")`;
|
|
|
|
|
}
|
2022-09-14 13:03:14 +02:00
|
|
|
if (noNullPoints) memoryQuery.noNullPoints = true;
|
|
|
|
|
|
|
|
|
|
const [memoryError, memoryResponse] = await safe(superagent.get(GRAPHITE_RENDER_URL)
|
|
|
|
|
.query(memoryQuery)
|
|
|
|
|
.timeout(30 * 1000)
|
|
|
|
|
.ok(() => true));
|
|
|
|
|
|
|
|
|
|
if (memoryError) throw new BoxError(BoxError.NETWORK_ERROR, memoryError.message);
|
2022-09-15 11:28:41 +02:00
|
|
|
if (memoryResponse.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, `Unknown error: ${memoryResponse.status} ${memoryResponse.text}`);
|
2022-09-14 13:03:14 +02:00
|
|
|
|
|
|
|
|
const diskQuery = {
|
|
|
|
|
target: `summarize(collectd.localhost.du-${appId}.capacity-usage, "${timeBucketSize}min", "avg")`,
|
|
|
|
|
format: 'json',
|
|
|
|
|
from: `-${fromMinutes}min`,
|
|
|
|
|
until: 'now'
|
|
|
|
|
};
|
|
|
|
|
if (noNullPoints) diskQuery.noNullPoints = true;
|
|
|
|
|
|
|
|
|
|
const [diskError, diskResponse] = await safe(superagent.get(GRAPHITE_RENDER_URL)
|
|
|
|
|
.query(diskQuery)
|
|
|
|
|
.timeout(30 * 1000)
|
|
|
|
|
.ok(() => true));
|
|
|
|
|
|
|
|
|
|
if (diskError) throw new BoxError(BoxError.NETWORK_ERROR, diskError.message);
|
2022-09-15 11:28:41 +02:00
|
|
|
if (diskResponse.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, `Unknown error: ${diskResponse.status} ${diskResponse.text}`);
|
2022-09-14 13:03:14 +02:00
|
|
|
|
|
|
|
|
return { memory: memoryResponse.body[0], disk: diskResponse.body[0] };
|
|
|
|
|
}
|
2022-09-15 11:28:41 +02:00
|
|
|
|
|
|
|
|
async function getSystem(fromMinutes, noNullPoints) {
|
|
|
|
|
assert.strictEqual(typeof fromMinutes, 'number');
|
|
|
|
|
assert.strictEqual(typeof noNullPoints, 'boolean');
|
|
|
|
|
|
|
|
|
|
const timeBucketSize = fromMinutes > (24 * 60) ? (6*60) : 5;
|
|
|
|
|
|
|
|
|
|
const cpuQuery = `summarize(sum(collectd.localhost.aggregation-cpu-average.cpu-system, collectd.localhost.aggregation-cpu-average.cpu-user), "${timeBucketSize}min", "avg")`;
|
|
|
|
|
const memoryQuery = `summarize(sum(collectd.localhost.memory.memory-used, collectd.localhost.swap.swap-used), "${timeBucketSize}min", "avg")`;
|
|
|
|
|
|
|
|
|
|
const query = {
|
|
|
|
|
target: [ cpuQuery, memoryQuery ],
|
|
|
|
|
format: 'json',
|
|
|
|
|
from: `-${fromMinutes}min`,
|
|
|
|
|
until: 'now'
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const [memCpuError, memCpuResponse] = await safe(superagent.get(GRAPHITE_RENDER_URL)
|
|
|
|
|
.query(query)
|
|
|
|
|
.timeout(30 * 1000)
|
|
|
|
|
.ok(() => true));
|
|
|
|
|
|
|
|
|
|
if (memCpuError) throw new BoxError(BoxError.NETWORK_ERROR, memCpuError.message);
|
|
|
|
|
if (memCpuResponse.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, `Unknown error: ${memCpuResponse.status} ${memCpuResponse.text}`);
|
|
|
|
|
|
|
|
|
|
const allApps = await apps.list();
|
|
|
|
|
const appResponses = {};
|
|
|
|
|
for (const app of allApps) {
|
|
|
|
|
appResponses[app.id] = await getByAppId(app.id, fromMinutes, noNullPoints);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// const disks = await system.getDisks();
|
|
|
|
|
// const diskResponses = {}; // indexed by filesystem
|
|
|
|
|
// for (const disk of disks) {
|
|
|
|
|
// // /dev/sda1 -> sda1
|
|
|
|
|
// // /dev/mapper/foo.com -> mapper_foo_com (see #348)
|
|
|
|
|
// let diskName = disk.filesystem.slice(disk.filesystem.indexOf('/', 1) + 1);
|
|
|
|
|
// diskName = diskName.replace(/\/|\./g, '_');
|
|
|
|
|
|
|
|
|
|
// const target = [
|
|
|
|
|
// `absolute(collectd.localhost.df-${diskName}.df_complex-free)`,
|
|
|
|
|
// `absolute(collectd.localhost.df-${diskName}.df_complex-reserved)`, // reserved for root (default: 5%) tune2fs -l/m
|
|
|
|
|
// `absolute(collectd.localhost.df-${diskName}.df_complex-used)`
|
|
|
|
|
// ];
|
|
|
|
|
|
|
|
|
|
// const diskQuery = {
|
|
|
|
|
// target: target,
|
|
|
|
|
// format: 'json',
|
|
|
|
|
// from: '-1min',
|
|
|
|
|
// until: 'now'
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
// const [diskError, diskResponse] = await safe(superagent.get(GRAPHITE_RENDER_URL).query(diskQuery).timeout(30 * 1000).ok(() => true));
|
|
|
|
|
// if (diskError) throw new BoxError(BoxError.NETWORK_ERROR, diskError.message);
|
|
|
|
|
// if (diskResponse.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, `Unknown error: ${diskResponse.status} ${diskResponse.text}`);
|
|
|
|
|
// diskResponses[disk.filesystem] = diskResponse.body[0];
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
return { cpu: memCpuResponse.body[0], memory: memCpuResponse.body[1], apps: appResponses };
|
|
|
|
|
}
|