/usr/lib/nagios/plugins/check_lm_sensors is in nagios-plugins-contrib 21.20170222.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 | #!/usr/bin/perl
# check_lm_sensors is a Nagios plugin to monitor the values of on board sensors and hard
# disk temperatures on Linux systems
#
# See the INSTALL file for installation instructions
#
# Copyright (c) 2009-2010, ETH Zurich.
# Copyright (c) 2009-2015
#
# This module is free software; you can redistribute it and/or modify it
# under the terms of GNU general public license (gpl) version 3.
# See the LICENSE file for details.
#
use 5.00800;
use strict;
use warnings;
use Carp;
use Data::Dumper;
use English qw(-no_match_vars);
use Getopt::Long;
use List::MoreUtils qw(apply);
use Readonly;
my $plugin_module = load_module( 'Monitoring::Plugin', 'Nagios::Plugin' );
my $plugin_threshold_module =
load_module( 'Monitoring::Plugin::Threshold', 'Nagios::Plugin::Threshold' );
my $plugin_getopt_module =
load_module( 'Monitoring::Plugin::Getopt', 'Nagios::Plugin::Getopt' );
# Check which version of the monitoring plugins is available
sub load_module {
my @names = @_;
my $loaded_module;
for my $name (@names) {
my $file = $name;
# requires need either a bare word or a file name
$file =~ s{::}{/}gsxm;
$file .= '.pm';
eval { ## no critic (ErrorHandling::RequireCheckingReturnValueOfEval)
require $file;
$name->import();
};
if ( !$EVAL_ERROR ) {
$loaded_module = $name;
last;
}
}
if ( !$loaded_module ) {
#<<<
print 'CHECK_UPDATES: plugin not found: ' . join( ', ', @names ) . "\n"; ## no critic (RequireCheckedSyscall)
#>>>
exit 2;
}
return $loaded_module;
}
use version; our $VERSION = '4.1.1';
my $desc;
my $drives;
my $hddtemp_bin;
my $help;
my $limits;
my $list;
my $name;
my $plugin;
my $prog_name;
my $raw;
my $result;
my $sensors;
my $status;
my $unknowns;
my $verbosity;
my %highs;
my %lows;
my %renames;
my %ranges;
my %sensor_values;
my %sensor_names;
# initialization
$desc = q{};
$drives = 1;
$help = q{};
$prog_name = 'LM_SENSORS';
$plugin = $plugin_module->new( shortname => $prog_name );
$sensors = 1;
$status = q{};
$unknowns = q{};
$verbosity = 0;
##############################################################################
# subroutines
##############################################################################
# Usage : verbose("some message string", $optional_verbosity_level);
# Purpose : write a message if the verbosity level is high enough
# Returns : n/a
# Arguments : message : message string
# level : options verbosity level
# Throws : n/a
# Comments : n/a
# See also : n/a
sub verbose {
# arguments
my $message = shift;
my $level = shift;
if ( !defined $message ) {
$plugin->nagios_exit( $plugin_module->UNKNOWN,
q{Internal error: not enough parameters for 'verbose'} );
}
if ( !defined $level ) {
$level = 0;
}
if ( $level < $verbosity ) {
## no critic (InputOutput::RequireCheckedSyscalls)
print $message;
## use critic
}
return;
}
##############################################################################
# Usage : check_arguments( \%arguments, $number_of_expected_arguments )
# Purpose : performs sanity checks on arguments
# Returns : n/a
# Arguments : \%arguments : an arguments hash (highs, lows or ranges)
# $number_of_expected_arguments : number of numberical arguments
# Throws : n/a
# Comments : n/a
# See also : n/a
sub check_arguments {
my ( $checks_ref, $number_of_arguments ) = @_;
for my $sensor ( keys %{$checks_ref} ) {
if ( !defined $sensor_names{$sensor} ) {
$plugin->nagios_exit( $plugin_module->UNKNOWN,
"unknown sensor $sensor" );
}
else {
my @numbers = split /,/mxs, $checks_ref->{$sensor};
if ( @numbers != $number_of_arguments ) {
$plugin->nagios_exit( $plugin_module->UNKNOWN,
"wrong number of arguments ($checks_ref->{$sensor}) for $sensor"
);
}
for my $number (@numbers) {
if ( !( $number =~ /^-?\d+[.]?\d*$/mxs ) ) {
$plugin->nagios_exit( $plugin_module->UNKNOWN,
"$number (in $checks_ref->{$sensor}) is not a number" );
}
}
}
}
return;
}
##############################################################################
# Usage : usage() or usage("error message");
# Purpose : prints the usage of the plugin and exits with unknown status
# Returns : n/a
# Arguments : message : message string
# Throws : n/a
# Comments : n/a
# See also : n/a
sub usage {
my $msg = shift;
if ( defined $msg ) {
## no critic (InputOutput::RequireCheckedSyscalls)
print "$msg\n";
## use critic
}
## no critic (InputOutput::RequireCheckedSyscalls)
print <<'EOT';
check_lm_sensors [--help] [--verbose] [--version] [OPTIONS]
Options:
-?, --help help
-l, --low specifies a check for a sensor value which is too low.
Example:
--low fan1=2000,1000
will give a warning if the value of the fan1 sensor drops
below 2000 RPMs and a critical status if it drops below
1000 RPMs
-h, --high specifies a check for a sensor value which is too high.
Example:
--high temp1=50,60
will give a warning if the value of the temp1 sensor reaches
50 degrees and a critical status if it reaches 60 degrees
-r, --range specifies a check for a sensor value which should stay
in a given range.
Example:
--range v1=1,2,12
will give a warning if the value of the sensor gets outside
the 11-13 range (12+-1) and a critical status if the value is
outside the 10-14 range (12+-2)
--rename renames a sensor in the performance output (useful if you
want to have common names for similar sensors across
different machines)
Example:
--rename cputemp=temp1
--list list all available sensors
--nosensors disable checks on check lm_sensors
--nodrives disable checks on drive temperatures
-d, --drives enable checks on drive temperature
--hddtemp_bin manually specifies the location of the hddtemp binary
--raw use the sensor raw output and do not convert to standard units
-v, --verbose verbose output
--version prints the version and exits
EOT
## use critic
$plugin->nagios_exit( $plugin_module->UNKNOWN, 'Invalid arguments' );
return;
}
##############################################################################
# Usage : get_path('program_name');
# Purpose : retrieves the path of an executable file using the
# 'which' utility
# Returns : the path of the program (if found)
# Arguments : the program name
# Throws : n/a
# Comments : n/a
# See also : n/a
sub get_path {
my $prog = shift;
my $command = "which $prog";
my $output;
my $path;
## no critic (InputOutput::RequireBriefOpen)
my $pid = open $output, q{-|},
"$command 2>&1"
or $plugin->nagios_exit( $plugin_module->UNKNOWN,
"Cannot execute $command: $OS_ERROR" );
while (<$output>) {
chomp;
if ( !/^which:/mxs ) {
$path = $_;
}
}
if ( !( close $output )
&& ( $OS_ERROR != 0 ) )
{
# close to a piped open return false if the command with non-zero
# status. In this case $! is set to 0
$plugin->nagios_exit( $plugin_module->UNKNOWN,
"Error while closing pipe to $command: $OS_ERROR\n" );
}
## use critic
return $path;
}
##############################################################################
# Usage : parse_drives()
# Purpose : parses /proc/partitions to find available drives and tries to
# get their temperature
# Returns : n/a
# Arguments : n/a
# Throws : n/a
# Comments : n/a
# See also : n/a
sub parse_drives {
my $IN;
if ( -x $hddtemp_bin ) {
verbose "Looking for drives in /proc/partitions\n";
my @disks;
## no critic (InputOutput::RequireBriefOpen)
open $IN, '<',
'/proc/partitions'
or $plugin->nagios_exit( $plugin_module->UNKNOWN,
'Cannot open /proc/partitions' );
## use critic
while (<$IN>) {
chomp;
my ( $major, $minor, $blocs, $device_name ) = split;
if ( !defined $major
|| $major eq 'major'
|| $major eq q{}
|| $device_name =~ /[\d]$/mxs )
{
next;
}
push @disks, $device_name;
}
close $IN
or $plugin->nagios_exit( $plugin_module->UNKNOWN,
"Cannot close input: $OS_ERROR\n" );
for my $name (@disks) {
verbose " checking disk /dev/$name\n", 1;
my $command = "$hddtemp_bin -n /dev/$name";
my $output;
my $temp;
my $pid = open $output, q{-|},
"$command 2>&1"
or $plugin->nagios_exit( $plugin_module->UNKNOWN,
"Cannot execute $command: $OS_ERROR" );
while (<$output>) {
chomp;
$temp = $_;
last;
}
close $output
or $plugin->nagios_exit( $plugin_module->UNKNOWN,
"Error while executing $command: $OS_ERROR\n" );
if ( $temp =~ /^[\d]+$/mxs ) {
# check if the sensor has to be renamed
$sensor_values{$name} = $temp;
$sensor_names{$name} = $name;
while ( my ( $original_name, $chosen_name ) = each %renames ) {
if ( $original_name eq $name ) {
$sensor_names{$chosen_name} = $name;
}
}
verbose "found temperature for drive $name ($name = $_)\n";
}
else {
verbose "warning: temperature for /dev/$name not available\n";
}
}
}
else {
verbose
"warning: $hddtemp_bin not found: HDD temperatures not checked\n";
}
return;
}
##############################################################################
# Usage : parse_sensors()
# Purpose : retrieves the values of the available sensors
# Returns : n/a
# Arguments : n/a
# Throws : n/a
# Comments : n/a
# See also : n/a
sub parse_sensors {
my $dir_handle;
Readonly my $DEV_DIR => '/sys/class/hwmon';
opendir $dir_handle, $DEV_DIR
or return;
my @devices = grep { m/^[^.]/mxs } readdir $dir_handle;
closedir $dir_handle
or $plugin->nagios_exit( $plugin_module->UNKNOWN,
"Error closing $DEV_DIR: $OS_ERROR" );
for my $device (@devices) {
my $device_name;
# get device name
my $name_handler;
## no critic (InputOutput::RequireBriefOpen)
open $name_handler, q{<}, "$DEV_DIR/$device/name"
or open $name_handler, q{<},
"$DEV_DIR/$device/device/name"
or $plugin->nagios_exit( $plugin_module->UNKNOWN,
"Error reading $DEV_DIR/$device/[device/]name: $OS_ERROR" );
while (<$name_handler>) {
chomp;
$device_name = $_;
last;
}
close $name_handler
or $plugin->nagios_exit( $plugin_module->UNKNOWN,
"Error closing $DEV_DIR/$device/device/name: $OS_ERROR" );
## use critic
# list sensors
opendir $dir_handle, "$DEV_DIR/$device/device/"
or return;
my @sensors = apply { s/_input//mxs; }
grep { m/^[^.].*_input/mxs } readdir $dir_handle;
closedir $dir_handle
or $plugin->nagios_exit( $plugin_module->UNKNOWN,
"Error closing $DEV_DIR/$device/device: $OS_ERROR" );
for my $sensor (@sensors) {
# get device name
my $sensor_handler;
my $value;
## no critic (InputOutput::RequireBriefOpen)
open $sensor_handler, q{<},
"$DEV_DIR/$device/device/$sensor"
. '_input'
or $plugin->nagios_exit(
$plugin_module->UNKNOWN,
"Error reading $DEV_DIR/$device/device/$sensor"
. "_input: $OS_ERROR"
);
while (<$sensor_handler>) {
chomp;
$value = $_;
last;
}
close $sensor_handler
or $plugin->nagios_exit(
$plugin_module->UNKNOWN,
"Error closing $DEV_DIR/$device/device/$sensor"
. "_input: $OS_ERROR"
);
## use critic
my $full_name = $device_name . q{_} . $sensor;
# check if the sensor has to be renamed
$sensor_values{$full_name} = $value;
$sensor_names{$full_name} = $full_name;
while ( my ( $original_name, $chosen_name ) = each %renames ) {
if ( $original_name eq $full_name ) {
$sensor_names{$chosen_name} = $full_name;
}
}
if ($verbosity) {
## no critic (InputOutput::RequireCheckedSyscalls)
print "found sensor $full_name ($value)\n";
## use critic
}
}
}
return;
}
##############################################################################
# Usage : convert_values()
# Purpose : converts raw values to standard units
# Returns : n/a
# Arguments : n/a
# Throws : n/a
# Comments : n/a
# See also : n/a
sub convert_values {
for my $sensor ( keys %sensor_values ) {
if ( $sensor =~ /\w+_(in|cpu|temp|curr)[\d]+/mxs ) {
# convert from millidegrees, milliamperes or millivolts
## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
$sensor_values{$sensor} = $sensor_values{$sensor} / 1_000;
## use critic
}
elsif ( $sensor =~ /\w+_(power|energy)[\d]+/mxs ) {
# convert from microJoule or microWarr
## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
$sensor_values{$sensor} = $sensor_values{$sensor} / 1_000_000;
## use critic
}
elsif ( $sensor =~ /\w+_pwm[\d]+/mxs ) {
# value (0-255) is a percentage
## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
$sensor_values{$sensor} = $sensor_values{$sensor} / 2.55;
## use critic
}
}
return;
}
##############################################################################
# main
#
########################
# Command line arguments
Getopt::Long::Configure( 'bundling', 'no_ignore_case' );
$result = GetOptions(
'drives!' => \$drives,
'hddtemp_bin=s' => \$hddtemp_bin,
'help|?' => sub { usage() },
'high|h=s' => \%highs,
'list' => \$list,
'low|l=s' => \%lows,
'range|r=s' => \%ranges,
'rename=s' => \%renames,
'raw' => \$raw,
'sensors!' => \$sensors,
'verbose|v+' => \$verbosity,
'version' => sub {
## no critic (InputOutput::RequireCheckedSyscalls)
print "check_lm_sensors version $VERSION\n";
## use critic
exit $plugin_module->UNKNOWN;
}
);
if ( !$result ) {
usage();
}
if ( !( defined $list )
&& !(%highs)
&& !(%ranges)
&& !(%lows) )
{
$plugin->nagios_exit( $plugin_module->UNKNOWN,
'at least one check has to be specified' );
}
if ($drives) {
if ( !$hddtemp_bin ) {
$hddtemp_bin = get_path('hddtemp');
}
if ( !$hddtemp_bin ) {
verbose "warning: hddtemp not found: HDD temperatures not checked\n";
}
else {
verbose "hddtemp found at $hddtemp_bin\n";
parse_drives();
}
}
parse_sensors();
if ( !$raw ) {
convert_values();
}
if ($list) {
for my $sensor ( sort keys %sensor_values ) {
## no critic (InputOutput::RequireCheckedSyscalls)
print "$sensor -> $sensor_values{$sensor}\n";
## use critic
}
}
###############
# sanity checks
## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
check_arguments( \%highs, 2 );
check_arguments( \%lows, 2 );
check_arguments( \%ranges, 3 );
## use critic
################
# perform checks
my $criticals;
my $warnings;
my @status;
my @desc;
# highs
while ( ( $name, $limits ) = each %highs ) {
my ( $warn, $crit ) = split /,/mxs, $limits;
my $value = $sensor_values{ $sensor_names{$name} };
push @status, "$name=$value;$warn;$crit;;";
push @desc, "$name=$value";
$criticals = $criticals || ( $value > $crit );
$warnings = $warnings || ( $value > $warn );
}
# lows
while ( ( $name, $limits ) = each %lows ) {
my ( $warn, $crit ) = split /,/mxs, $limits;
my $value = $sensor_values{ $sensor_names{$name} };
push @status, "$name=$value;$warn;$crit;;";
push @desc, "$name=$value";
$criticals = $criticals || ( $value < $crit );
$warnings = $warnings || ( $value < $warn );
}
# ranges
while ( ( $name, $limits ) = each %ranges ) {
my ( $warn, $crit, $ref ) = split /,/mxs, $limits;
my $value = $sensor_values{ $sensor_names{$name} };
my $diff = abs( $value - $ref ); ## no critic (ProhibitParensWithBuiltins)
push @status, "$name=$value;$warn;$crit;;";
push @desc, "$name=$value";
$criticals = $criticals || ( $diff > $crit );
$warnings = $warnings || ( $diff > $warn );
}
#########################
# build the status string
my $output = ( join q{ }, @desc ) . q{|} . ( join q{ }, @status );
if ($criticals) {
$plugin->nagios_exit( $plugin_module->CRITICAL, $output );
}
if ($warnings) {
$plugin->nagios_exit( $plugin_module->WARNING, $output );
}
$plugin->nagios_exit( $plugin_module->OK, $output );
1;
|