api: Added support for retrieving health information and graphs #5399 (#5446)

This commit is contained in:
Neil Lathwood
2017-01-19 10:09:20 +00:00
committed by GitHub
parent 09c396e3fc
commit 6df39b9bbd
3 changed files with 225 additions and 0 deletions

View File

@ -123,8 +123,13 @@ function get_graph_generic_by_hostname()
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$hostname = $router['hostname'];
$sensor_id = $router['sensor_id'] ?: null;
$vars = array();
$vars['type'] = $router['type'] ?: 'device_uptime';
if (isset($sensor_id)) {
$vars['id'] = $sensor_id;
$vars['type'] = str_replace('device_', 'sensor_', $vars['type']);
}
// use hostname as device_id if it's all digits
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
@ -678,6 +683,53 @@ function get_graphs()
echo _json_encode($output);
}
function list_available_health_graphs()
{
global $config;
$code = 200;
$status = 'ok';
$message = '';
$app = \Slim\Slim::getInstance();
$router = $app->router()->getCurrentRoute()->getParams();
$hostname = $router['hostname'];
$device_id = ctype_digit($hostname) ? $hostname : getidbyname($hostname);
if (isset($router['type'])) {
list($dump, $type) = explode('_', $router['type']);
}
$sensor_id = $router['sensor_id'] ?: null;
$graphs = array();
if (isset($type)) {
if (isset($sensor_id)) {
$graphs = dbFetchRows('SELECT * FROM `sensors` WHERE `sensor_id` = ?', array($sensor_id));
} else {
foreach (dbFetchRows('SELECT `sensor_id`, `sensor_descr` FROM `sensors` WHERE `device_id` = ? AND `sensor_class` = ? AND `sensor_deleted` = 0', array($device_id, $type)) as $graph) {
$graphs[] = array(
'sensor_id' => $graph['sensor_id'],
'desc' => $graph['sensor_descr'],
);
}
}
} else {
foreach (dbFetchRows('SELECT `sensor_class` FROM `sensors` WHERE `device_id` = ? AND `sensor_deleted` = 0 GROUP BY `sensor_class`', array($device_id)) as $graph) {
$graphs[] = array(
'desc' => ucfirst($graph['sensor_class']),
'name' => 'device_'.$graph['sensor_class'],
);
}
}
$total_graphs = count($graphs);
$output = array(
'status' => "$status",
'err-msg' => $message,
'count' => $total_graphs,
'graphs' => $graphs,
);
$app->response->setStatus($code);
$app->response->headers->set('Content-Type', 'application/json');
echo _json_encode($output);
}
function get_port_graphs()
{