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

Annotation of nagios/check_bioctl/check_bioctl, Revision 1.20

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

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