#
# Host is $args{"host"}, host alias is $args{"alias"}
#
my $host = $args{"host"};
my $alias = $args{"alias"};
my $snmp;

#
# Create an evaluator check object for "snmpvar"
#
$args{"_eval"} = "snmpvar";
do "_evaluator";

#
# create snmp query object
#
my $community = $args{"community"}?$args{"community"}:"public";
unless( $snmp = $memory{"snmp_$args{host}_$community_$args{frequency}"} ) {
    &uxmon::load_module( "snmp" );
    &uxmon::checker(
	$memory{"snmp_$args{host}_$community_$args{frequency}"} = $snmp = Monitor::snmp->new( $host, $community )
    );
    $snmp->sched_frequency( $args{"frequency"} );
}


#
# register all the necessary variables our SNMP object should poll
#
my $query = $args{"query"};
foreach my $query (split( ",", $args{"query"})) {
    $snmp->add_check( $query );
}

{
    #
    # "compile" the arguments in uxmon-net
    #
    my $red = _compile( $args{"red"} );
    my $yellow = _compile( $args{"yellow"} );
    my $green = _compile( $args{"green"} );
    my $default;

    ($default = 1) if( ! defined $yellow );
    ($default = 0) if( ! defined $red );
    ($default = 3) if( ! defined $green );

    #
    # add an evaluator based check against the queried variables
    #
    $args{"_evaluator"}->add_check( ($args{"check"}?$args{"check"}:"snmp"), sub {
	my( $vars ) = @_;

	my $status = 2;
	my $text;
print "red: $red\nyellow: $yellow\ngreen:$green\n\n";
	eval {
	    if( $red && ($text = eval( $red )) ) {
		$status = 0;
   	    }
	    elsif( $yellow && ($text = eval( $yellow )) ) {
		$status = 1;
   	    }
	    elsif( $green && ($text = eval( $green )) ) {
		$status = 3;
   	    }
	    else {
		$status = $default;
		$text = "snmpvar OK";
	    }
	};
	($text = "configuration error") unless( $text );
	return( $status, $text );
      } );
}


sub _compile {
    my( $expr ) = @_;

    foreach my $query (split( ",", $args{"query"})) {
	$expr =~ s/\$$query/\(\$vars->{"snmp.$query"}\)/g;
    }
    return $expr;
}

1;
