Annotation of nagios/check_bioctl/check_bioctl, Revision 1.5
1.1 andrew 1: #!/usr/bin/perl -T
1.5 ! andrew 2: # $RedRiver: check_bioctl,v 1.4 2006/07/31 20:47:07 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',
1.4 andrew 31: Offline => 'CRITICAL',
1.1 andrew 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.5 ! andrew 62: print_revision($PROGNAME,'$Revision: 1.4 $ ');
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;
1.5 ! andrew 139: foreach my $error (sort { $ERRORS{$b} <=> $ERRORS{$a} } keys %ERRORS) {
1.1 andrew 140: if (exists $states{$error}) {
141: $have_results++;
1.5 ! andrew 142: $state = $error if $ERORRS{$state} < $ERRORS{$error};
! 143:
1.1 andrew 144: if (NAGIOS_OUTPUT) {
145: print "$error (" . scalar(@{ $states{ $error } }) . ")";
146: unless ($error eq 'OK') {
147: print '<br>';
148: print map { " - $_<br>" } @{ $states{ $error } };
149: }
150: } else {
151: print "$error (" . scalar(@{ $states{ $error } }) . "):\n";
152: print map { " $_\n" } @{ $states{ $error } };
153: }
154: }
155: }
156: if ($have_results == 0) {
157: print "No results found\n";
158: }
159: exit $ERRORS{$state};
160:
161: sub print_help {
162: print <<EOL;
163: $PROGNAME plugin for Nagios monitors bioctl on OpenBSD
1.2 andrew 164: $PROGNAME -d <device> [ -d <device2> [ -d ... ] ]
1.1 andrew 165:
166: Usage:
167: -d, --device=DEVICE
1.3 andrew 168: DEVICE to check. Can be any device that bioctl(8) accepts
1.1 andrew 169: -h (--help) usage help
170: -V (--version) version information
171:
172: EOL
173:
1.5 ! andrew 174: print_revision($PROGNAME, '$Revision: 1.4 $');
1.1 andrew 175: }
176:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>