Commit f95dafd9 f95dafd9eadcb148bc4802c93f633e3915c29e0d by Christian Gerdes

Första fungerande version med grundläggande counters.

1 parent 11bc5265
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
...@@ -2,6 +2,7 @@ package se.lil.jm; ...@@ -2,6 +2,7 @@ package se.lil.jm;
2 2
3 import java.io.IOException; 3 import java.io.IOException;
4 import java.net.MalformedURLException; 4 import java.net.MalformedURLException;
5 import java.util.HashMap;
5 import java.util.Set; 6 import java.util.Set;
6 import java.util.concurrent.locks.Lock; 7 import java.util.concurrent.locks.Lock;
7 import java.util.concurrent.locks.ReentrantLock; 8 import java.util.concurrent.locks.ReentrantLock;
...@@ -32,12 +33,39 @@ public class JmxMon { ...@@ -32,12 +33,39 @@ public class JmxMon {
32 Thread myThread = null; 33 Thread myThread = null;
33 ClassLoader myCL = null; 34 ClassLoader myCL = null;
34 35
35 Collector col = new Collector();
36
37 int getDataCalls = 0; 36 int getDataCalls = 0;
38 int getDataSucess = 0; 37 int getDataSucess = 0;
39 38
40 public Object getAttributeValue(String name, String attribute) { 39 HashMap<String, LongDelta> longDeltaMap = new HashMap<String, LongDelta>();
40 HashMap<String, DoubleDelta> doubleDeltaMap = new HashMap<String, DoubleDelta>();
41
42 private void updateLongDelta (Long timestamp, String name, Long value) {
43 LongDelta ld = longDeltaMap.get(name);
44 if(ld == null) {
45 ld = new LongDelta();
46 longDeltaMap.put(name, ld);
47 }
48 ld.update(timestamp, value);
49 }
50
51 private LongDelta getAttributeLongDelta(String name, String attribute) {
52 return longDeltaMap.get(name + ":" + attribute);
53 }
54
55 private void updateDoubleDelta (Long timestamp, String name, Double value) {
56 DoubleDelta dd = doubleDeltaMap.get(name);
57 if(dd == null) {
58 dd = new DoubleDelta();
59 doubleDeltaMap.put(name, dd);
60 }
61 dd.update(timestamp, value);
62 }
63
64 private DoubleDelta getAttributeDoubleDelta(String name, String attribute) {
65 return doubleDeltaMap.get(name + ":" + attribute);
66 }
67
68 private Object getAttributeValue(String name, String attribute) {
41 Object ro = null; 69 Object ro = null;
42 if(mbsc != null) { 70 if(mbsc != null) {
43 try { 71 try {
...@@ -49,6 +77,28 @@ public class JmxMon { ...@@ -49,6 +77,28 @@ public class JmxMon {
49 return ro; 77 return ro;
50 } 78 }
51 79
80 private void fetchUpdateAttribute(String name, String attribute) {
81 String fullName = name + ":" + attribute;
82 Object obj = getAttributeValue(name, attribute);
83 Long ts = (Long)getAttributeValue("java.lang:type=Runtime", "Uptime");
84 if(obj != null) {
85 if(obj.getClass().equals(java.lang.Long.class)) {
86 updateLongDelta(ts, fullName, (Long)obj);
87 }
88 if(obj.getClass().equals(java.lang.Integer.class)) {
89 Integer i = (Integer)obj;
90 updateLongDelta(ts, fullName, i.longValue());
91 }
92 if(obj.getClass().equals(java.lang.Double.class)) {
93 updateDoubleDelta(ts, fullName, (Double)obj);
94 }
95 if(obj.getClass().equals(java.lang.Float.class)) {
96 Float f = (Float)obj;
97 updateDoubleDelta(ts, fullName, f.doubleValue());
98 }
99 }
100 }
101
52 public int getDataCalled() { 102 public int getDataCalled() {
53 return getDataCalls; 103 return getDataCalls;
54 } 104 }
...@@ -143,19 +193,17 @@ public class JmxMon { ...@@ -143,19 +193,17 @@ public class JmxMon {
143 long startTSns = System.nanoTime(); 193 long startTSns = System.nanoTime();
144 getDataCalls++; 194 getDataCalls++;
145 // Do the update of data 195 // Do the update of data
146 Object mb; 196 numCpus = (Integer)getAttributeValue("java.lang:type=OperatingSystem", "AvailableProcessors");
147 mb = mbsc.getAttribute(new ObjectName("java.lang:type=OperatingSystem"), "ProcessCpuTime"); 197 fetchUpdateAttribute("java.lang:type=OperatingSystem", "ProcessCpuTime");
148 System.out.println("Class: " + mb.getClass()); 198 fetchUpdateAttribute("java.lang:type=OperatingSystem", "OpenFileDescriptorCount");
149 System.out.println("toString: " + mb.toString()); 199 fetchUpdateAttribute("java.lang:type=OperatingSystem", "MaxFileDescriptorCount");
150 mb = mbsc.getAttribute(new ObjectName("java.lang:type=OperatingSystem"), "AvailableProcessors"); 200 fetchUpdateAttribute("java.lang:type=OperatingSystem", "TotalSwapSpaceSize");
151 System.out.println("Class: " + mb.getClass()); 201 fetchUpdateAttribute("java.lang:type=OperatingSystem", "FreeSwapSpaceSize");
152 System.out.println("toString: " + mb.toString()); 202 fetchUpdateAttribute("java.lang:type=OperatingSystem", "FreePhysicalMemorySize");
153 mb = mbsc.getAttribute(new ObjectName("java.lang:type=OperatingSystem"), "OpenFileDescriptorCount"); 203 fetchUpdateAttribute("java.lang:type=OperatingSystem", "TotalPhysicalMemorySize");
154 System.out.println("Class: " + mb.getClass()); 204 fetchUpdateAttribute("java.lang:type=OperatingSystem", "SystemLoadAverage");
155 System.out.println("toString: " + mb.toString()); 205 fetchUpdateAttribute("java.lang:type=Threading", "ThreadCount");
156 mb = mbsc.getAttribute(new ObjectName("java.lang:type=Runtime"), "Uptime"); 206 fetchUpdateAttribute("java.lang:type=ClassLoading", "LoadedClassCount");
157 System.out.println("Class: " + mb.getClass());
158 System.out.println("toString: " + mb.toString());
159 // Finished updating data 207 // Finished updating data
160 getDataSucess++; 208 getDataSucess++;
161 lastFetchTSns = System.nanoTime(); 209 lastFetchTSns = System.nanoTime();
...@@ -175,6 +223,59 @@ public class JmxMon { ...@@ -175,6 +223,59 @@ public class JmxMon {
175 return numCpus; 223 return numCpus;
176 } 224 }
177 225
226 public long getLoadedClassCount() {
227 LongDelta ld = getAttributeLongDelta("java.lang:type=ClassLoading", "LoadedClassCount");
228 return ld.getCurrentValue();
229 }
230
231 public double getCPUPercent() {
232 LongDelta ld = getAttributeLongDelta("java.lang:type=OperatingSystem", "ProcessCpuTime");
233 double cpuDeltaMS = (double)ld.getDelta() / (1000D*1000D);
234 double spentMS = ld.getMilliSeconds();
235 double perCPUMS = cpuDeltaMS / (double)numCpus;
236 double cpuPct = (perCPUMS / spentMS) * 100D;
237 return cpuPct;
238 }
239
240 public double getAverageLoad() {
241 DoubleDelta dd = getAttributeDoubleDelta("java.lang:type=OperatingSystem", "SystemLoadAverage");
242 return dd.getCurrentValue();
243 }
244
245 public double getAverageLoadPerCpu() {
246 DoubleDelta dd = getAttributeDoubleDelta("java.lang:type=OperatingSystem", "SystemLoadAverage");
247 return dd.getCurrentValue()/(double)numCpus;
248 }
249
250 public long getThreads() {
251 LongDelta ld = getAttributeLongDelta("java.lang:type=Threading", "ThreadCount");
252 return ld.getCurrentValue();
253 }
254
255 public double getSystemMemoryPercentUsed() {
256 LongDelta ldf = getAttributeLongDelta("java.lang:type=OperatingSystem", "FreePhysicalMemorySize");
257 LongDelta ldt = getAttributeLongDelta("java.lang:type=OperatingSystem", "TotalPhysicalMemorySize");
258 double free = ldf.getCurrentValue();
259 double total = ldt.getCurrentValue();
260 return ((total - free)/(total)) * 100D;
261 }
262
263 public double getSystemSwapPercentUsed() {
264 LongDelta ldf = getAttributeLongDelta("java.lang:type=OperatingSystem", "FreeSwapSpaceSize");
265 LongDelta ldt = getAttributeLongDelta("java.lang:type=OperatingSystem", "TotalSwapSpaceSize");
266 double free = ldf.getCurrentValue();
267 double total = ldt.getCurrentValue();
268 return ((total - free)/(total)) * 100D;
269 }
270
271 public double getSystemFileDescriptorsPercentUsed() {
272 LongDelta ldo = getAttributeLongDelta("java.lang:type=OperatingSystem", "OpenFileDescriptorCount");
273 LongDelta ldm = getAttributeLongDelta("java.lang:type=OperatingSystem", "MaxFileDescriptorCount");
274 double open = ldo.getCurrentValue();
275 double max = ldm.getCurrentValue();
276 return (open/max) * 100D;
277 }
278
178 public JmxMon() { 279 public JmxMon() {
179 } 280 }
180 281
......
...@@ -32,6 +32,13 @@ class LongDelta { ...@@ -32,6 +32,13 @@ class LongDelta {
32 return 0; 32 return 0;
33 } 33 }
34 } 34 }
35 public long getDelta() {
36 if(lastValue != null && curValue != null) {
37 return curValue - lastValue;
38 } else {
39 return 0;
40 }
41 }
35 public long getCurrentValue() { 42 public long getCurrentValue() {
36 if(this.curValue != null) return this.curValue; 43 if(this.curValue != null) return this.curValue;
37 else return 0; 44 else return 0;
......
...@@ -19,31 +19,38 @@ public class TestRunner { ...@@ -19,31 +19,38 @@ public class TestRunner {
19 19
20 Long ts1 = System.currentTimeMillis(); 20 Long ts1 = System.currentTimeMillis();
21 21
22 JmxMon mon1 = new JmxMon("service:jmx:rmi:///jndi/iiop://u30128:23032/weblogic.management.mbeanservers.runtime"); // 10.3.6.0.161018.2 22 JmxMon mon1 = new JmxMon("service:jmx:iiop:///jndi/iiop://u30457:29722/weblogic.management.mbeanservers.runtime"); // 10.3.6.0.161018.2
23 //JmxMon mon1 = new JmxMon("service:jmx:rmi:///jndi/iiop://u30009:26732/weblogic.management.mbeanservers.runtime"); // 10.3.6.0.12.1 23 //JmxMon mon1 = new JmxMon("service:jmx:rmi:///jndi/iiop://u30128:23032/weblogic.management.mbeanservers.runtime"); // 10.3.6.0.161018.2
24 //JmxMon mon2 = new JmxMon("service:jmx:rmi:///jndi/iiop://u30009:26732/weblogic.management.mbeanservers.runtime"); // 10.3.6.0.12.1
24 mon1.open(); 25 mon1.open();
26 //mon2.open();
25 jmxList.add(mon1); 27 jmxList.add(mon1);
28 //jmxList.add(mon2);
26 29
27 Long time = System.currentTimeMillis() - ts1; 30 Long time = System.currentTimeMillis() - ts1;
28 31
29 System.out.println( 32 System.out.println(
30 "Open called on " + mon1.getConString() 33 "Open called on all jmx monitors"
31 + "\nTime: " + time + "ms\n" 34 + "\nTime: " + time + "ms\n"
32 ); 35 );
33 36
34 System.out.println("Connected to server " + mon1.getServerName() + " on domain " + mon1.getDomainName() + " and machine " + mon1.getRuntimeName()); 37 int times = 10;
35
36 //mon1.listAllMBeans();
37 //mon1.dumpAllMBeans("java.lang:*");
38 /*
39 int times = 0;
40 while(times-- > 0) { 38 while(times-- > 0) {
41 for(JmxMon mon : jmxList) { 39 for(JmxMon mon : jmxList) {
42 ts1 = System.currentTimeMillis(); 40 ts1 = System.currentTimeMillis();
43 long age = mon1.getAgeTs();
44 mon.getData(); 41 mon.getData();
45 time = System.currentTimeMillis() - ts1; 42 time = System.currentTimeMillis() - ts1;
43 System.out.println(mon.getServerName() + " called in " + time + "ms");
44 System.out.println(mon.getServerName() + " Process CPU %: " + mon.getCPUPercent());
45 System.out.println(mon.getServerName() + " Process Threads #: " + mon.getThreads());
46 System.out.println(mon.getServerName() + " Process Loaded Classes #: " + mon.getLoadedClassCount());
47 System.out.println(mon.getServerName() + " Process File Descriptors %: " + mon.getSystemFileDescriptorsPercentUsed());
48 System.out.println(mon.getServerName() + " System Load (per cpu): " + mon.getAverageLoadPerCpu());
49 System.out.println(mon.getServerName() + " System Load (total): " + mon.getAverageLoad());
50 System.out.println(mon.getServerName() + " System Memory %: " + mon.getSystemMemoryPercentUsed());
51 System.out.println(mon.getServerName() + " System Swap %: " + mon.getSystemSwapPercentUsed());
46 52
53 System.out.println();
47 } 54 }
48 Thread.sleep(15000); 55 Thread.sleep(15000);
49 } 56 }
...@@ -53,6 +60,6 @@ public class TestRunner { ...@@ -53,6 +60,6 @@ public class TestRunner {
53 mon.close(); 60 mon.close();
54 System.out.println("Monitor closed. " + mon.getDataCalled() + " calls and " + mon.getDataSucceeded() + " succeeded and " + mon.getDataFailed() + " failed."); 61 System.out.println("Monitor closed. " + mon.getDataCalled() + " calls and " + mon.getDataSucceeded() + " succeeded and " + mon.getDataFailed() + " failed.");
55 } 62 }
56 */ 63
57 } 64 }
58 } 65 }
......