From: <tr...@hy...> - 2010-01-06 19:59:05
|
Author: trader Date: 2010-01-06 11:58:55 -0800 (Wed, 06 Jan 2010) New Revision: 14145 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=14145 Modified: trunk/plugins/netdevice/src/org/hyperic/hq/plugin/netdevice/NetworkDeviceDetector.java Log: Same as revision 14087, reformatted with project settings Modified: trunk/plugins/netdevice/src/org/hyperic/hq/plugin/netdevice/NetworkDeviceDetector.java =================================================================== --- trunk/plugins/netdevice/src/org/hyperic/hq/plugin/netdevice/NetworkDeviceDetector.java 2010-01-06 19:58:10 UTC (rev 14144) +++ trunk/plugins/netdevice/src/org/hyperic/hq/plugin/netdevice/NetworkDeviceDetector.java 2010-01-06 19:58:55 UTC (rev 14145) @@ -1,17 +1,18 @@ /* - * 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], 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 + * 'NetworkDeviceDetector.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. */ @@ -46,7 +47,6 @@ public class NetworkDeviceDetector extends PlatformServiceDetector { - private static final String SVC_NAME = "Interface"; private static final String PROP_IF = SVC_NAME.toLowerCase(); static final String PROP_IF_IX = PROP_IF + ".index"; @@ -55,7 +55,6 @@ private static final String IF_MAC = "ifPhysAddress"; private static final String IP_IF_IX = "ipAdEntIfIndex"; private static final String IP_NETMASK = "ipAdEntNetMask"; - private static final String PROP_IP = "ipaddress"; private static final String PROP_NETMASK = "netmask"; private static final String[] FILTER_PROPS = { PROP_IP, PROP_NETMASK }; @@ -63,7 +62,6 @@ private SNMPSession session; public List discoverServices(ConfigResponse serverConfig) throws PluginException { - Log log = getLog(); List services = new ArrayList(); @@ -86,37 +84,45 @@ protected boolean hasInterfaceService() { String type = getServiceTypeName(SVC_NAME); + ProductPluginManager manager = (ProductPluginManager) getManager().getParent(); + MeasurementPlugin plugin = manager.getMeasurementPlugin(type); + if (plugin == null) { - // Interface service is not defined + // Interface service is not defined... return false; } else { // Check that ifMtu cprop exists, if so assume standard IF-MIB - // interface + // interface... return plugin.getCustomPropertiesSchema().getOption("ifMtu") != null; } } protected List discoverInterfaces(ConfigResponse serverConfig) throws PluginException { + Log log = getLog(); - Log log = getLog(); List services = new ArrayList(); String type = getServiceTypeName(SVC_NAME); if (!hasInterfaceService()) { log.debug("Skipping discovery of " + type); + return services; } String[] keys = getCustomPropertiesSchema(type).getOptionNames(); + HashMap cpropColumns = new HashMap(); + for (int i = 0; i < keys.length; i++) { String key = keys[i]; + if (Arrays.binarySearch(FILTER_PROPS, key) != -1) { continue; } + try { cpropColumns.put(key, getIfColumn(key)); } catch (PluginException e) { @@ -125,40 +131,54 @@ } String columnName = serverConfig.getValue(PROP_IF_IX); + if (columnName == null) { columnName = IF_DESCR; } + Map interfaces = getIfColumn(columnName); + log.debug("Found " + interfaces.size() + " interfaces using " + columnName); String descrColumn = columnName.equals(IF_DESCR) ? IF_NAME : IF_DESCR; + Map descriptions; try { descriptions = getIfColumn(descrColumn); } catch (PluginException e) { descriptions = new HashMap(); + String msg = "Error getting descriptions from " + descrColumn + ": " + e; + log.warn(msg); } List ip_if_ix = getColumn(IP_IF_IX); + HashMap ips = new HashMap(); + HashMap netmasks = new HashMap(); + final String IF_IX_OID = SNMPClient.getOID(IP_IF_IX) + "."; final String NETMASK_OID = SNMPClient.getOID(IP_NETMASK) + "."; + String ip, netmask; for (int i = 0; i < ip_if_ix.size(); i++) { SNMPValue value = (SNMPValue) ip_if_ix.get(i); + String oid = value.getOID(); + String ix = value.toString(); - String ix = value.toString(); if (oid.startsWith(IF_IX_OID)) { ip = oid.substring(IF_IX_OID.length()); + ips.put(ix, ip); + try { netmask = this.session.getSingleValue(NETMASK_OID + ip).toString(); + netmasks.put(ix, netmask); } catch (SNMPException e) { log.debug("Failed to get netmask for " + ip); @@ -169,7 +189,9 @@ for (Iterator it = interfaces.entrySet().iterator(); it.hasNext();) { ConfigResponse config = new ConfigResponse(); ConfigResponse cprops = new ConfigResponse(); + Map.Entry entry = (Map.Entry) it.next(); + String ix = (String) entry.getKey(); String name = (String) entry.getValue(); String mac = null; @@ -179,49 +201,65 @@ config.setValue(PROP_IF, name); config.setValue(PROP_IF_IX, columnName); service.setProductConfig(config); + // required to auto-enable metric service.setMeasurementConfig(); for (int j = 0; j < keys.length; j++) { String key = keys[j]; + Map data = (Map) cpropColumns.get(key); + if (data == null) { continue; } + String val = (String) data.get(ix); + if (val == null) { continue; } + cprops.setValue(key, val); + if (key.equals(IF_MAC)) { mac = val; } } ip = (String) ips.get(ix); + netmask = (String) netmasks.get(ix); + if (ip == null) { ip = "0.0.0.0"; } + if (netmask == null) { netmask = "0.0.0.0"; } + cprops.setValue(PROP_IP, ip); cprops.setValue(PROP_NETMASK, netmask); service.setCustomProperties(cprops); - // might be more than 1 interface w/ the same name - // so append the mac address to make it unique + // Might be more than 1 interface w/ the same name, + // so append the mac address to make it unique... name = name + " " + SVC_NAME; + if ((mac != null) && !mac.equals("0:0:0:0:0:0")) { name += " (" + mac + ")"; } + service.setServiceName(name); + Object obj = descriptions.get(ix); + if (obj != null) { service.setDescription(obj.toString()); } + services.add(service); } @@ -229,7 +267,6 @@ } static SNMPSession getSession(ConfigResponse config) throws PluginException { - try { return new SNMPClient().getSession(config); } catch (SNMPException e) { @@ -237,7 +274,7 @@ } } - // XXX these could be in a base class of some sort + // These could be in a base class of some sort... protected void openSession(ConfigResponse config) throws PluginException { this.session = getSession(config); } @@ -248,24 +285,32 @@ private String getIfIndex(SNMPValue val) { String oid = val.getOID(); + int last = oid.lastIndexOf('.'); + return oid.substring(last + 1); } protected Map getIfColumn(String name) throws PluginException { Map map = new LinkedHashMap(); + List column = getColumn(name); + for (int i = 0; i < column.size(); i++) { SNMPValue ent = (SNMPValue) column.get(i); + String ix = getIfIndex(ent); String val; + if (name.equals(IF_MAC)) { val = ent.toPhysAddressString(); } else { val = ent.toString().trim(); } + map.put(ix, val); } + return map; } @@ -277,30 +322,39 @@ } } - // use platform.name instead of the generic type name + // Use platform.name instead of the generic type name... protected String getServerName(ConfigResponse config) { String fqdn = config.getValue(ProductPlugin.PROP_PLATFORM_FQDN); String name = config.getValue(ProductPlugin.PROP_PLATFORM_NAME); + return fqdn + " " + name; } public List getServerResources(ConfigResponse config) { Log log = getLog(); + if (log.isDebugEnabled()) { log.debug("Testing snmp config=" + config); } + if (config.getValue(SNMPClient.PROP_IP) == null) { log.debug("snmp config incomplete, defering server creation"); + return null; } + try { getSession(config).getSingleValue("sysName"); + } catch (Exception e) { - // wait till we have valid snmp config at the platform level + // Wait till we have valid snmp config at the platform level... log.debug("snmp config invalid, defering server creation"); + return null; } + log.debug("snmp config valid, creating server"); + return super.getServerResources(config); } |