#!/usr/bin/perl
#
# This works with Net-SNMP and UCD-SNMP

$host=shift;
$user=shift;
$comm=shift || "public";
$type=shift|| "xml";

$conf='/usr/local/dialup_admin/conf/admin.conf';
open CONF, "<$conf"
	or die "Could not open configuration file\n";
while(<CONF>){
	chomp;
	($key,$val)=(split /:\s*/,$_);
	$snmp_type = $val if ($key eq 'general_snmp_type');
	$snmpget = $val if ($key eq 'general_snmpget_command');
	$snmpwalk = $val if ($key eq 'general_snmpwalk_command');
}
close CONF;

die "general_snmp_type directive is not set in admin.conf\n" if ($snmp_type eq '');
die "Could not find snmpwalk binary. Please make sure that the \$snmpwalk variable points to the right location\n" if (! -x $snmpwalk);

if ($snmp_type = 'ucd') {
	$snmpgetcmd="$snmpget $host $comm";
	$snmpwalkcmd="$snmpwalk $host $comm";
}
if ($snmp_type = 'net') {
	$snmpgetcmd="$snmpget -v 1 -c $comm $host";
	$snmpwalkcmd="$snmpwalk -v 1 -c $comm $host";
}
#DEBUG#print "$snmpwalkcmd\n"; print "$snmpgetcmd\n";
@ModulationScheme = (
	"error",
	"unknown",
	"bell103a",
	"bell212a",
	"v21",
	"v22",
	"v22bis",
	"v32",
	"v32bis",
	"vfc",
	"v34",
	"v17",
	"v29",
	"v33",
	"k56flex",
	"v23",
	"v32terbo",
	"v34plus",
	"v90",
	"v27ter",
);
		  
@Protocol = (
	"error",
	"normal",
	"direct",
	"reliableMNP",
	"reliableLAPM",
	"syncMode",
	"asyncMode",
	"ara10",
	"ara20",
	"unknown",
);
#DEBUG#print "$snmpwalkcmd enterprises.9.2.9.2.1.18 | grep $user\n";		 
$modem=`$snmpwalkcmd enterprises.9.2.9.2.1.18 | grep $user`;
if($modem=~/enterprises\.9\.2\.9\.2\.1\.18\.(\d+) =/){
  $modem=$1;
  $slot=(1+int($modem/120));
  $port=$modem%120-1;
  $modem="$slot.$port";

#DEBUG#print "$snmpgetcmd enterprises.9.9.47.1.3.1.1.9.$modem\n";		 
  $duration=`$snmpgetcmd enterprises.9.9.47.1.3.1.1.9.$modem` or die "No MIB\n";
  $duration=~/\) (.*)\./;
  $duration=$1;

#DEBUG#print "$snmpgetcmd enterprises.9.9.47.1.3.1.1.12.$modem\n";		 
  $modulation=`$snmpgetcmd enterprises.9.9.47.1.3.1.1.12.$modem` or die "No MIB\n";
  $modulation=~/ \= (\d+)/;
  $modulation=$ModulationScheme[$1];

#DEBUG#print "$snmpgetcmd enterprises.9.9.47.1.3.1.1.13.$modem\n";		 
  $protocol=`$snmpgetcmd enterprises.9.9.47.1.3.1.1.13.$modem` or die "No MIB\n";
  $protocol=~/ \= (\d+)/;
  $protocol=$Protocol[$1];

#DEBUG#print "$snmpgetcmd enterprises.9.9.47.1.3.1.1.14.$modem\n";		 
  $txrate=`$snmpgetcmd enterprises.9.9.47.1.3.1.1.14.$modem` or die "No MIB\n";
  $txrate=~/Gauge32\: (\d+)/;
  $txrate=$1;

#DEBUG#print "$snmpgetcmd enterprises.9.9.47.1.3.1.1.15.$modem\n";		 
  $rxrate=`$snmpgetcmd enterprises.9.9.47.1.3.1.1.15.$modem` or die "No MIB\n";
  $rxrate=~/Gauge32\: (\d+)/;
  $rxrate=$1;

#DEBUG#print "$snmpgetcmd enterprises.9.9.47.1.3.1.1.17.$modem\n";		 
  $rxsignal=`$snmpgetcmd enterprises.9.9.47.1.3.1.1.17.$modem` or die "No MIB\n";
#  $rxsignal=~ s/INTEGER\://;
  $rxsignal=~/ \= (.*)\n/;
  $rxsignal=$1;

  if($type eq "xml"){
    print "<User>$user</User>\n";
    print "\t<Duration>$duration</Duration>\n";
    print "\t<Modulation>$modulation</Modulation>\n";
    print "\t<Protocol>$protocol</Protocol>\n";
    print "\t<TxRate>$txrate</TxRate>\n";
    print "\t<RxRate>$rxrate</RxRate>\n";
    print "\t<RxSignal>$rxsignal dBm</RxSignal>\n\n";
  }else{
    printf("%14s\t%s\n","User",$user);
    printf("%14s\t%s\n","Duration",$duration);
    printf("%14s\t%s\n","Modulation",$modulation);
    printf("%14s\t%s\n","Protocol",$protocol);
    printf("%14s\t%s\n","TxRate",$txrate);
    printf("%14s\t%s\n","RxRate",$rxrate);
    printf("%14s\t%s dBm\n\n","RxSignal",$rxsignal);
  }
}
