check_hddtemp.pl
changeset 0 2fde8823787b
child 1 5f67fdc48747
equal deleted inserted replaced
-1:000000000000 0:2fde8823787b
       
     1 #! /usr/bin/perl -w
       
     2 
       
     3 #    Copyright (C) 2012  Christian Arnold
       
     4 #
       
     5 #    This program is free software: you can redistribute it and/or modify
       
     6 #    it under the terms of the GNU General Public License as published by
       
     7 #    the Free Software Foundation, either version 3 of the License, or
       
     8 #    (at your option) any later version.
       
     9 #
       
    10 #    This program is distributed in the hope that it will be useful,
       
    11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13 #    GNU General Public License for more details.
       
    14 #
       
    15 #    You should have received a copy of the GNU General Public License
       
    16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    17 #
       
    18 #    Christian Arnold <arnold@schlittermann.de>
       
    19 
       
    20 use 5.010;
       
    21 use strict;
       
    22 use File::Basename;
       
    23 use Getopt::Long;
       
    24 use Pod::Usage;
       
    25 
       
    26 delete @ENV{ grep /^LC_/ => keys %ENV };
       
    27 $ENV{LANG}   = "C";
       
    28 $ENV{LC_ALL} = "C";
       
    29 
       
    30 sub version($$);
       
    31 sub hddtemp($$$$);
       
    32 
       
    33 my %ERRORS = (
       
    34     OK        => 0,
       
    35     WARNING   => 1,
       
    36     CRITICAL  => 2,
       
    37     UNKNOWN   => 3,
       
    38     DEPENDENT => 4
       
    39 );
       
    40 
       
    41 my $ME      = basename $0;
       
    42 my $NAME    = "HDDTEMP";
       
    43 my $VERSION = "1.5";
       
    44 
       
    45 my ( @devices, @output );
       
    46 
       
    47 my %opt = (
       
    48     "warning"  => 55,
       
    49     "critical" => 65,
       
    50     "devices"  => "/dev/sda,/dev/sdb",
       
    51     "binary"   => "/usr/sbin/hddtemp"
       
    52 );
       
    53 
       
    54 MAIN: {
       
    55     Getopt::Long::Configure('bundling');
       
    56     GetOptions(
       
    57         "b|binary=s"   => \$opt{binary},
       
    58         "d|devices=s"  => \$opt{devices},
       
    59         "w|warning=i"  => \$opt{warning},
       
    60         "c|critical=i" => \$opt{critical},
       
    61         "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
       
    62         "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
       
    63         "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
       
    64     ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
       
    65 
       
    66     hddtemp( $opt{binary}, $opt{devices}, $opt{warning}, $opt{critical} );
       
    67 }
       
    68 
       
    69 sub hddtemp($$$$) {
       
    70     my ( $binary, $devices, $warning, $critical ) = @_;
       
    71     my $rc;
       
    72     my %temp;
       
    73 
       
    74     if ( !-x $binary ) {
       
    75         say "$NAME CRITICAL: $binary - not found or not executable";
       
    76         exit $ERRORS{CRITICAL};
       
    77     }
       
    78 
       
    79     my @devices = split( /,/, $devices );
       
    80 
       
    81     foreach my $dev (@devices) {
       
    82         if ( !-r $dev ) {
       
    83             say
       
    84 "$NAME CRITICAL: $dev - not exists or not read permission is granted";
       
    85             exit $ERRORS{CRITICAL};
       
    86         }
       
    87         my @line = grep { /^$dev/ } `$binary $dev`;
       
    88 
       
    89         if ( $line[0] ) {
       
    90             $line[0] =~ /^.*:\s.*:\s+(\d+)/;
       
    91             if ( !$1 ) {
       
    92                 print "$NAME CRITICAL: $line[0]";
       
    93                 exit $ERRORS{CRITICAL};
       
    94             }
       
    95 
       
    96             if ( $1 < $warning ) { $rc = $ERRORS{OK}; }
       
    97             elsif ( ( $1 >= $warning ) and ( $1 < $critical ) ) {
       
    98                 $rc = $ERRORS{WARNING};
       
    99             }
       
   100             else { $rc = $ERRORS{CRITICAL}; }
       
   101             ${temp}{$dev}{"rc"}{$rc} = $1;
       
   102         }
       
   103     }
       
   104 
       
   105     my ( $ok, $warn, $crit ) = (0) x 4;
       
   106     my @output = ();
       
   107     my @perf   = ();
       
   108     foreach my $dev ( sort keys %temp ) {
       
   109         if ( $temp{$dev}{'rc'}{'0'} ) {
       
   110             $ok = 1;
       
   111             push @output, "$dev $temp{$dev}{'rc'}{'0'} C";
       
   112             push @perf,   "$dev=$temp{$dev}{'rc'}{'0'};$warning;$critical";
       
   113         }
       
   114         if ( $temp{$dev}{'rc'}{'1'} ) {
       
   115             $warn = 1;
       
   116             push @output, "$dev $temp{$dev}{'rc'}{'1'} C";
       
   117             push @perf,   "$dev=$temp{$dev}{'rc'}{'1'};$warning;$critical";
       
   118         }
       
   119         if ( $temp{$dev}{'rc'}{'2'} ) {
       
   120             $crit = 1;
       
   121             push @output, "$dev $temp{$dev}{'rc'}{'2'} C";
       
   122             push @perf,   "$dev=$temp{$dev}{'rc'}{'2'};$warning;$critical";
       
   123         }
       
   124     }
       
   125 
       
   126     if ($crit) {
       
   127         say "$NAME CRITICAL: "
       
   128           . join( ' ', @output ) . " | "
       
   129           . join( ' ', @perf );
       
   130         exit $ERRORS{CRITICAL};
       
   131     }
       
   132     elsif ($warn) {
       
   133         say "$NAME WARNING: "
       
   134           . join( ' ', @output ) . " | "
       
   135           . join( ' ', @perf );
       
   136         exit $ERRORS{WARNING};
       
   137     }
       
   138 
       
   139     say "$NAME OK: " . join( ' ', @output ) . " | " . join( ' ', @perf );
       
   140     exit $ERRORS{OK};
       
   141 }
       
   142 
       
   143 sub version($$) {
       
   144     my ( $progname, $version ) = @_;
       
   145 
       
   146     say <<_VERSION;
       
   147 $progname version $version
       
   148 Copyright (C) 2012 by Christian Arnold and Schlittermann internet & unix support.
       
   149 
       
   150 $ME comes with ABSOLUTELY NO WARRANTY. This is free software,
       
   151 and you are welcome to redistribute it under certain conditions.
       
   152 See the GNU General Public Licence for details.
       
   153 _VERSION
       
   154 }
       
   155 
       
   156 __END__
       
   157 
       
   158 =head1 NAME
       
   159 
       
   160 check_hddtemp - nagios plugin to check hard drive temperature
       
   161 
       
   162 =head1 SYNOPSIS
       
   163 
       
   164 check_hddtemp [B<-b>|B<--binary>]
       
   165 
       
   166 check_hddtemp [B<-w>|B<--warning>]
       
   167 
       
   168 check_hddtemp [B<-c>|B<--critical>]
       
   169 
       
   170 check_hddtemp [B<-d>|B<--devices>]
       
   171 
       
   172 =head1 OPTIONS
       
   173 
       
   174 =over
       
   175 
       
   176 =item B<-b>|B<--binary>
       
   177 
       
   178 Path to hddtemp binary (default: /usr/sbin/hddtemp)
       
   179 
       
   180 =item B<-w>|B<--warning>
       
   181 
       
   182 Temperature of your hard drive to generate warning alert (default: 55)
       
   183 
       
   184 =item B<-c>|B<--critical>
       
   185 
       
   186 Temperature of your hard drive to generate critical alert (default: 65)
       
   187 
       
   188 =item B<-d>|B<--devices>
       
   189 
       
   190 Device drive path (default: /dev/sda,/dev/sdb)
       
   191 
       
   192 =item B<-h>|B<--help>
       
   193 
       
   194 Print detailed help screen.
       
   195 
       
   196 =item B<-m>|B<--man>
       
   197 
       
   198 Print manual page.
       
   199 
       
   200 =item B<-V>|B<--version>
       
   201 
       
   202 Print version information.
       
   203 
       
   204 =back
       
   205 
       
   206 =head1 DESCRIPTION
       
   207 
       
   208 This plugin checks the hard drive temperature.
       
   209 
       
   210 =head1 VERSION
       
   211 
       
   212 This man page is current for version 1.5 of B<check_hddtemp>.
       
   213 
       
   214 =head1 AUTHOR
       
   215 
       
   216 Written by Christian Arnold L<arnold@schlittermann.de>
       
   217 
       
   218 =head1 COPYRIGHT
       
   219 
       
   220 Copyright (C) 2012 by Christian Arnold and Schlittermann internet & unix support.
       
   221 This is free software, and you are welcome to redistribute it under certain conditions.
       
   222 See the GNU General Public Licence for details.
       
   223 
       
   224 =cut