From 2c29e4ecaa63a1f33c59bac74d4422c42be431fc Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Thu, 29 Jun 2023 12:12:07 +0200 Subject: [PATCH] Better sensor output parsing --- zabbix_scripts/util_generate_sensors_ini | 26 ++++++++++++------------ 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/zabbix_scripts/util_generate_sensors_ini b/zabbix_scripts/util_generate_sensors_ini index 025c462..4a4b5e2 100755 --- a/zabbix_scripts/util_generate_sensors_ini +++ b/zabbix_scripts/util_generate_sensors_ini @@ -72,7 +72,7 @@ my $sensors = {}; # Try to detect IPMI sensors if ($ipmitool && -x $ipmitool){ # First check for temperature sensors - my @lines = qx($ipmitool sdr type Temperature); + my @lines = qx($ipmitool sdr type Temperature 2>/dev/null); if ($? == 0){ SENSOR: foreach my $l (@lines){ chomp $l; @@ -84,7 +84,7 @@ if ($ipmitool && -x $ipmitool){ my $name = $1; my $sensor = {}; - my @details = qx($ipmitool sdr get '$name'); + my @details = qx($ipmitool sdr get '$name' 2>/dev/null); if ($? != 0){ print "Couldn't get detail for sensor $name\n"; next SENSOR; @@ -124,7 +124,7 @@ if ($ipmitool && -x $ipmitool){ $sensor->{description} = $name; $sensor->{type} = 'temp'; $sensor->{unit} = '°C'; - $sensor->{cmd} = "$ipmitool sdr get '$name' 2>/dev/null | perl -ne '/Sensor Reading\\s*:\\s*([^\\s]+)/ && print \"\$1\\n\"'"; + $sensor->{cmd} = "$ipmitool sdr get '$name' 2>/dev/null | perl -ne 'if (/Sensor Reading\\s*:\\s*([^\\s]+)/) { print \"\$1\\n\"; last }'"; my $id = sensor_name($name); $sensors->{$id} = $sensor; print "Found a temperature sensor using IPMI: $name\n"; @@ -133,7 +133,7 @@ if ($ipmitool && -x $ipmitool){ # Now check for Fan, nearly the same as Temp, but # * We try to detect the unit # * threshold handling is not the same - @lines = qx($ipmitool sdr type Fan); + @lines = qx($ipmitool sdr type Fan 2>/dev/null); if ($? == 0){ SENSOR: foreach my $l (@lines){ chomp $l; @@ -144,7 +144,7 @@ if ($ipmitool && -x $ipmitool){ my $value = $3; my $sensor = {}; - my @details = qx($ipmitool sdr get '$name'); + my @details = qx($ipmitool sdr get '$name' 2>/dev/null); if ($? != 0){ print "Couldn't get detail for sensor $name\n"; next SENSOR; @@ -174,14 +174,14 @@ if ($ipmitool && -x $ipmitool){ $sensor->{description} = $name; $sensor->{type} = 'fan'; $sensor->{unit} = ($value =~ m/percent|%/ || $val < 100) ? '%' : 'rpm'; - $sensor->{cmd} = "$ipmitool sdr get '$name' 2>/dev/null | perl -ne '/Sensor Reading\\s*:\\s*([^\\s]+)/ && print \"\$1\\n\"'"; + $sensor->{cmd} = "$ipmitool sdr get '$name' 2>/dev/null | perl -ne 'if (/Sensor Reading\\s*:\\s*([^\\s]+)/) { print \"\$1\\n\"; last }'"; my $id = sensor_name($name); $sensors->{$id} = $sensor; print "Found a fan sensor using IPMI: $name\n"; } } # Now look for power information - @lines = qx($ipmitool sdr type 'Current'); + @lines = qx($ipmitool sdr type 'Current' 2>/dev/null); if ($? == 0){ SENSOR: foreach my $l (@lines){ chomp $l; @@ -193,7 +193,7 @@ if ($ipmitool && -x $ipmitool){ my $value = $4; my $sensor = {}; if ($name =~ m/(Power)|(Pwr)|(Consumption)|(PS\d+\sCurr\sOut)/i || $value =~ m/W(att)?/i){ - my @details = qx($ipmitool sdr get '$name'); + my @details = qx($ipmitool sdr get '$name' 2>/dev/null); if ($? != 0){ print "Couldn't get detail for sensor $name\n"; next SENSOR; @@ -224,7 +224,7 @@ if ($ipmitool && -x $ipmitool){ $sensor->{description} = $name; $sensor->{type} = 'power'; $sensor->{unit} = ($name =~ m/%/) ? '%' : 'Watt'; - $sensor->{cmd} = "$ipmitool sdr get '$name' 2>/dev/null | perl -ne '/Sensor Reading\\s*:\\s*([^\\s]+)/ && print \"\$1\\n\"'"; + $sensor->{cmd} = "$ipmitool sdr get '$name' 2>/dev/null | perl -ne 'if (/Sensor Reading\\s*:\\s*([^\\s]+)/) { print \"\$1\\n\"; last }'"; my $id = sensor_name($name); $sensors->{$id} = $sensor; print "Found a power sensor using IPMI: $name\n"; @@ -276,7 +276,7 @@ if ($lmsensor && -x $lmsensor){ $sensor->{description} = $name; $sensor->{type} = 'temp'; $sensor->{unit} = '°C'; - $sensor->{cmd} = "$lmsensor | grep '$name:' | cut -d+ -f2 | cut -d. -f1 | head -1"; + $sensor->{cmd} = "$lmsensor | perl -ne 'if (/^$name:\\s*\\+(\\d+)/) { print \"\$1\\n\"; last }'" my $id = sensor_name($name); $sensors->{$id} = $sensor; print "Found a temperature sensor using lm_sensors: $name\n"; @@ -297,7 +297,7 @@ if ($smartctl && -x $smartctl){ threshold_high => $def_hd_temp_thres_high, type => 'temp', unit => '°C', - cmd => "$smartctl -A /dev/$block | grep $1 | awk '{print \$10}'" + cmd => "$smartctl -A /dev/$block | perl -ne 'if (/Temperature_Celsius(\\s+[^\\s]+){7}\\s+(\\d+(\.\d+)?)/) { print \"\$2\\n\"; last }'" }; print "Found a temperature sensor using smartctl: $block\n"; last; @@ -310,7 +310,7 @@ if ($smartctl && -x $smartctl){ threshold_high => $def_hd_temp_thres_high, type => 'temp', unit => '°C', - cmd => "$smartctl -A /dev/$block | grep Temperature: | awk '{ print \$2 }'" + cmd => "$smartctl -A /dev/$block | perl -ne 'if (/Temperature:\\s+(\\d+(\\.\\d+)?)/) { print \"\$1\\n\"; last }'" }; print "Found a temperature sensor using smartctl: $block\n"; last; @@ -343,7 +343,7 @@ if ($smartctl && -x $smartctl){ threshold_high => $def_hd_temp_thres_high, type => 'temp', unit => '°C', - cmd => "$smartctl -A -d megaraid,$i /dev/sda | grep $1 | awk '{print \$10}'" + cmd => "$smartctl -A -d megaraid,$i /dev/sda | perl -ne 'if (/(Temperature_Celsius|Airflow_Temperature_Cel)(\\s+[^\\s]+){7}\\s+(\\d+)/) { print \"\$3\\n\"; last }'" }; print "Found a temperature sensor using smartctl (megaraid): sda-$i\n"; last;