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.
This commit is contained in:
Martijn Schmidt
2019-06-27 00:30:10 -05:00
committed by Tony Murray
parent ebd004edc7
commit 9d68f27296
7 changed files with 34 additions and 141 deletions

View File

@@ -58,7 +58,8 @@ function printEntPhysical($device, $ent, $level, $class)
}
if ($ent['entPhysicalClass'] == 'sensor' && isset($sensor)) {
echo ' (' . trim($sensor['sensor_current'] . ' ' . __('sensors.' . $sensor['sensor_class'] . '.unit')) . ')';
echo ' ';
echo $sensor['sensor_class'] == 'state' ? get_state_label($sensor) : get_sensor_label_color($sensor);
}
if (isset($link)) {

View File

@@ -3,10 +3,6 @@
$row = 1;
foreach (dbFetchRows('SELECT * FROM `sensors` WHERE `sensor_class` = ? AND `device_id` = ? ORDER BY `sensor_descr`', array($class, $device['device_id'])) as $sensor) {
$state_translation = array();
if (($graph_type == 'sensor_state')) {
$state_translation = dbFetchRows('SELECT * FROM state_translations as ST, sensors_to_state_indexes as SSI WHERE ST.state_index_id=SSI.state_index_id AND SSI.sensor_id = ? AND ST.state_value = ? ', array($sensor['sensor_id'], $sensor['sensor_current']));
}
if (!is_integer($row / 2)) {
$row_colour = \LibreNMS\Config::get('list_colour.even');
} else {
@@ -19,12 +15,7 @@ foreach (dbFetchRows('SELECT * FROM `sensors` WHERE `sensor_class` = ? AND `devi
$sensor_descr = $sensor['sensor_descr'];
}
if (($graph_type == 'sensor_state') && !empty($state_translation['0']['state_descr'])) {
$sensor_current = get_state_label($state_translation[0]['state_generic_value'], $state_translation[0]['state_descr'] . " (".$sensor['sensor_current'].")");
} else {
$current_label = get_sensor_label_color($sensor);
$sensor_current = "<span class='label $current_label'>".trim(format_si($sensor['sensor_current']).$unit)."</span>";
}
$sensor_current = $graph_type == 'sensor_state' ? get_state_label($sensor) : get_sensor_label_color($sensor);
$sensor_limit = trim(format_si($sensor['sensor_limit']).$unit);
$sensor_limit_low = trim(format_si($sensor['sensor_limit_low']).$unit);

View File

@@ -1,9 +1,5 @@
<?php
if ($sensor_class == 'state') {
$sensors = dbFetchRows('SELECT `sensors`.*, `state_indexes`.`state_index_id` FROM `sensors` LEFT JOIN `sensors_to_state_indexes` ON sensors_to_state_indexes.sensor_id = sensors.sensor_id LEFT JOIN state_indexes ON state_indexes.state_index_id = sensors_to_state_indexes.state_index_id WHERE `sensor_class` = ? AND device_id = ? ORDER BY `group`, `sensor_type`, `sensor_descr`, `sensor_index`+0', array($sensor_class, $device['device_id']));
} else {
$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']));
}
$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();
@@ -19,11 +15,6 @@ if (count($sensors)) {
<table class="table table-hover table-condensed table-striped">';
$group = '';
foreach ($sensors as $sensor) {
$state_translation = array();
if (!empty($sensor['state_index_id'])) {
$state_translation = dbFetchRows('SELECT * FROM `state_translations` WHERE `state_index_id` = ? AND `state_value` = ? ', array($sensor['state_index_id'], $sensor['sensor_current']));
}
if (!isset($sensor['sensor_current'])) {
$sensor['sensor_current'] = 'NaN';
}
@@ -73,43 +64,14 @@ if (count($sensors)) {
$graph_array['from'] = \LibreNMS\Config::get('time.day');
$sensor_minigraph = generate_lazy_graph_tag($graph_array);
if (!empty($state_translation['0']['state_descr'])) {
$state_style = "";
switch ($state_translation['0']['state_generic_value']) {
case 0:
// OK
$state_style = "class='label label-success'";
break;
case 1:
// Warning
$state_style = "class='label label-warning'";
break;
case 2:
// Critical
$state_style = "class='label label-danger'";
break;
case 3:
// Unknown
default:
$state_style = "class='label label-default'";
break;
}
$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, '<span '.$state_style.'>'.$state_translation['0']['state_descr'].'</span>', $overlib_content, $sensor_class).'</td>
</tr>';
} else {
$sensor_current = $sensor_class == 'runtime' ? formatUptime($sensor['sensor_current'] * 60, 'short') : $sensor['sensor_current'] . $sensor_unit;
$alarmed = ((!is_null($sensor['sensor_limit_low']) && $sensor['sensor_current'] < $sensor['sensor_limit_low']) || (!is_null($sensor['sensor_limit']) && $sensor['sensor_current'] > $sensor['sensor_limit']));
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, '<span '.($alarmed ? "style='color: red'" : '').'>'.$sensor_current.'</span>', $overlib_content, $sensor_class).'</td>
</tr>';
}//end if
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>';

View File

@@ -3,12 +3,7 @@
$sensors = dbFetchRows("SELECT * FROM `sensors` WHERE `device_id` = ? AND `entPhysicalIndex` = ? AND entPhysicalIndex_measured = 'ports' ORDER BY `sensor_type` ASC", array($device['device_id'],$port['ifIndex']));
foreach ($sensors as $sensor) {
$unit = get_unit_for_sensor_class($sensor['sensor_class']);
$state_translation = array();
if (($graph_type == 'sensor_state')) {
$state_translation = dbFetchRows('SELECT * FROM state_translations as ST, sensors_to_state_indexes as SSI WHERE ST.state_index_id=SSI.state_index_id AND SSI.sensor_id = ? AND ST.state_value = ? ', array($sensor['sensor_id'], $sensor['sensor_current']));
}
$unit = __('sensors.' . $sensor['sensor_class'] . '.unit');
if ($sensor['poller_type'] == 'ipmi') {
$sensor_descr = ipmiSensorName($device['hardware'], $sensor['sensor_descr']);
@@ -16,12 +11,7 @@ foreach ($sensors as $sensor) {
$sensor_descr = $sensor['sensor_descr'];
}
if (($graph_type == 'sensor_state') && !empty($state_translation['0']['state_descr'])) {
$sensor_current = get_state_label($sensor['state_generic_value'], $state_translation[0]['state_descr'] . ' (' . $sensor['sensor_current'] . ')');
} else {
$current_label = get_sensor_label_color($sensor);
$sensor_current = "<span class='label $current_label'>" . trim(format_si($sensor['sensor_current']) . $unit). '</span>';
}
$sensor_current = $graph_type == 'sensor_state' ? get_state_label($sensor) : get_sensor_label_color($sensor);
$sensor_limit = trim(format_si($sensor['sensor_limit']) . $unit);
$sensor_limit_low = trim(format_si($sensor['sensor_limit_low']) . $unit);