check_hddtemp.pl
changeset 0 2fde8823787b
child 1 5f67fdc48747
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/check_hddtemp.pl	Thu Aug 02 10:48:39 2012 +0200
@@ -0,0 +1,224 @@
+#! /usr/bin/perl -w
+
+#    Copyright (C) 2012  Christian Arnold
+#
+#    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 3 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, see <http://www.gnu.org/licenses/>.
+#
+#    Christian Arnold <arnold@schlittermann.de>
+
+use 5.010;
+use strict;
+use File::Basename;
+use Getopt::Long;
+use Pod::Usage;
+
+delete @ENV{ grep /^LC_/ => keys %ENV };
+$ENV{LANG}   = "C";
+$ENV{LC_ALL} = "C";
+
+sub version($$);
+sub hddtemp($$$$);
+
+my %ERRORS = (
+    OK        => 0,
+    WARNING   => 1,
+    CRITICAL  => 2,
+    UNKNOWN   => 3,
+    DEPENDENT => 4
+);
+
+my $ME      = basename $0;
+my $NAME    = "HDDTEMP";
+my $VERSION = "1.5";
+
+my ( @devices, @output );
+
+my %opt = (
+    "warning"  => 55,
+    "critical" => 65,
+    "devices"  => "/dev/sda,/dev/sdb",
+    "binary"   => "/usr/sbin/hddtemp"
+);
+
+MAIN: {
+    Getopt::Long::Configure('bundling');
+    GetOptions(
+        "b|binary=s"   => \$opt{binary},
+        "d|devices=s"  => \$opt{devices},
+        "w|warning=i"  => \$opt{warning},
+        "c|critical=i" => \$opt{critical},
+        "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
+        "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
+        "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
+    ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
+
+    hddtemp( $opt{binary}, $opt{devices}, $opt{warning}, $opt{critical} );
+}
+
+sub hddtemp($$$$) {
+    my ( $binary, $devices, $warning, $critical ) = @_;
+    my $rc;
+    my %temp;
+
+    if ( !-x $binary ) {
+        say "$NAME CRITICAL: $binary - not found or not executable";
+        exit $ERRORS{CRITICAL};
+    }
+
+    my @devices = split( /,/, $devices );
+
+    foreach my $dev (@devices) {
+        if ( !-r $dev ) {
+            say
+"$NAME CRITICAL: $dev - not exists or not read permission is granted";
+            exit $ERRORS{CRITICAL};
+        }
+        my @line = grep { /^$dev/ } `$binary $dev`;
+
+        if ( $line[0] ) {
+            $line[0] =~ /^.*:\s.*:\s+(\d+)/;
+            if ( !$1 ) {
+                print "$NAME CRITICAL: $line[0]";
+                exit $ERRORS{CRITICAL};
+            }
+
+            if ( $1 < $warning ) { $rc = $ERRORS{OK}; }
+            elsif ( ( $1 >= $warning ) and ( $1 < $critical ) ) {
+                $rc = $ERRORS{WARNING};
+            }
+            else { $rc = $ERRORS{CRITICAL}; }
+            ${temp}{$dev}{"rc"}{$rc} = $1;
+        }
+    }
+
+    my ( $ok, $warn, $crit ) = (0) x 4;
+    my @output = ();
+    my @perf   = ();
+    foreach my $dev ( sort keys %temp ) {
+        if ( $temp{$dev}{'rc'}{'0'} ) {
+            $ok = 1;
+            push @output, "$dev $temp{$dev}{'rc'}{'0'} C";
+            push @perf,   "$dev=$temp{$dev}{'rc'}{'0'};$warning;$critical";
+        }
+        if ( $temp{$dev}{'rc'}{'1'} ) {
+            $warn = 1;
+            push @output, "$dev $temp{$dev}{'rc'}{'1'} C";
+            push @perf,   "$dev=$temp{$dev}{'rc'}{'1'};$warning;$critical";
+        }
+        if ( $temp{$dev}{'rc'}{'2'} ) {
+            $crit = 1;
+            push @output, "$dev $temp{$dev}{'rc'}{'2'} C";
+            push @perf,   "$dev=$temp{$dev}{'rc'}{'2'};$warning;$critical";
+        }
+    }
+
+    if ($crit) {
+        say "$NAME CRITICAL: "
+          . join( ' ', @output ) . " | "
+          . join( ' ', @perf );
+        exit $ERRORS{CRITICAL};
+    }
+    elsif ($warn) {
+        say "$NAME WARNING: "
+          . join( ' ', @output ) . " | "
+          . join( ' ', @perf );
+        exit $ERRORS{WARNING};
+    }
+
+    say "$NAME OK: " . join( ' ', @output ) . " | " . join( ' ', @perf );
+    exit $ERRORS{OK};
+}
+
+sub version($$) {
+    my ( $progname, $version ) = @_;
+
+    say <<_VERSION;
+$progname version $version
+Copyright (C) 2012 by Christian Arnold and Schlittermann internet & unix support.
+
+$ME comes with ABSOLUTELY NO WARRANTY. This is free software,
+and you are welcome to redistribute it under certain conditions.
+See the GNU General Public Licence for details.
+_VERSION
+}
+
+__END__
+
+=head1 NAME
+
+check_hddtemp - nagios plugin to check hard drive temperature
+
+=head1 SYNOPSIS
+
+check_hddtemp [B<-b>|B<--binary>]
+
+check_hddtemp [B<-w>|B<--warning>]
+
+check_hddtemp [B<-c>|B<--critical>]
+
+check_hddtemp [B<-d>|B<--devices>]
+
+=head1 OPTIONS
+
+=over
+
+=item B<-b>|B<--binary>
+
+Path to hddtemp binary (default: /usr/sbin/hddtemp)
+
+=item B<-w>|B<--warning>
+
+Temperature of your hard drive to generate warning alert (default: 55)
+
+=item B<-c>|B<--critical>
+
+Temperature of your hard drive to generate critical alert (default: 65)
+
+=item B<-d>|B<--devices>
+
+Device drive path (default: /dev/sda,/dev/sdb)
+
+=item B<-h>|B<--help>
+
+Print detailed help screen.
+
+=item B<-m>|B<--man>
+
+Print manual page.
+
+=item B<-V>|B<--version>
+
+Print version information.
+
+=back
+
+=head1 DESCRIPTION
+
+This plugin checks the hard drive temperature.
+
+=head1 VERSION
+
+This man page is current for version 1.5 of B<check_hddtemp>.
+
+=head1 AUTHOR
+
+Written by Christian Arnold L<arnold@schlittermann.de>
+
+=head1 COPYRIGHT
+
+Copyright (C) 2012 by Christian Arnold and Schlittermann internet & unix support.
+This is free software, and you are welcome to redistribute it under certain conditions.
+See the GNU General Public Licence for details.
+
+=cut