From: <sc...@hy...> - 2007-06-07 23:58:39
|
Author: scottmf Date: 2007-06-07 16:58:37 -0700 (Thu, 07 Jun 2007) New Revision: 4700 URL: http://svn.hyperic.org/?view=rev&root=Hyperic+HQ&revision=4700 Added: trunk/plugins/openldap/src/org/hyperic/hq/plugin/openldap/OpenLDAPMeasurementPlugin.java trunk/plugins/openldap/src/org/hyperic/hq/plugin/openldap/OpenLDAPServerDetector.java Log: [HHQ-918] openldap plugin auto discovered and with all metrics Added: trunk/plugins/openldap/src/org/hyperic/hq/plugin/openldap/OpenLDAPMeasurementPlugin.java =================================================================== --- trunk/plugins/openldap/src/org/hyperic/hq/plugin/openldap/OpenLDAPMeasurementPlugin.java (rev 0) +++ trunk/plugins/openldap/src/org/hyperic/hq/plugin/openldap/OpenLDAPMeasurementPlugin.java 2007-06-07 23:58:37 UTC (rev 4700) @@ -0,0 +1,195 @@ +/* + * 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 Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +package org.hyperic.hq.plugin.openldap; + +import org.hyperic.util.config.ConfigResponse; +import org.hyperic.util.config.ConfigSchema; +import org.hyperic.util.config.SchemaBuilder; +import org.hyperic.hq.product.MeasurementPlugin; +import org.hyperic.hq.product.Metric; +import org.hyperic.hq.product.MetricUnreachableException; +import org.hyperic.hq.product.MetricInvalidException; +import org.hyperic.hq.product.MetricNotFoundException; +import org.hyperic.hq.product.MetricValue; +import org.hyperic.hq.product.PluginException; +import org.hyperic.hq.product.PluginManager; + +import org.hyperic.sigar.Sigar; +import org.hyperic.sigar.SigarException; +import org.hyperic.sigar.ptql.ProcessFinder; + +import javax.naming.Context; +import javax.naming.directory.Attribute; +import javax.naming.directory.Attributes; +import javax.naming.directory.DirContext; +import javax.naming.directory.InitialDirContext; +import javax.naming.directory.InvalidSearchControlsException; +import javax.naming.directory.InvalidSearchFilterException; +import javax.naming.directory.SearchControls; +import javax.naming.directory.SearchResult; +import javax.naming.NamingEnumeration; +import javax.naming.NamingException; + +import java.util.Collection; +import java.util.Hashtable; +import java.util.Properties; +import java.util.TreeSet; + +public class OpenLDAPMeasurementPlugin + extends MeasurementPlugin +{ + private Sigar mySigar = new Sigar(); + private Integer myInt = new Integer(0); + private DirContext ctx = null; + + public OpenLDAPMeasurementPlugin() + { + } + + public DirContext getDirContext(Metric metric) + throws NamingException + { + if (ctx != null) + return ctx; + synchronized(myInt) + { + if (ctx != null) + return ctx; + Properties props = metric.getProperties(); + Collection rtn = new TreeSet(); + Hashtable ldapEnv = new Hashtable(); + String ldapDriver = props.getProperty("ldapDriver"), + ldapHostURL = props.getProperty("ldapHostURL"), + ldapAuthType = props.getProperty("ldapAuthType"), + ldapPasswd = "", +// ldapPasswd = props.getProperty("ldapPasswd"), + ldapTreePathToDN = ""; +// ldapTreePathToDN = props.getProperty("ldapTreePathToDN"); + ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, ldapDriver); + ldapEnv.put(Context.PROVIDER_URL, ldapHostURL); + ldapEnv.put(Context.SECURITY_AUTHENTICATION, ldapAuthType); + ldapEnv.put(Context.SECURITY_PRINCIPAL, ldapTreePathToDN); + ldapEnv.put(Context.SECURITY_CREDENTIALS, ldapPasswd); + ctx = new InitialDirContext(ldapEnv); + return ctx; + } + } + + public MetricValue getValue(Metric metric) + throws PluginException, + MetricUnreachableException, + MetricInvalidException, + MetricNotFoundException + { + // will look like "generic:Type=GenericService,option1=option1,option2=option2" + String objectName = metric.getObjectName(); + String option1 = metric.getObjectProperty("option1"), + option2 = metric.getObjectProperty("option2"); + + // will look like "Availability" + // -OR- + // "cn=PDU,cn=Statistics,cn=Monitor:monitorCounter" + String alias = metric.getAttributeName(); + + try + { + if (objectName.indexOf("Server") != -1) + { + if (alias.equalsIgnoreCase("availability")) + return getAvail(metric); + + String[] attrs = alias.split(":"); + if (attrs[0] == null || attrs[1] == null) + throw new MetricNotFoundException(""); + + return getMetric(metric, attrs[0], attrs[1]); + } + throw new MetricNotFoundException(""); + } + catch (NamingException e) { + throw new MetricNotFoundException("Service "+objectName+", "+alias+" not found", e); + } + catch (MetricNotFoundException e) { + throw new MetricNotFoundException("Service "+objectName+", "+alias+" not found", e); + } + } + + private MetricValue getMetric(Metric metric, String tree, String attr) + throws MetricNotFoundException, NamingException + { + NamingEnumeration enumer = null; + try + { + String[] a = {attr}; + SearchControls cons = new SearchControls(); + cons.setSearchScope(SearchControls.OBJECT_SCOPE); + cons.setReturningAttributes(a); + enumer = getDirContext(metric).search(tree, + "(&(objectClass=*))", cons); + while (enumer.hasMore()) + { + SearchResult searchresult = (SearchResult)enumer.next(); + Attributes attrs = searchresult.getAttributes(); + Attribute val; + if (null != (val = attrs.get(attr))) { + return new MetricValue(new Double(val.get().toString()), + System.currentTimeMillis()); + } + } + throw new MetricNotFoundException(""); + } + finally { + if (enumer != null) enumer.close(); + } + } + + private MetricValue getAvail(Metric metric) + throws NamingException + { + NamingEnumeration enumer = null; + try + { + String[] a = {"+"}; + SearchControls cons = new SearchControls(); + cons.setSearchScope(SearchControls.OBJECT_SCOPE); + cons.setReturningAttributes(a); + enumer = getDirContext(metric).search("", "(&(objectClass=*))", cons); + return new MetricValue(Metric.AVAIL_UP, System.currentTimeMillis()); + } + catch (InvalidSearchFilterException e) { + return new MetricValue(Metric.AVAIL_DOWN, System.currentTimeMillis()); + } + catch (InvalidSearchControlsException e) { + return new MetricValue(Metric.AVAIL_DOWN, System.currentTimeMillis()); + } + catch (NamingException e) { + return new MetricValue(Metric.AVAIL_DOWN, System.currentTimeMillis()); + } + finally { + if (enumer != null) enumer.close(); + } + } +} Added: trunk/plugins/openldap/src/org/hyperic/hq/plugin/openldap/OpenLDAPServerDetector.java =================================================================== --- trunk/plugins/openldap/src/org/hyperic/hq/plugin/openldap/OpenLDAPServerDetector.java (rev 0) +++ trunk/plugins/openldap/src/org/hyperic/hq/plugin/openldap/OpenLDAPServerDetector.java 2007-06-07 23:58:37 UTC (rev 4700) @@ -0,0 +1,125 @@ +/* + * 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 Place, Suite 330, Boston, MA 02111-1307 + * USA. + */ + +package org.hyperic.hq.plugin.openldap; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Iterator; + +import org.hyperic.hq.product.AutoServerDetector; +import org.hyperic.hq.product.FileServerDetector; +import org.hyperic.hq.product.MeasurementPlugin; +import org.hyperic.hq.product.Metric; +import org.hyperic.hq.product.MetricUnreachableException; +import org.hyperic.hq.product.MetricInvalidException; +import org.hyperic.hq.product.MetricNotFoundException; +import org.hyperic.hq.product.MetricValue; +import org.hyperic.hq.product.PluginException; +import org.hyperic.hq.product.ServerDetector; +import org.hyperic.hq.product.ServerResource; +import org.hyperic.hq.product.ServiceResource; + +import org.hyperic.util.config.ConfigResponse; +import org.hyperic.util.config.ConfigSchema; +import org.hyperic.util.config.SchemaBuilder; + +import org.hyperic.sigar.win32.RegistryKey; + +public class OpenLDAPServerDetector + extends ServerDetector + implements AutoServerDetector +{ + private static final String SERVER_NAME = "OpenLDAP"; + // generic process name, generic server daemon + private static final String PROCESS_NAME = "slapd"; + // this PTQL query matches the PROCESS_NAME and returns the parent process id + private static final String PTQL_QUERY = + "State.Name.re="+PROCESS_NAME+",State.Name.Pne=$1,Args.0.re=.*"+PROCESS_NAME+"$"; + + public List getServerResources(ConfigResponse platformConfig) + throws PluginException + { + List servers = new ArrayList(); + List paths = getServerProcessList(); + for (int i=0; i<paths.size(); i++) + { + String dir = (String)paths.get(i); + List found = getServerList(dir); + if (!found.isEmpty()) + servers.addAll(found); + } + return servers; + } + + private static List getServerProcessList() + { + List servers = new ArrayList(); + long[] pids = getPids(PTQL_QUERY); + for (int i=0; i<pids.length; i++) + { + String exe = getProcExe(pids[i]); + if (exe == null) + continue; + File binary = new File(exe); + if (!binary.isAbsolute()) + continue; + servers.add(binary.getAbsolutePath()); + } + return servers; + } + + public List getServerList(String path) throws PluginException + { + ConfigResponse productConfig = new ConfigResponse(); + productConfig.setValue("username", "myuser"); + productConfig.setValue("password", "mypassword"); + + List servers = new ArrayList(); + String installdir = getParentDir(path, 2); + String version = ""; + + // Only check the binaries if they match the path we expect + if (path.indexOf(PROCESS_NAME) == -1) + return servers; + + if ((new File(installdir+"/lib/libldap-2.3.so.0")).exists()) + version = "2.3.x"; + + ServerResource server = createServerResource(installdir); + // Set custom properties + ConfigResponse cprop = new ConfigResponse(); + cprop.setValue("version", version); + server.setCustomProperties(cprop); + server.setProductConfig(); + // sets a default Measurement Config property with no values + server.setMeasurementConfig(); + server.setName(SERVER_NAME+" "+version); + servers.add(server); + + return servers; + } +} |