# Register the routine with the plugin registry
$CHECKFUNCS{'disk'} = \&check_disk;

# 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 $

use Spong::SafeExec qw(safe_exec);

sub check_disk { 
   my $color    = "green";
   my( $summary, $message, $check, @problems, $large, $lpercent, $page );

   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 );
	 my $skip;

	 my $panic = $DFCRIT{$name} || $DFCRIT{$rawfs} || $DFCRIT{"ALL"};
	 my $warn  = $DFWARN{$name} || $DFWARN{$rawfs} || $DFWARN{"ALL"};

	 foreach $check ( @DFIGNORE ) { 
	    next DFPIPE if $rawfs =~ m{$check};
	    next DFPIPE if $name  =~ m{$check};
	 }

	 if( $percent > $lpercent ) { $lpercent = $percent; $large = $name; }

	 $message .= $_;
	 if( $percent >= $panic ) { 
	    $color = "red"; 
	    push( @problems, "$name $percent" );
	 } elsif( $percent >= $warn )  { 
	    $color = "yellow" unless $color eq "red";
	    push( @problems, "$name $percent" );
	 }
      }
   }
   close( PIPE );

   # This checks the paging space as well.

   if( $LSPS ) {
      my $panic = $DFCRIT{"page"} || $DFCRIT{"ALL"};
      my $warn  = $DFWARN{"page"} || $DFWARN{"ALL"};
      
      $page = `$LSPS 2>&1`;
      $message .= "\n$page";
      $page =~ s/.*\s(\d+)%.*/$1/s;
      
      if( $page >= $panic ) {
	 $color = "red";
	 push( @problems, "page $page" );
      } elsif( $page >= $warn ) {
	 $color = "yellow" unless $color eq "red";
	 push( @problems, "page $page" );
      }
   }

   if (defined &get_swap) {
      my $panic = $DFCRIT{"page"} || $DFCRIT{"ALL"};
      my $warn  = $DFWARN{"page"} || $DFWARN{"ALL"};

      ($msg, $page) = &get_swap;
      $message .= "\nSwap Space\n$msg";
      $page =~ s/.*\s(\d+)%.*/$1/s;

      if( $page >= $panic ) {
	 $color = "red";
	 push( @problems, "page $page" );
      } elsif( $page >= $warn ) {
	 $color = "yellow" unless $color eq "red";
	 push( @problems, "page $page" );
      }
   }

   # Collect the problems, and print a single message describing the problem(s)

   if( $#problems < 0 ) {
      $summary = "largest filesystem $large at $lpercent%";
   } elsif( $#problems == 0 ) {
      my( $disk, $percent) = (split( /\s+/, $problems[0] ));
      $summary = "$disk is $percent% full";
   } else {
      my( $gstr, $sstr );

      $summary = "multiple problems: ";
      foreach( @problems ) {
	 my( $disk, $percent) = (split( /\s+/, $_ ));
	 $summary .= "$disk ($percent%), "; 
      }
      chop $summary; chop $summary;
   }

   &debug( "disk - $color, $summary" );
   &status( $SPONGSERVER, $HOST, "disk", $color, $summary, $message );
}

# I'm include perl code, so I need this
1;
