# This check check the amount of free diskspace for all of the mounted disk
# partitions and the the amount of free swapspace.
-# $Id: check_disk,v 1.5 2002/05/10 21:07:43 sljohnson Exp $
+# $Id: check_disk,v 1.6 2005/09/13 14:11:00 willamowius Exp $
+
+# v1.2 02/26/04 Alan Premselaar <alien@12inch.com>
+#
+# added additional criteria to reporting red.
+# if filesystem is (x)GB and even though it is 98% used
+# if it has (X)GB free, it's not really critical.
+# so the logic is, if the size of the filesystem is GB
+# and the available space is GB then even 98% full is ok, although
+# if the size is GB and the avail is MB then it's critical.
+
+# v1.3 02/26/04 Alan Premselaar <alien@12inch.com>
+#
+# add code to correctly determine if GIG free even if
+# human readable output from DF is not returned.
+# assumes 1-K block size reports from DF
+
+# v1.4 02/26/04 Alan Premselaar <alien@12inch.com>
+#
+# minor bug fixes with regex
+
+# v1.5 02/26/04 Alan Premselaar <alien@12inch.com>
+#
+# add 'human readable' output for swap space information
use Spong::SafeExec qw(safe_exec);
+use constant REALGIG => 1073741824;
+use constant GIG => 1048576; # DF will normally return 1-K blocks
+use constant KILO => 1024;
+
sub check_disk {
+
+ # nested sub routine
+
+ sub get_size {
+ my ($size) = @_;
+
+ my ($gigs,$megs,$kilo);
+ my $retval = "";
+
+ return("0") if (!$size);
+
+ $kilo = $size / KILO;
+
+ $megs = ($size / GIG);
+
+ $gigs = ($size / REALGIG);
+
+ $retval = sprintf("%.0fK",$kilo) if ($kilo >= 1);
+ $retval = sprintf("%.0fM",$megs) if ($megs >= 1);
+ $retval = sprintf("%.1fG",$gigs) if ($gigs >= 1);
+
+ return($retval);
+ }
+
+
my $color = "green";
my( $summary, $message, $check, @problems, $large, $lpercent, $page );
+ my ($size,$used,$avail);
+ my $old_regex = '(\S+)\s.*?\s(\d+)\%\s+[^/]*(/.*)';
+ my $human_regex = '(\S+)\s.*?([0-9.]+[KMG]?)\s+([0-9.]+[KMG]?)\s+([0-9.]+[KMG]?)\s+(\d+)\%\s+[^/]*(/.*)';
+ my $norm_regex = '(\S+)\s.*?(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\%\s+[^/]*(/.*)';
+
+ my ($myregex,$humanform) = ();
+
+ if ($DF =~ /\-h/) {
+ $myregex = $human_regex;
+ $humanform = 1;
+ } else {
+ $myregex = $norm_regex;
+ }
+
my @msg = safe_exec($DF);
$message = shift(@msg); # Header
DFPIPE:
foreach ( @msg ) {
- if( m!^(\S+)\s.*?\s(\d+)\%\s+[^/]*(/.*)$! ) {
- my( $rawfs, $percent, $name ) = ( $1, $2, $3 );
+# if( m!^(\S+)\s.*?\s(\d+)\%\s+[^/]*(/.*)$! ) {
+ if ( m!^$myregex$! ) {
+# if ($humanform) {
+# my ($rawfs,$size,$used,$avail,$percent,$name) = ( $1, $2, $3, $4, $5, $6 );
+# } else {
+# my( $rawfs, $percent, $name ) = ( $1, $2, $3 );
+# }
+ my ($rawfs,$size,$used,$avail,$percent,$name) = ( $1, $2, $3, $4, $5, $6 );
my $skip;
my $panic = $DFCRIT{$name} || $DFCRIT{$rawfs} || $DFCRIT{"ALL"};
if( $percent > $lpercent ) { $lpercent = $percent; $large = $name; }
+ my $gig_free = ( ($size =~ /(\d+)G/) && ($used =~ /(\d+)G/) ) if ($humanform);
+ my $gig_free = ( ($size / GIG > 1) && ($used / GIG > 1) ) if (!$humanform);
+
$message .= $_;
- if( $percent >= $panic ) {
+ if( $percent >= $panic && !$gig_free ) {
$color = "red";
push( @problems, "$name $percent" );
} elsif( $percent >= $warn ) {
my $warn = $DFWARN{"page"} || $DFWARN{"ALL"};
($msg, $page) = &get_swap;
+
+ if ($humanform) {
+
+ $msg =~ /(\d+)\s+(\d+)\s+(\d+)/;
+ my ($total,$used,$avail) = ($1,$2,$3);
+
+ my $total_gig = get_size($total);
+ my $used_gig = get_size($used);
+ my $avail_gig = get_size($avail);
+
+ $msg = sprintf("Swap: %s %s %s",$total_gig,$used_gig,$avail_gig);
+ }
+
$message .= "\nSwap Space\n$msg";
$page =~ s/.*\s(\d+)%.*/$1/s;