check_drbd.pl
changeset 0 d2456e857a47
child 1 9542011381ce
equal deleted inserted replaced
-1:000000000000 0:d2456e857a47
       
     1 #! /usr/bin/perl -w
       
     2 
       
     3 #    Copyright (C) 2011  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 strict;
       
    21 use File::Basename;
       
    22 use Getopt::Long;
       
    23 use Pod::Usage;
       
    24 
       
    25 my %ERRORS = (
       
    26     OK        => 0,
       
    27     WARNING   => 1,
       
    28     CRITICAL  => 2,
       
    29     UNKNOWN   => 3,
       
    30     DEPENDENT => 4
       
    31 );
       
    32 
       
    33 sub check_status($);
       
    34 sub version($$);
       
    35 
       
    36 my $ME      = basename $0;
       
    37 my $NAME    = 'DRBD';
       
    38 my $VERSION = "2.0";
       
    39 
       
    40 my %opt = ( file => "/proc/drbd" );
       
    41 
       
    42 MAIN: {
       
    43     Getopt::Long::Configure('bundling');
       
    44     GetOptions(
       
    45         "f|file=s" => \$opt{file},
       
    46         "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
       
    47         "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
       
    48         "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
       
    49     ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
       
    50 
       
    51     check_status( $opt{file} );
       
    52 }
       
    53 
       
    54 sub check_status($) {
       
    55     my $file = shift;
       
    56     my ( %drbd, @warning, @critical, @ok ) = ();
       
    57 
       
    58     unless ( -r $file ) {
       
    59         print "$NAME CRITICAL: $file $!\n";
       
    60         exit $ERRORS{CRITICAL};
       
    61     }
       
    62 
       
    63     open( FH, $file )
       
    64       or print "$NAME CRITICAL: $file $!\n" and exit $ERRORS{CRITICAL};
       
    65     while (<FH>) {
       
    66         chomp;
       
    67 /^.*(?<line>(?<resource>\d+):\s+cs:(?<connection_state>\S+)\s+(?:st|ro):(?<roles>\S+)\s+(?:ds|ld):(?<disk_states>\S+))/
       
    68           or next;
       
    69         $drbd{ $+{resource} } = {
       
    70             line             => $+{line},
       
    71             connection_state => $+{connection_state},
       
    72             roles            => $+{roles},
       
    73             disk_states      => $+{disk_states}
       
    74         };
       
    75     }
       
    76     close(FH);
       
    77 
       
    78     unless (%drbd) {
       
    79         print "$NAME CRITICAL: $file found, but no entries\n";
       
    80         exit $ERRORS{CRITICAL};
       
    81     }
       
    82 
       
    83     foreach my $dev ( map { $drbd{$_} } sort keys %drbd ) {
       
    84         $dev->{state} = "CRITICAL", next
       
    85           if ( $dev->{roles} ne "Primary/Secondary" )
       
    86           and ( $dev->{roles} ne "Secondary/Primary" );
       
    87 
       
    88         $dev->{state} = "CRITICAL", next
       
    89           if ( $dev->{disk_states} ne "UpToDate/UpToDate" )
       
    90           and ( $dev->{disk_states} ne "Consistent" );
       
    91 
       
    92         $dev->{state} = "WARNING", next
       
    93           if $dev->{connection_state} ne "Connected";
       
    94 
       
    95         $dev->{state} = "OK";
       
    96     }
       
    97 
       
    98     foreach my $dev ( map { $drbd{$_} } sort keys %drbd ) {
       
    99         $dev->{state} eq "WARNING"  and push( @warning,  $dev->{line} ), next;
       
   100         $dev->{state} eq "CRITICAL" and push( @critical, $dev->{line} ), next;
       
   101         $dev->{state} eq "OK"       and push( @ok,       $dev->{line} ), next;
       
   102     }
       
   103 
       
   104     print("$NAME CRITICAL: @critical\n"), exit $ERRORS{CRITICAL} if @critical;
       
   105     print("$NAME WARNING: @warning\n"),   exit $ERRORS{WARNING}  if @warning;
       
   106     print("$NAME OK: @ok\n"),             exit $ERRORS{OK};
       
   107 }
       
   108 
       
   109 sub version($$) {
       
   110     my $progname = shift;
       
   111     my $version  = shift;
       
   112 
       
   113     print <<_VERSION;
       
   114 $progname version $version
       
   115 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.
       
   116 
       
   117 $ME comes with ABSOLUTELY NO WARRANTY. This is free software,
       
   118 and you are welcome to redistribute it under certain conditions.
       
   119 See the GNU General Public Licence for details.
       
   120 _VERSION
       
   121 }
       
   122 
       
   123 __END__
       
   124 
       
   125 =head1 NAME
       
   126 
       
   127 check_drbd - nagios plugin to check drbd status
       
   128 
       
   129 =head1 SYNOPSIS
       
   130 
       
   131 check_drbd [-f|--file path]
       
   132            [-h|--help]
       
   133            [-m|--man]
       
   134            [-V|--version]
       
   135 
       
   136 =head1 OPTIONS
       
   137 
       
   138 =over
       
   139 
       
   140 =item B<-f>|B<--file> I<path>
       
   141 
       
   142 Absolute path of drbd status file. (default: /proc/drbd)
       
   143 
       
   144 =item B<-h>|B<--help>
       
   145 
       
   146 Print detailed help screen.
       
   147 
       
   148 =item B<-m>|B<--man>
       
   149 
       
   150 Print manual page.
       
   151 
       
   152 =item B<-V>|B<--version>
       
   153 
       
   154 Print version information.
       
   155 
       
   156 =back
       
   157 
       
   158 =head1 DESCRIPTION
       
   159 
       
   160 This plugin check drbd status.
       
   161 
       
   162 =head1 VERSION
       
   163 
       
   164 This man page is current for version 2.0 of check_drbd.
       
   165 
       
   166 =head1 AUTHOR
       
   167 
       
   168 Written by Christian Arnold L<arnold@schlittermann.de>
       
   169 
       
   170 =head1 COPYRIGHT
       
   171 
       
   172 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.
       
   173 This is free software, and you are welcome to redistribute it under certain conditions.
       
   174 See the GNU General Public Licence for details.
       
   175 
       
   176 =cut