From: <tr...@hy...> - 2010-01-06 19:51:08
|
Author: trader Date: 2010-01-06 11:50:57 -0800 (Wed, 06 Jan 2010) New Revision: 14138 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=14138 Modified: trunk/plugins/netdevice/src/org/hyperic/hq/plugin/netdevice/NetworkDevicePlatformDetector.java Log: Same as revision 4450, reformatted with project settings Modified: trunk/plugins/netdevice/src/org/hyperic/hq/plugin/netdevice/NetworkDevicePlatformDetector.java =================================================================== --- trunk/plugins/netdevice/src/org/hyperic/hq/plugin/netdevice/NetworkDevicePlatformDetector.java 2010-01-06 19:45:53 UTC (rev 14137) +++ trunk/plugins/netdevice/src/org/hyperic/hq/plugin/netdevice/NetworkDevicePlatformDetector.java 2010-01-06 19:50:57 UTC (rev 14138) @@ -1,7 +1,4 @@ /* - * 'NetworkDevicePlatformDetector.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 @@ -9,7 +6,7 @@ * 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. + * Copyright (C) [2004, 2005, 2006], Hyperic, Inc. * This file is part of HQ. * * HQ is free software; you can redistribute it and/or modify @@ -43,238 +40,167 @@ import org.hyperic.snmp.SNMPSession; import org.hyperic.util.config.ConfigResponse; -public class NetworkDevicePlatformDetector extends PlatformDetector +public class NetworkDevicePlatformDetector + extends PlatformDetector { - private static final String PROP_VERSION = "Version"; - private Properties props; - private boolean autoDefaults; + private static final String PROP_VERSION = "Version"; - public void init ( PluginManager manager ) throws PluginException - { - super.init ( manager ); + private Properties props; + private boolean autoDefaults; - this.props = manager.getProperties ( ); + public void init(PluginManager manager) throws PluginException { + super.init(manager); + this.props = manager.getProperties(); + this.autoDefaults = "true".equals(this.props.getProperty("snmp.autoDefaults")); + if (!this.autoDefaults) { + // command-line -DsnmpCommunity=public + this.autoDefaults = this.props.getProperty(SNMPClient.PROP_COMMUNITY) != null; + } + } - this.autoDefaults = "true".equals ( this.props.getProperty ( "snmp.autoDefaults" ) ); + private SNMPSession getSession(ConfigResponse config) { + try { + return NetworkDeviceDetector.getSession(config); + } catch (PluginException e) { + getLog().error(e.getMessage(), e); + return null; + } + } - if ( !this.autoDefaults ) - { - // Command-line -DsnmpCommunity=public... - this.autoDefaults = this.props.getProperty ( SNMPClient.PROP_COMMUNITY ) != null; - } - } - - private SNMPSession getSession ( ConfigResponse config ) - { - try - { - return NetworkDeviceDetector.getSession ( config ); - } - catch ( PluginException e ) - { - getLog().error ( e.getMessage ( ), e ); - - return null; - } - } - - private String getString ( SNMPSession session, - String oid ) - { - try - { - Object obj = session.getSingleValue ( oid ); - - if ( obj == null ) - { + private String getString(SNMPSession session, String oid) { + try { + Object obj = session.getSingleValue(oid); + if (obj == null) { + return null; + } + String value = obj.toString(); + // XXX cisco has \r\n in description, strip out? + return value; + } catch (SNMPException e) { + getLog().warn("Error getting '" + oid + "': " + e.getMessage()); return null; - } + } + } - String value = obj.toString ( ); + // allow defaults to be configure in agent.properties like so: + // snmpCommunity.192.168.1.102=MyCommunity + // snmpVersion.192.168.1.102=v1 + // snmpPort.192.168.1.102=1611 + private String getIpProp(String key, String ip, String defVal) { + String propDefault = this.props.getProperty(key, defVal); + return this.props.getProperty(key + "." + ip, propDefault); + } - // Cisco has \r\n in description, strip out? - return value; - } - catch ( SNMPException e ) - { - getLog().warn ( "Error getting '" + oid+ "': " + e.getMessage ( ) ); + public PlatformResource getPlatformResource(ConfigResponse config) throws PluginException { - return null; - } - } + String platformIp = config.getValue(ProductPlugin.PROP_PLATFORM_IP); + // for command-line -DsnmpIp=x.x.x.x usage + platformIp = getIpProp(SNMPClient.PROP_IP, platformIp, platformIp); - // - // Allow defaults to be configure in agent.properties, e.g. - // - // snmpCommunity.192.168.1.102 = MyCommunity - // snmpVersion.192.168.1.102 = v1 - // snmpPort.192.168.1.102 = 1611 - // - private String getIpProp ( String key, - String ip, - String defVal ) - { - String propDefault = this.props.getProperty ( key, defVal ); + String defaultVersion = getIpProp(SNMPClient.PROP_VERSION, platformIp, SNMPClient.VALID_VERSIONS[1]); // v2c + String fallbackVersion = SNMPClient.VALID_VERSIONS[0]; // v1 - return this.props.getProperty ( key + "." + ip, propDefault ); - } + PlatformResource platform = super.getPlatformResource(config); - public PlatformResource getPlatformResource ( ConfigResponse config ) throws PluginException - { - String platformIp = config.getValue ( ProductPlugin.PROP_PLATFORM_IP ); + Log log = getLog(); + ConfigResponse metricConfig; + boolean hasConfig = config.getValue(SNMPClient.PROP_IP) != null; - // For command-line -DsnmpIp=x.x.x.x usage... - platformIp = getIpProp ( SNMPClient.PROP_IP, platformIp, platformIp ); + if (hasConfig) { + // we've already been here + metricConfig = config; + if (log.isDebugEnabled()) { + log.debug("Using approved snmp config=" + metricConfig); + } + } else if (this.autoDefaults) { + // platform was just created, attempt to auto-configure + metricConfig = new ConfigResponse(); + metricConfig.setValue(SNMPClient.PROP_IP, platformIp); + metricConfig.setValue(SNMPClient.PROP_VERSION, defaultVersion); + metricConfig.setValue(SNMPClient.PROP_COMMUNITY, getIpProp(SNMPClient.PROP_COMMUNITY, + platformIp, + SNMPClient.DEFAULT_COMMUNITY)); + metricConfig.setValue(SNMPClient.PROP_PORT, getIpProp(SNMPClient.PROP_PORT, + platformIp, + SNMPClient.DEFAULT_PORT_STRING)); + metricConfig.setValue(NetworkDeviceDetector.PROP_IF_IX, getIpProp(NetworkDeviceDetector.PROP_IF_IX, + platformIp, + NetworkDeviceDetector.IF_DESCR)); + if (log.isDebugEnabled()) { + log.debug("Using default snmp config=" + metricConfig); + } + } else { + if (log.isDebugEnabled()) { + log.debug("Need user input for snmp config=" + config); + } + return platform; + } - String defaultVersion = getIpProp ( SNMPClient.PROP_VERSION, - platformIp, - SNMPClient.VALID_VERSIONS[1] ); // v2c + ConfigResponse cprops = new ConfigResponse(); - String fallbackVersion = SNMPClient.VALID_VERSIONS[0]; // v1 + SNMPSession session; - PlatformResource platform = super.getPlatformResource ( config ); - - Log log = getLog ( ); - - ConfigResponse metricConfig; - - boolean hasConfig = config.getValue ( SNMPClient.PROP_IP ) != null; - - if ( hasConfig ) - { - // We've already been here... - metricConfig = config; - - if ( log.isDebugEnabled ( ) ) - { - log.debug ( "Using approved snmp config=" + metricConfig ); - } - } - else if ( this.autoDefaults ) - { - // Platform was just created, attempt to auto-configure... - metricConfig = new ConfigResponse(); - - metricConfig.setValue ( SNMPClient.PROP_IP, platformIp ); - metricConfig.setValue ( SNMPClient.PROP_VERSION, defaultVersion ); - - metricConfig.setValue ( SNMPClient.PROP_COMMUNITY, getIpProp ( SNMPClient.PROP_COMMUNITY, - platformIp, - SNMPClient.DEFAULT_COMMUNITY ) ); - - metricConfig.setValue ( SNMPClient.PROP_PORT, getIpProp ( SNMPClient.PROP_PORT, - platformIp, - SNMPClient.DEFAULT_PORT_STRING ) ); - - metricConfig.setValue ( NetworkDeviceDetector.PROP_IF_IX, getIpProp ( NetworkDeviceDetector.PROP_IF_IX, - platformIp, - NetworkDeviceDetector.IF_DESCR ) ); - if ( log.isDebugEnabled ( ) ) - { - log.debug ( "Using default snmp config=" + metricConfig ); - } - } - else - { - if ( log.isDebugEnabled ( ) ) - { - log.debug ( "Need user input for snmp config=" + config ); - } - - return platform; - } - - ConfigResponse cprops = new ConfigResponse ( ); - - SNMPSession session; - - if ( ( session = getSession ( metricConfig ) ) == null ) - { - return platform; - } - - try - { - session.getSingleValue ( "sysName" ); - } - catch ( SNMPException e ) - { - getLog().debug ( "Unable to connect using " + defaultVersion + ", trying version " + fallbackVersion ); - - metricConfig.setValue ( SNMPClient.PROP_VERSION, fallbackVersion ); - - if ( ( session = getSession ( metricConfig ) ) == null ) - { + if ((session = getSession(metricConfig)) == null) { return platform; - } - } + } - String[] keys = getCustomPropertiesSchema().getOptionNames ( ); + try { + session.getSingleValue("sysName"); + } catch (SNMPException e) { + getLog().debug("Unable to connect using " + defaultVersion + ", trying version " + fallbackVersion); + metricConfig.setValue(SNMPClient.PROP_VERSION, fallbackVersion); + if ((session = getSession(metricConfig)) == null) { + return platform; + } + } - for ( int i = 0; i < keys.length; i++ ) - { - String key = keys[i]; + String[] keys = getCustomPropertiesSchema().getOptionNames(); - if ( Character.isUpperCase ( key.charAt ( 0 ) ) ) - { - continue; // Not a MIB name - } + for (int i = 0; i < keys.length; i++) { + String key = keys[i]; + if (Character.isUpperCase(key.charAt(0))) { + continue; // not a mib name + } + String val = getString(session, key); + if (val == null) { + log.debug("'" + key + "'==null"); + continue; + } + cprops.setValue(key, val); + } - String val = getString ( session, key ); + if (!hasConfig) { + // should only happen when the platform is created + config.merge(metricConfig, false); + platform.setProductConfig(config); + platform.setMeasurementConfig(new ConfigResponse()); + log.debug("Setting measurement config=" + metricConfig); + } - if ( val == null ) - { - log.debug ( "'" + key + "'==null" ); + String description = getString(session, "sysDescr"); + if (description != null) { + platform.setDescription(description); - continue; - } - - cprops.setValue(key, val); - } + boolean hasVersionCprop = getCustomPropertiesSchema().getOption(PROP_VERSION) != null; - if ( !hasConfig ) - { - // Should only happen when the platform is created... - config.merge ( metricConfig, false ); + if (hasVersionCprop) { + // this works for Cisco IOS at least + StringTokenizer tok = new StringTokenizer(description, " ,"); - platform.setProductConfig ( config ); - platform.setMeasurementConfig ( new ConfigResponse ( ) ); - - log.debug ( "Setting measurement config=" + metricConfig ); - } - - String description = getString ( session, "sysDescr" ); - - if ( description != null ) - { - platform.setDescription ( description ); - - boolean hasVersionCprop = getCustomPropertiesSchema().getOption ( PROP_VERSION ) != null; - - if ( hasVersionCprop ) - { - // This works for Cisco IOS at least... - StringTokenizer tok = new StringTokenizer ( description, " ," ); - - while ( tok.hasMoreTokens ( ) ) - { - String s = tok.nextToken ( ); - - if ( s.equalsIgnoreCase ( PROP_VERSION ) && tok.hasMoreTokens ( ) ) - { - String version = tok.nextToken ( ); - - cprops.setValue ( PROP_VERSION, version ); - - break; - } + while (tok.hasMoreTokens()) { + String s = tok.nextToken(); + if (s.equalsIgnoreCase(PROP_VERSION) && tok.hasMoreTokens()) { + String version = tok.nextToken(); + cprops.setValue(PROP_VERSION, version); + break; + } + } } - } - } + } - platform.setCustomProperties ( cprops ); + platform.setCustomProperties(cprops); - return platform; - } + return platform; + } } |