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