package Statusmon::history;

use Statusmon::Statusmon;

@Statusmon::history::ISA = qw(Statusmon::Statusmon);


sub new
{

    my ($this) = @_;
    my  $class = ref($this) || $this;
    my  $self = {};

    bless($self, $class);

    # I want to receive "statuschange" events
    $self->register( "statuschange" );
    $self->register( "alarm_acking" );
    $self->sched_frequency( 5 );
    $self->{"logfile"} = "$BigSister::common::fs{'var'}/display.history";
    return($self);
}


sub start {
    my( $self ) = @_;
    my $file = $self->getarg( "logfile" );
    if( $file ) {
	$file = BigSister::common::find_file( $file );
	$self->{"logfile"} = $file;
    }
}
    

sub event {
    my( $self, $cmd ) = @_;
    my $text;

    print STDERR "Statusmon::history got an event\n" if( $BigSister::common::dl > 6 );
    if( ($cmd->{"cmd"} eq "statuschange") && $cmd->{"oldstatus"} ) {
	$text = ($cmd->{"host"}).".".($cmd->{"check"}).": ".(
	$cmd->{"oldstatus"})."->".($cmd->{"status"})." ".($cmd->{"rawtext"});
    }
    elsif( $cmd->{"cmd"} eq "ack" ) {
	$text = $cmd->{"host"}.".".$cmd->{"check"}.": ACK ";
	$text .= $cmd->{"duration"}." ".$cmd->{"user"}." ".$cmd->{"mode"};
	$text .= " (".$cmd->{"time"}.") ".(scalar localtime( $cmd->{"time"} ))." ";
	$text .= $cmd->{"comment"};
    }
    else {
	return;
    }
    $text =~ s/\n/\|\>/sg;
    return unless open( _LOG, ">>".($self->{"logfile"}) );
    print _LOG $text."\n";
    close _LOG;
}


sub run {
    my( $self ) = @_;

    return unless open( _LOG, ">>".($self->{"logfile"}) );
    print _LOG "localhost.bbd: checkpoint (".time.") ".(scalar localtime)."\n";
    close _LOG;
}
