Skip to content

Commit 0db1135

Browse files
committed
add another example... MemoryPool... this uses
the typeName functionality to make the attribute names more unique.
1 parent 018b45a commit 0db1135

File tree

7 files changed

+249
-73
lines changed

7 files changed

+249
-73
lines changed

memorypool-rrd-template.xml

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<rrd_def>
2+
<path>${database}</path>
3+
<!-- not mandatory
4+
<start>1000123456</start> -->
5+
<!-- not mandatory -->
6+
<step>60</step>
7+
<!-- at least one datasource must be supplied -->
8+
<datasource><name>COGCUCommitted</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
9+
<datasource><name>COGCUInit</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
10+
<datasource><name>COGCUMax</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
11+
<datasource><name>COGCUUsed</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
12+
<datasource><name>COGCUTC</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
13+
<datasource><name>COGCUTCC</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
14+
<datasource><name>COGPUCommitted</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
15+
<datasource><name>COGPUInit</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
16+
<datasource><name>COGPUMax</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
17+
<datasource><name>COGPUUsed</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
18+
<datasource><name>COGUCommitted</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
19+
<datasource><name>COGUInit</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
20+
<datasource><name>COGUMax</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
21+
<datasource><name>COGUUsed</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
22+
<datasource><name>COGUTUsageThreshold</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
23+
<datasource><name>COGUTCU</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
24+
<datasource><name>PESCUCommitted</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
25+
<datasource><name>PESCUInit</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
26+
<datasource><name>PESCUMax</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
27+
<datasource><name>PESCUUsed</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
28+
<datasource><name>PESCUTC</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
29+
<datasource><name>PESCUTCC</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
30+
<datasource><name>PESPUCommitted</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
31+
<datasource><name>PESPUInit</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
32+
<datasource><name>PESPUMax</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
33+
<datasource><name>PESPUUsed</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
34+
<datasource><name>PESUCommitted</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
35+
<datasource><name>PESUInit</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
36+
<datasource><name>PESUMax</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
37+
<datasource><name>PESUUsed</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
38+
<datasource><name>CCPUCommitted</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
39+
<datasource><name>CCPUInit</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
40+
<datasource><name>CCPUMax</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
41+
<datasource><name>CCPUUsed</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
42+
<datasource><name>CCUCommitted</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
43+
<datasource><name>CCUInit</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
44+
<datasource><name>CCUMax</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
45+
<datasource><name>CCUUsed</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
46+
<datasource><name>CCUTUsageThreshold</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
47+
<datasource><name>CCUTCU</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
48+
<datasource><name>CPGCUCommitted</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
49+
<datasource><name>CPGCUInit</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
50+
<datasource><name>CPGCUMax</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
51+
<datasource><name>CPGCUUsed</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
52+
<datasource><name>CPGCUTC</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
53+
<datasource><name>CPGCUTCC</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
54+
<datasource><name>CPGPUCommitted</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
55+
<datasource><name>CPGPUInit</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
56+
<datasource><name>CPGPUMax</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
57+
<datasource><name>CPGPUUsed</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
58+
<datasource><name>CPGUCommitted</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
59+
<datasource><name>CPGUInit</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
60+
<datasource><name>CPGUMax</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
61+
<datasource><name>CPGUUsed</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
62+
<datasource><name>CPGUTUsageThreshold</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
63+
<datasource><name>CPGUTCU</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
64+
<datasource><name>PSSCUCommitted</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
65+
<datasource><name>PSSCUInit</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
66+
<datasource><name>PSSCUMax</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
67+
<datasource><name>PSSCUUsed</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
68+
<datasource><name>PSSCUTC</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
69+
<datasource><name>PSSCUTCC</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
70+
<datasource><name>PSSPUCommitted</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
71+
<datasource><name>PSSPUInit</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
72+
<datasource><name>PSSPUMax</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
73+
<datasource><name>PSSPUUsed</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
74+
<datasource><name>PSSUCommitted</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
75+
<datasource><name>PSSUInit</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
76+
<datasource><name>PSSUMax</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
77+
<datasource><name>PSSUUsed</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>
78+
<!-- at least one archive must be supplied -->
79+
<archive>
80+
<cf>AVERAGE</cf>
81+
<xff>0.5</xff>
82+
<steps>1</steps>
83+
<rows>7000</rows>
84+
</archive>
85+
<archive>
86+
<cf>MIN</cf>
87+
<xff>0.5</xff>
88+
<steps>1</steps>
89+
<rows>7000</rows>
90+
</archive>
91+
<archive>
92+
<cf>MAX</cf>
93+
<xff>0.5</xff>
94+
<steps>1</steps>
95+
<rows>7000</rows>
96+
</archive>
97+
</rrd_def>

memorypool.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"servers" : [ {
3+
"port" : "1099",
4+
"host" : "w2",
5+
"queries" : [ {
6+
"obj" : "java.lang:type=MemoryPool,name=*",
7+
"outputWriters" : [ {
8+
"@class" : "com.googlecode.jmxtrans.model.output.RRDToolWriter",
9+
"settings" : {
10+
"templateFile" : "memorypool-rrd-template.xml",
11+
"outputFile" : "target/memorypool.rrd",
12+
"binaryPath" : "/opt/local/bin",
13+
"typeNames" : ["name"],
14+
"debug" : true,
15+
"generate" : true
16+
}
17+
} ]
18+
} ]
19+
} ]
20+
}

