1
package org.munin.plugin.jmx;
3
import java.io.IOException;
4
import java.io.PrintWriter;
5
import java.net.MalformedURLException;
7
import javax.management.MBeanServerConnection;
9
public abstract class AbstractGraphsProvider {
10
protected final Config config;
11
private MBeanServerConnection connection;
13
protected AbstractGraphsProvider(final Config config) {
15
throw new IllegalArgumentException("config must not be null");
20
protected MBeanServerConnection getConnection() {
21
if (connection == null) {
23
connection = BasicMBeanConnection.get(config);
24
} catch (MalformedURLException e) {
25
throw new IllegalStateException(
26
"Failed to get MBean Server Connection", e);
27
} catch (IOException e) {
28
throw new IllegalStateException(
29
"Failed to get MBean Server Connection", e);
35
public abstract void printConfig(PrintWriter out);
37
public abstract void printValues(PrintWriter out);
39
protected void printGraphConfig(PrintWriter out, String title,
40
String vlabel, String info, String args, boolean update,
42
out.println("graph_title " + title);
43
out.println("graph_vlabel " + vlabel);
44
if (info != null && info.length() > 0) {
45
out.println("graph_info " + info);
47
if (args != null && args.length() > 0) {
48
out.println("graph_args " + args);
50
out.println("graph_category " + config.getCategory());
52
out.println("update no");
55
out.println("graph no");
59
protected void printFieldConfig(PrintWriter out, String name, String label,
60
String info, String type, String draw, String colour, double min,
62
printFieldAttribute(out, name, "label", label);
63
printFieldAttribute(out, name, "info", info);
64
printFieldAttribute(out, name, "type", type);
65
if (!Double.isNaN(min)) {
66
printFieldAttribute(out, name, "min", min);
68
if (!Double.isNaN(max)) {
69
printFieldAttribute(out, name, "max", max);
71
printFieldAttribute(out, name, "draw", draw);
72
printFieldAttribute(out, name, "colour", colour);
75
protected void printFieldAttribute(PrintWriter out, String fieldName,
76
String attributeName, Object value) {
78
String stringValue = String.valueOf(value);
79
if (stringValue.length() > 0) {
80
out.println(fieldName + "." + attributeName + " " + value);
85
static void runGraph(String[] args) {
86
String providerClassName = Thread.currentThread().getStackTrace()[2].getClassName();
87
Class<? extends AbstractGraphsProvider> providerClass;
89
providerClass = Class.forName(providerClassName).asSubclass(AbstractGraphsProvider.class);
90
} catch (ClassNotFoundException e) {
91
throw new IllegalStateException("Failed to get provider class", e);
93
Config config = getConfig(args);
94
AbstractGraphsProvider provider = getProvider(providerClass, config);
95
runGraph(provider, config, args);
98
private static AbstractGraphsProvider getProvider(
99
Class<? extends AbstractGraphsProvider> providerClass, Config config) {
101
return providerClass.getConstructor(Config.class).newInstance(
103
} catch (NoSuchMethodException e) {
104
// just try default constructor
106
return providerClass.newInstance();
107
} catch (Exception e1) {
108
throw new IllegalArgumentException(
109
"Can't instantiate provider with default constructor: "
112
} catch (Exception e) {
113
throw new IllegalArgumentException(
114
"Can't instantiate provider with constructor accepting Config object: "
119
private static Config getConfig(String[] args) {
121
if (args.length >= 2) {
126
return new Config(prefix);
129
private static void runGraph(final AbstractGraphsProvider provider,
130
Config config, String[] args) {
132
PrintWriter out = new PrintWriter(System.out);
133
if (args[0].equals("config")) {
134
provider.printConfig(out);
135
if (config.isDirtyConfg()) {
136
provider.printValues(out);
139
provider.printValues(out);
142
} catch (Exception e) {
143
e.printStackTrace(System.err);
147
protected static String toFieldName(String name) {
148
return name.replaceAll("[^A-Za-z0-9_]", "_");