diff --git a/includes/html/functions.inc.php b/includes/html/functions.inc.php index 53d076729b..6c08054b8e 100644 --- a/includes/html/functions.inc.php +++ b/includes/html/functions.inc.php @@ -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 = "$state_text"; - return $state; + return "$state_text"; } /** - * 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 "".trim(format_si($sensor['sensor_current']).$unit).""; } diff --git a/includes/html/pages/device/entphysical.inc.php b/includes/html/pages/device/entphysical.inc.php index b3b3a16429..760b6bd6f7 100644 --- a/includes/html/pages/device/entphysical.inc.php +++ b/includes/html/pages/device/entphysical.inc.php @@ -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)) { diff --git a/includes/html/pages/device/health/sensors.inc.php b/includes/html/pages/device/health/sensors.inc.php index 8552926dad..0e1ffec6c2 100644 --- a/includes/html/pages/device/health/sensors.inc.php +++ b/includes/html/pages/device/health/sensors.inc.php @@ -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 = "".trim(format_si($sensor['sensor_current']).$unit).""; - } + $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); diff --git a/includes/html/pages/device/overview/generic/sensor.inc.php b/includes/html/pages/device/overview/generic/sensor.inc.php index f55a494fbf..255f71427f 100644 --- a/includes/html/pages/device/overview/generic/sensor.inc.php +++ b/includes/html/pages/device/overview/generic/sensor.inc.php @@ -1,9 +1,5 @@ '; $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 ' - '.overlib_link($link, shorten_interface_type($sensor['sensor_descr']), $overlib_content, $sensor_class).' - '.overlib_link($link, $sensor_minigraph, $overlib_content, $sensor_class).' - '.overlib_link($link, ''.$state_translation['0']['state_descr'].'', $overlib_content, $sensor_class).' - '; - } 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 ' - '.overlib_link($link, shorten_interface_type($sensor['sensor_descr']), $overlib_content, $sensor_class).' - '.overlib_link($link, $sensor_minigraph, $overlib_content, $sensor_class).' - '.overlib_link($link, ''.$sensor_current.'', $overlib_content, $sensor_class).' - '; - }//end if + echo ' + '.overlib_link($link, shorten_interface_type($sensor['sensor_descr']), $overlib_content, $sensor_class).' + '.overlib_link($link, $sensor_minigraph, $overlib_content, $sensor_class).' + '.overlib_link($link, $sensor_current, $overlib_content, $sensor_class).' + '; }//end foreach echo ''; diff --git a/includes/html/pages/device/port/sensors.inc.php b/includes/html/pages/device/port/sensors.inc.php index ca5bef3a1c..d14778d087 100644 --- a/includes/html/pages/device/port/sensors.inc.php +++ b/includes/html/pages/device/port/sensors.inc.php @@ -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 = "" . trim(format_si($sensor['sensor_current']) . $unit). ''; - } + $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); diff --git a/includes/html/table/sensors-common.php b/includes/html/table/sensors-common.php index 534aae99ee..e571990e09 100644 --- a/includes/html/table/sensors-common.php +++ b/includes/html/table/sensors-common.php @@ -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 = "".format_si($sensor['sensor_current']).$unit.""; - } + $sensor_current = $graph_type == 'sensor_state' ? get_state_label($sensor) : get_sensor_label_color($sensor); $response[] = array( 'hostname' => generate_device_link($sensor), diff --git a/resources/lang/en/sensors.php b/resources/lang/en/sensors.php index 8ac971bc65..985a772abc 100644 --- a/resources/lang/en/sensors.php +++ b/resources/lang/en/sensors.php @@ -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',