graphs: move system graph queries to the backend
This commit is contained in:
+67
-3
@@ -1,15 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
exports = module.exports = {
|
||||
getSystem,
|
||||
getByAppId
|
||||
};
|
||||
|
||||
const assert = require('assert'),
|
||||
const apps = require('./apps.js'),
|
||||
assert = require('assert'),
|
||||
BoxError = require('./boxerror.js'),
|
||||
fs = require('fs'),
|
||||
safe = require('safetydance'),
|
||||
superagent = require('superagent');
|
||||
|
||||
// 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
|
||||
const GRAPHITE_RENDER_URL = 'http://127.0.0.1:8417/graphite-web/render';
|
||||
|
||||
// https://rootlesscontaine.rs/getting-started/common/cgroup2/#checking-whether-cgroup-v2-is-already-enabled
|
||||
@@ -41,7 +45,7 @@ async function getByAppId(appId, fromMinutes, noNullPoints) {
|
||||
.ok(() => true));
|
||||
|
||||
if (memoryError) throw new BoxError(BoxError.NETWORK_ERROR, memoryError.message);
|
||||
if (memoryResponse.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, `Unknown error: ${memoryError.message}`);
|
||||
if (memoryResponse.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, `Unknown error: ${memoryResponse.status} ${memoryResponse.text}`);
|
||||
|
||||
const diskQuery = {
|
||||
target: `summarize(collectd.localhost.du-${appId}.capacity-usage, "${timeBucketSize}min", "avg")`,
|
||||
@@ -57,7 +61,67 @@ async function getByAppId(appId, fromMinutes, noNullPoints) {
|
||||
.ok(() => true));
|
||||
|
||||
if (diskError) throw new BoxError(BoxError.NETWORK_ERROR, diskError.message);
|
||||
if (diskResponse.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, `Unknown error: ${diskError.message}`);
|
||||
if (diskResponse.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, `Unknown error: ${diskResponse.status} ${diskResponse.text}`);
|
||||
|
||||
return { memory: memoryResponse.body[0], disk: diskResponse.body[0] };
|
||||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user