Annotation of nagios/check_bioctl/check_bioctl, Revision 1.3
1.1 andrew 1: #!/usr/bin/perl -T
1.3 ! andrew 2: # $RedRiver: check_bioctl,v 1.2 2006/07/27 01:08:13 andrew Exp $
1.1 andrew 3: ########################################################################
4: # check_bioctl *** A nagios check for OpenBSD bioctl
5: #
6: # 2006.07.26 #*#*# andrew fresh <andrew@mad-techies.org>
7: ########################################################################
8: # TODO:
9: # Really need real documentation.
10: ########################################################################
11: use strict;
12: use warnings;
13:
14: %ENV = ();
15:
16: use constant NAGIOS_OUTPUT => 1;
17:
18: use POSIX;
19: use lib "/usr/local/libexec/nagios";
20: use utils qw($TIMEOUT %ERRORS &print_revision &support);
21:
22: use Getopt::Long;
23: Getopt::Long::Configure('bundling');
24:
25: my $PROGNAME = "check_bioctl";
26: my $BIOCTL = '/sbin/bioctl';
27:
1.3 ! andrew 28: # This maps the status we get from bioctl to something nagios can use
1.1 andrew 29: my %Status_Map = (
30: Online => 'OK',
31: Offline => 'WARNING',
32: Degraded => 'CRITICAL',
33: Failed => 'CRITICAL',
34: Building => 'WARNING',
35: Rebuild => 'WARNING',
36: 'Hot spare' => 'OK',
37: Unused => 'OK',
38: Scrubbing => 'WARNING',
39: Invalid => 'CRITICAL',
40: );
41:
42:
43: my $state = 'UNKNOWN'; # tells whether the it is warning, critical, or OK
44: my %states; # This stores the count of states;
1.2 andrew 45: my @devices;
1.1 andrew 46: my $opt_h;
47: my $opt_V;
48:
49: #Option checking
50: my $status = GetOptions(
51: "version|V" => \$opt_V,
52: "help|h" => \$opt_h,
1.2 andrew 53: "device|d=s" => \@devices,
1.1 andrew 54: );
55:
56: if ($status == 0) {
57: print_help() ;
58: exit $ERRORS{'OK'};
59: }
60:
61: if ($opt_V) {
1.3 ! andrew 62: print_revision($PROGNAME,'$Revision: 1.2 $ ');
1.1 andrew 63: exit $ERRORS{'OK'};
64: }
65:
1.2 andrew 66: if ($opt_h || not @devices) {
1.1 andrew 67: print_help();
68: exit $ERRORS{'OK'};
69: }
70:
71: my %VOLUMES;
1.2 andrew 72: foreach my $device (@devices) {
73: open my $bioctl, "-|", $BIOCTL, $device or die "Couldn't open bioctl: $!";
1.3 ! andrew 74: my $volume_id;
1.2 andrew 75:
76: while (<$bioctl>) {
77: chomp;
78: # Do these by columns cuZ that is the easiest for now
79: my @o = unpack("A6 A1 A11 A15 A7 A9 A*", $_);
80: next if $o[0] eq 'Volume';
81:
82: foreach (@o) {
83: s/^\s+//;
84: s/\s+$//;
85: }
1.1 andrew 86:
1.3 ! andrew 87: my ($controller, $id, $status, $size, $dev, $details, $name) = @o;
1.2 andrew 88: my $index = $id;
1.3 ! andrew 89: if ($controller) {
1.2 andrew 90: $volume_id = $id;
91: } else {
92: $index = "$volume_id.$id";
93: }
1.1 andrew 94:
1.3 ! andrew 95: $VOLUMES{$device}{$index} = {
1.2 andrew 96: type => 'volume',
1.1 andrew 97: controller => $controller,
1.2 andrew 98: id => $id,
1.1 andrew 99: status => $status,
100: size => $size,
101: device => $dev,
102: details => $details,
103: name => $name,
1.2 andrew 104: };
105:
106: if ($dev =~ /^\d+:\d+/) {
1.3 ! andrew 107: $VOLUMES{$device}{$index}{'volume'} =
! 108: $VOLUMES{$device}{$volume_id};
1.1 andrew 109: }
110:
111: }
1.2 andrew 112: close $bioctl;
1.1 andrew 113: }
114:
1.3 ! andrew 115: foreach my $device (sort keys %VOLUMES) {
! 116: foreach my $index (sort keys %{ $VOLUMES{$device} }) {
! 117: my $cur_state = $Status_Map{ $VOLUMES{$device}{$index}{'status'} } ?
! 118: $Status_Map{ $VOLUMES{$device}{$index}{'status'} } :
1.1 andrew 119: 'UNKNOWN';
1.2 andrew 120:
1.3 ! andrew 121: if ($VOLUMES{$device}{$index}{'device'} =~ /^\d+:\d/) {
1.2 andrew 122: push @{ $states{$cur_state} }, sprintf("%5s %-7s %-11s %s",
1.3 ! andrew 123: $VOLUMES{$device}{$index}{'volume'}{'controller'},
! 124: $VOLUMES{$device}{$index}{'device'},
! 125: $VOLUMES{$device}{$index}{'status'},
! 126: $VOLUMES{$device}{$index}{'name'}
1.2 andrew 127: );
128: } else {
129: push @{ $states{$cur_state} }, sprintf("%5s %-7s %s",
1.3 ! andrew 130: $VOLUMES{$device}{$index}{'controller'},
! 131: $VOLUMES{$device}{$index}{'device'},
! 132: $VOLUMES{$device}{$index}{'status'}
1.2 andrew 133: );
134: }
1.1 andrew 135: }
136: }
137:
138: my $have_results = 0;
139: foreach my $error (sort { $ERRORS{$a} <=> $ERRORS{$b} } keys %ERRORS) {
140: if (exists $states{$error}) {
141: $have_results++;
142: $state = $error;
143: }
144: }
145: foreach my $error (sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS) {
146: if (exists $states{$error}) {
147: if (NAGIOS_OUTPUT) {
148: print "$error (" . scalar(@{ $states{ $error } }) . ")";
149: unless ($error eq 'OK') {
150: print '<br>';
151: print map { " - $_<br>" } @{ $states{ $error } };
152: }
153: } else {
154: print "$error (" . scalar(@{ $states{ $error } }) . "):\n";
155: print map { " $_\n" } @{ $states{ $error } };
156: }
157: }
158: }
159: if ($have_results == 0) {
160: print "No results found\n";
161: }
162: exit $ERRORS{$state};
163:
164: sub print_help {
165: print <<EOL;
166: $PROGNAME plugin for Nagios monitors bioctl on OpenBSD
1.2 andrew 167: $PROGNAME -d <device> [ -d <device2> [ -d ... ] ]
1.1 andrew 168:
169: Usage:
170: -d, --device=DEVICE
1.3 ! andrew 171: DEVICE to check. Can be any device that bioctl(8) accepts
1.1 andrew 172: -h (--help) usage help
173: -V (--version) version information
174:
175: EOL
176:
1.3 ! andrew 177: print_revision($PROGNAME, '$Revision: 1.2 $');
1.1 andrew 178: }
179:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>