File: //usr/local/bin/zabbix/discover_temp_sensor.pl
#!/usr/bin/perl
# TODO add getops - pass sensor name to get temp ?
# TODO see about replacing this by having zabbix connect to IPMI directly.
use strict;
use warnings;
use Getopt::Long;
my $query;
GetOptions(
"s|sensor=s" => \$query,
);
sub exit_empty {
print '{ "data": [] }';
exit;
}
my $ipmi = "/usr/bin/ipmitool";
# Check whether this machine has ipmi or not.
(-e "/dev/ipmi0") || exit_empty();
(-e $ipmi) || exit_empty();
my %sensors;
my $command = $ipmi . " sdr elist full 2>/dev/null | grep degrees";
my $output = qx($command);
# Gather all the data
my @lines = split /\n/, $output; # Splits output on new lines
for my $line (@lines) {
$line =~ s/\h+/ /g; # Removes extra white spaces
# Expected line format example:
# Outlet_TEMP | 42h | ok | 66.2 | 52 degrees C
my @line_fields = split / \Q| /, $line; # Fields are separated by ' | '
my $sensor_name = $line_fields[0];
my $temperature = $line_fields[4];
$temperature =~ s/\s+.*$//; # strip off ' degrees C'
$sensors{$sensor_name} = $temperature;
}
# If a sensor name was passed in, return its temperature and quit.
if ($query) {
print $sensors{$query};
exit;
}
# json-ify the sensor names for zabbix discovery
my $json = qq({ "data": [\n);
for my $sensor (keys %sensors) {
$json .= qq( { "{#TEMPSENSOR}":"$sensor"},\n);
}
# trim the last ',' off, json doesn't like trailing commas
$json =~ s/,\n$/\n/;
# close up shop
$json .= qq(]}\n);
print $json;
exit;