[BACK]Return to check_bioctl CVS log [TXT][DIR] Up to [local] / nagios / check_bioctl

Annotation of nagios/check_bioctl/check_bioctl, Revision 1.9

1.1       andrew      1: #!/usr/bin/perl -T
1.9     ! andrew      2: # $RedRiver: check_bioctl,v 1.8 2009/11/09 18:11:33 andrew Exp $
1.1       andrew      3: ########################################################################
                      4: # check_bioctl *** A nagios check for OpenBSD bioctl
1.7       andrew      5: #
1.9     ! andrew      6: # 2006.07.26 #*#*# andrew fresh <andrew@afresh1.com>
1.1       andrew      7: ########################################################################
                      8: use strict;
                      9: use warnings;
                     10:
                     11: %ENV = ();
                     12:
                     13: use constant NAGIOS_OUTPUT => 1;
                     14:
1.9     ! andrew     15: my $License = <<'EOL';
        !            16: Copyright (c) 2009 Andrew Fresh <andrew@afresh1.com>
        !            17: Permission to use, copy, modify, and distribute this software for any
        !            18: purpose with or without fee is hereby granted, provided that the above
        !            19: copyright notice and this permission notice appear in all copies.
        !            20:
        !            21: THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !            22: WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            23: MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            24: ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            25: WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        !            26: ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        !            27: OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
        !            28: EOL
        !            29:
1.1       andrew     30: use POSIX;
                     31: use lib "/usr/local/libexec/nagios";
1.9     ! andrew     32: use utils qw($TIMEOUT %ERRORS &support);
1.1       andrew     33:
                     34: use Getopt::Long;
                     35: Getopt::Long::Configure('bundling');
                     36:
                     37: my $PROGNAME = "check_bioctl";
1.7       andrew     38: my $BIOCTL   = '/sbin/bioctl';
1.1       andrew     39:
1.3       andrew     40: # This maps the status we get from bioctl to something nagios can use
1.1       andrew     41: my %Status_Map = (
1.7       andrew     42:     Online      => 'OK',
                     43:     Offline     => 'CRITICAL',
                     44:     Degraded    => 'CRITICAL',
                     45:     Failed      => 'CRITICAL',
                     46:     Building    => 'WARNING',
                     47:     Rebuild     => 'WARNING',
                     48:     'Hot spare' => 'OK',
                     49:     Unused      => 'OK',
                     50:     Scrubbing   => 'WARNING',
                     51:     Invalid     => 'CRITICAL',
1.1       andrew     52: );
                     53:
1.7       andrew     54: my $state = 'UNKNOWN';    # tells whether the it is warning, critical, or OK
                     55: my %states;               # This stores the count of states;
1.2       andrew     56: my @devices;
1.1       andrew     57: my $opt_h;
                     58: my $opt_V;
                     59:
                     60: #Option checking
                     61: my $status = GetOptions(
1.7       andrew     62:     "version|V"  => \$opt_V,
                     63:     "help|h"     => \$opt_h,
                     64:     "device|d=s" => \@devices,
1.1       andrew     65: );
                     66:
1.7       andrew     67: if ( $status == 0 ) {
                     68:     print_help();
                     69:     exit $ERRORS{'OK'};
1.1       andrew     70: }
                     71:
                     72: if ($opt_V) {
1.9     ! andrew     73:     print_revision( $PROGNAME, '$Revision: 1.8 $ ' );
1.7       andrew     74:     exit $ERRORS{'OK'};
1.1       andrew     75: }
                     76:
1.7       andrew     77: if ( $opt_h || not @devices ) {
                     78:     print_help();
                     79:     exit $ERRORS{'OK'};
1.1       andrew     80: }
                     81:
                     82: my %VOLUMES;
