Last active
April 6, 2022 03:04
-
-
Save chrisjrob/6bb2bdfa1ddee0bd94d9cc6b617d907a to your computer and use it in GitHub Desktop.
Check_MK Local Check for One Wire Temperature Sensors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# | |
# rpitemp - Rasberry Pi Temperature Monitoring | |
# | |
=pod | |
=head1 NAME | |
rpitemp: Raspberry Pi Temperature Monitoring | |
=head1 DESCRIPTION | |
Simple script to get readings from one wire | |
temperature sensors and returns in a format | |
suitable for Check_MK monitoring | |
=head1 INSTALLATION | |
Install Check_MK Agent on the Raspberry Pi: | |
# apt install check-mk-agent | |
Add this script into the following location: | |
/usr/lib/check_mk_agent/local | |
Make script executable: | |
# chmod 755 /usr/lib/check_mk_agent/local/rpitemp.pl | |
=head1 CONFIGURATION | |
Discover the names of your devices with: | |
# ls -al /sys/bus/w1/devices | |
Update the configuration section below with | |
the names of your devices. | |
If you only have a single sensor, please | |
delete the $sensors B block. | |
You may add additional sensors simply by | |
copying the code block for sensor B down | |
and labelling it C and so on. | |
On your Check MK server you should be able to | |
discover the new service in the usual way. | |
=head1 AUTHOR | |
chrisjrob - <http://chrisjrob.com> | |
=head1 SEE ALSO | |
<https://mathias-kettner.de/checkmk_localchecks.html> | |
=cut | |
# Configuration | |
my $device_path = "/sys/bus/w1/devices"; | |
my $sensors = { | |
'A' => { | |
'device' => "$device_path/28-0417031131ff/w1_slave", | |
'name' => 'Room', | |
'warning' => 26, | |
'critical' => 28, | |
}, | |
'B' => { | |
'device' => "$device_path/28-0517024e2cff/w1_slave", | |
'name' => 'Cabinet', | |
'warning' => 28, | |
'critical' => 30, | |
}, | |
}; | |
# End of configuration | |
my ($reading, $status, $statustext) = get_readings($sensors); | |
my @responses; | |
my @extended; | |
foreach my $label ( sort keys %{ $sensors} ) { | |
push(@responses, | |
"$sensors->{ $label }{'name'}=$reading->{ $label }{temperature}" | |
); | |
push(@extended, | |
"$sensors->{ $label }{'name'}=$reading->{ $label }{temperature}C" | |
); | |
} | |
my $response = join("|", @responses); | |
my $extended = join(" ", @extended); | |
print "$status Temperature $response $statustext Sensors $extended\n"; | |
exit; | |
sub get_readings { | |
my $sensors = shift; | |
my $overallstatus = 0; | |
my $overallstatustext = 'OK'; | |
my $reading; | |
foreach my $label ( sort keys %{ $sensors} ) { | |
my $temperature = read_temperature($sensors->{ $label }{'device'}); | |
my $status = 0; | |
my $statustext = 'OK'; | |
if ($temperature >= $sensors->{ $label }{'warning'}) { | |
$status = 1; | |
$statustext = 'WARNING'; | |
} elsif ($temperature >= $sensors->{ $label }{'critical'}) { | |
$status = 2; | |
$statustext = 'CRITICAL'; | |
} | |
if ($status > $overallstatus) { | |
$overallstatus = $status; | |
$overallstatustext = $statustext; | |
} | |
$reading->{ $label } = { | |
'temperature' => $temperature, | |
'status' => $status, | |
'statustext' => $statustext, | |
}; | |
} | |
return($reading, $overallstatus, $overallstatustext); | |
} | |
sub read_temperature { | |
my $sensor = shift; | |
if (! -e $sensor) { | |
die "$sensor does not exist"; | |
} | |
my $text = read_file($sensor); | |
my $temperature; | |
if ($text =~ /t=(\d+)\s*$/s) { | |
$temperature = $1/1000; | |
} | |
return $temperature; | |
} | |
sub read_file { | |
my $file = shift; | |
open(my $fh, "<", $file) | |
or die "Cannot open $file for reading: $!"; | |
local $/ = undef; | |
my $text = <$fh>; | |
close($fh) | |
or die "Cannot close $file: $!"; | |
return $text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment