From b2cc891890bedafe0be78dcad2d456a0a96dbc48 Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Wed, 8 Jul 2015 11:31:37 +0200 Subject: [PATCH] Convert sensors config to a ini format Automatically migrate from the old to the new format for existing install --- conf/sensors.conf | 29 --------------------- conf/sensors.ini | 45 ++++++++++++++++++++++++++++++++ zabbix-agent-addons.spec | 6 +++++ zabbix_scripts/check_sensors_sudo | 23 ++++++++++------- zabbix_scripts/disco_sensors | 44 +++++++++++++++++-------------- zabbix_scripts/util_convert_sensors_ini | 46 +++++++++++++++++++++++++++++++++ 6 files changed, 134 insertions(+), 59 deletions(-) delete mode 100644 conf/sensors.conf create mode 100644 conf/sensors.ini create mode 100644 zabbix_scripts/util_convert_sensors_ini diff --git a/conf/sensors.conf b/conf/sensors.conf deleted file mode 100644 index cfc0951..0000000 --- a/conf/sensors.conf +++ /dev/null @@ -1,29 +0,0 @@ -# You can configure here the sensors -# Format is =!!! -# An alert is triggerd if the temperature is above the high threshold -# The alert is cleared if the temperature is less than low threshold -# the last field (sensor type) is optional and defaults to temp -# It's used if you want to monitor other sensors, like fan, or power -# Example: -# -# -## Examples with ipmitool -# cpu0 = /usr/bin/ipmitool sdr get 'P1 Therm Margin' | grep 'Sensor Reading' | awk '{print $4}'!-30!-39 -# mb = /usr/bin/ipmitool sdr get 'Baseboard Temp' | grep 'Sensor Reading' | awk '{print $4}'!50!45 -# -# fan1 = fan1a=/usr/bin/ipmitool sdr get 'Fan1A RPM' | grep 'Sensor Reading' | awk '{print $4}'!0!0!fan -# pwr1=/usr/bin/ipmitool sdr get 'Pwr Consumption' | grep 'Sensor Reading' | awk '{print $4}'!0!0!power -# -## Examples with smartctl -# sda = /usr/sbin/smartctl -a /dev/sda | grep Temperature_Celsius | awk '{print $10}'!45!40 -# sdb = /usr/sbin/smartctl -a /dev/sdb | grep Temperature_Celsius | awk '{print $10}'!45!50 -# -## Examples with lm_sensors -# cpu0=/usr/bin/sensors | grep temp1 | cut -d':' -f 2 | awk '{print $1'} | sed -e "s/+//g" -e "s/.C//g"!65!55 -# -## Examples with acpi -# cpu0=cat /proc/acpi/thermal_zone/THRM/temperature | awk '{print $2}'!65!55 -# -# -# !!! WARNING !!! -# All the commands will be executed with root privileges diff --git a/conf/sensors.ini b/conf/sensors.ini new file mode 100644 index 0000000..322cd20 --- /dev/null +++ b/conf/sensors.ini @@ -0,0 +1,45 @@ +# This file lets you configure which sensors will be monitored by Zabbix +# Sensors defined here will be sent to Zabbix through its low level discovery feature +# You then have to create discovery rules and prototypes to make use of them +# +# This file is in ini format, each sensor has its own block and a set of key/value pair +# +# Example: +# +# [cpu0] +# description=Temperature of the first CPU +# threshold_high=60 +# threshold_low=50 +# cmd="/usr/bin/sensors | grep temp1 | cut -d':' -f 2 | awk '{print $1}' | sed -e 's/+//g' -e 's/.C//g'" +# type=temp +# unit=°C +# +# [mb] +# description=Motherboard's temperature +# threshold_high=50 +# threshold_low=45 +# cmd="/usr/bin/ipmitool sdr get 'Baseboard Temp' | grep 'Sensor Reading' | awk '{print $4}'" +# type=temp +# unit=°C +# +# [sda] +# description=hard drive temperature +# threshold_high=50 +# threshold_low=45 +# cmd="/usr/sbin/smartctl -A /dev/sda | grep Temperature_Celsius | awk '{print $10}'" +# type=temp +# unit=°C +# +# [fan1] +# description=front fan +# threshold_high=12000 +# threshold_low=1400 +# cmd="/usr/bin/ipmitool sdr get 'Fan1A RPM' | grep 'Sensor Reading' | awk '{print $4}'" +# type=fan +# unit=rpm +# +# +# !!! WARNING !!! +# * All the commands will be executed with root privileges +# * If your cmd contains quotes, you must double quote the whole command +# * If your cmd contains double quotes, you must escape them as \" diff --git a/zabbix-agent-addons.spec b/zabbix-agent-addons.spec index d3aa846..93dd816 100644 --- a/zabbix-agent-addons.spec +++ b/zabbix-agent-addons.spec @@ -17,6 +17,7 @@ Requires: perl(Linux::LVM) Requires: perl(POSIX) Requires: perl(MIME::Base64) Requires: perl(File::Which) +Requires: per(Config::Simple) AutoReqProv: no @@ -55,6 +56,11 @@ LVM, RAID status, S.M.A.R.T. drives, BackupPC etc... %preun %post +if [ $1 -eq 1 ] ; then + if [ -e "/etc/zabbix/sensors.conf" ]; then + /var/lib/zabbix/bin/util_convert_sensors_ini + fi +fi %files %defattr(-,root,root,-) diff --git a/zabbix_scripts/check_sensors_sudo b/zabbix_scripts/check_sensors_sudo index 42c1b7e..4d2c20b 100644 --- a/zabbix_scripts/check_sensors_sudo +++ b/zabbix_scripts/check_sensors_sudo @@ -1,29 +1,32 @@ #!/usr/bin/perl -w +use strict; +use warnings; +use Config::Simple; + my $what = $ARGV[0]; unless (defined $what){ - usage(); - exit(1); + usage(); + exit(1); } -open SENSORS, ('read('/etc/zabbix/sensors.ini'); my $ret = 'ZBX_NOTSUPPORTED'; - -foreach (){ - next unless (/^$what(\s+)?=(\s+)?(.*)!(\-?\d+)!(\-?\d+)(!(\w+))?$/); - my $cmd = $3; - $ret = `$cmd`; +my $sensor = $cfg->get_block($what); +if ($sensor && $sensor->{cmd}){ + $ret = qx($sensor->{cmd}); } + print $ret; exit(0); sub usage { print <<"EOF"; -Usage: $0 sensor_name [high|low] +Usage: $0 sensor_name EOF } diff --git a/zabbix_scripts/disco_sensors b/zabbix_scripts/disco_sensors index dba1cff..50cd3b2 100644 --- a/zabbix_scripts/disco_sensors +++ b/zabbix_scripts/disco_sensors @@ -1,36 +1,40 @@ #!/usr/bin/perl -w -use JSON; +use Config::Simple; use Getopt::Long; +use JSON; -my $sensor_type = 'temp'; +my $type = 'temp'; GetOptions( - "type:s" => \$sensor_type + "type:s" => \$type ); # empty means temp -$sensor_type = ($sensor_type eq '') ? 'temp' : $sensor_type; +$type = ($type eq '') ? 'temp' : $type; my $json; @{$json->{data}} = (); -open SENSORS, ('read('/etc/zabbix/sensors.ini'); +my %sensors = (); +foreach my $k (keys $cfg->vars){ + $k =~ s/\..*$//; + $sensors{$k} = 1 unless $sensors{$k}; +} -foreach (){ - next unless (/^(\w+)(\s+)?=(\s+)?(.*)!(\-?\d+)!(\-?\d+)(!(\w+))?$/); - my ($sensor,$threshigh,$threslow,$type) = ($1,$5,$6,$8); - $type ||= 'temp'; - next if ($sensor_type ne 'all' && $type ne $sensor_type); - push @{$json->{data}}, { - "{#SENSORNAME}" => $sensor, - "{#SENSORTHRESHIGH}" => $threshigh, - "{#SENSORTHRESLOW}" => $threslow, - "{#SENSORTYPE}" => $type - }; +foreach my $k (keys %sensors){ + my $sensor = $cfg->get_block($k); + next if ($type ne 'all' && $type ne $sensor->{type}); + push @{$json->{data}}, { + "{#SENSORNAME}" => $k, + "{#SENSORDESC}" => $sensor->{description}, + "{#SENSORTHRESHIGH}" => $sensor->{threshold_high}, + "{#SENSORTHRESLOW}" => $sensor->{threshold_low}, + "{#SENSORTYPE}" => $sensor->{type}, + "{#SENSORUNIT}" => $sensor->{unit} + }; } -close SENSORS; + print to_json($json); exit(0); - diff --git a/zabbix_scripts/util_convert_sensors_ini b/zabbix_scripts/util_convert_sensors_ini new file mode 100644 index 0000000..2224ee9 --- /dev/null +++ b/zabbix_scripts/util_convert_sensors_ini @@ -0,0 +1,46 @@ +#!/usr/bin/env perl -w + +use strict; +use warnings; + +use Config::Simple '-strict'; +use JSON; + +my $old = '/etc/zabbix/sensors.conf'; +my $new = '/etc/zabbix/sensors.ini'; + +my $sensors = {}; + +my $units = { + temp => '°C', + fan => 'rpm', + power => 'W' +}; + +open OLDSENSORS, ("<$old") || + die "Couldn't open $old: $!\n"; + + +foreach (){ + next unless (/^(\w+)(\s+)?=(\s+)?(.*)!(\-?\d+)!(\-?\d+)(!(\w+))?$/); + my ($sensor,$cmd,$threshigh,$threslow,$type) = ($1,$4,$5,$6,$8); + $type ||= 'temp'; + $sensors->{$sensor} = { + description => $sensor, + cmd => $cmd, + threshold_high => $threshigh, + threshold_low => $threslow, + type => $type, + unit => $units->{$type} + }; +} + +my $cfg = new Config::Simple(syntax => 'ini'); + +foreach my $k (keys %$sensors){ + $cfg->set_block($k, $sensors->{$k}); +} + +$cfg->write($new); + +rename $old, $old . '.bak';