1.2       andrew     83: foreach my $device (@devices) {
1.8       andrew     84:     open my $bioctl, '-|', $BIOCTL, $device or die "Couldn't open bioctl: $!";
1.7       andrew     85:     my $volume_id;
                     86:
                     87:     while (<$bioctl>) {
                     88:         chomp;
1.2       andrew     89:
1.7       andrew     90:         # Do these by columns cuZ that is the easiest for now
                     91:         my @o = unpack( "A6 A1 A11 A15 A7 A9 A*", $_ );
                     92:         next if $o[0] eq 'Volume';
                     93:
                     94:         foreach (@o) {
                     95:             s/^\s+//;
                     96:             s/\s+$//;
                     97:         }
                     98:
                     99:         my ( $controller, $id, $status, $size, $dev, $details, $name ) = @o;
                    100:         my $index = $id;
                    101:         if ($controller) {
                    102:             $volume_id = $id;
                    103:         }
                    104:         else {
                    105:             $index = "$volume_id.$id";
                    106:         }
                    107:
                    108:         $VOLUMES{$device}{$index} = {
                    109:             type       => 'volume',
                    110:             controller => $controller,
                    111:             id         => $id,
                    112:             status     => $status,
                    113:             size       => $size,
                    114:             device     => $dev,
                    115:             details    => $details,
                    116:             name       => $name,
                    117:         };
                    118:
                    119:         if ( $dev =~ /^\d+:\d+/ ) {
                    120:             $VOLUMES{$device}{$index}{'volume'}
                    121:                 = $VOLUMES{$device}{$volume_id};
                    122:         }
                    123:
                    124:     }
                    125:     close $bioctl;
                    126: }
                    127:
                    128: foreach my $device ( sort keys %VOLUMES ) {
                    129:     foreach my $index ( sort keys %{ $VOLUMES{$device} } ) {
                    130:         my $cur_state
                    131:             = $Status_Map{ $VOLUMES{$device}{$index}{'status'} }
                    132:             ? $Status_Map{ $VOLUMES{$device}{$index}{'status'} }
                    133:             : 'UNKNOWN';
                    134:
                    135:         if ( $VOLUMES{$device}{$index}{'device'} =~ /^\d+:\d/ ) {
                    136:             push @{ $states{$cur_state} },
                    137:                 sprintf(
                    138:                 "%5s %-7s %-11s %s",
                    139:                 $VOLUMES{$device}{$index}{'volume'}{'controller'},
                    140:                 $VOLUMES{$device}{$index}{'device'},
                    141:                 $VOLUMES{$device}{$index}{'status'},
                    142:                 $VOLUMES{$device}{$index}{'name'}
                    143:                 );
                    144:         }
                    145:         else {
                    146:             push @{ $states{$cur_state} },
                    147:                 sprintf( "%5s %-7s %s",
                    148:                 $VOLUMES{$device}{$index}{'controller'},
                    149:                 $VOLUMES{$device}{$index}{'device'},
                    150:                 $VOLUMES{$device}{$index}{'status'} );
                    151:         }
                    152:     }
1.1       andrew    153: }
                    154:
                    155: my $have_results = 0;
1.8       andrew    156: $state = 'OK';
1.7       andrew    157: foreach my $error ( sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS ) {
                    158:     if ( exists $states{$error} ) {
                    159:         $have_results++;
1.6       andrew    160:         $state = $error if $ERRORS{$state} < $ERRORS{$error};
1.5       andrew    161:
1.7       andrew    162:         if (NAGIOS_OUTPUT) {
                    163:             print "$error (" . scalar( @{ $states{$error} } ) . ")";
1.8       andrew    164:             if ( $error ne 'OK' ) {
1.7       andrew    165:                 print '<br>';
                    166:                 print map {" - $_<br>"} @{ $states{$error} };
                    167:             }
                    168:         }
                    169:         else {
                    170:             print "$error (" . scalar( @{ $states{$error} } ) . "):\n";
                    171:             print map {"    $_\n"} @{ $states{$error} };
                    172:         }
                    173:     }
1.1       andrew    174: }
1.7       andrew    175: if ( $have_results == 0 ) {
                    176:     print "No results found\n";
1.1       andrew    177: }
                    178: exit $ERRORS{$state};
                    179:
                    180: sub print_help {
1.9     ! andrew    181:     print <<"EOL";
1.1       andrew    182: $PROGNAME plugin for Nagios monitors bioctl on OpenBSD
1.2       andrew    183:     $PROGNAME -d <device> [ -d <device2> [ -d ... ] ]
1.1       andrew    184:
                    185: Usage:
                    186:     -d, --device=DEVICE
1.3       andrew    187:         DEVICE to check.  Can be any device that bioctl(8) accepts
1.1       andrew    188:     -h (--help)       usage help
                    189:        -V (--version)    version information
                    190:
                    191: EOL
1.7       andrew    192:
1.9     ! andrew    193:     print_revision( $PROGNAME, '$Revision: 1.8 $' );
        !           194:
        !           195:     print $License;
1.1       andrew    196: }
                    197:
1.9     ! andrew    198:
        !           199: sub print_revision {
        !           200:     my ($prog, $rev) = @_;
        !           201:     $rev =~ s/^\D+([\d\.]+)\D+$/v$1/xms;
        !           202:
        !           203:     print "$prog $rev\n";
        !           204: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>