zabbix-agent-addons/zabbix_scripts/disco_sensors
Daniel Berteaud a30233bf33 Force ini syntax
Prevent error when the config is empty
2016-11-23 19:18:10 +01:00

42 lines
896 B
Perl

#!/usr/bin/perl -w
use Config::Simple;
use Getopt::Long;
use JSON;
my $type = 'temp';
GetOptions(
"type:s" => \$type
);
# empty means temp
$type = ($type eq '') ? 'temp' : $type;
my $json;
@{$json->{data}} = ();
my $cfg = new Config::Simple;
$cfg->read('/etc/zabbix/sensors.ini');
$cfg->syntax('ini');
my %sensors = ();
foreach my $k (keys %{$cfg->vars}){
$k =~ s/\..*$//;
$sensors{$k} = 1 unless $sensors{$k};
}
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}
};
}
print to_json($json);
exit(0);