Merge branch 'collectd-4.7'
[collectd.git] / bindings / java / org / collectd / java / GenericJMXConfConnection.java
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  */
21
22 package org.collectd.java;
23
24 import java.util.List;
25 import java.util.Iterator;
26 import java.util.ArrayList;
27
28 import javax.management.MBeanServerConnection;
29 import javax.management.ObjectName;
30 import javax.management.MalformedObjectNameException;
31
32 import javax.management.remote.JMXServiceURL;
33 import javax.management.remote.JMXConnector;
34 import javax.management.remote.JMXConnectorFactory;
35
36 import org.collectd.api.Collectd;
37 import org.collectd.api.PluginData;
38 import org.collectd.api.OConfigValue;
39 import org.collectd.api.OConfigItem;
40
41 class GenericJMXConfConnection
42 {
43   private String _host = null;
44   private String _service_url = null;
45   private MBeanServerConnection _jmx_connection = null;
46   private List<GenericJMXConfMBean> _mbeans = null;
47
48   /*
49    * private methods
50    */
51   private String getConfigString (OConfigItem ci) /* {{{ */
52   {
53     List<OConfigValue> values;
54     OConfigValue v;
55
56     values = ci.getValues ();
57     if (values.size () != 1)
58     {
59       Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
60           + " configuration option needs exactly one string argument.");
61       return (null);
62     }
63
64     v = values.get (0);
65     if (v.getType () != OConfigValue.OCONFIG_TYPE_STRING)
66     {
67       Collectd.logError ("GenericJMXConfConnection: The " + ci.getKey ()
68           + " configuration option needs exactly one string argument.");
69       return (null);
70     }
71
72     return (v.getString ());
73   } /* }}} String getConfigString */
74
75 private void connect () /* {{{ */
76 {
77   JMXServiceURL service_url;
78   JMXConnector connector;
79
80   if (_jmx_connection != null)
81     return;
82
83   try
84   {
85     service_url = new JMXServiceURL (this._service_url);
86     connector = JMXConnectorFactory.connect (service_url);
87     _jmx_connection = connector.getMBeanServerConnection ();
88   }
89   catch (Exception e)
90   {
91     Collectd.logError ("GenericJMXConfConnection: "
92         + "Creating MBean server connection failed: " + e);
93     return;
94   }
95 } /* }}} void connect */
96
97 /*
98  * public methods
99  *
100  * <Connection>
101  *   Host "tomcat0.mycompany"
102  *   ServiceURL "service:jmx:rmi:///jndi/rmi://localhost:17264/jmxrmi"
103  *   Collect "java.lang:type=GarbageCollector,name=Copy"
104  *   Collect "java.lang:type=Memory"
105  * </Connection>
106  *
107  */
108   public GenericJMXConfConnection (OConfigItem ci) /* {{{ */
109     throws IllegalArgumentException
110   {
111     List<OConfigItem> children;
112     Iterator<OConfigItem> iter;
113
114     this._mbeans = new ArrayList<GenericJMXConfMBean> ();
115
116     children = ci.getChildren ();
117     iter = children.iterator ();
118     while (iter.hasNext ())
119     {
120       OConfigItem child = iter.next ();
121
122       if (child.getKey ().equalsIgnoreCase ("Host"))
123       {
124         String tmp = getConfigString (child);
125         if (tmp != null)
126           this._host = tmp;
127       }
128       else if (child.getKey ().equalsIgnoreCase ("ServiceURL"))
129       {
130         String tmp = getConfigString (child);
131         if (tmp != null)
132           this._service_url = tmp;
133       }
134       else if (child.getKey ().equalsIgnoreCase ("Collect"))
135       {
136         String tmp = getConfigString (child);
137         if (tmp != null)
138         {
139           GenericJMXConfMBean mbean;
140
141           mbean = GenericJMX.getMBean (tmp);
142           if (mbean == null)
143             throw (new IllegalArgumentException ("No such MBean defined: "
144                   + tmp + ". Please make sure all `MBean' blocks appear "
145                   + "before (above) all `Connection' blocks."));
146           Collectd.logDebug ("GenericJMXConfConnection: " + this._host + ": Add " + tmp);
147           this._mbeans.add (mbean);
148         }
149       }
150       else
151         throw (new IllegalArgumentException ("Unknown option: "
152               + child.getKey ()));
153     }
154
155     if (this._service_url == null)
156       throw (new IllegalArgumentException ("No service URL was defined."));
157     if (this._mbeans.size () == 0)
158       throw (new IllegalArgumentException ("No valid collect statement "
159             + "present."));
160   } /* }}} GenericJMXConfConnection (OConfigItem ci) */
161
162   public void query () /* {{{ */
163   {
164     PluginData pd;
165
166     connect ();
167
168     if (this._jmx_connection == null)
169       return;
170
171     Collectd.logDebug ("GenericJMXConfConnection.query: "
172         + "Reading " + this._mbeans.size () + " mbeans from "
173         + ((this._host != null) ? this._host : "(null)"));
174
175     pd = new PluginData ();
176     pd.setHost ((this._host != null) ? this._host : "localhost");
177     pd.setPlugin ("GenericJMX");
178
179     for (int i = 0; i < this._mbeans.size (); i++)
180       this._mbeans.get (i).query (this._jmx_connection, pd);
181   } /* }}} void query */
182
183   public String toString ()
184   {
185     return (new String ("host = " + this._host + "; "
186           + "url = " + this._service_url));
187   }
188 }
189
190 /* vim: set sw=2 sts=2 et fdm=marker : */