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

Annotation of nagios/check_bioctl/check_bioctl, Revision 1.13

1.1       andrew      1: #!/usr/bin/perl -T
1.13    ! andrew      2: # $RedRiver: check_bioctl,v 1.12 2009/11/23 21:52:53 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:
1.11      andrew     11: local %ENV = ();
                     12:
1.12      andrew     13: my $NAGIOS_OUTPUT = 1;
1.1       andrew     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.11      andrew     30: my $PROGNAME = 'check_bioctl';
1.10      andrew     31: my $BIOCTL   = '/sbin/bioctl';
                     32:
1.1       andrew     33: use POSIX;
1.10      andrew     34: my $PREFIX;
1.11      andrew     35:
1.10      andrew     36: BEGIN {
                     37:     ## no critic 'warnings'
                     38:     no warnings 'uninitialized';
1.11      andrew     39:     $PREFIX = "${PREFIX}" || '/usr/local';    # Magic for OpenBSD ports tree
1.10      andrew     40: }
                     41: use lib $PREFIX . '/libexec/nagios';
1.9       andrew     42: use utils qw($TIMEOUT %ERRORS &support);
1.1       andrew     43:
1.10      andrew     44: $SIG{'ALRM'} = sub {
1.12      andrew     45:     print "ERROR: $PROGNAME timeout\n";
1.11      andrew     46:     exit $ERRORS{'UNKNOWN'};
1.10      andrew     47: };
                     48: alarm($TIMEOUT);
                     49:
1.1       andrew     50: use Getopt::Long;
                     51: Getopt::Long::Configure('bundling');
                     52:
1.3       andrew     53: # This maps the status we get from bioctl to something nagios can use
1.1       andrew     54: my %Status_Map = (
1.7       andrew     55:     Online      => 'OK',
                     56:     Offline     => 'CRITICAL',
                     57:     Degraded    => 'CRITICAL',
                     58:     Failed      => 'CRITICAL',
                     59:     Building    => 'WARNING',
                     60:     Rebuild     => 'WARNING',
                     61:     'Hot spare' => 'OK',
                     62:     Unused      => 'OK',
                     63:     Scrubbing   => 'WARNING',
                     64:     Invalid     => 'CRITICAL',
1.1       andrew     65: );
                     66:
1.7       andrew     67: my $state = 'UNKNOWN';    # tells whether the it is warning, critical, or OK
1.2       andrew     68: my @devices;
1.1       andrew     69: my $opt_h;
                     70: my $opt_V;
                     71:
                     72: #Option checking
                     73: my $status = GetOptions(
1.7       andrew     74:     "version|V"  => \$opt_V,
                     75:     "help|h"     => \$opt_h,
                     76:     "device|d=s" => \@devices,
1.1       andrew     77: );
                     78:
1.7       andrew     79: if ( $status == 0 ) {
                     80:     print_help();
                     81:     exit $ERRORS{'OK'};
1.1       andrew     82: }
                     83:
                     84: if ($opt_V) {
1.13    ! andrew     85:     print_revision( $PROGNAME, '$Revision: 1.12 $ ' );
1.7       andrew     86:     exit $ERRORS{'OK'};
1.1       andrew     87: }
                     88:
1.11      andrew     89: if ( $opt_h || !@devices ) {
1.7       andrew     90:     print_help();
                     91:     exit $ERRORS{'OK'};
1.1       andrew     92: }
                     93:
1.11      andrew     94: my %VOLUMES = read_bioctl( \@devices );
                     95: my %STATES  = check_status( \%VOLUMES );
1.1       andrew     96:
                     97: my $have_results = 0;
1.8       andrew     98: $state = 'OK';
1.7       andrew     99: foreach my $error ( sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS ) {
1.11      andrew    100:     if ( exists $STATES{$error} ) {
1.7       andrew    101:         $have_results++;
1.6       andrew    102:         $state = $error if $ERRORS{$state} < $ERRORS{$error};
1.5       andrew    103:
1.11      andrew    104:         if ($NAGIOS_OUTPUT) {
                    105:             print "$error (" . scalar( @{ $STATES{$error} } ) . ")";
1.8       andrew    106:             if ( $error ne 'OK' ) {
1.7       andrew    107:                 print '<br>';
1.11      andrew    108:                 print map {" - $_<br>"} @{ $STATES{$error} };
1.7       andrew    109:             }
                    110:         }
                    111:         else {
1.11      andrew    112:             print "$error (" . scalar( @{ $STATES{$error} } ) . "):\n";
                    113:             print map {"    $_\n"} @{ $STATES{$error} };
1.7       andrew    114:         }
                    115:     }
1.1       andrew    116: }
1.7       andrew    117: if ( $have_results == 0 ) {
                    118:     print "No results found\n";
1.1       andrew    119: }
                    120: exit $ERRORS{$state};
                    121:
