Convert sensors config to a ini format

Automatically migrate from the old to the new format for existing install
tags/zabbix-agent-addons-0.2.20-1
Daniel Berteaud 10 years ago
parent fd6de22593
commit b2cc891890
  1. 29
      conf/sensors.conf
  2. 45
      conf/sensors.ini
  3. 6
      zabbix-agent-addons.spec
  4. 23
      zabbix_scripts/check_sensors_sudo
  5. 44
      zabbix_scripts/disco_sensors
  6. 46
      zabbix_scripts/util_convert_sensors_ini

@ -1,29 +0,0 @@
# You can configure here the sensors
# Format is <sensors_name>=<command>!<high threshold>!<low threshold>!<sensor type>
# 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

@ -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 \"

@ -17,6 +17,7 @@ Requires: perl(Linux::LVM)
Requires: perl(POSIX) Requires: perl(POSIX)
Requires: perl(MIME::Base64) Requires: perl(MIME::Base64)
Requires: perl(File::Which) Requires: perl(File::Which)
Requires: per(Config::Simple)
AutoReqProv: no AutoReqProv: no
@ -55,6 +56,11 @@ LVM, RAID status, S.M.A.R.T. drives, BackupPC etc...
%preun %preun
%post %post
if [ $1 -eq 1 ] ; then
if [ -e "/etc/zabbix/sensors.conf" ]; then
/var/lib/zabbix/bin/util_convert_sensors_ini
fi
fi
%files %files
%defattr(-,root,root,-) %defattr(-,root,root,-)

@ -1,29 +1,32 @@
#!/usr/bin/perl -w #!/usr/bin/perl -w
use strict;
use warnings;
use Config::Simple;
my $what = $ARGV[0]; my $what = $ARGV[0];
unless (defined $what){ unless (defined $what){
usage(); usage();
exit(1); exit(1);
} }
open SENSORS, ('</etc/zabbix/sensors.conf') || my $cfg = new Config::Simple;
die "Couldn't open /etc/zabbix/sensors.conf: $!\n"; $cfg->read('/etc/zabbix/sensors.ini');
my $ret = 'ZBX_NOTSUPPORTED'; my $ret = 'ZBX_NOTSUPPORTED';
my $sensor = $cfg->get_block($what);
foreach (<SENSORS>){ if ($sensor && $sensor->{cmd}){
next unless (/^$what(\s+)?=(\s+)?(.*)!(\-?\d+)!(\-?\d+)(!(\w+))?$/); $ret = qx($sensor->{cmd});
my $cmd = $3;
$ret = `$cmd`;
} }
print $ret; print $ret;
exit(0); exit(0);
sub usage { sub usage {
print <<"EOF"; print <<"EOF";
Usage: $0 sensor_name [high|low] Usage: $0 sensor_name
EOF EOF
} }

@ -1,36 +1,40 @@
#!/usr/bin/perl -w #!/usr/bin/perl -w
use JSON; use Config::Simple;
use Getopt::Long; use Getopt::Long;
use JSON;
my $sensor_type = 'temp'; my $type = 'temp';
GetOptions( GetOptions(
"type:s" => \$sensor_type "type:s" => \$type
); );
# empty means temp # empty means temp
$sensor_type = ($sensor_type eq '') ? 'temp' : $sensor_type; $type = ($type eq '') ? 'temp' : $type;
my $json; my $json;
@{$json->{data}} = (); @{$json->{data}} = ();
open SENSORS, ('</etc/zabbix/sensors.conf') || my $cfg = new Config::Simple;
die "Couldn't open /etc/zabbix/sensors.conf: $!\n"; $cfg->read('/etc/zabbix/sensors.ini');
my %sensors = ();
foreach my $k (keys $cfg->vars){
$k =~ s/\..*$//;
$sensors{$k} = 1 unless $sensors{$k};
}
foreach (<SENSORS>){ foreach my $k (keys %sensors){
next unless (/^(\w+)(\s+)?=(\s+)?(.*)!(\-?\d+)!(\-?\d+)(!(\w+))?$/); my $sensor = $cfg->get_block($k);
my ($sensor,$threshigh,$threslow,$type) = ($1,$5,$6,$8); next if ($type ne 'all' && $type ne $sensor->{type});
$type ||= 'temp'; push @{$json->{data}}, {
next if ($sensor_type ne 'all' && $type ne $sensor_type); "{#SENSORNAME}" => $k,
push @{$json->{data}}, { "{#SENSORDESC}" => $sensor->{description},
"{#SENSORNAME}" => $sensor, "{#SENSORTHRESHIGH}" => $sensor->{threshold_high},
"{#SENSORTHRESHIGH}" => $threshigh, "{#SENSORTHRESLOW}" => $sensor->{threshold_low},
"{#SENSORTHRESLOW}" => $threslow, "{#SENSORTYPE}" => $sensor->{type},
"{#SENSORTYPE}" => $type "{#SENSORUNIT}" => $sensor->{unit}
}; };
} }
close SENSORS;
print to_json($json); print to_json($json);
exit(0); exit(0);

@ -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 (<OLDSENSORS>){
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';
Loading…
Cancel
Save