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