1.11      andrew    122: sub read_bioctl {
                    123:     my ($devices) = @_;
                    124:     my %volumes;
                    125:
                    126:     foreach my $d ( @{$devices} ) {
                    127:         open my $bioctl, q{-|}, $BIOCTL, $d
                    128:             or die "Couldn't open bioctl: $!\n";
1.13    ! andrew    129:     LINE: while ( my $line = <$bioctl> ) {
1.11      andrew    130:             my ( $i, $item ) = parse_bioctl_line($line);
1.13    ! andrew    131:             next LINE if !defined $i;
1.11      andrew    132:             $volumes{$d}{$i} = $item;
                    133:         }
                    134:         ## no critic 'die'
                    135:         close $bioctl
                    136:             or die $!
                    137:             ? "Error closing bioctl pipe: $!\n"
                    138:             : "Exit status $? from bioctl \n";
                    139:     }
                    140:
                    141:     foreach my $d ( keys %volumes ) {
                    142:         foreach my $i ( keys %{ $volumes{$d} } ) {
                    143:             my $item = $volumes{$d}{$i};
                    144:             if ( $item->{device} =~ /^\d+:\d+/xms ) {
1.12      andrew    145:                 $item->{'volume'} = $volumes{$d}{ $item->{volume_id} };
1.11      andrew    146:             }
                    147:         }
                    148:     }
                    149:
                    150:     return %volumes;
                    151: }
                    152:
1.12      andrew    153: {
                    154:     my $vid;
1.11      andrew    155:
1.13    ! andrew    156:     sub parse_bioctl_line {
        !           157:         my ($line) = @_;
        !           158:         chomp $line;
        !           159:
        !           160:         # Do these by columns cuZ that is the easiest for now
        !           161:         my @o = unpack( "A6 A1 A11 A15 A7 A9 A*", $line );
        !           162:         return if $o[0] eq 'Volume';
        !           163:
        !           164:         foreach (@o) {
        !           165:             s/^\s+//xms;
        !           166:             s/\s+$//xms;
        !           167:         }
        !           168:
        !           169:         my ( $controller, $id, $status, $size, $dev, $details, $name ) = @o;
        !           170:         my $index = $id;
        !           171:         if ($controller) {
        !           172:             $vid = $id;
        !           173:         }
        !           174:         else {
        !           175:             $index = "$vid.$id";
        !           176:         }
1.11      andrew    177:
1.13    ! andrew    178:         my %item = (
        !           179:             type       => 'volume',
        !           180:             controller => $controller,
        !           181:             id         => $id,
        !           182:             status     => $status,
        !           183:             size       => $size,
        !           184:             device     => $dev,
        !           185:             details    => $details,
        !           186:             name       => $name,
        !           187:             volume_id  => $vid,
        !           188:         );
1.11      andrew    189:
1.13    ! andrew    190:         return $index, \%item;
        !           191:     }
1.11      andrew    192: }
1.12      andrew    193: }
1.11      andrew    194:
                    195: sub check_status {
                    196:     my ($volumes) = @_;
                    197:
                    198:     my %states;
                    199:     foreach my $device ( sort keys %{$volumes} ) {
                    200:         foreach my $index ( sort keys %{ $volumes->{$device} } ) {
                    201:             my $cur_volume = $volumes->{$device}->{$index};
                    202:             my $cur_state  = $Status_Map{ $cur_volume->{'status'} }
                    203:                 || 'UNKNOWN';
                    204:
                    205:             if ( $cur_volume->{'device'} =~ /^\d+:\d/xms ) {
                    206:                 push @{ $states{$cur_state} },
                    207:                     sprintf(
                    208:                     "%5s %-7s %-11s %s",
                    209:                     $cur_volume->{'volume'}{'controller'},
                    210:                     $cur_volume->{'device'},
                    211:                     $cur_volume->{'status'},
                    212:                     $cur_volume->{'name'}
                    213:                     );
                    214:             }
                    215:             else {
                    216:                 push @{ $states{$cur_state} },
                    217:                     sprintf( "%5s %-7s %s",
                    218:                     $cur_volume->{'controller'},
                    219:                     $cur_volume->{'device'},
                    220:                     $cur_volume->{'status'} );
                    221:             }
                    222:         }
                    223:     }
                    224:     return %states;
                    225: }
                    226:
1.1       andrew    227: sub print_help {
1.9       andrew    228:     print <<"EOL";
1.1       andrew    229: $PROGNAME plugin for Nagios monitors bioctl on OpenBSD
1.2       andrew    230:     $PROGNAME -d <device> [ -d <device2> [ -d ... ] ]
1.1       andrew    231:
                    232: Usage:
                    233:     -d, --device=DEVICE
1.3       andrew    234:         DEVICE to check.  Can be any device that bioctl(8) accepts
1.1       andrew    235:     -h (--help)       usage help
                    236:        -V (--version)    version information
                    237:
                    238: EOL
1.7       andrew    239:
1.13    ! andrew    240:     print_revision( $PROGNAME, '$Revision: 1.12 $' );
1.9       andrew    241:
                    242:     print $License;
1.11      andrew    243:
                    244:     return 1;
1.1       andrew    245: }
                    246:
1.9       andrew    247: sub print_revision {
1.11      andrew    248:     my ( $prog, $rev ) = @_;
1.9       andrew    249:     $rev =~ s/^\D+([\d\.]+)\D+$/v$1/xms;
                    250:
1.12      andrew    251:     print "$prog $rev\n";
1.11      andrew    252:
                    253:     return 1;
1.9       andrew    254: }

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