# SYNOPSIS
#
# check /etc/dumpdates
#	&uxmon::checker(
#	    $chk = Monitor::dumpdates->new("/etc/dumpdates")
#	);
#
# check for level 7 dumps, report 1 (yellow) for partitions with the last level
# 7 dump older than 4.5 days, 0 (red) for those older than 6 days
#	$chk->add_check(undef, 7, undef, "dump days", 4.5*24*3600, 6*24*3600 );
#
# check for level 0 dumps, report 1 (yellow) for partitions with the last level
# 0 dump older than 30 days, 0 (red) for those older than 37 days
#	$chk->add_check(undef, 0, undef, "dump days", 30*24*3600, 37*24*3600 );

package Monitor::dumpdates;

require 5.002;

use strict;
use Time::Local;

@Monitor::dumpdates::ISA = qw(Monitor::Monitor);

sub new
{
    my ($this, $file ) = @_;
    my  $class = ref($this) || $this;
    my  $self = {};

    bless($self, $class);

    $file = "/etc/dumpdates" unless( $file );
    $self->{"file"} = $file;
    $self->{"lastpoll"} = 0;
    $self->{"freq"} = 3600;

    $self->{"cache"} = {};
    $self->{"htable"} = [];

    return($self);
}



sub frequency {
    my( $self, $freq ) = @_;
    $self->{"freq"} = $freq;
    $self->{"freq"} = 3600 unless( $self->{"freq"} );
}


sub add_check {
    my( $self, $dev, $level, $item, $name, $max1, $max2 ) = @_;
    my( $htable ) = $self->{"htable"};

    ($item = $uxmon::hostname.".bak") unless( $item );
    push( @$htable, [ $dev, $level, $item, $name, $max1, $max2 ] );
}



sub check
{
    my ($self) = @_;
    my( $htable ) = $self->{"htable"};
    my( $now ) = time;
    my( @tab );
    my( $dev, $level, $date, $dev1 );
    my( $wday, $sec,$min,$hours,$mday,$mon,$year );
    my( %mon ) = ( "Jan", 0, "Feb", 1, "Mar", 2, "Apr", 3,
		   "May", 4, "Jun", 5, "Jul", 6, "Aug", 7,
		   "Sep", 8, "Oct", 9, "Nov", 10, "Dec", 11 );
    my( $mounts );

    if( $now - $self->{"lastpoll"} >= $self->{"freq"} ) {
	$self->{"lastpoll"} = $now;
	$mounts = `mount`;
	open( IN, "<".$self->{"file"} );
	while( <IN> ) {
	    chomp;
	    if( /^([^\s\t]+)[\s\t]+([0-9])[\s\t]+(.*)/ ) {
		($dev, $level, $date) = ($1, $2, $3);
		($wday,$mon,$mday,$hours,$min,$sec,$year) = split( /[\s\t:]+/, $date );
		$mon = $mon{$mon};
		$dev1 = $dev;
		$dev1 =~ s/rdsk/dsk/;
		if( $mounts =~ /($dev|$dev1)/ ) {
		    $date = timegm($sec,$min,$hours,$mday,$mon,$year);
		    push( @tab, [$dev,$level,$date] );
		    $self->set_variable( "", "backup.dump.$dev.$level", $date );
		    $self->set_variable( "", "backup.dump.$dev.$level.age", time-$date );
		}
	    }
	}
	close IN;
	$self->{"cache"} = \@tab;
    }

    my( $cache ) = $self->{"cache"};
    my( $item, $name, $max1, $max2, $ref, $orgdev, $orglevel, $diff, $ddiff );
    my( $displaydev );
    my( $check, $i, $j );
    for( $i=0; $i<=$#$htable; $i++ ) {
	$check = $$htable[$i];
	( $dev, $level, $item, $name, $max1, $max2 ) = @$check;
	for( $j=$#$cache; $j>=0; $j-- ) {
	    $ref = $$cache[$j];
	    ($orgdev,$orglevel,$date) = @$ref;
	    next if( $dev && ($dev ne $orgdev) );
	    next if( length($level) && ($level ne $orglevel) );
	    $diff = $now - $date;
	    $ddiff = int($diff/3600/24);
	    print "dump $orgdev $orglevel $date\n" if( $BigSister::common::dl>5 );
	    $displaydev = $orgdev;
	    $displaydev =~ s#.*/##;
	    if( $diff>$max2 ) {
		$self->report( $item, $BigSister::common::status_codes{"red"}, "$name $displaydev.$orglevel >>($ddiff)" );
	    }
	    elsif( $diff>$max1 ) {
		$self->report( $item, $BigSister::common::status_codes{"yellow"}, "$name $displaydev.$orglevel >($ddiff)" );
	    }
	    else {
		$self->report( $item, $BigSister::common::status_codes{"green"}, "$name $displaydev.$orglevel = $ddiff" );
	    }
	}
    }
}

1;
