// Home | Go Back //
package agent.mib2;
import java.io.*;
import java.util.*;
import com.ireasoning.util.*;
import com.ireasoning.protocol.*;
import com.ireasoning.protocol.snmp.*;
import javax.management.*;
public class Agent extends SnmpBaseAgent
{
static Agent _agent;
public Agent(MBeanServer server, int port, String configFileName) throws Exception
{
super(server, port, configFileName, false);
_agent = this;
}
public Agent(Object configFileName) throws Exception
{
super(configFileName);
_agent = this;
}
public Agent(DefaultAgentConfig config) throws Exception
{
super(config);
_agent = this;
}
public static Agent getAgent()
{
return _agent;
}
public static boolean isWindows()
{
String os = System.getProperty("os.name").toUpperCase();
return (os.indexOf("WINDOWS") >= 0);
}
public static boolean isLinux()
{
String os = System.getProperty("os.name").toUpperCase();
return (os.indexOf("LINUX") >= 0);
}
public static void main(String[] args)
{
try
{
String os = System.getProperty("os.name").toUpperCase();
if(!isWindows() && !isLinux())
{
System.out.println( "The purpose of this demo is to demononstrate how to create a simple agent. To make it as simple as possible, it makes use of ' \"netstat\" command to figure out interfaces instead of making native calls, so it can only run correctly on Windows or Linux, although snmpagent.jar itself is platform independent.");
return;
}
String configFile = "SnmpAgent.xml";
Agent agent = new Agent(configFile);
agent.start();
sendV1ColdStartTrap();
agent.addAgentEventListener(new ListenerImpl());
}
catch(java.net.BindException be)
{
System.out.println( "Cannot bind to the port.");
System.exit(1);
}
catch(Exception e)
{
Logger.error(e);
System.exit(1);
}
}
protected void setOIDTree()
{
_root = OIDTree.getTree();
}
protected void registerMBeans() throws SnmpException
{
AgentMib.registerMBeans(_mbeanServer, _root);
}
protected void unregisterMBeans()
{
AgentMib.unregisterMBeans();
}
static class ListenerImpl implements Listener
{
public void handleMsg(Object msgSender, Msg msg)
{
AgentEvent e = (AgentEvent) msg;
if(e.getType() == AgentEvent.AUTHENTICATION_FAILURE)
{
System.out.println( "AuthenticationFailed.");
SnmpPdu pdu = e.getPdu();
if(pdu != null)
{
System.out.println( "IpAddress: " + pdu.getIpAddress());
}
}
}
}
}