Annotation of nagios/check_bioctl/check_bioctl, Revision 1.10
1.1 andrew 1: #!/usr/bin/perl -T
1.10 ! andrew 2: # $RedRiver: check_bioctl,v 1.9 2009/11/09 20:22:43 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.10 ! andrew 30: my $PROGNAME = "check_bioctl";
! 31: my $BIOCTL = '/sbin/bioctl';
! 32:
1.1 andrew 33: use POSIX;
1.10 ! andrew 34: my $PREFIX;
! 35: BEGIN {
! 36: ## no critic 'warnings'
! 37: no warnings 'uninitialized';
! 38: $PREFIX = "${PREFIX}" || '/usr/local'; # Magic for OpenBSD ports tree
! 39: }
! 40: use lib $PREFIX . '/libexec/nagios';
1.9 andrew 41: use utils qw($TIMEOUT %ERRORS &support);
1.1 andrew 42:
1.10 ! andrew 43: $SIG{'ALRM'} = sub {
! 44: print ("ERROR: $PROGNAME timeout\n");
! 45: exit $ERRORS{'UNKNOWN'};
! 46: };
! 47: alarm($TIMEOUT);
! 48:
1.1 andrew 49: use Getopt::Long;
50: Getopt::Long::Configure('bundling');
51:
1.3 andrew 52: # This maps the status we get from bioctl to something nagios can use
1.1 andrew 53: my %Status_Map = (
1.7 andrew 54: Online => 'OK',
55: Offline => 'CRITICAL',
56: Degraded => 'CRITICAL',
57: Failed => 'CRITICAL',
58: Building => 'WARNING',
59: Rebuild => 'WARNING',
60: 'Hot spare' => 'OK',
61: Unused => 'OK',
62: Scrubbing => 'WARNING',
63: Invalid => 'CRITICAL',
1.1 andrew 64: );
65:
1.7 andrew 66: my $state = 'UNKNOWN'; # tells whether the it is warning, critical, or OK
67: my %states; # This stores the count of states;
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.10 ! andrew 85: print_revision( $PROGNAME, '$Revision: 1.9 $ ' );
1.7 andrew 86: exit $ERRORS{'OK'};
1.1 andrew 87: }
88:
1.7 andrew 89: if ( $opt_h || not @devices ) {
90: print_help();
91: exit $ERRORS{'OK'};
1.1 andrew 92: }
93:
94: my %VOLUMES;
1.2 andrew 95: foreach my $device (@devices) {
1.8 andrew 96: open my $bioctl, '-|', $BIOCTL, $device or die "Couldn't open bioctl: $!";
1.7 andrew 97: my $volume_id;
98:
99: while (<$bioctl>) {
100: chomp;
1.2 andrew 101:
1.7 andrew 102: # Do these by columns cuZ that is the easiest for now
103: my @o = unpack( "A6 A1 A11 A15 A7 A9 A*", $_ );
104: next if $o[0] eq 'Volume';
105:
106: foreach (@o) {
107: s/^\s+//;
108: s/\s+$//;
109: }
110:
111: my ( $controller, $id, $status, $size, $dev, $details, $name ) = @o;
112: my $index = $id;
113: if ($controller) {
114: $volume_id = $id;
115: }
116: else {
117: $index = "$volume_id.$id";
118: }
119:
120: $VOLUMES{$device}{$index} = {
121: type => 'volume',
122: controller => $controller,
123: id => $id,
124: status => $status,
125: size => $size,
126: device => $dev,
127: details => $details,
128: name => $name,
129: };
130:
131: if ( $dev =~ /^\d+:\d+/ ) {
132: $VOLUMES{$device}{$index}{'volume'}
133: = $VOLUMES{$device}{$volume_id};
134: }
135:
136: }
137: close $bioctl;
138: }
139:
140: foreach my $device ( sort keys %VOLUMES ) {
141: foreach my $index ( sort keys %{ $VOLUMES{$device} } ) {
142: my $cur_state
143: = $Status_Map{ $VOLUMES{$device}{$index}{'status'} }
144: ? $Status_Map{ $VOLUMES{$device}{$index}{'status'} }
145: : 'UNKNOWN';
146:
147: if ( $VOLUMES{$device}{$index}{'device'} =~ /^\d+:\d/ ) {
148: push @{ $states{$cur_state} },
149: sprintf(
150: "%5s %-7s %-11s %s",
151: $VOLUMES{$device}{$index}{'volume'}{'controller'},
152: $VOLUMES{$device}{$index}{'device'},
153: $VOLUMES{$device}{$index}{'status'},
154: $VOLUMES{$device}{$index}{'name'}
155: );
156: }
157: else {
158: push @{ $states{$cur_state} },
159: sprintf( "%5s %-7s %s",
160: $VOLUMES{$device}{$index}{'controller'},
161: $VOLUMES{$device}{$index}{'device'},
162: $VOLUMES{$device}{$index}{'status'} );
163: }
164: }
1.1 andrew 165: }
166:
167: my $have_results = 0;
1.8 andrew 168: $state = 'OK';
1.7 andrew 169: foreach my $error ( sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS ) {
170: if ( exists $states{$error} ) {
171: $have_results++;
1.6 andrew 172: $state = $error if $ERRORS{$state} < $ERRORS{$error};
1.5 andrew 173:
1.7 andrew 174: if (NAGIOS_OUTPUT) {
175: print "$error (" . scalar( @{ $states{$error} } ) . ")";
1.8 andrew 176: if ( $error ne 'OK' ) {
1.7 andrew 177: print '<br>';
178: print map {" - $_<br>"} @{ $states{$error} };
179: }
180: }
181: else {
182: print "$error (" . scalar( @{ $states{$error} } ) . "):\n";
183: print map {" $_\n"} @{ $states{$error} };
184: }
185: }
1.1 andrew 186: }
1.7 andrew 187: if ( $have_results == 0 ) {
188: print "No results found\n";
1.1 andrew 189: }
190: exit $ERRORS{$state};
191:
192: sub print_help {
1.9 andrew 193: print <<"EOL";
1.1 andrew 194: $PROGNAME plugin for Nagios monitors bioctl on OpenBSD
1.2 andrew 195: $PROGNAME -d <device> [ -d <device2> [ -d ... ] ]
1.1 andrew 196:
197: Usage:
198: -d, --device=DEVICE
1.3 andrew 199: DEVICE to check. Can be any device that bioctl(8) accepts
1.1 andrew 200: -h (--help) usage help
201: -V (--version) version information
202:
203: EOL
1.7 andrew 204:
1.10 ! andrew 205: print_revision( $PROGNAME, '$Revision: 1.9 $' );
1.9 andrew 206:
207: print $License;
1.1 andrew 208: }
209:
1.9 andrew 210:
211: sub print_revision {
212: my ($prog, $rev) = @_;
213: $rev =~ s/^\D+([\d\.]+)\D+$/v$1/xms;
214:
215: print "$prog $rev\n";
216: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>