1 /**
2 * collectd - bindings/java/org/collectd/java/GenericJMXConfConnection.java
3 * Copyright (C) 2009-2012 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 */
27 package org.collectd.java;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Iterator;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.net.InetAddress;
35 import java.net.UnknownHostException;
37 import javax.management.MBeanServerConnection;
39 import javax.management.remote.JMXServiceURL;
40 import javax.management.remote.JMXConnector;
41 import javax.management.remote.JMXConnectorFactory;
43 import org.collectd.api.Collectd;
44 import org.collectd.api.PluginData;
45 import org.collectd.api.OConfigValue;
46 import org.collectd.api.OConfigItem;
48 class GenericJMXConfConnection
49 {
50 private String _username = null;
51 private String _password = null;
52 private String _host = null;
53 private String _instance_prefix = null;
54 private String _service_url = null;
55 private MBeanServerConnection _jmx_connection = null;
56 private List<GenericJMXConfMBean> _mbeans = null;
58 /*
59 * private methods
60 */
61 private String getConfigString (OConfigItem ci) /* {{{ */
62 {
63 List<OConfigValue> values;
64 OConfigValue v;
66 values = ci.getValues ();
67 if (values.size () != 1)
68 {
69 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
70 + " configuration option needs exactly one string argument.");
71 return (null);
72 }
74 v = values.get (0);
75 if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
76 {
77 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
78 + " configuration option needs exactly one string argument.");
79 return (null);
80 }
82 return (v.getString ());
83 } /* }}} String getConfigString */
85 private String getHost () /* {{{ */
86 {
87 if (this._host != null)
88 {
89 return (this._host);
90 }
92 return Collectd.getHostname();
93 } /* }}} String getHost */
95 private void connect () /* {{{ */
96 {
97 JMXServiceURL service_url;
98 JMXConnector connector;
99 Map environment;
101 if (_jmx_connection != null)
102 return;
104 environment = null;
105 if (this._password != null)
106 {
107 String[] credentials;
109 if (this._username == null)
110 this._username = new String ("monitorRole");
112 credentials = new String[] { this._username, this._password };
114 environment = new HashMap ();
115 environment.put (JMXConnector.CREDENTIALS, credentials);
116 environment.put(JMXConnectorFactory.PROTOCOL_PROVIDER_CLASS_LOADER, this.getClass().getClassLoader());
117 }
119 try
120 {
121 service_url = new JMXServiceURL (this._service_url);
122 connector = JMXConnectorFactory.connect (service_url, environment);
123 _jmx_connection = connector.getMBeanServerConnection ();
124 }
125 catch (Exception e)
126 {
127 Collectd.logError ("GenericJMXConfConnection: "
128 + "Creating MBean server connection failed: " + e);
129 return;
130 }
131 } /* }}} void connect */
133 /*
134 * public methods
135 *
136 * <Connection>
137 * Host "tomcat0.mycompany"
138 * ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:17264/jmxrmi"
139 * Collect "java.lang:type=GarbageCollector,name=Copy"
140 * Collect "java.lang:type=Memory"
141 * </Connection>
142 *
143 */
144 public GenericJMXConfConnection (OConfigItem ci) /* {{{ */
145 throws IllegalArgumentException
146 {
147 List<OConfigItem> children;
148 Iterator<OConfigItem> iter;
150 this._mbeans = new ArrayList<GenericJMXConfMBean> ();
152 children = ci.getChildren ();
153 iter = children.iterator ();
154 while (iter.hasNext ())
155 {
156 OConfigItem child = iter.next ();
158 if (child.getKey ().equalsIgnoreCase ("Host"))
159 {
160 String tmp = getConfigString (child);
161 if (tmp != null)
162 this._host = tmp;
163 }
164 else if (child.getKey ().equalsIgnoreCase ("User"))
165 {
166 String tmp = getConfigString (child);
167 if (tmp != null)
168 this._username = tmp;
169 }
170 else if (child.getKey ().equalsIgnoreCase ("Password"))
171 {
172 String tmp = getConfigString (child);
173 if (tmp != null)
174 this._password = tmp;
175 }
176 else if (child.getKey ().equalsIgnoreCase ("ServiceURL"))
177 {
178 String tmp = getConfigString (child);
179 if (tmp != null)
180 this._service_url = tmp;
181 }
182 else if (child.getKey ().equalsIgnoreCase ("InstancePrefix"))
183 {
184 String tmp = getConfigString (child);
185 if (tmp != null)
186 this._instance_prefix = tmp;
187 }
188 else if (child.getKey ().equalsIgnoreCase ("Collect"))
189 {
190 String tmp = getConfigString (child);
191 if (tmp != null)
192 {
193 GenericJMXConfMBean mbean;
195 mbean = GenericJMX.getMBean (tmp);
196 if (mbean == null)
197 throw (new IllegalArgumentException ("No such MBean defined: "
198 + tmp + ". Please make sure all `MBean' blocks appear "
199 + "before (above) all `Connection' blocks."));
200 Collectd.logDebug ("GenericJMXConfConnection: " + this._host + ": Add " + tmp);
201 this._mbeans.add (mbean);
202 }
203 }
204 else
205 throw (new IllegalArgumentException ("Unknown option: "
206 + child.getKey ()));
207 }
209 if (this._service_url == null)
210 throw (new IllegalArgumentException ("No service URL was defined."));
211 if (this._mbeans.size () == 0)
212 throw (new IllegalArgumentException ("No valid collect statement "
213 + "present."));
214 } /* }}} GenericJMXConfConnection (OConfigItem ci) */
216 public void query () /* {{{ */
217 {
218 PluginData pd;
220 connect ();
222 if (this._jmx_connection == null)
223 return;
225 Collectd.logDebug ("GenericJMXConfConnection.query: "
226 + "Reading " + this._mbeans.size () + " mbeans from "
227 + ((this._host != null) ? this._host : "(null)"));
229 pd = new PluginData ();
230 pd.setHost (this.getHost ());
231 pd.setPlugin ("GenericJMX");
233 for (int i = 0; i < this._mbeans.size (); i++)
234 {
235 int status;
237 status = this._mbeans.get (i).query (this._jmx_connection, pd,
238 this._instance_prefix);
239 if (status != 0)
240 {
241 this._jmx_connection = null;
242 return;
243 }
244 } /* for */
245 } /* }}} void query */
247 public String toString ()
248 {
249 return (new String ("host = " + this._host + "; "
250 + "url = " + this._service_url));
251 }
252 }
254 /* vim: set sw=2 sts=2 et fdm=marker : */