check_drbd.pl
changeset 0 d2456e857a47
child 1 9542011381ce
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/check_drbd.pl	Thu Aug 25 16:50:49 2011 +0200
@@ -0,0 +1,176 @@
+#! /usr/bin/perl -w
+
+#    Copyright (C) 2011  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 strict;
+use File::Basename;
+use Getopt::Long;
+use Pod::Usage;
+
+my %ERRORS = (
+    OK        => 0,
+    WARNING   => 1,
+    CRITICAL  => 2,
+    UNKNOWN   => 3,
+    DEPENDENT => 4
+);
+
+sub check_status($);
+sub version($$);
+
+my $ME      = basename $0;
+my $NAME    = 'DRBD';
+my $VERSION = "2.0";
+
+my %opt = ( file => "/proc/drbd" );
+
+MAIN: {
+    Getopt::Long::Configure('bundling');
+    GetOptions(
+        "f|file=s" => \$opt{file},
+        "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} );
+
+    check_status( $opt{file} );
+}
+
+sub check_status($) {
+    my $file = shift;
+    my ( %drbd, @warning, @critical, @ok ) = ();
+
+    unless ( -r $file ) {
+        print "$NAME CRITICAL: $file $!\n";
+        exit $ERRORS{CRITICAL};
+    }
+
+    open( FH, $file )
+      or print "$NAME CRITICAL: $file $!\n" and exit $ERRORS{CRITICAL};
+    while (<FH>) {
+        chomp;
+/^.*(?<line>(?<resource>\d+):\s+cs:(?<connection_state>\S+)\s+(?:st|ro):(?<roles>\S+)\s+(?:ds|ld):(?<disk_states>\S+))/
+          or next;
+        $drbd{ $+{resource} } = {
+            line             => $+{line},
+            connection_state => $+{connection_state},
+            roles            => $+{roles},
+            disk_states      => $+{disk_states}
+        };
+    }
+    close(FH);
+
+    unless (%drbd) {
+        print "$NAME CRITICAL: $file found, but no entries\n";
+        exit $ERRORS{CRITICAL};
+    }
+
+    foreach my $dev ( map { $drbd{$_} } sort keys %drbd ) {
+        $dev->{state} = "CRITICAL", next
+          if ( $dev->{roles} ne "Primary/Secondary" )
+          and ( $dev->{roles} ne "Secondary/Primary" );
+
+        $dev->{state} = "CRITICAL", next
+          if ( $dev->{disk_states} ne "UpToDate/UpToDate" )
+          and ( $dev->{disk_states} ne "Consistent" );
+
+        $dev->{state} = "WARNING", next
+          if $dev->{connection_state} ne "Connected";
+
+        $dev->{state} = "OK";
+    }
+
+    foreach my $dev ( map { $drbd{$_} } sort keys %drbd ) {
+        $dev->{state} eq "WARNING"  and push( @warning,  $dev->{line} ), next;
+        $dev->{state} eq "CRITICAL" and push( @critical, $dev->{line} ), next;
+        $dev->{state} eq "OK"       and push( @ok,       $dev->{line} ), next;
+    }
+
+    print("$NAME CRITICAL: @critical\n"), exit $ERRORS{CRITICAL} if @critical;
+    print("$NAME WARNING: @warning\n"),   exit $ERRORS{WARNING}  if @warning;
+    print("$NAME OK: @ok\n"),             exit $ERRORS{OK};
+}
+
+sub version($$) {
+    my $progname = shift;
+    my $version  = shift;
+
+    print <<_VERSION;
+$progname version $version
+Copyright (C) 2011 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_drbd - nagios plugin to check drbd status
+
+=head1 SYNOPSIS
+
+check_drbd [-f|--file path]
+           [-h|--help]
+           [-m|--man]
+           [-V|--version]
+
+=head1 OPTIONS
+
+=over
+
+=item B<-f>|B<--file> I<path>
+
+Absolute path of drbd status file. (default: /proc/drbd)
+
+=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 check drbd status.
+
+=head1 VERSION
+
+This man page is current for version 2.0 of check_drbd.
+
+=head1 AUTHOR
+
+Written by Christian Arnold L<arnold@schlittermann.de>
+
+=head1 COPYRIGHT
+
+Copyright (C) 2011 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