src/com/googlecode/jmxtrans/example/ActiveMQ.java

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.googlecode.jmxtrans.example;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
5-
63
import com.googlecode.jmxtrans.model.JmxProcess;
74
import com.googlecode.jmxtrans.model.Query;
85
import com.googlecode.jmxtrans.model.Server;
@@ -20,23 +17,23 @@
2017
*/
2118
public class ActiveMQ {
2219

20+
private static final String GW_HOST = "192.168.192.133";
21+
2322
/** */
2423
public static void main(String[] args) throws Exception {
2524

2625
Server server = new Server("w2", "1105");
2726
server.setAlias("w2_activemq_1105");
2827
GraphiteWriter gw = new GraphiteWriter();
29-
// gw.addSetting(GraphiteWriter.HOST, "192.168.192.133");
30-
gw.addSetting(GraphiteWriter.HOST, "localhost");
28+
gw.addSetting(GraphiteWriter.HOST, GW_HOST);
29+
// gw.addSetting(GraphiteWriter.HOST, "localhost");
3130
gw.addSetting(GraphiteWriter.PORT, 2003);
3231

3332
// use this to add data to GW path
34-
List<String> typeNames = new ArrayList<String>();
35-
typeNames.add("destinationName");
36-
typeNames.add("Destination");
37-
gw.addSetting(GraphiteWriter.TYPE_NAMES, typeNames);
38-
gw.addSetting(GraphiteWriter.DEBUG, false);
39-
33+
gw.addTypeName("destinationName");
34+
gw.addTypeName("Destination");
35+
gw.addSetting(GraphiteWriter.DEBUG, true);
36+
4037
Query q = new Query();
4138
q.setObj("org.apache.activemq:BrokerName=localhost,Type=Subscription,clientId=*,consumerId=*,destinationName=*,destinationType=Queue,persistentMode=Non-Durable");
4239
q.addAttr("PendingQueueSize");
@@ -129,11 +126,22 @@ public static void main(String[] args) throws Exception {
129126
q8.addKey("CollectionTime");
130127
q8.addOutputWriter(gw);
131128
server.addQuery(q8);
132-
133-
JmxProcess process = new JmxProcess(server);
134129

130+
GraphiteWriter gw2 = new GraphiteWriter();
131+
gw2.addSetting(GraphiteWriter.HOST, GW_HOST);
132+
gw2.addSetting(GraphiteWriter.PORT, 2003);
133+
134+
gw2.addTypeName("name");
135+
gw2.addSetting(GraphiteWriter.DEBUG, true);
136+
137+
Query q9 = new Query();
138+
q9.setObj("java.lang:type=MemoryPool,name=*");
139+
q9.addOutputWriter(gw2);
140+
server.addQuery(q9);
141+
142+
JmxProcess process = new JmxProcess(server);
135143
JmxUtils.prettyPrintJson(process);
136-
// JmxUtils.processServer(server);
144+
JmxUtils.processServer(server);
137145

138146
// for (int i = 0; i < 160; i++) {
139147
// JmxUtils.processServer(server);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.googlecode.jmxtrans.example;
2+
3+
import java.io.File;
4+
5+
import com.googlecode.jmxtrans.model.JmxProcess;
6+
import com.googlecode.jmxtrans.util.JmxUtils;
7+
8+
/**
9+
* Shows how to process a file.
10+
* @author jon
11+
*/
12+
public class MemoryPool {
13+
14+
/**
15+
*
16+
*/
17+
public static void main(String[] args) throws Exception {
18+
19+
JmxProcess jmx = JmxUtils.getJmxProcess(new File("memorypool.json"));
20+
JmxUtils.printJson(jmx);
21+
JmxUtils.execute(jmx);
22+
23+
// for (int i = 0; i < 160; i++) {
24+
// JmxUtils.execute(jmx);
25+
// Thread.sleep(1000);
26+
// }
27+
28+
System.out.println("done!");
29+
}
30+
}

src/com/googlecode/jmxtrans/model/output/GraphiteWriter.java

+4-53
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
import java.io.PrintWriter;
44
import java.io.Writer;
55
import java.net.Socket;
6-
import java.util.List;
76
import java.util.Map;
87
import java.util.Map.Entry;
98

109
import org.apache.commons.io.IOUtils;
11-
import org.codehaus.jackson.annotate.JsonIgnore;
1210
import org.slf4j.Logger;
1311
import org.slf4j.LoggerFactory;
1412

@@ -31,9 +29,6 @@ public class GraphiteWriter extends BaseOutputWriter {
3129
private String host = null;
3230
private Integer port = null;
3331

34-
public static final String TYPE_NAMES = "typeNames";
35-
private List<String> typeNames = null;
36-
3732
public GraphiteWriter() {
3833
}
3934

@@ -46,15 +41,6 @@ public void validateSetup() throws Exception {
4641
}
4742
}
4843

49-
public String cleanupName(String name) {
50-
if (name == null) {
51-
return null;
52-
}
53-
String clean = name.replace('.', '_');
54-
clean = clean.replace(" ", "");
55-
return clean;
56-
}
57-
5844
public void doWrite(Query query) throws Exception {
5945
Writer writer = connect();
6046
try {
@@ -79,21 +65,21 @@ public void doWrite(Query query) throws Exception {
7965
} else {
8066
alias = query.getServer().getHost() + "_" + query.getServer().getPort();
8167
}
82-
alias = cleanupName(alias);
68+
alias = cleanupStr(alias);
8369

8470
StringBuilder sb = new StringBuilder();
8571
sb.append("servers.");
8672
sb.append(alias);
8773
sb.append(".");
88-
sb.append(cleanupName(r.getClassName()));
74+
sb.append(cleanupStr(r.getClassName()));
8975
sb.append(".");
9076

91-
String typeName = cleanupName(handleTypeName(r.getTypeName()));
77+
String typeName = cleanupStr(retrieveTypeNameValue(r.getTypeName()));
9278
if (typeName != null) {
9379
sb.append(typeName);
9480
sb.append(".");
9581
}
96-
sb.append(cleanupName(keyStr));
82+
sb.append(cleanupStr(keyStr));
9783

9884
sb.append(" ");
9985
sb.append(values.getValue());
@@ -116,44 +102,9 @@ public void doWrite(Query query) throws Exception {
116102
}
117103
}
118104

119-
private String handleTypeName(String typeName) {
120-
List<String> typeNames = getTypeNames();
121-
if (typeNames == null || typeNames.size() == 0) {
122-
return null;
123-
}
124-
String[] tokens = typeName.split(",");
125-
boolean foundIt = false;
126-
for (String token : tokens) {
127-
String[] keys = token.split("=");
128-
for (String key : keys) {
129-
// we want the next item in the array.
130-
if (foundIt) {
131-
return key;
132-
}
133-
if (typeNames.contains(key)) {
134-
foundIt = true;
135-
}
136-
}
137-
}
138-
return null;
139-
}
140-
141105
private Writer connect() throws Exception {
142106
Socket socket = new Socket(host, port);
143107
return new PrintWriter(socket.getOutputStream(), true);
144108
}
145109

146-
public void setTypeNames(List<String> typeNames) {
147-
this.getSettings().put(TYPE_NAMES, typeNames);
148-
this.typeNames = typeNames;
149-
}
150-
151-
@JsonIgnore
152-
@SuppressWarnings("unchecked")
153-
public List<String> getTypeNames() {
154-
if (this.typeNames == null) {
155-
this.typeNames = (List<String>) this.getSettings().get(TYPE_NAMES);
156-
}
157-
return this.typeNames;
158-
}
159110
}

src/com/googlecode/jmxtrans/model/output/RRDToolWriter.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ public void validateSetup() throws Exception {
6565
* work to make it shorter. Not ideal at all, but works fairly
6666
* well it seems.
6767
*/
68-
public static String getDataSourceName(String str, String entry) {
69-
String caps = WordUtils.capitalize(str, PERIOD);
68+
public String getDataSourceName(String typeName, String attributeName, String entry) {
69+
String typeNameClean = "";
70+
if (typeName != null) {
71+
typeNameClean = cleanupStr(typeName);
72+
}
73+
String caps = WordUtils.capitalize(typeNameClean + "." + attributeName, PERIOD);
7074
String[] split = StringUtils.splitByCharacterTypeCamelCase(caps);
7175
String join = StringUtils.join(split, '.');
7276
String result = WordUtils.initials(join, INITIALS);
@@ -93,7 +97,7 @@ public void doWrite(Query query) throws Exception {
9397
Map<String, Object> values = res.getValues();
9498
if (values != null) {
9599
for (Entry<String, Object> entry : values.entrySet()) {
96-
String key = getDataSourceName(res.getAttributeName(), entry.getKey());
100+
String key = getDataSourceName(retrieveTypeNameValue(res.getTypeName()), res.getAttributeName(), entry.getKey());
97101
boolean isNumeric = JmxUtils.isNumeric(entry.getValue());
98102

99103
if (isDebugEnabled() && isNumeric) {
@@ -113,8 +117,11 @@ public void doWrite(Query query) throws Exception {
113117
Map<String, Object> values = res.getValues();
114118
if (values != null) {
115119
for (Entry<String, Object> entry : values.entrySet()) {
116-
String key = getDataSourceName(res.getAttributeName(), entry.getKey());
117-
sb.append("<datasource><name>" + key + "</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>\n");
120+
boolean isNumeric = JmxUtils.isNumeric(entry.getValue());
121+
if (isNumeric) {
122+
String key = getDataSourceName(retrieveTypeNameValue(res.getTypeName()), res.getAttributeName(), entry.getKey());
123+
sb.append("<datasource><name>" + key + "</name><type>GAUGE</type><heartbeat>400</heartbeat><min>U</min><max>U</max></datasource>\n");
124+
}
118125
}
119126
}
120127
}

0 commit comments

Comments
 (0)