From: <tr...@hy...> - 2010-01-06 19:36:56
|
Author: trader Date: 2010-01-06 11:36:44 -0800 (Wed, 06 Jan 2010) New Revision: 14135 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=14135 Modified: trunk/plugins/netdevice/src/org/hyperic/hq/plugin/netdevice/SNMPTrapReceiver.java Log: Same as revision 14133 (ALSO 14087, NOT 10487!), reformatted with project settings Modified: trunk/plugins/netdevice/src/org/hyperic/hq/plugin/netdevice/SNMPTrapReceiver.java =================================================================== --- trunk/plugins/netdevice/src/org/hyperic/hq/plugin/netdevice/SNMPTrapReceiver.java 2010-01-06 19:34:18 UTC (rev 14134) +++ trunk/plugins/netdevice/src/org/hyperic/hq/plugin/netdevice/SNMPTrapReceiver.java 2010-01-06 19:36:44 UTC (rev 14135) @@ -1,3 +1,21 @@ +/* + * 'SNMPTrapReceiver.java' NOTE: This copyright does *not* cover user programs + * that use HQ program services by normal system calls through the application + * program interfaces provided as part of the Hyperic Plug-in Development Kit or + * the Hyperic Client Development Kit - this is merely considered normal use of + * the program, and does *not* fall under the heading of "derived work". + * Copyright (C) [2004, 2005, 2006, 2007, 2008, 2009], Hyperic, Inc. This file + * is part of HQ. HQ is free software; you can redistribute it and/or modify it + * under the terms version 2 of the GNU General Public License as published by + * the Free Software Foundation. This program is distributed in the hope that it + * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. You should have received a copy of the GNU + * General Public License along with this program; if not, write to the Free + * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + package org.hyperic.hq.plugin.netdevice; import java.io.IOException; @@ -36,11 +54,11 @@ import org.snmp4j.util.ThreadPool; public class SNMPTrapReceiver implements CommandResponder { - static final String PROP_LISTEN_ADDRESS = "snmpTrapReceiver.listenAddress"; static final String DEFAULT_LISTEN_ADDRESS = "udp:0.0.0.0/162"; private static SNMPTrapReceiver instance = null; + private static final Log log = LogFactory.getLog(SNMPTrapReceiver.class.getName()); private MultiThreadedMessageDispatcher _dispatcher; @@ -48,6 +66,7 @@ private Address _listenAddress; private ThreadPool _threadPool; private Map _plugins = new HashMap(); + LogTrackPluginManager _manager; private int _received = 0; @@ -57,7 +76,6 @@ } public static SNMPTrapReceiver getInstance(Properties props) throws IOException { - if (!hasInstance()) { instance = new SNMPTrapReceiver(); instance.init(props); @@ -67,15 +85,16 @@ } public static void start(Properties props) throws IOException { - getInstance(props); } public static void shutdown() throws IOException { if (hasInstance()) { log.debug("Shutdown instance"); + instance._threadPool.cancel(); instance._snmp.close(); + instance = null; } } @@ -85,10 +104,10 @@ } private static String getConfig(LogTrackPlugin plugin, String key, String def) { + String value = plugin.getConfig(key); - String value = plugin.getConfig(key); if (value == null) { - // commandline-testing + // Commandline-testing... return plugin.getManager().getProperty(key, def); } else { return value; @@ -105,13 +124,15 @@ private LogTrackPlugin getPlugin(String address, String community) { String key = getPluginKey(address, community); + return (LogTrackPlugin) _plugins.get(key); } public void add(LogTrackPlugin plugin) throws IOException { + String key = getPluginKey(plugin); - String key = getPluginKey(plugin); log.debug("Add " + plugin.getName() + " for " + key); + _plugins.put(key, plugin); } @@ -119,8 +140,11 @@ if (!hasInstance()) { return; } + String key = getPluginKey(plugin); + log.debug("Remove " + plugin.getName() + " for " + key); + instance._plugins.remove(key); } @@ -128,7 +152,6 @@ } private void init(Properties props) throws IOException { - String address = props.getProperty(PROP_LISTEN_ADDRESS, DEFAULT_LISTEN_ADDRESS).trim(); String numThreads = props.getProperty("snmpTrapReceiver.numThreads", "1"); @@ -139,6 +162,7 @@ try { _listenAddress = GenericAddress.parse(address); + if (!_listenAddress.isValid()) { throw new IllegalArgumentException(); } @@ -149,6 +173,7 @@ log.debug(PROP_LISTEN_ADDRESS + "=" + address); TransportMapping transport; + if (_listenAddress instanceof UdpAddress) { transport = new DefaultUdpTransportMapping((UdpAddress) _listenAddress); } else { @@ -156,11 +181,13 @@ } _snmp = new Snmp(_dispatcher, transport); + _snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1()); _snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c()); _snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3()); USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); + SecurityModels.getInstance().addSecurityModel(usm); _snmp.addCommandResponder(this); @@ -175,10 +202,14 @@ StringBuffer msg = new StringBuffer(); Vector vars = event.getPDU().getVariableBindings(); + int size = vars.size(); + for (int i = 0; i < size; i++) { VariableBinding var = (VariableBinding) vars.get(i); + msg.append(var.getVariable().toString().trim()); + if (i < size - 1) { msg.append(", "); } @@ -188,9 +219,11 @@ } private LogTrackPlugin getPlatformPlugin() { - // XXX return _manager.getDefaultPlatformPlugin() + // return _manager.getDefaultPlatformPlugin ( ) final String prop = LogTrackPluginManager.DEFAULT_PLATFORM_PLUGIN; + String name = _manager.getProperty(prop); + return _manager.getLogTrackPlugin(name); } @@ -202,13 +235,16 @@ _received++; Address peer = event.getPeerAddress(); + InetAddress peerAddress; + if (peer instanceof UdpAddress) { peerAddress = ((UdpAddress) peer).getInetAddress(); } else if (peer instanceof TcpAddress) { peerAddress = ((TcpAddress) peer).getInetAddress(); } else { log.debug("Unsupported transport: " + peer.getClass().getName()); + return; } @@ -217,23 +253,28 @@ String community = new OctetString(event.getSecurityName()).toString(); LogTrackPlugin plugin = getPlugin(address, community); + if (plugin == null) { plugin = getPlatformPlugin(); + if (plugin != null) { if (log.isDebugEnabled()) { log.debug("No plugin for " + address + ", routing to default platform: " + plugin.getName()); } } } + if (plugin == null) { if (log.isDebugEnabled()) { log.debug("No plugin for " + address + ", msg=" + getMessage(event)); } } else { String msg = getMessage(event); + if (log.isDebugEnabled()) { log.debug("Msg=" + msg); } + plugin.reportEvent(System.currentTimeMillis(), LogTrackPlugin.LOGLEVEL_ERROR, address, msg); } } @@ -244,7 +285,9 @@ System.out.println("Ready"); System.in.read(); System.out.print("Shutting down..."); + SNMPTrapReceiver.shutdown(); + System.out.println("done"); } } |