| 1 22 23 package org.snmp4j.agent.agentx.master; 24 25 import java.io.File ; 26 import java.io.IOException ; 27 28 import org.snmp4j.TransportMapping; 29 import org.snmp4j.agent.BaseAgent; 30 import org.snmp4j.agent.DuplicateRegistrationException; 31 import org.snmp4j.agent.agentx.AgentX; 32 import org.snmp4j.agent.agentx.AgentXMessageDispatcherImpl; 33 import org.snmp4j.agent.agentx.AgentXProtocol; 34 import org.snmp4j.agent.mo.snmp.*; 35 import org.snmp4j.log.LogAdapter; 36 import org.snmp4j.log.LogFactory; 37 import org.snmp4j.mp.MPv3; 38 import org.snmp4j.security.USM; 39 import org.snmp4j.smi.IpAddress; 40 import org.snmp4j.smi.OctetString; 41 import org.snmp4j.transport.ConnectionOrientedTransportMapping; 42 import org.snmp4j.transport.TransportStateEvent; 43 import org.snmp4j.transport.TransportStateListener; 44 import java.net.InetAddress ; 45 import java.util.Arrays ; 46 47 58 public class AgentXMasterAgent extends BaseAgent implements 59 TransportStateListener { 60 61 private static final LogAdapter LOGGER = 62 LogFactory.getLogger(AgentXMasterAgent.class); 63 64 private static short maxGetBulkRepetitions = Short.MAX_VALUE; 65 private AgentX agentX; 66 private AgentXCommandProcessor commandProcessor; 67 private AgentXMib agentXMIB; 68 private OctetString localEngineID = 69 new OctetString(MPv3.createLocalEngineID()); 70 private boolean localhostSubagentsOnly; 71 72 public AgentXMasterAgent(File bootCounterFile, 73 File configFile) { 74 super(); 75 setBootCounterFile(bootCounterFile); 76 setConfigFile(configFile); 77 agentX = new AgentX(new AgentXMessageDispatcherImpl()); 78 } 79 80 public void init() throws IOException { 81 AgentXQueue queue = new AgentXQueue(); 82 commandProcessor = 83 new AgentXCommandProcessor(localEngineID, queue, agentX, server); 84 setAgent(commandProcessor); 85 agentX.addCommandResponder(commandProcessor); 86 this.agentXMIB = new AgentXMib(commandProcessor); 87 super.init(); 88 commandProcessor.setNotificationOriginator(getNotificationOriginator()); 89 commandProcessor.addAgentXMasterListener(this.agentXMIB); 90 } 91 92 public void addTransportMapping(TransportMapping transport) { 93 agentX.getMessageDispatcher().addTransportMapping(transport); 94 if (transport instanceof ConnectionOrientedTransportMapping) { 95 ConnectionOrientedTransportMapping cotm = 96 (ConnectionOrientedTransportMapping)transport; 97 cotm.addTransportStateListener(this); 98 cotm.setConnectionTimeout(0); 99 cotm.setMessageLengthDecoder(new AgentXProtocol()); 100 } 101 } 102 103 public void removeTransportMapping(TransportMapping transport) { 104 agentX.getMessageDispatcher().removeTransportMapping(transport); 105 if (transport instanceof ConnectionOrientedTransportMapping) { 106 ConnectionOrientedTransportMapping cotm = 107 (ConnectionOrientedTransportMapping)transport; 108 cotm.removeTransportStateListener(this); 109 } 110 } 111 112 120 public static short getMaxGetBulkRepetitions() { 121 return maxGetBulkRepetitions; 122 } 123 124 public AgentXMib getAgentXMIB() { 125 return agentXMIB; 126 } 127 128 public AgentXCommandProcessor getCommandProcessor() { 129 return commandProcessor; 130 } 131 132 public OctetString getLocalEngineID() { 133 return localEngineID; 134 } 135 136 143 public boolean isLocalhostSubagentsOnly() { 144 return localhostSubagentsOnly; 145 } 146 147 165 public static void setMaxGetBulkRepetitions(short maxRepetitions) { 166 if (maxRepetitions < 1) { 167 throw new IllegalArgumentException ( 168 "Max repetitions needs an unsigned value"); 169 } 170 maxGetBulkRepetitions = maxRepetitions; 171 } 172 173 public void setLocalEngineID(OctetString localEngineID) { 174 this.localEngineID = localEngineID; 175 } 176 177 183 public void setLocalhostSubagentsOnly(boolean localhostSubagentsOnly) { 184 this.localhostSubagentsOnly = localhostSubagentsOnly; 185 } 186 187 public void connectionStateChanged(TransportStateEvent change) { 188 if (localhostSubagentsOnly && 189 (change.getNewState() == TransportStateEvent.STATE_CONNECTED) && 190 (change.getPeerAddress() instanceof IpAddress)) { 191 IpAddress peerAddress = (IpAddress)change.getPeerAddress(); 192 if (!peerAddress.getInetAddress().isLoopbackAddress()) { 193 LOGGER.warn("Connection attempt made from non loopback (i.e. local) address '"+ 194 peerAddress+"' which will be ignored"); 195 change.setCancelled(true); 196 return; 197 } 198 } 199 ((AgentXCommandProcessor)getAgent()).connectionStateChanged(change); 200 } 201 202 protected void registerManagedObjects() { 203 try { 204 agentXMIB.registerMOs(server, null); 205 } 206 catch (DuplicateRegistrationException ex) { 207 LOGGER.error("Unable to register AgentX MIB", ex); 208 } 209 } 210 211 protected void unregisterManagedObjects() { 212 agentXMIB.unregisterMOs(server, null); 213 } 214 215 protected void addUsmUser(USM usm) { 216 } 217 218 protected void addNotificationTargets(SnmpTargetMIB targetMIB, 219 SnmpNotificationMIB notificationMIB) { 220 } 221 222 protected void addViews(VacmMIB vacmMIB) { 223 } 224 225 protected void addCommunities(SnmpCommunityMIB communityMIB) { 226 } 227 } 228 | Popular Tags |