mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
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:
committed by
Tony Murray
parent
ebd004edc7
commit
9d68f27296
@@ -1589,92 +1589,52 @@ function get_device_name($device)
|
||||
* Returns state generic label from value with optional text
|
||||
*/
|
||||
|
||||
function get_state_label($state_value, $state_text_param = null)
|
||||
function get_state_label($sensor)
|
||||
{
|
||||
switch ($state_value) {
|
||||
$state_translation = dbFetchRow('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']));
|
||||
|
||||
switch ($state_translation['state_generic_value']) {
|
||||
case 0: // OK
|
||||
$state_text = (is_null($state_text_param) ? "OK": $state_text_param);
|
||||
$state_text = $state_translation['state_descr'] ?: "OK";
|
||||
$state_label = "label-success";
|
||||
break;
|
||||
case 1: // Warning
|
||||
$state_text = (is_null($state_text_param) ? "Warning": $state_text_param);
|
||||
$state_text = $state_translation['state_descr'] ?: "Warning";
|
||||
$state_label = "label-warning";
|
||||
break;
|
||||
case 2: // Critical
|
||||
$state_text = (is_null($state_text_param) ? "Critical": $state_text_param);
|
||||
$state_text = $state_translation['state_descr'] ?: "Critical";
|
||||
$state_label = "label-danger";
|
||||
break;
|
||||
case 3:// Unknown
|
||||
case 3: // Unknown
|
||||
default:
|
||||
$state_text = (is_null($state_text_param) ? "Unknown": $state_text_param);
|
||||
$state_text = $state_translation['state_descr'] ?: "Unknown";
|
||||
$state_label = "label-default";
|
||||
}
|
||||
$state = "<span class='label $state_label'>$state_text</span>";
|
||||
return $state;
|
||||
return "<span class='label $state_label'>$state_text</span>";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get state label color
|
||||
* Get sensor label and state color
|
||||
*/
|
||||
function get_sensor_label_color($sensor)
|
||||
{
|
||||
$current_label_color = "label-success";
|
||||
$label_style = "label-success";
|
||||
if (is_null($sensor)) {
|
||||
return "label-unknown";
|
||||
}
|
||||
if (!is_null($sensor['sensor_limit_warn']) && $sensor['sensor_current'] > $sensor['sensor_limit_warn']) {
|
||||
$current_label_color = "label-warning";
|
||||
$label_style = "label-warning";
|
||||
}
|
||||
if (!is_null($sensor['sensor_limit_low_warn']) && $sensor['sensor_current'] < $sensor['sensor_limit_low_warn']) {
|
||||
$current_label_color = "label-warning";
|
||||
$label_style = "label-warning";
|
||||
}
|
||||
if (!is_null($sensor['sensor_limit']) && $sensor['sensor_current'] > $sensor['sensor_limit']) {
|
||||
$current_label_color = "label-danger";
|
||||
$label_style = "label-danger";
|
||||
}
|
||||
if (!is_null($sensor['sensor_limit_low']) && $sensor['sensor_current'] < $sensor['sensor_limit_low']) {
|
||||
$current_label_color = "label-danger";
|
||||
$label_style = "label-danger";
|
||||
}
|
||||
|
||||
return $current_label_color;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the unit for the sensor class given as parameter
|
||||
* @param $class
|
||||
* @return string The unit
|
||||
*/
|
||||
function get_unit_for_sensor_class($class)
|
||||
{
|
||||
$units_by_classes = array(
|
||||
'ber' => '',
|
||||
'charge' => '%',
|
||||
'chromatic_dispersion' => 'ps/nm',
|
||||
'cooling' => 'W',
|
||||
'count' => '',
|
||||
'current' => 'A',
|
||||
'dbm' => 'dBm',
|
||||
'delay' => 's',
|
||||
'eer' => '',
|
||||
'fanspeed' => 'rpm',
|
||||
'frequency' => 'Hz',
|
||||
'humidity' => '%',
|
||||
'load' => '%',
|
||||
'power' => 'W',
|
||||
'power_consumed' => 'kWh',
|
||||
'power_factor' => '',
|
||||
'pressure' => 'kPa',
|
||||
'quality_factor' => 'dB',
|
||||
'signal' => 'dBm',
|
||||
'snr' => 'dB',
|
||||
'state' => '',
|
||||
'temperature' => '°C',
|
||||
'voltage' => 'V',
|
||||
'waterflow' => 'l/m',
|
||||
);
|
||||
|
||||
if (!array_key_exists($class, $units_by_classes)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $units_by_classes[$class];
|
||||
$unit = __('sensors.' . $sensor['sensor_class'] . '.unit');
|
||||
return "<span class='label $label_style'>".trim(format_si($sensor['sensor_current']).$unit)."</span>";
|
||||
}
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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>';
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -112,18 +112,7 @@ foreach (dbFetchRows($sql, $param) as $sensor) {
|
||||
|
||||
$sensor['sensor_descr'] = substr($sensor['sensor_descr'], 0, 48);
|
||||
|
||||
if ($graph_type == 'sensor_state') {
|
||||
// If we have a state, let's display a label with textual state translation
|
||||
$state_translation = array();
|
||||
$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']));
|
||||
|
||||
//$current_label = get_state_label_color($sensor);
|
||||
$sensor_current = get_state_label($state_translation['0']['state_generic_value'], (!empty($state_translation['0']['state_descr'])) ? $state_translation[0]['state_descr'] . " (".$sensor['sensor_current'].")" : $sensor['sensor_current']);
|
||||
} else {
|
||||
// we have another sensor
|
||||
$current_label = get_sensor_label_color($sensor);
|
||||
$sensor_current = "<span class='label $current_label'>".format_si($sensor['sensor_current']).$unit."</span>";
|
||||
}
|
||||
$sensor_current = $graph_type == 'sensor_state' ? get_state_label($sensor) : get_sensor_label_color($sensor);
|
||||
|
||||
$response[] = array(
|
||||
'hostname' => generate_device_link($sensor),
|
||||
|
||||
@@ -4,7 +4,7 @@ return [
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Sesnors Language Lines
|
||||
| Sensors Language Lines
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The following language lines are used to translate names and units of sensors
|
||||
@@ -57,7 +57,7 @@ return [
|
||||
'short' => 'dBm',
|
||||
'long' => 'dBm',
|
||||
'unit' => 'dBm',
|
||||
'unit_long' => 'Decibal-Milliwatts',
|
||||
'unit_long' => 'Decibel-Milliwatts',
|
||||
],
|
||||
'delay' => [
|
||||
'short' => 'Delay',
|
||||
|
||||
Reference in New Issue
Block a user