package Requester::snmp;

use strict;
use snmp;

@Requester::snmp::ISA = qw(Requester::Requester);

my $snmp_requester = new Requester::snmp;

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

    bless($self, $class);
    $self->register( "snmp" );
    return $self;
}


sub request {
    my( $self, $env, $domain, $variables ) = @_;

    my @gets;
    my @walks;
    my $community = $env->expand_value( "community" );
    if( ! defined $community ) {
        $community = "public";
    }
    foreach my $variable (@$variables) {
        if( $variable =~ /^@/ ) {
	    push( @walks, $' );
	}
	else {
	    push( @gets, $variable );
	}
    }
    my $host = $env->{"nodeinstance"}->{"host"};

    my %result = ();
    if( @gets ) {
	my $time = time;
	my @getresults = snmp::get( $community, $host, @gets ); 
	return() unless( @getresults );
	$time = (time + $time) / 2;
	for( my $i=0; $i<=$#gets; $i++ ) {
	    $result{$gets[$i]} = { 0 => {
	        "time" => $time,
		"value" => $getresults[$i]
	    }};
	}
    }

    foreach my $walk (@walks) {
	my $time = time;
        my @results = snmp::walk( $community, $host, $walk );
	return( ) unless( @results );
	my %walkresults = ();
	$time = (time + $time) / 2;
	foreach my $walk (@results) {
   	    if( $walk =~ /^(\d+):/ ) {
 		$walkresults{$1} = {
		    "time" => $time,
		    "value" => $'
		};
	    }
	}
	$result{"\@$walk"} = \%walkresults;
    }
    return \%result;
}




1;

