1 20 package org.jboss.jmx.adaptor.snmp.agent; 21 22 import java.io.InputStream ; 23 import java.net.InetAddress ; 24 import java.net.UnknownHostException ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Set ; 30 31 import javax.management.Notification ; 32 33 import org.jboss.jmx.adaptor.snmp.config.manager.Manager; 34 import org.jboss.logging.Logger; 35 import org.jboss.system.server.ServerConfig; 36 import org.jboss.xb.binding.MappingObjectModelFactory; 37 import org.jboss.xb.binding.Unmarshaller; 38 import org.jboss.xb.binding.UnmarshallerFactory; 39 import org.opennms.protocols.snmp.SnmpIPAddress; 40 import org.opennms.protocols.snmp.SnmpPduPacket; 41 import org.opennms.protocols.snmp.SnmpPduTrap; 42 43 55 public class TrapEmitter 56 { 57 58 private static final Logger log = Logger.getLogger(TrapEmitter.class); 59 60 61 private TrapFactory trapFactory = null; 62 63 64 private String trapFactoryClassName = null; 65 66 67 private String managersResName = null; 68 69 70 private String notificationMapResName = null; 71 72 73 private Counter trapCount = null; 74 75 76 private Clock uptime = null; 77 78 79 private Set managers = Collections.synchronizedSet(new HashSet ()); 80 81 84 public TrapEmitter(String trapFactoryClassName, 85 Counter trapCount, 86 Clock uptime, 87 String managersResName, 88 String notificationMapResName) 89 { 90 this.trapFactoryClassName = trapFactoryClassName; 91 this.trapCount = trapCount; 92 this.uptime = uptime; 93 this.managersResName = managersResName; 94 this.notificationMapResName = notificationMapResName; 95 } 96 97 100 public void start() 101 throws Exception 102 { 103 load(); 105 106 this.trapFactory = (TrapFactory) Class.forName(this.trapFactoryClassName, 108 true, 109 this.getClass().getClassLoader()).newInstance(); 110 111 this.trapFactory.set(this.notificationMapResName, 113 this.uptime, 114 this.trapCount); 115 116 this.trapFactory.start(); 118 } 119 120 123 public void stop() 124 throws Exception 125 { 126 synchronized(this.managers) { 127 128 Iterator i = this.managers.iterator(); 130 131 while (i.hasNext()) { 132 ManagerRecord s = (ManagerRecord)i.next(); 133 s.closeSession(); 134 } 135 136 this.managers.clear(); 138 } 139 } 140 141 149 public void send(Notification n) 150 throws Exception 151 { 152 synchronized(this.trapFactory) { 154 if(this.trapFactory == null) { 155 log.error("Received notifications before trap factory set. Discarding."); 156 return; 157 } 158 } 159 160 SnmpPduTrap v1TrapPdu = null; 162 SnmpPduPacket v2TrapPdu = null; 163 164 synchronized(this.managers) { 167 168 Iterator i = this.managers.iterator(); 170 while (i.hasNext()) { 171 ManagerRecord s = (ManagerRecord)i.next(); 172 173 try { 174 switch (s.getVersion()) { 175 case SnmpAgentService.SNMPV1: 176 if (v1TrapPdu == null) 177 v1TrapPdu = this.trapFactory.generateV1Trap(n); 178 179 v1TrapPdu.setAgentAddress(new SnmpIPAddress(s.getLocalAddress())); 181 182 this.trapCount.advance(); 184 185 s.getSession().send(v1TrapPdu); 187 break; 188 189 case SnmpAgentService.SNMPV2: 190 if (v2TrapPdu == null) 191 v2TrapPdu = this.trapFactory.generateV2Trap(n); 192 193 this.trapCount.advance(); 195 196 s.getSession().send(v2TrapPdu); 198 break; 199 200 default: 201 log.error("Skipping session: Unknown SNMP version found"); 202 } 203 } 204 catch(MappingFailedException e) { 205 log.error("Translating notification - " + e.getMessage()); 206 } 207 catch(Exception e) { 208 log.error("SNMP send error for " + 209 s.getAddress().toString() + ":" + 210 s.getPort() + ": <" + e + 211 ">"); 212 } 213 } 214 } 215 } 216 217 220 private void load() throws Exception 221 { 222 log.debug("Reading resource: '" + this.managersResName + "'"); 223 224 MappingObjectModelFactory momf = new MappingObjectModelFactory(); 227 momf.mapElementToClass("manager-list", ArrayList .class); 228 momf.mapElementToClass("manager", Manager.class); 229 230 ArrayList managerList = null; 231 InputStream is = null; 232 try 233 { 234 is = this.getClass().getResourceAsStream(this.managersResName); 236 237 Unmarshaller unmarshaller = UnmarshallerFactory.newInstance() 239 .newUnmarshaller(); 240 241 managerList = (ArrayList )unmarshaller.unmarshal(is, momf, null); 243 } 244 catch (Exception e) 245 { 246 log.error("Accessing resource '" + managersResName + "'"); 247 throw e; 248 } 249 finally 250 { 251 if (is != null) 252 { 253 is.close(); 255 } 256 } 257 log.debug("Found " + managerList.size() + " monitoring managers"); 258 259 for (Iterator i = managerList.iterator(); i.hasNext(); ) 260 { 261 Manager m = (Manager)i.next(); 263 264 try 265 { 266 ManagerRecord mr = new ManagerRecord( 268 InetAddress.getByName(m.getAddress()), 269 m.getPort(), 270 toInetAddressWithDefaultBinding(m.getLocalAddress()), 271 m.getLocalPort(), 272 m.getVersion() 273 ); 274 275 if (this.managers.add(mr) == false) 278 { 279 log.warn("Ignoring duplicate manager: " + m); 280 } 281 else 282 { 283 mr.openSession(); 285 } 286 } 287 catch (Exception e) 288 { 289 log.warn("Error enabling monitoring manager: " + m, e); 290 } 291 } 292 } 293 294 297 private InetAddress toInetAddressWithDefaultBinding(String host) 298 throws UnknownHostException 299 { 300 if (host == null || host.length() == 0) { 301 302 String defaultBindAddress = System.getProperty(ServerConfig.SERVER_BIND_ADDRESS); 303 if (defaultBindAddress != null && !defaultBindAddress.equals("0.0.0.0")) 304 return InetAddress.getByName(defaultBindAddress); 305 else 306 return InetAddress.getLocalHost(); 307 } 308 else 309 return InetAddress.getByName(host); 310 } 311 312 } | Popular Tags |