1 22 package org.jboss.jmx.adaptor.snmp.agent; 23 24 import java.net.InetAddress ; 25 26 import javax.management.MBeanServer ; 27 28 import org.jboss.logging.Logger; 29 import org.opennms.protocols.snmp.SnmpAgentSession; 30 import org.opennms.protocols.snmp.SnmpObjectId; 31 import org.opennms.protocols.snmp.SnmpOctetString; 32 import org.opennms.protocols.snmp.SnmpPduPacket; 33 import org.opennms.protocols.snmp.SnmpPduRequest; 34 import org.opennms.protocols.snmp.SnmpSyntax; 35 import org.opennms.protocols.snmp.SnmpVarBind; 36 37 46 public class RequestHandlerSupport implements RequestHandler 47 { 48 50 51 protected Logger log; 52 53 54 protected MBeanServer server; 55 56 57 protected String resourceName; 58 59 60 protected Clock clock; 61 62 64 67 public RequestHandlerSupport() 68 { 69 } 71 72 74 77 public void initialize(String resourceName, MBeanServer server, Logger log, Clock uptime) 78 throws Exception 79 { 80 this.resourceName = resourceName; 81 this.server = server; 82 this.log = log; 83 this.clock = uptime; 84 } 85 86 88 101 public SnmpPduRequest snmpReceivedGet(SnmpPduPacket pdu, boolean getNext) 102 { 103 SnmpPduRequest response = null; 104 int pduLength = pdu.getLength(); 105 106 log.debug("requestId=" + pdu.getRequestId() + ", pduLength=" + pduLength); 107 108 SnmpVarBind[] vblist = new SnmpVarBind[pduLength]; 109 int errorStatus = SnmpPduPacket.ErrNoError; 110 int errorIndex = 0; 111 112 for (int i = 0; i < pduLength ; i++ ) 114 { 115 SnmpVarBind vb = pdu.getVarBindAt(i); 116 SnmpObjectId oid = vb.getName(); 117 if (getNext) 118 { 119 log.debug( 120 "Should call getNextOid() to find out what is the next valid OID " + 121 "instance in the supported MIB tree. Assign that OID to the VB List " + 122 "and then proceed same as that of get request" ); 123 } 124 vblist[i] = new SnmpVarBind(oid); 125 126 log.debug("oid=" + oid.toString()); 127 128 log.debug("Should call the respective interface to retrieve current value for this OID" ); 129 130 SnmpSyntax result = null; 131 132 if (result == null) 133 { 134 errorStatus = SnmpPduPacket.ErrNoSuchName; 135 errorIndex = i + 1; 136 } 138 else 139 { 140 vblist[i].setValue(result); 141 log.debug("Varbind[" + i + "] := " + vblist[i].getName().toString()); 142 log.debug(" --> " + vblist[i].getValue().toString()); 143 } 144 } 145 response = new SnmpPduRequest(SnmpPduPacket.RESPONSE, vblist); 146 response.setErrorStatus(errorStatus); 147 response.setErrorIndex(errorIndex); 148 return response; 149 } 150 151 162 public SnmpPduRequest snmpReceivedSet(SnmpPduPacket pdu) 163 { 164 SnmpPduRequest response = null; 165 int errorStatus = SnmpPduPacket.ErrNoError; 166 int errorIndex = 0; 167 int k = pdu.getLength(); 168 SnmpVarBind[] vblist = new SnmpVarBind[k]; 169 170 for (int i = 0; i < k ; i++ ) 171 { 172 SnmpVarBind vb = pdu.getVarBindAt(i); 173 vblist[i] = new SnmpVarBind(vb); 174 SnmpObjectId oid = vb.getName(); 175 176 SnmpSyntax result = null; 177 log.debug("Should call the respective interface to assign a value for this OID" ); 178 179 if (result != null) 180 { 181 errorStatus = SnmpPduPacket.ErrReadOnly; 182 errorIndex = i + 1; 183 log.debug("Error occured " + vb.getName().toString()); 184 } 185 186 log.debug("Varbind[" + i + "] := " + vb.getName().toString()); 187 log.debug(" --> " + vb.getValue().toString()); 188 } 189 190 response = new SnmpPduRequest(SnmpPduPacket.RESPONSE, vblist); 191 response.setErrorStatus(errorStatus); 192 response.setErrorIndex(errorIndex); 193 194 return response; 195 } 196 197 210 public void snmpReceivedPdu(SnmpAgentSession session, InetAddress manager, int port, 211 SnmpOctetString community, SnmpPduPacket pdu) 212 { 213 log.error("Message from manager " + manager.toString() + " on port " + port); 214 int cmd = pdu.getCommand(); 215 log.error("Unsupported PDU command......... " + cmd); 216 } 217 218 238 public void SnmpAgentSessionError(SnmpAgentSession session, int error, Object ref) 239 { 240 log.error("An error occured in the trap session"); 241 log.error("Session error code = " + error); 242 if(ref != null) 243 { 244 log.error("Session error reference: " + ref.toString()); 245 } 246 247 if(error == SnmpAgentSession.ERROR_EXCEPTION) 248 { 249 synchronized(session) 250 { 251 session.notify(); } 253 } 254 } 255 } 256 | Popular Tags |