2 * collectd/java - org/collectd/java/GenericJMXConfConnection.java
3 * Copyright (C) 2009-2012 Florian octo Forster
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Florian octo Forster <octo at collectd.org>
22 package org.collectd.java;
24 import java.util.List;
26 import java.util.Iterator;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.net.InetAddress;
31 import javax.management.MBeanServerConnection;
32 import javax.management.ObjectName;
33 import javax.management.MalformedObjectNameException;
35 import javax.management.remote.JMXServiceURL;
36 import javax.management.remote.JMXConnector;
37 import javax.management.remote.JMXConnectorFactory;
39 import org.collectd.api.Collectd;
40 import org.collectd.api.PluginData;
41 import org.collectd.api.OConfigValue;
42 import org.collectd.api.OConfigItem;
44 class GenericJMXConfConnection
46 private String _username = null;
47 private String _password = null;
48 private String _host = null;
49 private String _instance_prefix = null;
50 private String _service_url = null;
51 private MBeanServerConnection _jmx_connection = null;
52 private List<GenericJMXConfMBean> _mbeans = null;
57 private String getConfigString (OConfigItem ci) /* {{{ */
59 List<OConfigValue> values;
62 values = ci.getValues ();
63 if (values.size () != 1)
65 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
66 + " configuration option needs exactly one string argument.");
71 if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
73 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
74 + " configuration option needs exactly one string argument.");
78 return (v.getString ());
79 } /* }}} String getConfigString */
81 private String getHost () /* {{{ */
83 if (this._host != null)
90 InetAddress localHost = InetAddress.getLocalHost();
91 return (localHost.getHostName ());
93 catch (UnknownHostException e)
97 } /* }}} String getHost */
99 private void connect () /* {{{ */
101 JMXServiceURL service_url;
102 JMXConnector connector;
105 if (_jmx_connection != null)
109 if (this._password != null)
111 String[] credentials;
113 if (this._username == null)
114 this._username = new String ("monitorRole");
116 credentials = new String[] { this._username, this._password };
118 environment = new HashMap ();
119 environment.put (JMXConnector.CREDENTIALS, credentials);
124 service_url = new JMXServiceURL (this._service_url);
125 connector = JMXConnectorFactory.connect (service_url, environment);
126 _jmx_connection = connector.getMBeanServerConnection ();
130 Collectd.logError ("GenericJMXConfConnection: "
131 + "Creating MBean server connection failed: " + e);
134 } /* }}} void connect */
140 * Host "tomcat0.mycompany"
141 * ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:17264/jmxrmi"
142 * Collect "java.lang:type=GarbageCollector,name=Copy"
143 * Collect "java.lang:type=Memory"
147 public GenericJMXConfConnection (OConfigItem ci) /* {{{ */
148 throws IllegalArgumentException
150 List<OConfigItem> children;
151 Iterator<OConfigItem> iter;
153 this._mbeans = new ArrayList<GenericJMXConfMBean> ();
155 children = ci.getChildren ();
156 iter = children.iterator ();
157 while (iter.hasNext ())
159 OConfigItem child = iter.next ();
161 if (child.getKey ().equalsIgnoreCase ("Host"))
163 String tmp = getConfigString (child);
167 else if (child.getKey ().equalsIgnoreCase ("User"))
169 String tmp = getConfigString (child);
171 this._username = tmp;
173 else if (child.getKey ().equalsIgnoreCase ("Password"))
175 String tmp = getConfigString (child);
177 this._password = tmp;
179 else if (child.getKey ().equalsIgnoreCase ("ServiceURL"))
181 String tmp = getConfigString (child);
183 this._service_url = tmp;
185 else if (child.getKey ().equalsIgnoreCase ("InstancePrefix"))
187 String tmp = getConfigString (child);
189 this._instance_prefix = tmp;
191 else if (child.getKey ().equalsIgnoreCase ("Collect"))
193 String tmp = getConfigString (child);
196 GenericJMXConfMBean mbean;
198 mbean = GenericJMX.getMBean (tmp);
200 throw (new IllegalArgumentException ("No such MBean defined: "
201 + tmp + ". Please make sure all `MBean' blocks appear "
202 + "before (above) all `Connection' blocks."));
203 Collectd.logDebug ("GenericJMXConfConnection: " + this._host + ": Add " + tmp);
204 this._mbeans.add (mbean);
208 throw (new IllegalArgumentException ("Unknown option: "
212 if (this._service_url == null)
213 throw (new IllegalArgumentException ("No service URL was defined."));
214 if (this._mbeans.size () == 0)
215 throw (new IllegalArgumentException ("No valid collect statement "
217 } /* }}} GenericJMXConfConnection (OConfigItem ci) */
219 public void query () /* {{{ */
225 if (this._jmx_connection == null)
228 Collectd.logDebug ("GenericJMXConfConnection.query: "
229 + "Reading " + this._mbeans.size () + " mbeans from "
230 + ((this._host != null) ? this._host : "(null)"));
232 pd = new PluginData ();
233 pd.setHost (this.getHost ());
234 pd.setPlugin ("GenericJMX");
236 for (int i = 0; i < this._mbeans.size (); i++)
240 status = this._mbeans.get (i).query (this._jmx_connection, pd,
241 this._instance_prefix);
244 this._jmx_connection = null;
248 } /* }}} void query */
250 public String toString ()
252 return (new String ("host = " + this._host + "; "
253 + "url = " + this._service_url));
257 /* vim: set sw=2 sts=2 et fdm=marker : */