0c81bc9ad941760f07021530d6663f32ee34b75f
1 /*
2 * collectd/java - org/collectd/java/GenericJMXConfConnection.java
3 * Copyright (C) 2009,2010 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 verplant.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;
30 import javax.management.MBeanServerConnection;
31 import javax.management.ObjectName;
32 import javax.management.MalformedObjectNameException;
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
44 {
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;
53 /*
54 * private methods
55 */
56 private String getConfigString (OConfigItem ci) /* {{{ */
57 {
58 List<OConfigValue> values;
59 OConfigValue v;
61 values = ci.getValues ();
62 if (values.size () != 1)
63 {
64 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
65 + " configuration option needs exactly one string argument.");
66 return (null);
67 }
69 v = values.get (0);
70 if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
71 {
72 Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
73 + " configuration option needs exactly one string argument.");
74 return (null);
75 }
77 return (v.getString ());
78 } /* }}} String getConfigString */
80 private void connect () /* {{{ */
81 {
82 JMXServiceURL service_url;
83 JMXConnector connector;
84 Map environment;
86 if (_jmx_connection != null)
87 return;
89 environment = null;
90 if (this._password != null)
91 {
92 String[] credentials;
94 if (this._username == null)
95 this._username = new String ("monitorRole");
97 credentials = new String[] { this._username, this._password };
99 environment = new HashMap ();
100 environment.put (JMXConnector.CREDENTIALS, credentials);
101 }
103 try
104 {
105 service_url = new JMXServiceURL (this._service_url);
106 connector = JMXConnectorFactory.connect (service_url, environment);
107 _jmx_connection = connector.getMBeanServerConnection ();
108 }
109 catch (Exception e)
110 {
111 Collectd.logError ("GenericJMXConfConnection: "
112 + "Creating MBean server connection failed: " + e);
113 return;
114 }
115 } /* }}} void connect */
117 /*
118 * public methods
119 *
120 * <Connection>
121 * Host "tomcat0.mycompany"
122 * ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:17264/jmxrmi"
123 * Collect "java.lang:type=GarbageCollector,name=Copy"
124 * Collect "java.lang:type=Memory"
125 * </Connection>
126 *
127 */
128 public GenericJMXConfConnection (OConfigItem ci) /* {{{ */
129 throws IllegalArgumentException
130 {
131 List<OConfigItem> children;
132 Iterator<OConfigItem> iter;
134 this._mbeans = new ArrayList<GenericJMXConfMBean> ();
136 children = ci.getChildren ();
137 iter = children.iterator ();
138 while (iter.hasNext ())
139 {
140 OConfigItem child = iter.next ();
142 if (child.getKey ().equalsIgnoreCase ("Host"))
143 {
144 String tmp = getConfigString (child);
145 if (tmp != null)
146 this._host = tmp;
147 }
148 else if (child.getKey ().equalsIgnoreCase ("User"))
149 {
150 String tmp = getConfigString (child);
151 if (tmp != null)
152 this._username = tmp;
153 }
154 else if (child.getKey ().equalsIgnoreCase ("Password"))
155 {
156 String tmp = getConfigString (child);
157 if (tmp != null)
158 this._password = tmp;
159 }
160 else if (child.getKey ().equalsIgnoreCase ("ServiceURL"))
161 {
162 String tmp = getConfigString (child);
163 if (tmp != null)
164 this._service_url = tmp;
165 }
166 else if (child.getKey ().equalsIgnoreCase ("InstancePrefix"))
167 {
168 String tmp = getConfigString (child);
169 if (tmp != null)
170 this._instance_prefix = tmp;
171 }
172 else if (child.getKey ().equalsIgnoreCase ("Collect"))
173 {
174 String tmp = getConfigString (child);
175 if (tmp != null)
176 {
177 GenericJMXConfMBean mbean;
179 mbean = GenericJMX.getMBean (tmp);
180 if (mbean == null)
181 throw (new IllegalArgumentException ("No such MBean defined: "
182 + tmp + ". Please make sure all `MBean' blocks appear "
183 + "before (above) all `Connection' blocks."));
184 Collectd.logDebug ("GenericJMXConfConnection: " + this._host + ": Add " + tmp);
185 this._mbeans.add (mbean);
186 }
187 }
188 else
189 throw (new IllegalArgumentException ("Unknown option: "
190 + child.getKey ()));
191 }
193 if (this._service_url == null)
194 throw (new IllegalArgumentException ("No service URL was defined."));
195 if (this._mbeans.size () == 0)
196 throw (new IllegalArgumentException ("No valid collect statement "
197 + "present."));
198 } /* }}} GenericJMXConfConnection (OConfigItem ci) */
200 public void query () /* {{{ */
201 {
202 PluginData pd;
204 connect ();
206 if (this._jmx_connection == null)
207 return;
209 Collectd.logDebug ("GenericJMXConfConnection.query: "
210 + "Reading " + this._mbeans.size () + " mbeans from "
211 + ((this._host != null) ? this._host : "(null)"));
213 pd = new PluginData ();
214 pd.setHost ((this._host != null) ? this._host : "localhost");
215 pd.setPlugin ("GenericJMX");
217 for (int i = 0; i < this._mbeans.size (); i++)
218 {
219 int status;
221 status = this._mbeans.get (i).query (this._jmx_connection, pd,
222 this._instance_prefix);
223 if (status != 0)
224 {
225 this._jmx_connection = null;
226 return;
227 }
228 } /* for */
229 } /* }}} void query */
231 public String toString ()
232 {
233 return (new String ("host = " + this._host + "; "
234 + "url = " + this._service_url));
235 }
236 }
238 /* vim: set sw=2 sts=2 et fdm=marker : */