From 51325104cb33093a02084fbcb176f86529811a25 Mon Sep 17 00:00:00 2001 From: Russ Allbery Date: Wed, 20 Oct 2010 10:13:45 -0700 Subject: [PATCH] Single partition checking and formatted sizes in check_afsspace Support checking a single partition in check_afsspace and print more verbose information about total, used, and free space in that mode. Format partition sizes using Number::Format if available. Based on work by Steve Rader. --- NEWS | 5 ++++ README | 3 ++ check_afsspace | 81 +++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 75 insertions(+), 14 deletions(-) diff --git a/NEWS b/NEWS index 6a32c08..b2e9d7b 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,11 @@ afs-monitor 2.0 (unreleased) Initial tarball release, based on check_afsspace 1.16, check_bos 1.7, check_rxdebug 1.11, and check_udebug 1.3. + Support checking a single partition in check_afsspace and print more + verbose information about total, used, and free space in that mode. + Format partition sizes using Number::Format if available. Based on + work by Steve Rader. + If the salvager is running (such as when started manually with bos salvage), check_bos now reports a warning stating that, rather than a critical error showing the auxiliary status line. Reported by Steve diff --git a/README b/README index 08aea05..00df0ec 100644 --- a/README +++ b/README @@ -65,6 +65,9 @@ REQUIREMENTS bos, rxdebug, and udebug) and expect them to be in either /usr/bin or in /usr/local/bin. + check_afsspace will use Number::Format, if available, to format sizes + with IEC 60027 prefixes. + INSTALLATION All that's required for installation is to copy the check_* scripts into diff --git a/check_afsspace b/check_afsspace index 29da098..091cf50 100755 --- a/check_afsspace +++ b/check_afsspace @@ -25,6 +25,16 @@ use strict; use Getopt::Long qw(GetOptions); +# Use Number::Format if it's available, but don't require it. +our $FORMAT = 0; +eval { + require Number::Format; + Number::Format->import ('format_bytes'); +}; +unless ($@) { + $FORMAT = 1; +} + ############################################################################## # Site configuration ############################################################################## @@ -56,14 +66,15 @@ sub syntax { } # Parse command line options. -my ($help, $host, $version); +my ($help, $host, $partition, $version); Getopt::Long::config ('bundling', 'no_ignore_case'); -GetOptions ('c|critical=i' => \$CRITICAL, - 'H|hostname=s' => \$host, - 'h|help' => \$help, - 't|timeout=i' => \$TIMEOUT, - 'V|version' => \$version, - 'w|warning=i' => \$WARNINGS) +GetOptions ('c|critical=i' => \$CRITICAL, + 'H|hostname=s' => \$host, + 'h|help' => \$help, + 'p|partition=s' => \$partition, + 't|timeout=i' => \$TIMEOUT, + 'V|version' => \$version, + 'w|warning=i' => \$WARNINGS) or syntax ("invalid option"); if ($help) { print "Feeding myself to perldoc, please wait....\n"; @@ -78,6 +89,10 @@ syntax ("host to check not specified") unless (defined $host); if ($WARNINGS > $CRITICAL) { syntax ("warning level $WARNINGS greater than critical level $CRITICAL"); } +if ($partition) { + $partition = "/vicep$partition" if length ($partition) <= 2; + $partition = "/$partition" if $partition !~ m%^/%; +} # Set up the alarm. $SIG{ALRM} = sub { @@ -90,20 +105,45 @@ alarm ($TIMEOUT); # partition. Accumulate critical messages in @critical and warnings in # @warnings. Accumulate all percentages in @all. my (@critical, @warnings, @all); -my @data = `$VOS partinfo '$host' 2> /dev/null`; +my $command = "$VOS partinfo -server '$host'"; +$command .= " -partition $partition" if defined ($partition); +my @data = `$command 2> /dev/null`; if ($? != 0) { print "AFS CRITICAL - cannot contact server\n"; exit 2; } +$partition .= ':'; for (@data) { - my ($partition, $free, $total) = (split)[4,5,11]; + my ($part, $free, $total) = (split)[4,5,11]; + next if (defined ($partition) and $part ne $partition); my $percent = int ((($total - $free) / $total) * 100); + my $used = $total - $free; + if ($FORMAT) { + $total = format_bytes ($total, mode => 'iec'); + $free = format_bytes ($free, mode => 'iec'); + $used = format_bytes ($used, mode => 'iec'); + } + my $summary; + if ($partition) { + $summary = "$part$percent% used" + . " ($total total, $used used, $free free)"; + } else { + $summary = "$part$percent% (free $free)"; + } if ($percent >= $CRITICAL) { - push (@critical, "$partition$percent% (free $free)"); + push (@critical, $summary); } elsif ($percent >= $WARNINGS) { - push (@warnings, "$partition$percent% (free $free)"); + push (@warnings, $summary); } - push (@all, "$partition$percent%"); + if ($partition) { + push (@all, $summary); + } else { + push (@all, "$part$percent%"); + } +} +unless (@all) { + print "AFS CRITICAL - no partition found\n"; + exit 2; } # Exit with the appropriate error messages. @@ -129,7 +169,7 @@ check_afsspace - Monitor AFS disk space usage under Nagios =head1 SYNOPSIS B [B<-hV>] [B<-c> I] [B<-w> I] - [B<-t> I] B<-H> I + [B<-p> I] [B<-t> I] B<-H> I =head1 DESCRIPTION @@ -149,6 +189,10 @@ the critical errors if any, otherwise giving the warnings if any, otherwise listing in an abbreviated form the percentage free space for all partitions. +The check can be limited to a single partition by specifying that +partition with the B<-p> option. In this case, more verbose information +about the total, used, and free space is given in the one line of output. + =head1 OPTIONS =over 4 @@ -168,6 +212,14 @@ option is required. Print out this documentation (which is done simply by feeding the script to C). +=item B<-p> I, B<--partition>=I + +Limit the results to the specified partition. The partition can be given +as the partition letter (C, for example) or the full partition name +(C), with or without the leading slash. If this option is given, +only that partition will be checked and more verbose information about +total, used, and free space will be printed. + =item B<-t> I, B<--timeout>=I Change the timeout for the C command. The default timeout @@ -223,7 +275,8 @@ tools page at L. Originally written by Susan Feng for use with mon. Updated by Quanah Gibson-Mount to work with Nagios, and then further updated by Russ Allbery to support more standard options and to use a more -uniform coding style. +uniform coding style. Support for checking a single partition based on +work by Steve Rader. =head1 COPYRIGHT AND LICENSE -- 2.39.5