Annotation of nagios/check_bioctl/check_bioctl, Revision 1.14
1.1 andrew 1: #!/usr/bin/perl -T
1.14 ! andrew 2: # $RedRiver: check_bioctl,v 1.13 2009/11/23 21:57:31 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.14 ! andrew 85: print_revision( $PROGNAME, '$Revision: 1.13 $ ' );
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: }
193:
194: sub check_status {
195: my ($volumes) = @_;
196:
197: my %states;
198: foreach my $device ( sort keys %{$volumes} ) {
199: foreach my $index ( sort keys %{ $volumes->{$device} } ) {
200: my $cur_volume = $volumes->{$device}->{$index};
201: my $cur_state = $Status_Map{ $cur_volume->{'status'} }
202: || 'UNKNOWN';
203:
204: if ( $cur_volume->{'device'} =~ /^\d+:\d/xms ) {
205: push @{ $states{$cur_state} },
206: sprintf(
207: "%5s %-7s %-11s %s",
208: $cur_volume->{'volume'}{'controller'},
209: $cur_volume->{'device'},
210: $cur_volume->{'status'},
211: $cur_volume->{'name'}
212: );
213: }
214: else {
215: push @{ $states{$cur_state} },
216: sprintf( "%5s %-7s %s",
217: $cur_volume->{'controller'},
218: $cur_volume->{'device'},
219: $cur_volume->{'status'} );
220: }
221: }
222: }
223: return %states;
224: }
225:
1.1 andrew 226: sub print_help {
1.9 andrew 227: print <<"EOL";
1.1 andrew 228: $PROGNAME plugin for Nagios monitors bioctl on OpenBSD
1.2 andrew 229: $PROGNAME -d <device> [ -d <device2> [ -d ... ] ]
1.1 andrew 230:
231: Usage:
232: -d, --device=DEVICE
1.3 andrew 233: DEVICE to check. Can be any device that bioctl(8) accepts
1.1 andrew 234: -h (--help) usage help
235: -V (--version) version information
236:
237: EOL
1.7 andrew 238:
1.14 ! andrew 239: print_revision( $PROGNAME, '$Revision: 1.13 $' );
1.9 andrew 240:
241: print $License;
1.11 andrew 242:
243: return 1;
1.1 andrew 244: }
245:
1.9 andrew 246: sub print_revision {
1.11 andrew 247: my ( $prog, $rev ) = @_;
1.9 andrew 248: $rev =~ s/^\D+([\d\.]+)\D+$/v$1/xms;
249:
1.12 andrew 250: print "$prog $rev\n";
1.11 andrew 251:
252: return 1;
1.9 andrew 253: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>