Last active
April 11, 2021 11:36
-
-
Save Leeft/84842b2ee8b5b8f56733446b19e69139 to your computer and use it in GitHub Desktop.
checkmk local script for lm-sensors CPU temperature readings
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/env perl | |
use utf8; | |
use strict; | |
use warnings; | |
use feature 'say'; | |
use JSON::XS; | |
binmode STDOUT, ':utf8'; | |
# Crude checkmk script to read "sensors -j" JSON output (from lm-sensors, | |
# configure that first) and output CPU package and core temperatures to a | |
# format that the checkmk agent understands. | |
# | |
# Does not support more than 1 CPU without modification, but should | |
# support any number of cores on that single CPU. | |
# | |
# Dump this script in your agent local directory (/usr/lib/check_mk_agent/local | |
# on linux) and do a service scan to add the sources to checkmk. | |
# | |
# https://docs.checkmk.com/latest/en/localchecks.html for more info. | |
# | |
# Example output: | |
# | |
# $ ./lm-sensors-temperatures.pl | |
# 0 cpu0-package-temp package-temp=34;80;100 OK: CPU 0 package temperature | |
# 0 cpu0-core-temp-0 core-temp=32;80;100 OK: CPU 0 core 0 temperature | |
# 0 cpu0-core-temp-1 core-temp=32;80;100 OK: CPU 0 core 1 temperature | |
# 0 cpu0-core-temp-2 core-temp=30;80;100 OK: CPU 0 core 2 temperature | |
# 0 cpu0-core-temp-3 core-temp=31;80;100 OK: CPU 0 core 3 temperature | |
my %state = ( | |
0 => 'OK', | |
1 => 'WARN', | |
2 => 'CRIT', | |
3 => 'UNKNOWN', | |
); | |
my $decoded = decode_json `sensors -j`; | |
foreach my $key ( sort keys %{ $decoded } ) | |
{ | |
if ( $key eq 'acpitz-acpi-0' ) | |
{ | |
# Already reported, not interested in these here | |
} | |
elsif ( $key =~ /^coretemp-/ ) | |
{ | |
# Get CPU numbers; hardcoded as I have no way to know what the | |
# data looks like with more than one CPU in the system. | |
my @package_ids = ( 0 ); | |
foreach my $cpu ( @package_ids ) | |
{ | |
if ( exists $decoded->{ $key }->{ "Package id $cpu" } ) | |
{ | |
my ( $value, $warn, $crit ) = values_for( $decoded->{ $key }{ "Package id $cpu" } ); | |
my $state = 3; | |
if ( defined $value and defined $warn and $value < $warn ) { | |
$state = 0; | |
} elsif ( defined $value and defined $warn and $value >= $warn ) { | |
$state = 1; | |
} | |
if ( defined $value and defined $crit and $value >= $crit ) { | |
$state = 2; | |
} | |
say "$state cpu$cpu-package-temp package-temp=$value;$warn;$crit $state{$state}: CPU $cpu package temperature at $value°C"; | |
} | |
# Get core temperatures for the CPU | |
for my $i ( 0 .. 127 ) | |
{ | |
last if not exists $decoded->{ $key }{ "Core $i" }; | |
my ( $value, $warn, $crit ) = values_for( $decoded->{ $key }{ "Core $i" } ); | |
my $state = 3; | |
if ( defined $value and defined $warn and $value < $warn ) { | |
$state = 0; | |
} elsif ( defined $value and defined $warn and $value >= $warn ) { | |
$state = 1; | |
} | |
if ( defined $value and defined $crit and $value >= $crit ) { | |
$state = 2; | |
} | |
say "$state cpu$cpu-core-temp-$i core-temp=$value;$warn;$crit $state{$state}: CPU $cpu core $i temperature at $value°C"; | |
} | |
} | |
} | |
} | |
sub values_for { | |
my $data = shift; | |
my %temp = %{ $data }; | |
foreach my $full ( keys %temp ) { | |
( my $stripped = $full ) =~ s/^temp\d+_//; | |
$temp{ $stripped } = delete $temp{ $full }; | |
} | |
my $value = $temp{ input }; | |
my $warn = $temp{ max }; | |
my $crit = $temp{ crit }; | |
return ( $value, $warn, $crit ); | |
} | |
# EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment