Annotation of nagios/check_bioctl/check_bioctl, Revision 1.12
1.1 andrew 1: #!/usr/bin/perl -T
1.12 ! andrew 2: # $RedRiver: check_bioctl,v 1.11 2009/11/23 21:45:58 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.12 ! andrew 85: print_revision( $PROGNAME, '$Revision: 1.11 $ ' );
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";
129: while ( my $line = <$bioctl> ) {
130: my ( $i, $item ) = parse_bioctl_line($line);
1.12 ! andrew 131: next unless 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: sub parse_bioctl_line {
156: my ($line) = @_;
157: chomp $line;
158:
159: # Do these by columns cuZ that is the easiest for now
160: my @o = unpack( "A6 A1 A11 A15 A7 A9 A*", $line );
1.12 ! andrew 161: return if $o[0] eq 'Volume';
1.11 andrew 162:
163: foreach (@o) {
164: s/^\s+//xms;
165: s/\s+$//xms;
166: }
167:
168: my ( $controller, $id, $status, $size, $dev, $details, $name ) = @o;
169: my $index = $id;
170: if ($controller) {
171: $vid = $id;
172: }
173: else {
174: $index = "$vid.$id";
175: }
176:
177: my %item = (
178: type => 'volume',
179: controller => $controller,
180: id => $id,
181: status => $status,
182: size => $size,
183: device => $dev,
184: details => $details,
185: name => $name,
186: volume_id => $vid,
187: );
188:
189: return $index, \%item;
190: }
1.12 ! andrew 191: }
1.11 andrew 192:
193: sub check_status {
194: my ($volumes) = @_;
195:
196: my %states;
197: foreach my $device ( sort keys %{$volumes} ) {
198: foreach my $index ( sort keys %{ $volumes->{$device} } ) {
199: my $cur_volume = $volumes->{$device}->{$index};
200: my $cur_state = $Status_Map{ $cur_volume->{'status'} }
201: || 'UNKNOWN';
202:
203: if ( $cur_volume->{'device'} =~ /^\d+:\d/xms ) {
204: push @{ $states{$cur_state} },
205: sprintf(
206: "%5s %-7s %-11s %s",
207: $cur_volume->{'volume'}{'controller'},
208: $cur_volume->{'device'},
209: $cur_volume->{'status'},
210: $cur_volume->{'name'}
211: );
212: }
213: else {
214: push @{ $states{$cur_state} },
215: sprintf( "%5s %-7s %s",
216: $cur_volume->{'controller'},
217: $cur_volume->{'device'},
218: $cur_volume->{'status'} );
219: }
220: }
221: }
222: return %states;
223: }
224:
1.1 andrew 225: sub print_help {
1.9 andrew 226: print <<"EOL";
1.1 andrew 227: $PROGNAME plugin for Nagios monitors bioctl on OpenBSD
1.2 andrew 228: $PROGNAME -d <device> [ -d <device2> [ -d ... ] ]
1.1 andrew 229:
230: Usage:
231: -d, --device=DEVICE
1.3 andrew 232: DEVICE to check. Can be any device that bioctl(8) accepts
1.1 andrew 233: -h (--help) usage help
234: -V (--version) version information
235:
236: EOL
1.7 andrew 237:
1.12 ! andrew 238: print_revision( $PROGNAME, '$Revision: 1.11 $' );
1.9 andrew 239:
240: print $License;
1.11 andrew 241:
242: return 1;
1.1 andrew 243: }
244:
1.9 andrew 245: sub print_revision {
1.11 andrew 246: my ( $prog, $rev ) = @_;
1.9 andrew 247: $rev =~ s/^\D+([\d\.]+)\D+$/v$1/xms;
248:
1.12 ! andrew 249: print "$prog $rev\n";
1.11 andrew 250:
251: return 1;
1.9 andrew 252: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>