1 /*
2 * collectd/java - org/collectd/java/GenericJMXConfConnection.java
3 * Copyright (C) 2009-2012 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at collectd.org>
20 */
22 package org.collectd.java;
24 import java.util.List;
25 import java.util.Map;
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;
33 import javax.management.ObjectName;
34 import javax.management.MalformedObjectNameException;
36 import javax.management.remote.JMXServiceURL;
37 import javax.management.remote.JMXConnector;
38 import javax.management.remote.JMXConnectorFactory;
40 import org.collectd.api.Collectd;
41 import org.collectd.api.PluginData;
42 import org.collectd.api.OConfigValue;
43 import org.collectd.api.OConfigItem;
45 class GenericJMXConfConnection
46 {
47 private String _username = null;
48 private String _password = null;
49 private String _host = null;
50 private String _instance_prefix = null;
51 private String _service_url = null;
52 private MBeanServerConnection _jmx_connection = null;
53 private List<GenericJMXConfMBean> _mbeans = null;
55 /*
56 * private methods
57 */
58 private String getConfigString (OConfigItem ci) /* {{{ */
59 {
60 List<OConfigValue> values;
61 OConfigValue v;
63 values = ci.getValues ();
64 if (values.size () != 1)
65 {
66 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
67 + " configuration option needs exactly one string argument.");
68 return (null);
69 }
71 v = values.get (0);
72 if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
73 {
74 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
75 + " configuration option needs exactly one string argument.");
76 return (null);
77 }
79 return (v.getString ());
80 } /* }}} String getConfigString */
82 private String getHost () /* {{{ */
83 {
84 if (this._host != null)
85 {
86 return (this._host);
87 }
89 return Collectd.getHostname();
90 } /* }}} String getHost */
92 private void connect () /* {{{ */
93 {
94 JMXServiceURL service_url;
95 JMXConnector connector;
96 Map environment;
98 if (_jmx_connection != null)
99 return;
101 environment = null;
102 if (this._password != null)
103 {
104 String[] credentials;
106 if (this._username == null)
107 this._username = new String ("monitorRole");
109 credentials = new String[] { this._username, this._password };
111 environment = new HashMap ();
112 environment.put (JMXConnector.CREDENTIALS, credentials);
113 environment.put(JMXConnectorFactory.PROTOCOL_PROVIDER_CLASS_LOADER, this.getClass().getClassLoader());
114 }
116 try
117 {
118 service_url = new JMXServiceURL (this._service_url);
119 connector = JMXConnectorFactory.connect (service_url, environment);
120 _jmx_connection = connector.getMBeanServerConnection ();
121 }
122 catch (Exception e)
123 {
124 Collectd.logError ("GenericJMXConfConnection: "
125 + "Creating MBean server connection failed: " + e);
126 return;
127 }
128 } /* }}} void connect */
130 /*
131 * public methods
132 *
133 * <Connection>
134 * Host "tomcat0.mycompany"
135 * ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:17264/jmxrmi"
136 * Collect "java.lang:type=GarbageCollector,name=Copy"
137 * Collect "java.lang:type=Memory"
138 * </Connection>
139 *
140 */
141 public GenericJMXConfConnection (OConfigItem ci) /* {{{ */
142 throws IllegalArgumentException
143 {
144 List<OConfigItem> children;
145 Iterator<OConfigItem> iter;
147 this._mbeans = new ArrayList<GenericJMXConfMBean> ();
149 children = ci.getChildren ();
150 iter = children.iterator ();
151 while (iter.hasNext ())
152 {
153 OConfigItem child = iter.next ();
155 if (child.getKey ().equalsIgnoreCase ("Host"))
156 {
157 String tmp = getConfigString (child);
158 if (tmp != null)
159 this._host = tmp;
160 }
161 else if (child.getKey ().equalsIgnoreCase ("User"))
162 {
163 String tmp = getConfigString (child);
164 if (tmp != null)
165 this._username = tmp;
166 }
167 else if (child.getKey ().equalsIgnoreCase ("Password"))
168 {
169 String tmp = getConfigString (child);
170 if (tmp != null)
171 this._password = tmp;
172 }
173 else if (child.getKey ().equalsIgnoreCase ("ServiceURL"))
174 {
175 String tmp = getConfigString (child);
176 if (tmp != null)
177 this._service_url = tmp;
178 }
179 else if (child.getKey ().equalsIgnoreCase ("InstancePrefix"))
180 {
181 String tmp = getConfigString (child);
182 if (tmp != null)
183 this._instance_prefix = tmp;
184 }
185 else if (child.getKey ().equalsIgnoreCase ("Collect"))
186 {
187 String tmp = getConfigString (child);
188 if (tmp != null)
189 {
190 GenericJMXConfMBean mbean;
192 mbean = GenericJMX.getMBean (tmp);
193 if (mbean == null)
194 throw (new IllegalArgumentException ("No such MBean defined: "
195 + tmp + ". Please make sure all `MBean' blocks appear "
196 + "before (above) all `Connection' blocks."));
197 Collectd.logDebug ("GenericJMXConfConnection: " + this._host + ": Add " + tmp);
198 this._mbeans.add (mbean);
199 }
200 }
201 else
202 throw (new IllegalArgumentException ("Unknown option: "
203 + child.getKey ()));
204 }
206 if (this._service_url == null)
207 throw (new IllegalArgumentException ("No service URL was defined."));
208 if (this._mbeans.size () == 0)
209 throw (new IllegalArgumentException ("No valid collect statement "
210 + "present."));
211 } /* }}} GenericJMXConfConnection (OConfigItem ci) */
213 public void query () /* {{{ */
214 {
215 PluginData pd;
217 connect ();
219 if (this._jmx_connection == null)
220 return;
222 Collectd.logDebug ("GenericJMXConfConnection.query: "
223 + "Reading " + this._mbeans.size () + " mbeans from "
224 + ((this._host != null) ? this._host : "(null)"));
226 pd = new PluginData ();
227 pd.setHost (this.getHost ());
228 pd.setPlugin ("GenericJMX");
230 for (int i = 0; i < this._mbeans.size (); i++)
231 {
232 int status;
234 status = this._mbeans.get (i).query (this._jmx_connection, pd,
235 this._instance_prefix);
236 if (status != 0)
237 {
238 this._jmx_connection = null;
239 return;
240 }
241 } /* for */
242 } /* }}} void query */
244 public String toString ()
245 {
246 return (new String ("host = " + this._host + "; "
247 + "url = " + this._service_url));
248 }
249 }
251 /* vim: set sw=2 sts=2 et fdm=marker : */