// Home | Go Back //
import com.ireasoning.protocol.*;
import com.ireasoning.protocol.snmp.*;
import com.ireasoning.util.ParseArguments;
import java.io.IOException;
public class snmptrapd extends snmp implements Listener
{
public static void main(String[] args)
{
snmptrapd s = new snmptrapd();
s.parseOptions(args, "snmptrapd", false, true);
s.trapd();
}
void trapd()
{
try
{
System.out.println( "Trap receiver listening on port: " + _port);
SnmpTrapdSession session = new SnmpTrapdSession(_port);
if(_isSnmpV3)
{
session.addV3Params(_user, _authProtocol, _authPassword, _privProtocol,
_privPassword, null);
}
session.addListener(this);
session.waitForTrap();
}
catch(IOException e)
{
System.out.println(e);
e.printStackTrace();
}
}
public void handleMsg(Object msgSender, Msg msg)
{
if(msg.getType() == Msg.ERROR_TYPE)
{
ErrorMsg err = (ErrorMsg) msg;
System.out.println( "Error occurred. Error code: " + err.getErrorCode() + ". Error message: " + err.getErrorString() );
}
else
{
printTrap((SnmpDataType)msg);
}
}
private void printTrap(SnmpDataType t)
{
System.out.println( "Received type:" + t.getType());
if(t.getType() == SnmpConst.V1TRAP)
{
SnmpV1Trap trap = (SnmpV1Trap) t;
String name = MibUtil.translateSnmpV1Trap(trap);
System.out.println("Received SNMPv1 trap:");
if(name != null) System.out.println( "Name:" + name);
System.out.println("Community: " + trap.getCommunity());
System.out.println("Enterprise OID: " + trap.getEnterprise());
System.out.println("Ip Address: " + trap.getIpAddress());
System.out.println("Generic: " + trap.getGenericString());
System.out.println("Specific: " + trap.getSpecific());
System.out.println("TimeStamp: " + trap.getTimestampString());
printVarBinds(trap.getVarBinds());
}
else if(t.getType() == SnmpConst.V2TRAP)
{
SnmpV2Notification trap = (SnmpV2Notification)t;
System.out.println( "Received SNMPv2/v3 trap from " + trap.getIpAddress());
printV2Notification(trap);
}
else if(t.getType() == SnmpConst.INFORM)
{
SnmpV2Notification trap = (SnmpV2Notification)t;
System.out.println( "Received SNMPv2/v3 inform from " + trap.getIpAddress());
printV2Notification(trap);
}
}
private void printV2Notification(SnmpV2Notification trap)
{
int version = trap.getVersion();
String ver = "" + (version == 1 ? (++version) : version); System.out.println("Version: " + ver);
System.out.println( "Community: " + trap.getCommunity() );
System.out.println( "sysUpTime.0: " + trap.getSysUpTimeString() );
String name = MibUtil.translateOID("" + trap.getSnmpTrapOID(), false);
if(name == null)
{
name = "" + trap.getSnmpTrapOID();
}
System.out.println("snmpTrapOID.0: " + name);
printVarBinds(trap.getObjects());
}
private void printVarBinds(SnmpVarBind[] vbs)
{
for (int i = 0; i < vbs.length ; i++ )
{
SnmpVarBind vb = vbs[i];
NameValue nv = MibUtil.translate(vb.getName(), vb.getValue().toString(), false);
if(nv != null)
{
System.out.println( nv.getName() + ": " + nv.getValue());
}
else
{
System.out.println( vb.getName() + ": " + vb.getValue());
}
}
System.out.println();
}
protected void printExample(String programName)
{
System.out.println( "java " + programName );
System.out.println( "java " + programName +
" -v 3 -u newUser -A abc12345 -X abc12345" );
}
protected void parseOptions(String[] args,
String programName,
boolean allowMultipleOIDs,
boolean allowNoOption)
{
super.parseOptions(args, programName, allowMultipleOIDs, allowNoOption);
_port = Integer.parseInt(_parseArgs.getOptionValue('p', "162"));
}
protected ParseArguments newParseArgumentsInstance(String[] args)
{
return new ParseArguments(args, "?ho", "utvaAXxcpm");
}
}