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