~ubuntu-branches/ubuntu/saucy/munin/saucy

« back to all changes in this revision

Viewing changes to plugins/javalib/org/munin/plugin/jmx/AbstractGraphsProvider.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2012-06-11 12:54:28 UTC
  • mfrom: (8.1.30 sid)
  • Revision ID: package-import@ubuntu.com-20120611125428-k8z25s77rp755vxe
Tags: 2.0.0-1ubuntu1
* Resync with Debian unstable.
* d/munin-node.upstart,munin.upstart: Add upstart configurations.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.munin.plugin.jmx;
 
2
 
 
3
import java.io.IOException;
 
4
import java.io.PrintWriter;
 
5
import java.net.MalformedURLException;
 
6
 
 
7
import javax.management.MBeanServerConnection;
 
8
 
 
9
public abstract class AbstractGraphsProvider {
 
10
        protected final Config config;
 
11
        private MBeanServerConnection connection;
 
12
 
 
13
        protected AbstractGraphsProvider(final Config config) {
 
14
                if (config == null) {
 
15
                        throw new IllegalArgumentException("config must not be null");
 
16
                }
 
17
                this.config = config;
 
18
        }
 
19
 
 
20
        protected MBeanServerConnection getConnection() {
 
21
                if (connection == null) {
 
22
                        try {
 
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);
 
30
                        }
 
31
                }
 
32
                return connection;
 
33
        }
 
34
 
 
35
        public abstract void printConfig(PrintWriter out);
 
36
 
 
37
        public abstract void printValues(PrintWriter out);
 
38
 
 
39
        protected void printGraphConfig(PrintWriter out, String title,
 
40
                        String vlabel, String info, String args, boolean update,
 
41
                        boolean graph) {
 
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);
 
46
                }
 
47
                if (args != null && args.length() > 0) {
 
48
                        out.println("graph_args " + args);
 
49
                }
 
50
                out.println("graph_category " + config.getCategory());
 
51
                if (!update) {
 
52
                        out.println("update no");
 
53
                }
 
54
                if (!graph) {
 
55
                        out.println("graph no");
 
56
                }
 
57
        }
 
58
 
 
59
        protected void printFieldConfig(PrintWriter out, String name, String label,
 
60
                        String info, String type, String draw, String colour, double min,
 
61
                        double max) {
 
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);
 
67
                }
 
68
                if (!Double.isNaN(max)) {
 
69
                        printFieldAttribute(out, name, "max", max);
 
70
                }
 
71
                printFieldAttribute(out, name, "draw", draw);
 
72
                printFieldAttribute(out, name, "colour", colour);
 
73
        }
 
74
 
 
75
        protected void printFieldAttribute(PrintWriter out, String fieldName,
 
76
                        String attributeName, Object value) {
 
77
                if (value != null) {
 
78
                        String stringValue = String.valueOf(value);
 
79
                        if (stringValue.length() > 0) {
 
80
                                out.println(fieldName + "." + attributeName + " " + value);
 
81
                        }
 
82
                }
 
83
        }
 
84
 
 
85
        static void runGraph(String[] args) {
 
86
                String providerClassName = Thread.currentThread().getStackTrace()[2].getClassName();
 
87
                Class<? extends AbstractGraphsProvider> providerClass;
 
88
                try {
 
89
                        providerClass = Class.forName(providerClassName).asSubclass(AbstractGraphsProvider.class);
 
90
                } catch (ClassNotFoundException e) {
 
91
                        throw new IllegalStateException("Failed to get provider class", e);
 
92
                }
 
93
                Config config = getConfig(args);
 
94
                AbstractGraphsProvider provider = getProvider(providerClass, config);
 
95
                runGraph(provider, config, args);
 
96
        }
 
97
 
 
98
        private static AbstractGraphsProvider getProvider(
 
99
                        Class<? extends AbstractGraphsProvider> providerClass, Config config) {
 
100
                try {
 
101
                        return providerClass.getConstructor(Config.class).newInstance(
 
102
                                        config);
 
103
                } catch (NoSuchMethodException e) {
 
104
                        // just try default constructor
 
105
                        try {
 
106
                                return providerClass.newInstance();
 
107
                        } catch (Exception e1) {
 
108
                                throw new IllegalArgumentException(
 
109
                                                "Can't instantiate provider with default constructor: "
 
110
                                                                + providerClass, e);
 
111
                        }
 
112
                } catch (Exception e) {
 
113
                        throw new IllegalArgumentException(
 
114
                                        "Can't instantiate provider with constructor accepting Config object: "
 
115
                                                        + providerClass, e);
 
116
                }
 
117
        }
 
118
 
 
119
        private static Config getConfig(String[] args) {
 
120
                String prefix;
 
121
                if (args.length >= 2) {
 
122
                        prefix = args[1];
 
123
                } else {
 
124
                        prefix = null;
 
125
                }
 
126
                return new Config(prefix);
 
127
        }
 
128
 
 
129
        private static void runGraph(final AbstractGraphsProvider provider,
 
130
                        Config config, String[] args) {
 
131
                try {
 
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);
 
137
                                }
 
138
                        } else {
 
139
                                provider.printValues(out);
 
140
                        }
 
141
                        out.flush();
 
142
                } catch (Exception e) {
 
143
                        e.printStackTrace(System.err);
 
144
                }
 
145
        }
 
146
 
 
147
        protected static String toFieldName(String name) {
 
148
                return name.replaceAll("[^A-Za-z0-9_]", "_");
 
149
        }
 
150
}