use strict;
use NetSNMP::agent (':all');
use NetSNMP::ASN qw(ASN_OCTET_STR);
use vars qw/$agent/;

my $reg_oid=".1.3.6.1.2.1.31.1.1.1.18";
my $base_oid=new NetSNMP::OID($reg_oid);

my $debug=0;


sub myhandler {
	my @quagga_descr = `/usr/local/bin/vtysh -e \"sh int desc\"`;

	my $oid_next;
	my $oid_val; 
	my $ifindex_count=0;
	foreach my $line (@quagga_descr){
		if ($ifindex_count>0){
			my ($ifname,$status,$protcol,$descr)=split(/\s+/,$line);
			my $this_oid = $base_oid + $ifindex_count; 
			my $next_oid = $base_oid + ( $ifindex_count + 1 ); 			
			$oid_val->{$this_oid}=$descr;			
			$oid_next->{$this_oid} = $next_oid;
		}
		$ifindex_count++;
	}

	
	my ($handler, $registration_info, $request_info, $requests) = @_;
	my $request;
	for($request = $requests; $request; $request = $request->next()) {
		my $oid = $request->getOID();		
		if ($request_info->getMode() == MODE_GET) {
			print STDERR "Called: GET $oid\n" if $debug;
			$request->setValue(ASN_OCTET_STR, $oid_val->{$oid}) if defined $oid_val->{$oid};
		} elsif ($request_info->getMode() == MODE_GETNEXT) {
			print STDERR "Called: GETNEXT $oid, VALUE: ".$oid_val->{$oid}." \n" if $debug;
			my $next_oid;
			if (defined($oid_next->{$oid})) { 				
				$next_oid=$oid_next->{$oid};
			} elsif ($oid <= $base_oid) {
				$next_oid=$base_oid + '.1';
			}
			$request->setOID($next_oid);
			$request->setValue(ASN_OCTET_STR, $oid_val->{$next_oid}) if defined $oid_val->{$next_oid};				
		}
		
	}	
}

$agent->register("quagga_descriptions", "$reg_oid", \&myhandler);


