1 20 package org.jboss.jmx.adaptor.snmp.trapd; 21 22 import java.net.InetAddress ; 23 24 import org.jboss.logging.Logger; 25 import org.opennms.protocols.snmp.SnmpOctetString; 26 import org.opennms.protocols.snmp.SnmpPduPacket; 27 import org.opennms.protocols.snmp.SnmpPduRequest; 28 import org.opennms.protocols.snmp.SnmpPduTrap; 29 import org.opennms.protocols.snmp.SnmpTrapHandler; 30 import org.opennms.protocols.snmp.SnmpTrapSession; 31 import org.opennms.protocols.snmp.SnmpVarBind; 32 33 41 public class TrapReceiver 42 implements SnmpTrapHandler 43 { 44 45 protected final Logger log; 46 47 52 public TrapReceiver(Logger log) 53 { 54 this.log = log; 55 } 56 57 66 public void snmpReceivedTrap(SnmpTrapSession session, InetAddress agent, 67 int port, SnmpOctetString community, 68 SnmpPduPacket pdu) 69 { 70 StringBuffer sbuf = new StringBuffer (); 71 72 sbuf.append("V2 Trap from ").append(agent.toString()); 73 sbuf.append(" on port ").append(port); 74 sbuf.append("\nPDU command......... ").append(pdu.getCommand()); 75 sbuf.append("\nPDU Length.......... ").append(pdu.getLength()); 76 sbuf.append("\nCommunity string.... ").append(community.toString()); 77 78 if(pdu instanceof SnmpPduRequest) 79 { 80 SnmpPduRequest spdu = (SnmpPduRequest) pdu; 81 82 sbuf.append("\nPDU Error Status.... ").append(spdu.getErrorStatus()); 83 sbuf.append("\nPDU Error Index..... ").append(spdu.getErrorIndex()); 84 sbuf.append("\n"); 85 } 86 87 for (int i = 0; i < pdu.getLength(); i++ ) 88 { 89 SnmpVarBind vb = pdu.getVarBindAt(i); 90 91 sbuf.append("Varbind[").append(i).append("] := "); 92 sbuf.append(vb.getName().toString()).append(" --> "); 93 sbuf.append(vb.getValue().toString()).append("\n"); 94 } 95 log.debug(sbuf.toString()); 96 } 98 107 public void snmpReceivedTrap(SnmpTrapSession session, InetAddress agent, 108 int port, SnmpOctetString community, 109 SnmpPduTrap pdu) 110 { 111 StringBuffer sbuf = new StringBuffer (); 112 113 sbuf.append("V1 Trap from agent ").append(agent.toString()); 114 sbuf.append(" on port ").append(port); 115 sbuf.append("\nIP Address......... ").append(pdu.getAgentAddress()); 116 sbuf.append("\nEnterprise Id...... ").append(pdu.getEnterprise()); 117 sbuf.append("\nGeneric ........... ").append(pdu.getGeneric()); 118 sbuf.append("\nSpecific .......... ").append(pdu.getSpecific()); 119 sbuf.append("\nTimeStamp ......... ").append(pdu.getTimeStamp()); 120 sbuf.append("\nLength............. ").append(pdu.getLength()); 121 sbuf.append("\nCommunity string... ").append(community.toString()); 122 sbuf.append("\n"); 123 124 for (int i = 0; i < pdu.getLength(); i++ ) 125 { 126 SnmpVarBind vb = pdu.getVarBindAt(i); 127 128 sbuf.append("Varbind[").append(i).append("] := "); 129 sbuf.append(vb.getName().toString()).append(" --> "); 130 sbuf.append(vb.getValue().toString()).append("\n"); 131 } 132 log.debug(sbuf.toString()); 133 } 135 142 public void snmpTrapSessionError(SnmpTrapSession session, 143 int error, Object ref) 144 { 145 StringBuffer sbuf = new StringBuffer (); 146 147 if(ref != null) { 148 sbuf.append("Session error (").append(error).append(") reference: "); 149 sbuf.append(ref.toString()); 150 } 151 else { 152 sbuf.append("Session error (").append(error).append(")"); 153 } 154 155 try { 156 if(error == SnmpTrapSession.ERROR_EXCEPTION) 157 session.raise(); 158 } 159 catch (Throwable e) { 160 sbuf.append(" <").append(e).append(">"); 161 } 162 163 log.error(sbuf.toString()); 164 165 } 167 } | Popular Tags |