SnmpTrapd.java.txt
上传用户:aonuowh
上传日期:2021-05-23
资源大小:35390k
文件大小:2k
- /* $Id: SnmpTrapd.java,v 1.1 2002/06/15 14:42:11 ram Exp $ */
- /*
- * @(#)SnmpTrapd.java
- * Copyright (c) 1996-2003 AdventNet, Inc. All Rights Reserved.
- * Please read the associated COPYRIGHTS file for more details.
- */
- /**
- * This is a tutorial example program for receiving traps using
- * the com.adventnet.snmp.snmp2 package of AdventNetSNMP api.
- *
- * The user could run this application by giving the following usage.
- *
- * java SnmpTrapd
- *
- * the application will start receiving traps in the port 8001.
- *
- *
- *
- */
- import java.lang.*;
- import java.util.*;
- import java.net.*;
- import com.adventnet.snmp.snmp2.*;
- public class SnmpTrapd implements SnmpClient
- {
- public static void main(String args[]) {
-
- // Start SNMP API
- SnmpAPI api = new SnmpAPI();
-
- // Open session
- SnmpSession session = new SnmpSession(api);
- session.addSnmpClient(new SnmpTrapd());
- // set local port
- SnmpPDU pdu = new SnmpPDU();
- UDPProtocolOptions option = new UDPProtocolOptions("localhost");
- pdu.setProtocolOptions(option);
- pdu.setLocalPort(8001);
- // Open the session
- try
- {
- session.open();
- }
- catch (SnmpException e )
- {
- System.err.println("Error opening socket: "+e);
- }
- System.out.println("Waiting to receive traps in the port "+option.getLocalPort() +".......");
- }
- public boolean authenticate(SnmpPDU pdu, String community)
- {
- return (pdu.getCommunity().equals(community));
- }
-
- public boolean callback(SnmpSession session, SnmpPDU pdu, int requestID)
- {
- // print the received trap information
- System.out.println("Trap received from: "+pdu.getAddress() +", community: " + pdu.getCommunity());
- System.out.println("Enterprise: " + pdu.getEnterprise());
- System.out.println("Agent: " + pdu.getAgentAddress());
- System.out.println("TRAP_TYPE: " + pdu.getTrapType());
- System.out.println("SPECIFIC NUMBER: " + pdu.getSpecificType());
- System.out.println("Time: " + pdu.getUpTime()+"nVARBINDS:");
- System.out.println(pdu.printVarBinds());
- System.out.println("Continues waiting to receive traps in the port "+options.getLocalPort() +".......");
- return true;
- }
- public void debugPrint(String debugOutput)
- {
- System.out.println(debugOutput);
- return;
- }
- }