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;
38 import javax.management.ObjectName;
39 import javax.management.MalformedObjectNameException;
41 import javax.management.remote.JMXServiceURL;
42 import javax.management.remote.JMXConnector;
43 import javax.management.remote.JMXConnectorFactory;
45 import org.collectd.api.Collectd;
46 import org.collectd.api.PluginData;
47 import org.collectd.api.OConfigValue;
48 import org.collectd.api.OConfigItem;
50 class GenericJMXConfConnection
51 {
52 private String _username = null;
53 private String _password = null;
54 private String _host = null;
55 private String _instance_prefix = null;
56 private String _service_url = null;
57 private MBeanServerConnection _jmx_connection = null;
58 private List<GenericJMXConfMBean> _mbeans = null;
60 /*
61 * private methods
62 */
63 private String getConfigString (OConfigItem ci) /* {{{ */
64 {
65 List<OConfigValue> values;
66 OConfigValue v;
68 values = ci.getValues ();
69 if (values.size () != 1)
70 {
71 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
72 + " configuration option needs exactly one string argument.");
73 return (null);
74 }
76 v = values.get (0);
77 if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
78 {
79 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
80 + " configuration option needs exactly one string argument.");
81 return (null);
82 }
84 return (v.getString ());
85 } /* }}} String getConfigString */
87 private String getHost () /* {{{ */
88 {
89 if (this._host != null)
90 {
91 return (this._host);
92 }
94 try
95 {
96 InetAddress localHost = InetAddress.getLocalHost();
97 return (localHost.getHostName ());
98 }
99 catch (UnknownHostException e)
100 {
101 return ("localhost");
102 }
103 } /* }}} String getHost */
105 private void connect () /* {{{ */
106 {
107 JMXServiceURL service_url;
108 JMXConnector connector;
109 Map environment;
111 if (_jmx_connection != null)
112 return;
114 environment = null;
115 if (this._password != null)
116 {
117 String[] credentials;
119 if (this._username == null)
120 this._username = new String ("monitorRole");
122 credentials = new String[] { this._username, this._password };
124 environment = new HashMap ();
125 environment.put (JMXConnector.CREDENTIALS, credentials);
126 environment.put(JMXConnectorFactory.PROTOCOL_PROVIDER_CLASS_LOADER, this.getClass().getClassLoader());
127 }
129 try
130 {
131 service_url = new JMXServiceURL (this._service_url);
132 connector = JMXConnectorFactory.connect (service_url, environment);
133 _jmx_connection = connector.getMBeanServerConnection ();
134 }
135 catch (Exception e)
136 {
137 Collectd.logError ("GenericJMXConfConnection: "
138 + "Creating MBean server connection failed: " + e);
139 return;
140 }
141 } /* }}} void connect */
143 /*
144 * public methods
145 *
146 * <Connection>
147 * Host "tomcat0.mycompany"
148 * ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:17264/jmxrmi"
149 * Collect "java.lang:type=GarbageCollector,name=Copy"
150 * Collect "java.lang:type=Memory"
151 * </Connection>
152 *
153 */
154 public GenericJMXConfConnection (OConfigItem ci) /* {{{ */
155 throws IllegalArgumentException
156 {
157 List<OConfigItem> children;
158 Iterator<OConfigItem> iter;
160 this._mbeans = new ArrayList<GenericJMXConfMBean> ();
162 children = ci.getChildren ();
163 iter = children.iterator ();
164 while (iter.hasNext ())
165 {
166 OConfigItem child = iter.next ();
168 if (child.getKey ().equalsIgnoreCase ("Host"))
169 {
170 String tmp = getConfigString (child);
171 if (tmp != null)
172 this._host = tmp;
173 }
174 else if (child.getKey ().equalsIgnoreCase ("User"))
175 {
176 String tmp = getConfigString (child);
177 if (tmp != null)
178 this._username = tmp;
179 }
180 else if (child.getKey ().equalsIgnoreCase ("Password"))
181 {
182 String tmp = getConfigString (child);
183 if (tmp != null)
184 this._password = tmp;
185 }
186 else if (child.getKey ().equalsIgnoreCase ("ServiceURL"))
187 {
188 String tmp = getConfigString (child);
189 if (tmp != null)
190 this._service_url = tmp;
191 }
192 else if (child.getKey ().equalsIgnoreCase ("InstancePrefix"))
193 {
194 String tmp = getConfigString (child);
195 if (tmp != null)
196 this._instance_prefix = tmp;
197 }
198 else if (child.getKey ().equalsIgnoreCase ("Collect"))
199 {
200 String tmp = getConfigString (child);
201 if (tmp != null)
202 {
203 GenericJMXConfMBean mbean;
205 mbean = GenericJMX.getMBean (tmp);
206 if (mbean == null)
207 throw (new IllegalArgumentException ("No such MBean defined: "
208 + tmp + ". Please make sure all `MBean' blocks appear "
209 + "before (above) all `Connection' blocks."));
210 Collectd.logDebug ("GenericJMXConfConnection: " + this._host + ": Add " + tmp);
211 this._mbeans.add (mbean);
212 }
213 }
214 else
215 throw (new IllegalArgumentException ("Unknown option: "
216 + child.getKey ()));
217 }
219 if (this._service_url == null)
220 throw (new IllegalArgumentException ("No service URL was defined."));
221 if (this._mbeans.size () == 0)
222 throw (new IllegalArgumentException ("No valid collect statement "
223 + "present."));
224 } /* }}} GenericJMXConfConnection (OConfigItem ci) */
226 public void query () /* {{{ */
227 {
228 PluginData pd;
230 connect ();
232 if (this._jmx_connection == null)
233 return;
235 Collectd.logDebug ("GenericJMXConfConnection.query: "
236 + "Reading " + this._mbeans.size () + " mbeans from "
237 + ((this._host != null) ? this._host : "(null)"));
239 pd = new PluginData ();
240 pd.setHost (this.getHost ());
241 pd.setPlugin ("GenericJMX");
243 for (int i = 0; i < this._mbeans.size (); i++)
244 {
245 int status;
247 status = this._mbeans.get (i).query (this._jmx_connection, pd,
248 this._instance_prefix);
249 if (status != 0)
250 {
251 this._jmx_connection = null;
252 return;
253 }
254 } /* for */
255 } /* }}} void query */
257 public String toString ()
258 {
259 return (new String ("host = " + this._host + "; "
260 + "url = " + this._service_url));
261 }
262 }
264 /* vim: set sw=2 sts=2 et fdm=marker : */