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;
30 import java.net.UnknownHostException;
32 import javax.management.MBeanServerConnection;
34 import javax.management.remote.JMXServiceURL;
35 import javax.management.remote.JMXConnector;
36 import javax.management.remote.JMXConnectorFactory;
38 import org.collectd.api.Collectd;
39 import org.collectd.api.PluginData;
40 import org.collectd.api.OConfigValue;
41 import org.collectd.api.OConfigItem;
43 class GenericJMXConfConnection
45 private String _username = null;
46 private String _password = null;
47 private String _host = null;
48 private String _instance_prefix = null;
49 private String _service_url = null;
50 private MBeanServerConnection _jmx_connection = null;
51 private List<GenericJMXConfMBean> _mbeans = null;
56 private String getConfigString (OConfigItem ci) /* {{{ */
58 List<OConfigValue> values;
61 values = ci.getValues ();
62 if (values.size () != 1)
64 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
65 + " configuration option needs exactly one string argument.");
70 if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
72 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
73 + " configuration option needs exactly one string argument.");
77 return (v.getString ());
78 } /* }}} String getConfigString */
80 private String getHost () /* {{{ */
82 if (this._host != null)
89 InetAddress localHost = InetAddress.getLocalHost();
90 return (localHost.getHostName ());
92 catch (UnknownHostException e)
96 } /* }}} String getHost */
98 private void connect () /* {{{ */
100 JMXServiceURL service_url;
101 JMXConnector connector;
104 if (_jmx_connection != null)
108 if (this._password != null)
110 String[] credentials;
112 if (this._username == null)
113 this._username = new String ("monitorRole");
115 credentials = new String[] { this._username, this._password };
117 environment = new HashMap ();
118 environment.put (JMXConnector.CREDENTIALS, credentials);
123 service_url = new JMXServiceURL (this._service_url);
124 connector = JMXConnectorFactory.connect (service_url, environment);
125 _jmx_connection = connector.getMBeanServerConnection ();
129 Collectd.logError ("GenericJMXConfConnection: "
130 + "Creating MBean server connection failed: " + e);
133 } /* }}} void connect */
139 * Host "tomcat0.mycompany"
140 * ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:17264/jmxrmi"
141 * Collect "java.lang:type=GarbageCollector,name=Copy"
142 * Collect "java.lang:type=Memory"
146 public GenericJMXConfConnection (OConfigItem ci) /* {{{ */
147 throws IllegalArgumentException
149 List<OConfigItem> children;
150 Iterator<OConfigItem> iter;
152 this._mbeans = new ArrayList<GenericJMXConfMBean> ();
154 children = ci.getChildren ();
155 iter = children.iterator ();
156 while (iter.hasNext ())
158 OConfigItem child = iter.next ();
160 if (child.getKey ().equalsIgnoreCase ("Host"))
162 String tmp = getConfigString (child);
166 else if (child.getKey ().equalsIgnoreCase ("User"))
168 String tmp = getConfigString (child);
170 this._username = tmp;
172 else if (child.getKey ().equalsIgnoreCase ("Password"))
174 String tmp = getConfigString (child);
176 this._password = tmp;
178 else if (child.getKey ().equalsIgnoreCase ("ServiceURL"))
180 String tmp = getConfigString (child);
182 this._service_url = tmp;
184 else if (child.getKey ().equalsIgnoreCase ("InstancePrefix"))
186 String tmp = getConfigString (child);
188 this._instance_prefix = tmp;
190 else if (child.getKey ().equalsIgnoreCase ("Collect"))
192 String tmp = getConfigString (child);
195 GenericJMXConfMBean mbean;
197 mbean = GenericJMX.getMBean (tmp);
199 throw (new IllegalArgumentException ("No such MBean defined: "
200 + tmp + ". Please make sure all `MBean' blocks appear "
201 + "before (above) all `Connection' blocks."));
202 Collectd.logDebug ("GenericJMXConfConnection: " + this._host + ": Add " + tmp);
203 this._mbeans.add (mbean);
207 throw (new IllegalArgumentException ("Unknown option: "
211 if (this._service_url == null)
212 throw (new IllegalArgumentException ("No service URL was defined."));
213 if (this._mbeans.size () == 0)
214 throw (new IllegalArgumentException ("No valid collect statement "
216 } /* }}} GenericJMXConfConnection (OConfigItem ci) */
218 public void query () /* {{{ */
224 if (this._jmx_connection == null)
227 Collectd.logDebug ("GenericJMXConfConnection.query: "
228 + "Reading " + this._mbeans.size () + " mbeans from "
229 + ((this._host != null) ? this._host : "(null)"));
231 pd = new PluginData ();
232 pd.setHost (this.getHost ());
233 pd.setPlugin ("GenericJMX");
235 for (int i = 0; i < this._mbeans.size (); i++)
239 status = this._mbeans.get (i).query (this._jmx_connection, pd,
240 this._instance_prefix);
243 this._jmx_connection = null;
247 } /* }}} void query */
249 public String toString ()
251 return (new String ("host = " + this._host + "; "
252 + "url = " + this._service_url));
256 /* vim: set sw=2 sts=2 et fdm=marker : */