/usr/lib/nagios/plugins/check_lm_sensors is in nagios-plugins-contrib 14.20141104.
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 | #!/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) 2007, ETH Zurich.
#
# 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.
#
# RCS information (required by Perl::Critic)
# $Id: check_lm_sensors 1030 2008-10-17 15:45:06Z corti $
# $Revision: 1030 $
# $HeadURL: https://svn.id.ethz.ch/nagios_plugins/check_lm_sensors/check_lm_sensors $
# $Date: 2008-10-17 17:45:06 +0200 (Fri, 17 Oct 2008) $
use strict;
use warnings;
use Getopt::Long;
use Carp;
use English qw(-no_match_vars);
use Nagios::Plugin;
use version; our $VERSION = '3.1.1';
# IMPORTANT: Nagios plugins could be executed using embedded perl in this case
# the main routine would be executed as a subroutine and all the
# declared subroutines would therefore be inner subroutines
# This will cause all the global lexical variables not to stay
# shared in the subroutines!
#
# All variables are therefore declared as package variables...
#
use vars qw(
$verbosity
$prog_name
$plugin
$hddtemp_bin
$sensors_bin
$drives
$help
$list
$result
$sanitize
$sensors
%rename
%sensor_values
@drives
%highs
%lows
%ranges
$space
$status
$desc
$criticals
$warnings
$unknowns
$name
$limits
$converted_name
%checks
);
# initialization
$criticals = q{};
$desc = q{};
$drives = 1;
$help = q{};
$prog_name = 'LM_SENSORS';
$plugin = Nagios::Plugin->new( shortname => $prog_name );
$sensors = 1;
$space = q{};
$status = q{};
$unknowns = q{};
$verbosity = 0;
$warnings = q{};
##############################################################################
# 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( UNKNOWN,
q{Internal error: not enough parameters for 'verbose'} );
}
if ( !defined $level ) {
$level = 0;
}
if ( $level < $verbosity ) {
print $message;
}
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 ) {
print "$msg\n";
}
print << 'EOT';
check_lm_sensors [--help] [--verbose] [--version] [OPTIONS]
Options:
-?, --help help
--check, -c specifies a check for a sensor
-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
--sanitize sanitize sensor names (e.g., removing spaces)
--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
--sensors_bin manually specified the location of the sensors binary
-v, --verbose verbose output
--version prints the version and exits
Deprecated options:
-c, --check specifies a check on a sensor. This options accepts two or
three numerical values:
* with two arguments works similarly to the --high option
* with three arguments works similarly to the --range option
Sensors with a space in the name can be specified
* by escaping the space: --high sda\ Temp=50,60
* by quoting the name: --high 'sda Temp'=50,60
* by substituting the space with an underscore: --high sda_Temp=50,60
* by using the --sanitize option to remove the space
--sanitize --high sdaTemp=50,60
EOT
$plugin->nagios_exit( 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;
my $pid = open $output, q{-|}, "$command 2>&1"
or $plugin->nagios_exit( UNKNOWN, "Cannot execute $command: $OS_ERROR" );
while (<$output>) {
chomp;
if ( !/^which:/mx ) {
$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( UNKNOWN,
"Error while closing pipe to $command: $OS_ERROR\n" );
}
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";
open $IN, '<', '/proc/partitions'
or $plugin->nagios_exit( UNKNOWN, 'Cannot open /proc/partitions' );
while (<$IN>) {
chomp;
my ( $major, $minor, $blocs, $name ) = split;
if ( !defined $major || $major eq 'major' || $major eq q{} ) {
next;
}
if ( $name =~ /[0-9]$/mx ) {
next;
}
verbose " checking disk /dev/$name\n", 1;
my $command = "$hddtemp_bin -n /dev/$name";
my $output;
my $pid = open $output, q{-|}, "$command 2>&1"
or $plugin->nagios_exit( UNKNOWN,
"Cannot execute $command: $OS_ERROR" );
while (<$output>) {
chomp;
if ( $_ =~ /^[0-9]+$/mx ) {
if ($sanitize) {
$name = $name . 'Temp';
}
else {
$name = "$name Temp";
}
# check if the sensor has to be renamed
if ( $rename{$name} ) {
$name = $rename{$name};
}
$sensor_values{$name} = $_;
if ( $verbosity || $list ) {
print
"found temperature for drive $name ($name = $_)\n";
}
}
else {
verbose
"warning: temperature for /dev/$name not available\n";
}
}
close $output
or $plugin->nagios_exit( UNKNOWN,
"Error while executing $command: $OS_ERROR\n" );
}
close $IN
or $plugin->nagios_exit( UNKNOWN, "Cannot close input: $OS_ERROR\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 {
if ( -x $sensors_bin ) {
# check if there are configured sensors
my $command = "$sensors_bin";
my $output;
my $pid = open $output, q{-|}, "$command 2>&1"
or
$plugin->nagios_exit( UNKNOWN, "Cannot execute $command: $OS_ERROR" );
select(undef, undef, undef, .2);
while (<$output>) {
chomp;
if ( /^No\ sensors\ found/mx
|| /^Can\'t/mx )
{
verbose "warning: no sensors found\n";
return;
}
last;
}
close $output
or $plugin->nagios_exit( UNKNOWN,
"Error while executing $command: $OS_ERROR\n" );
$command = "$sensors_bin -A | grep \:";
$pid = open $output, q{-|}, "$command 2>&1"
or
$plugin->nagios_exit( UNKNOWN, "Cannot execute $command: $OS_ERROR" );
while (<$output>) {
chomp;
if (/(.*):\s+\+?([0-9\-\.]*)\ ?([^\ ]*)\ /mx) {
my $name = $1;
my $value = $2;
if ($sanitize) {
$name =~ s/\ //gmx;
}
if ( $rename{$name} ) {
$name = $rename{$name};
}
$sensor_values{$name} = $value;
if ( $verbosity || $list ) {
print "found sensor $name ($value)\n";
}
}
}
close $output
or $plugin->nagios_exit( UNKNOWN,
"Error while executing $command: $OS_ERROR\n" );
}
else {
verbose
"warning: $sensors_bin not found: HDD temperatures not checked\n";
}
return;
}
##############################################################################
# main
#
########################
# Command line arguments
Getopt::Long::Configure( 'bundling', 'no_ignore_case' );
$result = GetOptions(
'check|c=s' => \%checks,
'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' => \%rename,
'sanitize' => \$sanitize,
'sensors!' => \$sensors,
'sensors_bin=s' => \$sensors_bin,
'verbose|v+' => \$verbosity,
'version' => sub { print "check_lm_sensors version $VERSION\n"; exit 3; }
);
if ( !$result ) {
usage();
}
if ( !( defined $list )
&& !(%checks)
&& !(%highs)
&& !(%ranges)
&& !(%lows) )
{
$plugin->nagios_exit( UNKNOWN, 'at least one check has to be specified' );
}
# convert old options
if (%checks) {
verbose
"warning: the --checks options is deprecated, use the --low, --high and --range options instead\n";
}
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();
}
}
if ($sensors) {
if ( !$sensors_bin ) {
$sensors_bin = get_path('sensors');
}
if ( !$sensors_bin ) {
verbose "warning: sensors not found: lm_sensors not checked\n";
}
else {
verbose "sensors found at $sensors_bin\n";
parse_sensors();
}
}
################
# perform checks
# old style checks
while ( ( $name, $limits ) = each %checks ) {
$converted_name = $name;
$converted_name =~ s/_/\ /gmx;
if ( !$sensor_values{$name} && $sensor_values{$converted_name} ) {
$name = $converted_name;
}
if ( !$sensor_values{$name} ) {
$unknowns = $unknowns . " $name";
next;
}
my ( $warn, $crit, $ref ) = split /,/mx, $limits;
my $value = $sensor_values{$name};
my $diff = $value;
if ($ref) {
$diff = abs( $value - $ref );
}
if ( $diff > $crit ) {
$criticals = $criticals . " $name=" . $sensor_values{$name};
}
elsif ( $diff > $warn ) {
$warnings = $warnings . " $name=" . $sensor_values{$name};
}
if ($space) {
$status = $status . q{ };
$desc = $desc . q{ };
}
$status = $status . "$name=$value;$warn;$crit;;";
$desc = $desc . "$name=$value";
$space = 1;
}
# lows
while ( ( $name, $limits ) = each %lows ) {
$converted_name = $name;
$converted_name =~ s/_/\ /gmx;
if ( !$sensor_values{$name} && $sensor_values{$converted_name} ) {
$name = $converted_name;
}
if ( !$sensor_values{$name} ) {
$unknowns = $unknowns . " $name";
next;
}
my ( $warn, $crit ) = split /,/mx, $limits;
my $value = $sensor_values{$name};
if ( $value < $crit ) {
$criticals = $criticals . " $name=" . $sensor_values{$name};
}
elsif ( $value < $warn ) {
$warnings = $warnings . " $name=" . $sensor_values{$name};
}
if ($space) {
$status = $status . q{ };
}
$status = $status . "$name=$value;$warn;$crit;;";
$desc = $desc . "$name=$value";
$space = 1;
}
# highs
while ( ( $name, $limits ) = each %highs ) {
$converted_name = $name;
$converted_name =~ s/_/\ /gmx;
if ( !$sensor_values{$name} && $sensor_values{$converted_name} ) {
$name = $converted_name;
}
if ( !$sensor_values{$name} ) {
$unknowns = $unknowns . " $name";
next;
}
my ( $warn, $crit ) = split /,/mx, $limits;
my $value = $sensor_values{$name};
if ( $value > $crit ) {
$criticals = $criticals . " $name=" . $sensor_values{$name};
}
elsif ( $value > $warn ) {
$warnings = $warnings . " $name=" . $sensor_values{$name};
}
if ($space) {
$status = $status . q{ };
$desc = $desc . q{ };
}
$status = $status . "$name=$value;$warn;$crit;;";
$desc = $desc . "$name=$value";
$space = 1;
}
# ranges
while ( ( $name, $limits ) = each %ranges ) {
$converted_name = $name;
$converted_name =~ s/_/\ /gmx;
if ( !$sensor_values{$name} && $sensor_values{$converted_name} ) {
$name = $converted_name;
}
if ( !$sensor_values{$name} ) {
$unknowns = $unknowns . " $name";
next;
}
my ( $warn, $crit, $ref ) = split /,/mx, $limits;
my $value = $sensor_values{$name};
my $diff = $value;
$diff = abs( $value - $ref );
if ( $diff > $crit ) {
$criticals = $criticals . " $name=" . $sensor_values{$name};
}
elsif ( $diff > $warn ) {
$warnings = $warnings . " $name=" . $sensor_values{$name};
}
if ($space) {
$status = $status . q{ };
$desc = $desc . q{ };
}
$status = $status . "$name=$value;$warn;$crit;;";
$desc = $desc . "$name=$value";
$space = 1;
}
#########################
# build the status string
if ( $criticals ne q{} ) {
$plugin->nagios_exit( CRITICAL, "$desc|$status" );
}
if ( $warnings ne q{} ) {
$plugin->nagios_exit( WARNING, "$desc|$status" );
}
if ( $unknowns ne q{} ) {
$plugin->nagios_exit( UNKNOWN, "$desc|$status" );
}
$plugin->nagios_exit( OK, "$desc|$status" );
1;
|