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

Annotation of nagios/check_bioctl/check_bioctl, Revision 1.19

1.1       andrew      1: #!/usr/bin/perl -T
1.19    ! andrew      2: # $AFresh1$
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;
1.19    ! andrew     10: use 5.010;
1.1       andrew     11:
1.11      andrew     12: local %ENV = ();
                     13:
1.16      andrew     14: my $NAGIOS_OUTPUT = 1;
1.1       andrew     15:
1.9       andrew     16: my $License = <<'EOL';
                     17: Copyright (c) 2009 Andrew Fresh <andrew@afresh1.com>
                     18: Permission to use, copy, modify, and distribute this software for any
                     19: purpose with or without fee is hereby granted, provided that the above
                     20: copyright notice and this permission notice appear in all copies.
                     21:
                     22: THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     23: WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     24: MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     25: ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     26: WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     27: ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     28: OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     29: EOL
                     30:
1.11      andrew     31: my $PROGNAME = 'check_bioctl';
1.10      andrew     32: my $BIOCTL   = '/sbin/bioctl';
                     33:
1.1       andrew     34: use POSIX;
1.10      andrew     35: my $PREFIX;
1.11      andrew     36:
1.10      andrew     37: BEGIN {
                     38:     ## no critic 'warnings'
                     39:     no warnings 'uninitialized';
1.11      andrew     40:     $PREFIX = "${PREFIX}" || '/usr/local';    # Magic for OpenBSD ports tree
1.10      andrew     41: }
                     42: use lib $PREFIX . '/libexec/nagios';
1.9       andrew     43: use utils qw($TIMEOUT %ERRORS &support);
1.1       andrew     44:
1.10      andrew     45: $SIG{'ALRM'} = sub {
1.19    ! andrew     46:     say "ERROR: $PROGNAME timeout";
1.11      andrew     47:     exit $ERRORS{'UNKNOWN'};
1.10      andrew     48: };
                     49: alarm($TIMEOUT);
                     50:
1.1       andrew     51: use Getopt::Long;
                     52: Getopt::Long::Configure('bundling');
                     53:
1.3       andrew     54: # This maps the status we get from bioctl to something nagios can use
1.1       andrew     55: my %Status_Map = (
1.7       andrew     56:     Online      => 'OK',
                     57:     Offline     => 'CRITICAL',
                     58:     Degraded    => 'CRITICAL',
                     59:     Failed      => 'CRITICAL',
                     60:     Building    => 'WARNING',
                     61:     Rebuild     => 'WARNING',
                     62:     'Hot spare' => 'OK',
                     63:     Unused      => 'OK',
                     64:     Scrubbing   => 'WARNING',
                     65:     Invalid     => 'CRITICAL',
1.1       andrew     66: );
                     67:
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.19    ! andrew     85:     print_revision();
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.15      andrew     98: my $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 ) {
1.19    ! andrew    118:     say 'No results found';
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.19    ! andrew    153: sub parse_bioctl_line {
        !           154:     ($_) = @_;
        !           155:     chomp;
        !           156:
        !           157:     my @o = map { s/^\s+|\s+$//g; $_ } split;
        !           158:     return if $o[0] eq 'Volume';
        !           159:
        !           160:     state $vid = '';
        !           161:     state $controller;
        !           162:
        !           163:     my $index = "$vid.$o[0]";
        !           164:     if ( $o[0] !~ /^\d+$/ ) {
        !           165:         $controller = shift @o;
        !           166:         $vid        = $o[0];
        !           167:         $index      = $vid;
        !           168:     }
1.11      andrew    169:
1.19    ! andrew    170:     return $index, {
        !           171:         controller  => $controller,
        !           172:         volume_id   => $vid,
        !           173:         id          => shift @o,
        !           174:         status      => shift @o,
        !           175:         size        => shift @o,
        !           176:         device      => shift @o,
        !           177:         name        => shift @o,
        !           178:         description => join ' ', @o,
        !           179:     };
1.11      andrew    180: }
                    181:
                    182: sub check_status {
                    183:     my ($volumes) = @_;
                    184:
                    185:     my %states;
1.15      andrew    186:     foreach my $d ( sort keys %{$volumes} ) {
                    187:         foreach my $i ( sort { $a <=> $b } keys %{ $volumes->{$d} } ) {
                    188:             my $volume = $volumes->{$d}->{$i};
                    189:             my $state = $Status_Map{ $volume->{'status'} } || 'UNKNOWN';
                    190:
                    191:             push @{ $states{$state} },
                    192:                 sprintf(
                    193:                 "%5s %-7s %-11s %s",
                    194:                 $volume->{'controller'}, $volume->{'device'},
                    195:                 $volume->{'status'},     $volume->{'name'}
                    196:                 );
1.11      andrew    197:         }
                    198:     }
                    199:     return %states;
                    200: }
                    201:
1.1       andrew    202: sub print_help {
1.9       andrew    203:     print <<"EOL";
1.1       andrew    204: $PROGNAME plugin for Nagios monitors bioctl on OpenBSD
1.2       andrew    205:     $PROGNAME -d <device> [ -d <device2> [ -d ... ] ]
1.1       andrew    206:
                    207: Usage:
                    208:     -d, --device=DEVICE
1.3       andrew    209:         DEVICE to check.  Can be any device that bioctl(8) accepts
1.1       andrew    210:     -h (--help)       usage help
                    211:        -V (--version)    version information
                    212:
                    213: EOL
1.7       andrew    214:
1.19    ! andrew    215:     print_revision();
1.9       andrew    216:
                    217:     print $License;
1.11      andrew    218:
                    219:     return 1;
1.1       andrew    220: }
                    221:
1.9       andrew    222: sub print_revision {
1.19    ! andrew    223:     my $rev = '$Revision: 1.18 $';
1.9       andrew    224:     $rev =~ s/^\D+([\d\.]+)\D+$/v$1/xms;
                    225:
1.19    ! andrew    226:     say "$PROGNAME $rev";
1.11      andrew    227:
                    228:     return 1;
1.9       andrew    229: }

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