#!/usr/bin/perl -w
#
# $Id: DfChk,v 1.5 2002/06/22 18:33:31 cvs Exp $
#
# DfChk: Detect filesystem full or near-full situations
#
# Miguel Armas <kuko@ulpgc.es>
#

#--------------------------------------------[ Initialization Section ]----

use strict;
use Getopt::Std;
use pifia;

#--------------------------------------------[ Configuration Section ]----- 
my $priority = "Emergency";
my $alarm = "DfChk";
my $remind = <#$remind#>;   # Seconds

#--------------------------------------------[ Code Section ]----

# proactive flag indicates if we should take proactive measures, that is,
# install/upgrade packages or just notify. Default is 0 but the global
# variable proactive takes precedence
my $proactive='<#$proactive#>';

# Set a safe PATH
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:/usr/sbin:/root/bin";

# Option declaration
use vars qw($opt_v $opt_n $opt_p $opt_q);
getopts('vnpq');

# Verbosity level. If not verbose, we shouldn't write any output unless
# there is a problem. If quiet is set, we won't write ANY output (even if
# there where errors). verbose has precedence
my $quiet = 1 if ($opt_q);
my $verbose = 1 if ($opt_v);
$quiet = 0 if ($verbose);

# proactive flag handling (parameter has precedence over global variable)
$proactive=1 if ($opt_p);
$proactive=0 if ($opt_n);

my @output=`df -h`;
use vars qw(%lastnotify %usage);

# Read object files
my @fsentries = readObjectFiles($alarm);

perseval {
   foreach my $fs (@output) {
        my ($regexp,$threshold,$fsentry);
        chomp $fs;
        next if ($fs !~ /^\//);
        # Get only fs name and usage
        my @tmp = split(/\s+/,$fs);
        my $name = $tmp[$#tmp];
        my $usage = $tmp[$#tmp-1];
        $usage =~ s/%//;
        # Try to find a regex that matchs...
        for $fsentry (@fsentries) {
	    chomp $fsentry;
	    ($regexp,$threshold) = split(/\s+/,$fsentry);
	    if ("$name" =~ /$regexp/) {
		print "threshold for $name: $threshold \n" if ($verbose);
		last;
	    };
	}
        if ((!$threshold) || ($usage < $threshold)) {
           # Set usage if it exist (are we recovered??)
           if (exists $usage{$name}) {
              $usage{$name} = $usage;
           }
           print "Usage of \"$name\" is $usage\n" if ($verbose);
           next;
        }
        # Soooo, we have a fs with usage over threshold...
        # If we haven't already sent an alarm, do it now
        if (!exists $usage{$name}) {
           print "FirstTime: Usage of \"$name\" is $usage \n";
           # We need the following trick because perl tie can't modify 
           # multidimensional hashes
           $usage{$name} = $usage;
           $lastnotify{$name} = time();
           next;
        }
        # We already sent an alarm. Only send it again if usage has grown or 
        # the remind time has passed
        if ($usage > $usage{$name}) {
           print "Usage of \"$name\" is $usage (has grown)\n";
           $usage{$name} = $usage;	
           $lastnotify{$name} = time();
           next;
        }
        if (needNotify($name,$remind)) {
	   print "Reminder: Usage of \"$name\" is $usage \n";
           $lastnotify{$name} = time();
           next;     
        }
   }
} preserve '%lastnotify', '%usage';
