Commit d6ba81ae d6ba81ae282c3397fb62dd34954a88ca935aa184 by Christian Gerdes

New library: LILJM for JMX Monitoring

1 parent 2cf7063d
1 <?xml version="1.0" encoding="UTF-8"?>
2 <classpath>
3 <classpathentry kind="src" path=""/>
4 <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
5 <classpathentry kind="output" path=""/>
6 </classpath>
1 <?xml version="1.0" encoding="UTF-8"?>
2 <projectDescription>
3 <name>LILJM Library</name>
4 <comment></comment>
5 <projects>
6 </projects>
7 <buildSpec>
8 <buildCommand>
9 <name>org.eclipse.jdt.core.javabuilder</name>
10 <arguments>
11 </arguments>
12 </buildCommand>
13 </buildSpec>
14 <natures>
15 <nature>org.eclipse.jdt.core.javanature</nature>
16 </natures>
17 </projectDescription>
1 eclipse.preferences.version=1
2 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
4 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5 org.eclipse.jdt.core.compiler.compliance=1.6
6 org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7 org.eclipse.jdt.core.compiler.debug.localVariable=generate
8 org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9 org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10 org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11 org.eclipse.jdt.core.compiler.source=1.6
1 /JmxMon.class
2 /Collector.class
3 /DoubleDelta.class
4 /LongDelta.class
5 /Registry.class
6 /TestRunner.class
1 package se.lil.jm;
2
3 import java.util.ArrayList;
4
5 class Collector {
6 ArrayList<LongDelta> listLong = new ArrayList<LongDelta>();
7
8 public void updateValue(Long ts, String name, Long value) {
9 LongDelta myDelta = null;
10 for(int x = 0; x<listLong.size() && myDelta == null; x++) {
11 if(name.equals(listLong.get(x).name)) {
12 myDelta = listLong.get(x);
13 }
14 }
15 if(myDelta != null) {
16 myDelta.update(ts, value);
17 } else {
18 myDelta = new LongDelta(ts, value);
19 listLong.add(myDelta);
20 }
21 }
22
23 public long getCurrentValue(String name) {
24 LongDelta myDelta = null;
25 for(int x = 0; x<listLong.size() && myDelta == null; x++) {
26 if(name.equals(listLong.get(x).name)) {
27 myDelta = listLong.get(x);
28 }
29 }
30 if(myDelta != null)
31 return myDelta.getCurrentValue();
32 else
33 return 0;
34 }
35
36 public double getPerSecValue(String name) throws Throwable {
37 LongDelta myDelta = null;
38 for(int x = 0; x<listLong.size() && myDelta == null; x++) {
39 if(name.equals(listLong.get(x).name)) {
40 myDelta = listLong.get(x);
41 }
42 }
43 if(myDelta != null)
44 return myDelta.getPerSecondValue();
45 else
46 return 0;
47 }
48 }
...\ No newline at end of file ...\ No newline at end of file
1 package se.lil.jm;
2
3 public class DoubleDelta {
4 public String name = null;
5 public Double curValue = null;
6 public Double lastValue = null;
7 public Long lastFetch = null;
8 public Long curFetch = null;
9
10 public DoubleDelta() {
11 }
12
13 public DoubleDelta(Long timestamp, Double value) {
14 this.curFetch = timestamp;
15 this.curValue = value;
16 }
17
18 public void update(Long timestamp, Double value) {
19 this.lastFetch = this.curFetch;
20 this.curFetch = timestamp;
21 this.lastValue = this.curValue;
22 this.curValue = value;
23 }
24
25 public double getPerSecondValue() {
26 if(this.curValue != null && this.lastValue != null && this.lastFetch != null && this.curFetch != null) {
27 // We have values, calculate the number of gets per second
28 double numMilliSeconds = this.curFetch - this.lastFetch;
29 double delta = this.curValue - this.lastValue;
30 double perMilli = delta/numMilliSeconds;
31 return perMilli * 1000;
32 } else {
33 return 0;
34 }
35 }
36 public double getCurrentValue() {
37 if(this.curValue != null) return this.curValue;
38 else return 0;
39 }
40 public double getSeconds() {
41 if(this.lastFetch != null && this.curFetch != null) {
42 double numSeconds = this.curFetch - this.lastFetch;
43 return numSeconds/1000;
44 }
45 return 0;
46 }
47 public long getMilliSeconds() {
48 if(this.lastFetch != null && this.curFetch != null) {
49 return this.curFetch - this.lastFetch;
50 }
51 return 0;
52 }
53 }
1 package se.lil.jm;
2
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5 import java.util.concurrent.locks.Lock;
6 import java.util.concurrent.locks.ReentrantLock;
7
8 import javax.management.JMX;
9 import javax.management.MBeanServerConnection;
10 import javax.management.ObjectName;
11 import javax.management.remote.JMXConnector;
12 import javax.management.remote.JMXConnectorFactory;
13 import javax.management.remote.JMXServiceURL;
14 import javax.management.openmbean.CompositeData;
15
16 public class JmxMon {
17 String conUser = "system";
18 String conPass = "passw0rd";
19 String domain = null;
20
21 JMXServiceURL url = null;
22 JMXConnector jmxc = null;
23 MBeanServerConnection mbsc = null;
24
25 Collector col = new Collector();
26
27 int getDataCalls = 0;
28 int getDataSucess = 0;
29
30 public int getDataCalled() {
31 return getDataCalls;
32 }
33
34 public int getDataSucceeded() {
35 return getDataSucess;
36 }
37
38 public int getDataFailed() {
39 return getDataCalls - getDataSucess;
40 }
41
42 public String getConString() {
43 if(this.url != null) return this.url.toString();
44 else return null;
45 }
46
47 public void open() throws IOException {
48 if(url != null) {
49 jmxc = JMXConnectorFactory.connect(url);
50 mbsc = jmxc.getMBeanServerConnection();
51 domain = mbsc.getDefaultDomain();
52 }
53 }
54
55 public void open(String serviceURL) throws IOException, MalformedURLException {
56 url = new JMXServiceURL(serviceURL);
57 open();
58 }
59
60 public void close() throws IOException {
61 if(jmxc != null) jmxc.close();
62 jmxc = null;
63 mbsc = null;
64 }
65
66 long lastRTns = 0;
67 public long getLastRTms() {
68 return lastRTns / (1000 * 1000);
69 }
70
71 long lastFetchTSns = 0;
72 public boolean getData(long ageTs) throws Throwable {
73 if (getAgeTs() > ageTs ) return getData();
74 else return false;
75 }
76
77 public long getAgeTs() {
78 if(lastFetchTSns > 0) return (System.nanoTime() - lastFetchTSns)/(1000*1000*1000);
79 else return 0;
80 }
81
82 Lock lock = new ReentrantLock();
83
84 public boolean getData() throws Throwable {
85 // Start thread safe
86 // Try to get a lock
87 boolean haveLock = lock.tryLock();
88 if( haveLock == false) {
89 // We could not get the lock, someone else is updating. Wait for it to complete by waiting for a lock, then unlock and return.
90 try {
91 lock.lock();
92 } finally {
93 lock.unlock();
94 }
95 return false;
96 }
97 // We do have the lock. Do the rest in a try catch everything and if we catch anything, re throw the catch but always release the lock.
98 try {
99 long startTSns = System.nanoTime();
100 getDataCalls++;
101 // Do the update of data
102 Object mb;
103 mb = mbsc.getAttribute(new ObjectName("java.lang:type=OperatingSystem"), "ProcessCpuTime");
104 System.out.println("Class: " + mb.getClass());
105 System.out.println("toString: " + mb.toString());
106 mb = mbsc.getAttribute(new ObjectName("java.lang:type=OperatingSystem"), "AvailableProcessors");
107 System.out.println("Class: " + mb.getClass());
108 System.out.println("toString: " + mb.toString());
109 mb = mbsc.getAttribute(new ObjectName("java.lang:type=OperatingSystem"), "OpenFileDescriptorCount");
110 System.out.println("Class: " + mb.getClass());
111 System.out.println("toString: " + mb.toString());
112 mb = mbsc.getAttribute(new ObjectName("java.lang:type=Runtime"), "Uptime");
113 System.out.println("Class: " + mb.getClass());
114 System.out.println("toString: " + mb.toString());
115 // Finished updating data
116 getDataSucess++;
117 lastFetchTSns = System.nanoTime();
118 lastRTns = lastFetchTSns - startTSns;
119 return true;
120 } catch (Throwable e) {
121 throw (e);
122 } finally {
123 lock.unlock();
124 }
125 // End thread safe
126 }
127
128 //NUM_CPUS
129 int numCpus = 0;
130 public long getNumberOfCPUs() {
131 return numCpus;
132 }
133
134 public JmxMon() {
135 }
136
137 public JmxMon(String serviceURL) throws MalformedURLException {
138 this.url = new JMXServiceURL(serviceURL);
139 }
140 }
1 package se.lil.jm;
2
3 class LongDelta {
4 public String name = null;
5 public Long curValue = null;
6 public Long lastValue = null;
7 public Long lastFetch = null;
8 public Long curFetch = null;
9
10 public LongDelta() {
11 }
12
13 public LongDelta(Long timestamp, Long value) {
14 this.curFetch = timestamp;
15 this.curValue = value;
16 }
17
18 public void update(Long timestamp, Long value) {
19 this.lastFetch = this.curFetch;
20 this.curFetch = timestamp;
21 this.lastValue = this.curValue;
22 this.curValue = value;
23 }
24 public double getPerSecondValue() {
25 if(this.curValue != null && this.lastValue != null && this.lastFetch != null && this.curFetch != null) {
26 // We have values, calculate the number of gets per second
27 double numMilliSeconds = this.curFetch - this.lastFetch;
28 double delta = this.curValue - this.lastValue;
29 double perMilli = delta/numMilliSeconds;
30 return perMilli * 1000;
31 } else {
32 return 0;
33 }
34 }
35 public long getCurrentValue() {
36 if(this.curValue != null) return this.curValue;
37 else return 0;
38 }
39 public double getSeconds() {
40 if(this.lastFetch != null && this.curFetch != null) {
41 double numSeconds = this.curFetch - this.lastFetch;
42 return numSeconds/1000;
43 }
44 return 0;
45 }
46 public long getMilliSeconds() {
47 if(this.lastFetch != null && this.curFetch != null) {
48 return this.curFetch - this.lastFetch;
49 }
50 return 0;
51 }
52 }
...\ No newline at end of file ...\ No newline at end of file
1 package se.lil.jm;
2
3 import java.net.MalformedURLException;
4 import java.util.ArrayList;
5
6 public class Registry {
7 private static ArrayList<JmxMon> jmxList = null;
8 public static boolean test = true;
9
10 public static synchronized ArrayList<JmxMon> getList() {
11 if (jmxList == null) {
12 jmxList = new ArrayList<JmxMon>();
13 }
14 return jmxList;
15 }
16
17 public static synchronized JmxMon findOrCreate(String conStr) throws MalformedURLException {
18 for (JmxMon item : getList()) {
19 if(item.getConString().equals(conStr)) {
20 return item;
21 }
22 }
23 // Not found, create it
24 JmxMon monitor = new JmxMon(conStr);
25 jmxList.add(monitor);
26 return monitor;
27 }
28 }
1 package se.lil.jm;
2
3 import java.util.ArrayList;
4
5 public class TestRunner {
6
7 public TestRunner() {
8 // TODO Auto-generated constructor stub
9 }
10
11 /**
12 * @param args
13 */
14 public static void main(String[] args) throws Throwable{
15 // TODO Auto-generated method stub
16
17 //ArrayList<OraMon> oraList = new ArrayList<OraMon>();
18 ArrayList<JmxMon> jmxList = Registry.getList();
19
20 Long ts1 = System.currentTimeMillis();
21
22 JmxMon mon1 = new JmxMon("service:jmx:iiop:///jndi/iiop://u01892.ef.kap.rsv.se:17020/weblogic.management.mbeanservers.runtime");
23 mon1.open();
24 jmxList.add(mon1);
25
26 Long time = System.currentTimeMillis() - ts1;
27
28 System.out.println(
29 "Open called on " + mon1.getConString()
30 + "\nTime: " + time + "ms\n"
31 );
32
33 int times = 3;
34 while(times-- > 0) {
35 for(JmxMon mon : jmxList) {
36 ts1 = System.currentTimeMillis();
37 long age = mon1.getAgeTs();
38 mon.getData();
39 time = System.currentTimeMillis() - ts1;
40
41 System.out.println(
42 "GetData called on " + mon.domain + " (age was " + age + "s)"
43 + "\nTime: " + time + "ms\n\n"
44 );
45
46 }
47 Thread.sleep(15000);
48 }
49
50 // Close all mon objects
51 for(JmxMon mon : jmxList) {
52 mon.close();
53 System.out.println("Monitor closed. " + mon.getDataCalled() + " calls and " + mon.getDataSucceeded() + " succeeded and " + mon.getDataFailed() + " failed.");
54 }
55 }
56 }