Beautify port health (#9981)

* Removal of the unused  variable
* Replace double quotes by single quotes when possible
* Adding spaces between operands of the concatenation operator
* Rewrite of code echoed to the browser
* Reuse the logic of the Device/Health page for displaying current and limit values
* New helper function to determine the unit for the different sensor classes
* Fixing wrong value for current unit
This commit is contained in:
Yann Gauteron 2019-03-20 16:17:49 +01:00 committed by PipoCanaja
parent f7b071a221
commit 96797aa813
2 changed files with 83 additions and 18 deletions

View File

@ -1628,3 +1628,44 @@ function get_sensor_label_color($sensor)
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];
}

View File

@ -2,35 +2,59 @@
$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']));
$row = 0;
foreach ($sensors as $sensor) {
if (!is_integer($row / 2)) {
$row_colour = $config['list_colour']['even'];
} else {
$row_colour = $config['list_colour']['odd'];
$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']));
}
if ($sensor['poller_type'] == "ipmi") {
if ($sensor['poller_type'] == 'ipmi') {
$sensor_descr = ipmiSensorName($device['hardware'], $sensor['sensor_descr']);
} else {
$sensor_descr = $sensor['sensor_descr'];
}
$sensor_current = format_si($sensor['sensor_current']).$unit;
$sensor_limit = format_si($sensor['sensor_limit']).$unit;
$sensor_limit_low = format_si($sensor['sensor_limit_low']).$unit;
echo "<div class='panel panel-default'>
<div class='panel-heading'>
<h3 class='panel-title'>$sensor_descr <div class='pull-right'>$sensor_current | $sensor_limit_low <> $sensor_limit</div></h3>
</div>";
echo "<div class='panel-body'>";
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_limit = trim(format_si($sensor['sensor_limit']) . $unit);
$sensor_limit_low = trim(format_si($sensor['sensor_limit_low']) . $unit);
$sensor_limit_warn = trim(format_si($sensor['sensor_limit_warn']) . $unit);
$sensor_limit_low_warn = trim(format_si($sensor['sensor_limit_low_warn']) . $unit);
echo "<div class='panel panel-default'>\n" .
" <div class='panel-heading'>\n" .
" <h3 class='panel-title'>$sensor_descr <div class='pull-right'>$sensor_current";
//Display low and high limit if they are not null (format_si() is changing null to '0')
if (!is_null($sensor['sensor_limit_low'])) {
echo " <span class='label label-default'>low: $sensor_limit_low</span>";
}
if (!is_null($sensor['sensor_limit_low_warn'])) {
echo " <span class='label label-default'>low_warn: $sensor_limit_low_warn</span>";
}
if (!is_null($sensor['sensor_limit_warn'])) {
echo " <span class='label label-default'>high_warn: $sensor_limit_warn</span>";
}
if (!is_null($sensor['sensor_limit'])) {
echo " <span class='label label-default'>high: $sensor_limit</span>";
}
echo " </div></h3>" .
" </div>\n" .
" <div class='panel-body'>\n";
$graph_array['id'] = $sensor['sensor_id'];
$graph_array['type'] = "sensor_" . $sensor['sensor_class'];
$graph_array['type'] = 'sensor_' . $sensor['sensor_class'];
include 'includes/print-graphrow.inc.php';
echo '</div></div>';
$row++;
echo " </div>\n" .
"</div>\n";
}
unset($row);