package Monitor::Evaluator;

require 5.001;
require Exporter;

use strict;
use vars qw(@ISA @EXPORT $VERSION);
use FileHandle;

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

$Monitor::Evaluator::default_statustexts = [
    sub { @_[0]->{"name"}." FAILED: ".@_[1]; },
    sub { @_[0]->{"name"}." CRITICAL: ".@_[1]; },
    sub { @_[0]->{"name"}." UNKNOWN: ".@_[1]; },
    sub { @_[0]->{"name"}." OK: ".@_[1]; },
];


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

    bless($self, $class);

    $self->{"checks"} = [];
    $self->{"host"} = $host;
    $self->{"name"} = $name;
    $self->{"alias"} = $alias;

    $self->{"statustexts"} = $Monitor::Evaluator::default_statustexts;

    return($self);
}


sub status_text {
    my( $self, $status, $sub ) = @_;

    if( length( $status ) > 1 ) {
	$status = $BigSister::common::status_codes{$status};
    }
    $self->{"statustexts"}->[$status] = $sub;
}


sub add_check {
    my( $self, $item, $check ) = @_;
    my( $checks ) = $self->{"checks"};

    push( @$checks, [ $item, $check ] );
}



sub late_check {
    my( $self ) = @_;
    my $checks = $self->{"checks"};
    my $check;

    print "Monitor::Evaluator::check()\n" if( $BigSister::common::dl>4 );
    foreach $check (@$checks) {
	print "  checking ...\n" if( $BigSister::common::dl>6 );
	my( $item, $sub ) = @$check;
	my( $vars ) = $uxmon::monitor_variables{$self->{"host"}};
	unless( $vars ) { $vars = {}; };
	my( $status, $text ) = $sub->($vars, $self);
	if( defined $self->{"statustexts"}->[$status] ) {
	    $text = $self->{"statustexts"}->[$status]->($self,$text,$vars);
	}
	$self->report( $self->{"alias"}.".".$item, $status, $text );
    }
}




1;
