Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

95 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2021-03-07 20:26:44 +01:00
<?php
use LibreNMS\Exceptions\JsonAppException;
use LibreNMS\Exceptions\JsonAppMissingKeysException;
use LibreNMS\RRD\RrdDefinition;
2023-03-01 01:02:01 +01:00
use LibreNMS\Util\Number;
2021-03-07 20:26:44 +01:00
$name = 'docker';
2023-03-01 01:02:01 +01:00
$version = 1;
2021-03-07 20:26:44 +01:00
$output = 'OK';
try {
2023-03-01 01:02:01 +01:00
$result = json_app_get($device, $name, 1);
$version = $result['version'];
$docker_data = $result['data'];
2021-03-07 20:26:44 +01:00
} catch (JsonAppMissingKeysException $e) {
$docker_data = $e->getParsedJson();
} catch (JsonAppException $e) {
echo PHP_EOL . $name . ':' . $e->getCode() . ':' . $e->getMessage() . PHP_EOL;
update_application($app, $e->getCode() . ':' . $e->getMessage(), []); // Set empty metrics and error message
return;
}
2023-03-01 01:02:01 +01:00
$version = intval($version);
if ($version == 1) {
$output = 'LEGACY';
} elseif ($version == 2) {
$output = 'OK';
} else {
$output = 'UNSUPPORTED';
}
$rrd_name = ['app', $name, $app->app_id];
2021-03-07 20:26:44 +01:00
$rrd_def = RrdDefinition::make()
->addDataset('cpu_usage', 'GAUGE', 0, 100)
->addDataset('pids', 'GAUGE', 0)
->addDataset('mem_perc', 'GAUGE', 0, 100)
->addDataset('mem_used', 'GAUGE', 0)
2023-03-01 01:02:01 +01:00
->addDataset('mem_limit', 'GAUGE', 0)
->addDataset('uptime', 'GAUGE', 0)
->addDataset('size_rw', 'GAUGE', 0)
->addDataset('size_root_fs', 'GAUGE', 0);
2021-03-07 20:26:44 +01:00
2023-03-01 01:02:01 +01:00
$totals = [
'created' => 0,
'restarting' => 0,
'running' => 0,
'removing' => 0,
'paused' => 0,
'exited' => 0,
'dead' => 0,
];
2021-03-07 20:26:44 +01:00
$metrics = [];
$containerNames = [];
2021-03-07 20:26:44 +01:00
foreach ($docker_data as $data) {
$containerNames[] = $container = $data['container'];
2021-03-07 20:26:44 +01:00
2023-03-01 01:02:01 +01:00
$status = isset($data['state']['status']) ? strtolower($data['state']['status']) : null;
if ($status) {
$totals[$status] += 1;
}
2021-03-07 20:26:44 +01:00
$fields = [
2021-03-12 18:10:14 -06:00
'cpu_usage' => (float) $data['cpu'],
2021-03-07 20:26:44 +01:00
'pids' => $data['pids'],
2021-03-12 18:10:14 -06:00
'mem_perc' => (float) $data['memory']['perc'],
2024-04-30 07:40:08 -05:00
'mem_used' => (int) Number::toBytes($data['memory']['used']),
'mem_limit' => (int) Number::toBytes($data['memory']['limit']),
2023-03-01 01:02:01 +01:00
'uptime' => $data['state']['uptime'],
'size_rw' => $data['size']['size_rw'],
'size_root_fs' => $data['size']['size_root_fs'],
2021-03-07 20:26:44 +01:00
];
2023-03-01 01:02:01 +01:00
$rrd_name = ['app', $name, $app->app_id, $container];
2021-03-07 20:26:44 +01:00
$metrics[$container] = $fields;
$tags = ['name' => $container, 'app_id' => $app->app_id, 'rrd_def' => $rrd_def, 'rrd_name' => $rrd_name];
2021-03-07 20:26:44 +01:00
data_update($device, 'app', $tags, $fields);
}
2023-03-01 01:02:01 +01:00
$rrd_def = RrdDefinition::make();
foreach ($totals as $status => $value) {
$rrd_def->addDataset($status, 'GAUGE', 0);
}
$rrd_name = ['app', $name, $app->app_id];
$tags = ['name' => $name, 'app_id' => $app->app_id, 'rrd_def' => $rrd_def, 'rrd_name' => $rrd_name];
data_update($device, 'app', $tags, $totals);
$metrics['total'] = $totals;
$app->data = ['containers' => $containerNames];
2021-03-07 20:26:44 +01:00
update_application($app, $output, $metrics);