check_drbd.pl
changeset 7 f0871534a792
parent 6 818e48975f3c
equal deleted inserted replaced
6:818e48975f3c 7:f0871534a792
     1 #! /usr/bin/perl -w
       
     2 
       
     3 #    Copyright (C) 2011-2014  Christian Arnold
       
     4 #    Copyright (C) 2014-2017  Matthias Förste
       
     5 #
       
     6 #    This program is free software: you can redistribute it and/or modify
       
     7 #    it under the terms of the GNU General Public License as published by
       
     8 #    the Free Software Foundation, either version 3 of the License, or
       
     9 #    (at your option) any later version.
       
    10 #
       
    11 #    This program is distributed in the hope that it will be useful,
       
    12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14 #    GNU General Public License for more details.
       
    15 #
       
    16 #    You should have received a copy of the GNU General Public License
       
    17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    18 #
       
    19 #    Christian Arnold <arnold@schlittermann.de>
       
    20 #    Matthias Förste <foerste@schlittermann.de>
       
    21 
       
    22 use strict;
       
    23 use File::Basename;
       
    24 use Getopt::Long;
       
    25 use Pod::Usage;
       
    26 
       
    27 my %ERRORS = (
       
    28     OK        => 0,
       
    29     WARNING   => 1,
       
    30     CRITICAL  => 2,
       
    31     UNKNOWN   => 3,
       
    32     DEPENDENT => 4
       
    33 );
       
    34 
       
    35 sub check_status($);
       
    36 sub version($$);
       
    37 
       
    38 my $ME      = basename $0;
       
    39 my $NAME    = 'DRBD';
       
    40 my $VERSION = "2.0";
       
    41 
       
    42 my %opt = ( file => "/proc/drbd" );
       
    43 
       
    44 MAIN: {
       
    45     Getopt::Long::Configure('bundling');
       
    46     GetOptions(
       
    47         "f|file=s" => \$opt{file},
       
    48         "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
       
    49         "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
       
    50         "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
       
    51     ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
       
    52 
       
    53     check_status( $opt{file} );
       
    54 }
       
    55 
       
    56 sub check_status($) {
       
    57     my $file = shift;
       
    58     my ( %drbd, @warning, @critical, @ok ) = ();
       
    59 
       
    60     unless ( -r $file ) {
       
    61         print "$NAME CRITICAL: $file $!\n";
       
    62         exit $ERRORS{CRITICAL};
       
    63     }
       
    64 
       
    65     open( FH, $file )
       
    66       or print "$NAME CRITICAL: $file $!\n" and exit $ERRORS{CRITICAL};
       
    67     my $r;
       
    68     while (<FH>) {
       
    69         chomp;
       
    70         if (/^\s*(?<line>(?<resource>\d+):\s+cs:(?<connection_state>\S+)\s+(?:st|ro):(?<roles>\S+)\s+(?:ds|ld):(?<disk_states>\S+))/) {
       
    71             $r = $+{resource};
       
    72             $drbd{$r} = {
       
    73                 line             => $+{line},
       
    74                 connection_state => $+{connection_state},
       
    75                 roles            => $+{roles},
       
    76                 disk_states      => $+{disk_states}
       
    77             };
       
    78         } elsif (defined $r and /oos:(?<out_of_sync>\d+)/) {
       
    79             $drbd{$r}->{out_of_sync} = $+{out_of_sync};
       
    80             $drbd{$r}->{line} .= " oos: $+{out_of_sync}";
       
    81         }
       
    82     }
       
    83     close(FH);
       
    84 
       
    85     unless (%drbd) {
       
    86         print "$NAME CRITICAL: $file found, but no entries\n";
       
    87         exit $ERRORS{CRITICAL};
       
    88     }
       
    89 
       
    90     foreach my $dev ( map { $drbd{$_} } sort keys %drbd ) {
       
    91         $dev->{state} = "CRITICAL", next
       
    92           if ( $dev->{roles} ne "Primary/Secondary" )
       
    93           and ( $dev->{roles} ne "Secondary/Primary" );
       
    94 
       
    95         $dev->{state} = "CRITICAL", next
       
    96           if ( $dev->{disk_states} ne "UpToDate/UpToDate" )
       
    97           and ( $dev->{disk_states} ne "Consistent" );
       
    98 
       
    99         $dev->{state} = "CRITICAL", next
       
   100           if ( $dev->{out_of_sync} != 0 );
       
   101 
       
   102         $dev->{state} = "WARNING", next
       
   103           if $dev->{connection_state} ne "Connected";
       
   104 
       
   105         $dev->{state} = "OK";
       
   106     }
       
   107 
       
   108     foreach my $dev ( map { $drbd{$_} } sort keys %drbd ) {
       
   109         $dev->{state} eq "WARNING"  and push( @warning,  $dev->{line} ), next;
       
   110         $dev->{state} eq "CRITICAL" and push( @critical, $dev->{line} ), next;
       
   111         $dev->{state} eq "OK"       and push( @ok,       $dev->{line} ), next;
       
   112     }
       
   113 
       
   114     print("$NAME CRITICAL: @critical\n"), exit $ERRORS{CRITICAL} if @critical;
       
   115     print("$NAME WARNING: @warning\n"),   exit $ERRORS{WARNING}  if @warning;
       
   116     print("$NAME OK: @ok\n"),             exit $ERRORS{OK};
       
   117 }
       
   118 
       
   119 sub version($$) {
       
   120     my $progname = shift;
       
   121     my $version  = shift;
       
   122 
       
   123     print <<_VERSION;
       
   124 $progname version $version
       
   125 Copyright (C) 2011-2014 by Christian Arnold and Schlittermann internet & unix support.
       
   126 Copyright (C) 2014-2017 by Matthias Förste and Schlittermann internet & unix support.
       
   127 
       
   128 $ME comes with ABSOLUTELY NO WARRANTY. This is free software,
       
   129 and you are welcome to redistribute it under certain conditions.
       
   130 See the GNU General Public Licence for details.
       
   131 _VERSION
       
   132 }
       
   133 
       
   134 __END__
       
   135 
       
   136 =head1 NAME
       
   137 
       
   138 check_drbd - nagios plugin to check drbd status
       
   139 
       
   140 =head1 SYNOPSIS
       
   141 
       
   142 check_drbd [-f|--file path]
       
   143            [-h|--help]
       
   144            [-m|--man]
       
   145            [-V|--version]
       
   146 
       
   147 =head1 OPTIONS
       
   148 
       
   149 =over
       
   150 
       
   151 =item B<-f>|B<--file> I<path>
       
   152 
       
   153 Absolute path of drbd status file. (default: /proc/drbd)
       
   154 
       
   155 =item B<-h>|B<--help>
       
   156 
       
   157 Print detailed help screen.
       
   158 
       
   159 =item B<-m>|B<--man>
       
   160 
       
   161 Print manual page.
       
   162 
       
   163 =item B<-V>|B<--version>
       
   164 
       
   165 Print version information.
       
   166 
       
   167 =back
       
   168 
       
   169 =head1 DESCRIPTION
       
   170 
       
   171 This plugin check drbd status.
       
   172 
       
   173 =head1 VERSION
       
   174 
       
   175 This man page is current for version 2.0 of check_drbd.
       
   176 
       
   177 =head1 AUTHOR
       
   178 
       
   179 Written by Christian Arnold L<arnold@schlittermann.de>. Currently maintained by Matthias Förste L<foerste@schlittermann.de>
       
   180 
       
   181 =head1 COPYRIGHT
       
   182 
       
   183 Copyright (C) 2011-2014 by Christian Arnold and Schlittermann internet & unix support.
       
   184 Copyright (C) 2014-2017 by Matthias Förste and Schlittermann internet & unix support.
       
   185 This is free software, and you are welcome to redistribute it under certain conditions.
       
   186 See the GNU General Public Licence for details.
       
   187 
       
   188 =cut