Files
librenms-librenms/includes/html/pages/device/overview/generic/sensor.inc.php
Martijn Schmidt 9d68f27296 Use sensor labels for overview/inventory pages, refactor some html-page related code (#10287)
* Refactor: use get_state_label() for the overview

* Move state translation to get_state_label()

Several html pages used similar database calls and sometimes very
different methodology to determine the state label and state text
before calling get_state_label(), so moved that part of the task
into the function itself instead of replicating the same code
multiple times on different pages.

* Move label creation to get_sensor_label_color()

Also removed a duplicate sensor class to unit function.

* Refactor some if/else statements for simplicity.

* Enable state translations for the inventory page.

* Remove blank line found at end of control structure

* Enable unit translation, fix typos.

* Change to dbFetchRow(), return string directly.

* Update functions.inc.php

* Stop displaying the raw value on state labels.

* Apply 1c7c3ca change for default case too.
2019-06-27 00:30:10 -05:00

82 lines
3.8 KiB
PHP

<?php
$sensors = dbFetchRows('SELECT * FROM `sensors` WHERE `sensor_class` = ? AND device_id = ? ORDER BY `group`, `sensor_descr`, `sensor_oid`, `sensor_index`', array($sensor_class, $device['device_id']));
if (count($sensors)) {
$icons = \App\Models\Sensor::getIconMap();
$sensor_fa_icon = 'fa-' . (isset($icons[$sensor_class]) ? $icons[$sensor_class] : 'delicious');
echo '
<div class="row">
<div class="col-md-12">
<div class="panel panel-default panel-condensed">
<div class="panel-heading">';
echo '<a href="device/device='.$device['device_id'].'/tab=health/metric='.strtolower($sensor_type).'/"><i class="fa '.$sensor_fa_icon.' fa-lg icon-theme" aria-hidden="true"></i><strong> '.nicecase($sensor_type).'</strong></a>';
echo ' </div>
<table class="table table-hover table-condensed table-striped">';
$group = '';
foreach ($sensors as $sensor) {
if (!isset($sensor['sensor_current'])) {
$sensor['sensor_current'] = 'NaN';
}
if ($group != $sensor['group']) {
$group = $sensor['group'];
echo "<tr><td colspan='3'><strong>$group</strong></td></tr>";
}
// FIXME - make this "four graphs in popup" a function/include and "small graph" a function.
// FIXME - So now we need to clean this up and move it into a function. Isn't it just "print-graphrow"?
// FIXME - DUPLICATED IN health/sensors
$graph_colour = str_replace('#', '', $row_colour);
$graph_array = array();
$graph_array['height'] = '100';
$graph_array['width'] = '210';
$graph_array['to'] = \LibreNMS\Config::get('time.now');
$graph_array['id'] = $sensor['sensor_id'];
$graph_array['type'] = $graph_type;
$graph_array['from'] = \LibreNMS\Config::get('time.day');
$graph_array['legend'] = 'no';
$link_array = $graph_array;
$link_array['page'] = 'graphs';
unset($link_array['height'], $link_array['width'], $link_array['legend']);
$link = generate_url($link_array);
if ($sensor['poller_type'] == "ipmi") {
$sensor['sensor_descr'] = substr(ipmiSensorName($device['hardware'], $sensor['sensor_descr']), 0, 48);
} else {
$sensor['sensor_descr'] = substr($sensor['sensor_descr'], 0, 48);
}
$overlib_content = '<div class=overlib><span class=overlib-text>' . $device['hostname'] . ' - ' . $sensor['sensor_descr'] . '</span><br />';
foreach (array('day', 'week', 'month', 'year') as $period) {
$graph_array['from'] = \LibreNMS\Config::get("time.$period");
$overlib_content .= str_replace('"', "\'", generate_graph_tag($graph_array));
}
$overlib_content .= '</div>';
$graph_array['width'] = 80;
$graph_array['height'] = 20;
$graph_array['bg'] = 'ffffff00';
// the 00 at the end makes the area transparent.
$graph_array['from'] = \LibreNMS\Config::get('time.day');
$sensor_minigraph = generate_lazy_graph_tag($graph_array);
$sensor['sensor_current'] = $sensor_class == 'runtime' ? formatUptime($sensor['sensor_current'] * 60, 'short') : $sensor['sensor_current'];
$sensor_current = $graph_type == 'sensor_state' ? get_state_label($sensor) : get_sensor_label_color($sensor);
echo '<tr>
<td class="col-md-4">'.overlib_link($link, shorten_interface_type($sensor['sensor_descr']), $overlib_content, $sensor_class).'</td>
<td class="col-md-4">'.overlib_link($link, $sensor_minigraph, $overlib_content, $sensor_class).'</td>
<td class="col-md-4">'.overlib_link($link, $sensor_current, $overlib_content, $sensor_class).'</td>
</tr>';
}//end foreach
echo '</table>';
echo '</div>';
echo '</div>';
echo '</div>';
}//end if