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