From: <tr...@hy...> - 2010-01-06 19:24:55
|
Author: trader Date: 2010-01-06 11:24:46 -0800 (Wed, 06 Jan 2010) New Revision: 14132 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=14132 Modified: trunk/plugins/netdevice/src/org/hyperic/hq/plugin/netdevice/SNMPTrapReceiver.java Log: Same as revision 9991, 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 09:22:50 UTC (rev 14131) +++ trunk/plugins/netdevice/src/org/hyperic/hq/plugin/netdevice/SNMPTrapReceiver.java 2010-01-06 19:24:46 UTC (rev 14132) @@ -1,32 +1,3 @@ -/* - * '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; @@ -64,304 +35,255 @@ import org.snmp4j.util.MultiThreadedMessageDispatcher; 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"; +public class SNMPTrapReceiver +implements CommandResponder { - private static SNMPTrapReceiver instance = null; + static final String PROP_LISTEN_ADDRESS = "snmpTrapReceiver.listenAddress"; + static final String DEFAULT_LISTEN_ADDRESS = "udp:0.0.0.0/162"; - private static final Log log = LogFactory.getLog ( SNMPTrapReceiver.class.getName ( ) ); + private static SNMPTrapReceiver instance = null; + private static final Log log = + LogFactory.getLog(SNMPTrapReceiver.class.getName()); - private MultiThreadedMessageDispatcher _dispatcher; - private Snmp _snmp; - private Address _listenAddress; - private ThreadPool _threadPool; - private Map _plugins = new HashMap ( ); + private MultiThreadedMessageDispatcher _dispatcher; + private Snmp _snmp; + private Address _listenAddress; + private ThreadPool _threadPool; + private Map _plugins = new HashMap(); + LogTrackPluginManager _manager; - LogTrackPluginManager _manager; + private int _received = 0; - private int _received = 0; + static boolean hasInstance() { + return instance != null; + } - static boolean hasInstance ( ) - { - return instance != null; - } + public static SNMPTrapReceiver getInstance(Properties props) + throws IOException { - public static SNMPTrapReceiver getInstance ( Properties props ) throws IOException - { - if ( !hasInstance ( ) ) - { - instance = new SNMPTrapReceiver ( ); - instance.init ( props ); - } + if (!hasInstance()) { + instance = new SNMPTrapReceiver(); + instance.init(props); + } - return instance; - } + return instance; + } - public static void start(Properties props) throws IOException - { - getInstance ( props ); - } + public static void start(Properties props) + throws IOException { - public static void shutdown ( ) throws IOException - { - if ( hasInstance ( ) ) - { - log.debug ( "Shutdown instance" ); + getInstance(props); + } - instance._threadPool.cancel ( ); - instance._snmp.close ( ); + public static void shutdown() throws IOException { + if (hasInstance()) { + log.debug("Shutdown instance"); + instance._threadPool.cancel(); + instance._snmp.close(); + instance = null; + } + } - instance = null; - } - } + private static String getPluginKey(String address, String community) { + return address + "-" + community; + } - private static String getPluginKey ( String address, - String community ) - { - return address + "-" + community; - } + private static String getConfig(LogTrackPlugin plugin, + String key, String def) { - 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 + return plugin.getManager().getProperty(key, def); + } + else { + return value; + } + } - if ( value == null ) - { - // Commandline-testing... - return plugin.getManager().getProperty ( key, def ); - } - else - { - return value; - } - } + private static String getPluginKey(LogTrackPlugin plugin) { + String address = + getConfig(plugin, + SNMPClient.PROP_IP, + SNMPClient.DEFAULT_IP); - private static String getPluginKey ( LogTrackPlugin plugin ) - { - String address = getConfig ( plugin, - SNMPClient.PROP_IP, - SNMPClient.DEFAULT_IP ); + String community = + getConfig(plugin, + SNMPClient.PROP_COMMUNITY, + SNMPClient.DEFAULT_COMMUNITY); - String community = getConfig ( plugin, - SNMPClient.PROP_COMMUNITY, - SNMPClient.DEFAULT_COMMUNITY ); + return getPluginKey(address, community); + } - return getPluginKey ( address, community ); - } + private LogTrackPlugin getPlugin(String address, String community) { + String key = getPluginKey(address, community); + return (LogTrackPlugin)_plugins.get(key); + } - private LogTrackPlugin getPlugin ( String address, String community ) - { - String key = getPluginKey ( address, community ); + public void add(LogTrackPlugin plugin) + throws IOException { - return (LogTrackPlugin)_plugins.get ( key ); - } + String key = getPluginKey(plugin); + log.debug("Add " + plugin.getName() + " for " + key); + _plugins.put(key, plugin); + } - public void add ( LogTrackPlugin plugin ) throws IOException - { - String key = getPluginKey ( plugin ); - - log.debug ( "Add " + plugin.getName ( ) + " for " + key ); - - _plugins.put ( key, plugin ); - } - - public static void remove ( LogTrackPlugin plugin ) - { - if ( !hasInstance ( ) ) - { - return; - } - - String key = getPluginKey ( plugin ); - - log.debug ( "Remove " + plugin.getName ( ) + " for " + key ); - - instance._plugins.remove ( key ); + public static void remove(LogTrackPlugin plugin) { + if (!hasInstance()) { + return; + } + String key = getPluginKey(plugin); + log.debug("Remove " + plugin.getName() + " for " + key); + instance._plugins.remove(key); } - private SNMPTrapReceiver ( ) - { } + private SNMPTrapReceiver () {} - private void init ( Properties props ) throws IOException - { - String address = props.getProperty ( PROP_LISTEN_ADDRESS, - DEFAULT_LISTEN_ADDRESS ).trim ( ); + private void init(Properties props) + throws IOException { - String numThreads = props.getProperty ( "snmpTrapReceiver.numThreads", - "1" ); + String address = + props.getProperty(PROP_LISTEN_ADDRESS, + DEFAULT_LISTEN_ADDRESS).trim(); - _threadPool = ThreadPool.create ( "SNMPTrapReceiver", - Integer.parseInt ( numThreads ) ); + String numThreads = + props.getProperty("snmpTrapReceiver.numThreads", "1"); - _dispatcher = new MultiThreadedMessageDispatcher ( _threadPool, - new MessageDispatcherImpl ( ) ); + _threadPool = + ThreadPool.create("SNMPTrapReceiver", + Integer.parseInt(numThreads)); - try - { - _listenAddress = GenericAddress.parse ( address ); + _dispatcher = + new MultiThreadedMessageDispatcher(_threadPool, + new MessageDispatcherImpl()); - if ( !_listenAddress.isValid ( ) ) - { - throw new IllegalArgumentException ( ); - } - } - catch ( Exception e ) - { - throw new IOException ( "Invalid " + PROP_LISTEN_ADDRESS + "=" + address ); - } + try { + _listenAddress = GenericAddress.parse(address); + if (!_listenAddress.isValid()) { + throw new IllegalArgumentException(); + } + } catch (Exception e) { + throw new IOException("Invalid " + + PROP_LISTEN_ADDRESS + "=" + address); + } - log.debug ( PROP_LISTEN_ADDRESS + "=" + address ); + log.debug(PROP_LISTEN_ADDRESS + "=" + address); - TransportMapping transport; + TransportMapping transport; + if (_listenAddress instanceof UdpAddress) { + transport = + new DefaultUdpTransportMapping((UdpAddress)_listenAddress); + } + else { + transport = + new DefaultTcpTransportMapping((TcpAddress)_listenAddress); + } - if ( _listenAddress instanceof UdpAddress ) - { - transport = new DefaultUdpTransportMapping ( (UdpAddress)_listenAddress ); - } - else - { - transport = new DefaultTcpTransportMapping ( (TcpAddress)_listenAddress ); - } + _snmp = new Snmp(_dispatcher, transport); + _snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1()); + _snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c()); + _snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3()); - _snmp = new Snmp ( _dispatcher, transport ); + USM usm = + new USM(SecurityProtocols.getInstance(), + new OctetString(MPv3.createLocalEngineID()), 0); + SecurityModels.getInstance().addSecurityModel(usm); - _snmp.getMessageDispatcher().addMessageProcessingModel ( new MPv1 ( ) ); - _snmp.getMessageDispatcher().addMessageProcessingModel ( new MPv2c ( ) ); - _snmp.getMessageDispatcher().addMessageProcessingModel ( new MPv3 ( ) ); + _snmp.addCommandResponder(this); + _snmp.listen(); + } - USM usm = new USM ( SecurityProtocols.getInstance ( ), - new OctetString ( MPv3.createLocalEngineID ( ) ), - 0 ); + public int getTrapsReceived() { + return _received; + } - SecurityModels.getInstance().addSecurityModel ( usm ); + private String getMessage(CommandResponderEvent event) { + StringBuffer msg = new StringBuffer(); - _snmp.addCommandResponder ( this ); - _snmp.listen ( ); - } + 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(", "); + } + } - public int getTrapsReceived ( ) - { - return _received; - } + return msg.toString(); + } - private String getMessage ( CommandResponderEvent event ) - { - StringBuffer msg = new StringBuffer ( ); + private LogTrackPlugin getPlatformPlugin() { + //XXX return _manager.getDefaultPlatformPlugin() + final String prop = LogTrackPluginManager.DEFAULT_PLATFORM_PLUGIN; + String name = _manager.getProperty(prop); + return _manager.getLogTrackPlugin(name); + } - 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 ( ", " ); - } - } - - return msg.toString ( ); - } - - private LogTrackPlugin getPlatformPlugin ( ) - { - // return _manager.getDefaultPlatformPlugin ( ) - final String prop = LogTrackPluginManager.DEFAULT_PLATFORM_PLUGIN; - - String name = _manager.getProperty ( prop ); - - return _manager.getLogTrackPlugin ( name ); + void setPluginManager(LogTrackPluginManager manager) { + _manager = manager; } - void setPluginManager ( LogTrackPluginManager manager ) - { - _manager = manager; - } + public void processPdu(CommandResponderEvent event) { + _received++; - public void processPdu ( CommandResponderEvent event ) - { - _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; + } - Address peer = event.getPeerAddress ( ); + String address = + peerAddress.getHostAddress(); - InetAddress peerAddress; + String community = + new OctetString(event.getSecurityName()).toString(); - 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; - } - - String address = peerAddress.getHostAddress ( ); - - 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 ( ) ); + 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); + } + } - if ( plugin == null ) - { - if ( log.isDebugEnabled ( ) ) - { - log.debug ( "No plugin for " + address + ", msg=" + getMessage ( event ) ); - } - } - else - { - String msg = getMessage ( event ); + public static void main(String[] args) throws Exception { + SNMPTrapReceiver.start(System.getProperties()); - if ( log.isDebugEnabled ( ) ) - { - log.debug ( "Msg=" + msg ); - } - - plugin.reportEvent ( System.currentTimeMillis ( ), - LogTrackPlugin.LOGLEVEL_ERROR, - address, - msg ); - } - } - - public static void main ( String[] args ) throws Exception - { - SNMPTrapReceiver.start ( System.getProperties ( ) ); - - System.out.println ( "Ready" ); - System.in.read ( ); - System.out.print ( "Shutting down..." ); - - SNMPTrapReceiver.shutdown ( ); - - System.out.println ( "done" ); - } + System.out.println("Ready"); + System.in.read(); + System.out.print("Shutting down..."); + SNMPTrapReceiver.shutdown(); + System.out.println("done"); + } } |