Commit 8f92a2ff authored by nuts's avatar nuts
Browse files

Add new plugin to monitor the chipset temperatur sensors of the local computer



git-svn-id: https://subversion.fem.tu-ilmenau.de/repository/nagios@38 0c029375-f9a5-47ba-aa0c-8883c34e326a
parent 2e6e04de
#!/usr/bin/perl
################################################################################
# #
# Copyright (C) 2011 Chad Columbus <ccolumbu@hotmail.com> #
# Special thanks to Jack-Benny Persson <jake@cyberinfo.se> #
# Copyright (C) 2016 Dustin Funk <nuts@fem-tu-ilmenau.de> #
# #
# #
# Last Modified: 2016-03-05 (Dustin Funk) #
# #
# #
# CHANGELOG: #
# v1.3 - Add performance data output (2016-03-05) #
# #
# #
# LICENSE: GPL-2 #
# #
# This program is free software; you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation; either version 2 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the Free Software #
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #
# #
################################################################################
###############################################################################
# #
# Nagios plugin to monitor CPU and M/B temperature with sensors. #
# Written in Perl (and uses Getopt::Std module). #
# Special thanks to Jack-Benny Persson as this whole program is based on his #
# Bash version of this script. #
# #
###############################################################################
use strict;
use Getopt::Std;
my $VERSION = "Version 1.3";
my %opts;
getopts('hw:c:s:', \%opts);
# Sensor program
my $SENSORPROG = "/usr/bin/sensors";
# Exit codes
my $STATE_OK = 0;
my $STATE_WARNING = 1;
my $STATE_CRITICAL = 2;
my $STATE_UNKNOWN = 3;
my $exit_code = '4';
# Default values:
my $sensor_regex = 'temp1';
my $error = 0;
my $default_thresh_warn = 80;
my $default_thresh_crit = 100;
my $thresh_warn = '';
my $thresh_crit = '';
my $matched_pattern = '';
my $message = '';
my %temp_hash;
my $temp_list = '';
my $temp_perfdata = '';
# See if we have sensors program installed and can execute it
if (! -x "$SENSORPROG") {
print "\nIt appears you don't have sensors installed in $SENSORPROG\n";
exit($STATE_UNKNOWN);
}
# Parse command line options
if ($opts{'h'}) {
&print_help();
exit($STATE_OK);
}
if (! defined $opts{'w'}) {
# Warning not provided
$thresh_warn = $default_thresh_warn;
} elsif ($opts{'w'} =~ /^\d+$/) {
# Warning is an integer
$thresh_warn = $opts{'w'};
} else {
# Warning is not an integer
print "Warning must be an integer\n";
}
if (! defined $opts{'c'}) {
# Critical not provided
$thresh_crit = $default_thresh_crit;
} elsif ($opts{'c'} =~ /^\d+$/) {
# Critical is an integer
if ($opts{'c'} <= $opts{'w'}) {
print "Critical -c must be greater than Warning -w\n";
exit($STATE_UNKNOWN);
} else {
$thresh_crit = $opts{'c'};
}
} else {
# Critical is not an integer
print "Critical must be an integer\n";
}
if (defined $opts{'s'}) {
if ($opts{'s'} ne '') {
# Sensor argument provided:
$sensor_regex = $opts{'s'};
}
}
my $sensors_output = `export LANG=C;$SENSORPROG`;
my $pattern = "($sensor_regex)" . '\:[\s\n\r]{1,}[\+\-]{0,1}(\d{1,3}(?:\.\d{1,2}){0,1}).[CF]';
my $matched_sensor;
my %sensor_name;
my $hold_temp;
my $loop = 0;
foreach $matched_pattern ($sensors_output =~ m/$pattern/imsg) {
if ($matched_pattern =~ m/$sensor_regex/) {
$matched_sensor = $matched_pattern;
} else {
$hold_temp = $matched_pattern;
# Check that the regex returned a number:
if ($hold_temp =~ /^\d{1,3}(?:\.\d{1,2}){0,1}$/) {
# Sucess, we got what looks like a temp!
$temp_hash{$loop} = $hold_temp;
($sensor_name{$loop} = $matched_sensor) =~ s/ //g;
if ($exit_code == 0 || $exit_code == 4) {
if ($hold_temp > $thresh_crit) {
$exit_code = $STATE_CRITICAL;
$message = "CRITICAL - ";
} elsif ($hold_temp > $thresh_warn) {
if ($exit_code == 0 || $exit_code == 3 || $exit_code == 4) {
$exit_code = $STATE_WARNING;
$message = "WARNING - ";
}
} else {
$message = "TEMP OK - ";
$exit_code = $STATE_OK;
}
}
}
$matched_sensor = '';
$hold_temp = '';
}
$loop++;
}
if ($message eq '') {
print "Error: I did not get a temp from the sensor data matching REGEX = \/$sensor_regex\:\/. Forget -s ?\n";
exit($STATE_UNKNOWN);
}
foreach my $key (sort keys %temp_hash) {
$temp_list .= "$sensor_name{$key} $temp_hash{$key}; ";
$temp_perfdata .= " $sensor_name{$key}=$temp_hash{$key};$thresh_warn;$thresh_crit;";
}
#$temp_list =~ s/, $//;
#$temp_perfdata =~ s/, $//;
print "$message $temp_list |$temp_perfdata\n";
exit($exit_code);
####################################
# Start Subs:
####################################
sub print_help() {
print << "EOF";
Monitor temperature with the use of $SENSORPROG
$VERSION
Options:
-h
Print detailed help screen
-s 'REGEX'
Set what to monitor. Default is 'temp1'.
-w INTEGER
If not set default is $default_thresh_warn
-c INTEGER
if not set default is $default_thresh_crit
EOF
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment