Commit f95dafd9 f95dafd9eadcb148bc4802c93f633e3915c29e0d by Christian Gerdes

Första fungerande version med grundläggande counters.

1 parent 11bc5265
package se.lil.jm;
import java.util.ArrayList;
class Collector {
ArrayList<LongDelta> listLong = new ArrayList<LongDelta>();
public void updateValue(Long ts, String name, Long value) {
LongDelta myDelta = null;
for(int x = 0; x<listLong.size() && myDelta == null; x++) {
if(name.equals(listLong.get(x).name)) {
myDelta = listLong.get(x);
}
}
if(myDelta != null) {
myDelta.update(ts, value);
} else {
myDelta = new LongDelta(ts, value);
listLong.add(myDelta);
}
}
public long getCurrentValue(String name) {
LongDelta myDelta = null;
for(int x = 0; x<listLong.size() && myDelta == null; x++) {
if(name.equals(listLong.get(x).name)) {
myDelta = listLong.get(x);
}
}
if(myDelta != null)
return myDelta.getCurrentValue();
else
return 0;
}
public double getPerSecValue(String name) throws Throwable {
LongDelta myDelta = null;
for(int x = 0; x<listLong.size() && myDelta == null; x++) {
if(name.equals(listLong.get(x).name)) {
myDelta = listLong.get(x);
}
}
if(myDelta != null)
return myDelta.getPerSecondValue();
else
return 0;
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@ package se.lil.jm;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
......@@ -32,12 +33,39 @@ public class JmxMon {
Thread myThread = null;
ClassLoader myCL = null;
Collector col = new Collector();
int getDataCalls = 0;
int getDataSucess = 0;
public Object getAttributeValue(String name, String attribute) {
HashMap<String, LongDelta> longDeltaMap = new HashMap<String, LongDelta>();
HashMap<String, DoubleDelta> doubleDeltaMap = new HashMap<String, DoubleDelta>();
private void updateLongDelta (Long timestamp, String name, Long value) {
LongDelta ld = longDeltaMap.get(name);
if(ld == null) {
ld = new LongDelta();
longDeltaMap.put(name, ld);
}
ld.update(timestamp, value);
}
private LongDelta getAttributeLongDelta(String name, String attribute) {
return longDeltaMap.get(name + ":" + attribute);
}
private void updateDoubleDelta (Long timestamp, String name, Double value) {
DoubleDelta dd = doubleDeltaMap.get(name);
if(dd == null) {
dd = new DoubleDelta();
doubleDeltaMap.put(name, dd);
}
dd.update(timestamp, value);
}
private DoubleDelta getAttributeDoubleDelta(String name, String attribute) {
return doubleDeltaMap.get(name + ":" + attribute);
}
private Object getAttributeValue(String name, String attribute) {
Object ro = null;
if(mbsc != null) {
try {
......@@ -49,6 +77,28 @@ public class JmxMon {
return ro;
}
private void fetchUpdateAttribute(String name, String attribute) {
String fullName = name + ":" + attribute;
Object obj = getAttributeValue(name, attribute);
Long ts = (Long)getAttributeValue("java.lang:type=Runtime", "Uptime");
if(obj != null) {
if(obj.getClass().equals(java.lang.Long.class)) {
updateLongDelta(ts, fullName, (Long)obj);
}
if(obj.getClass().equals(java.lang.Integer.class)) {
Integer i = (Integer)obj;
updateLongDelta(ts, fullName, i.longValue());
}
if(obj.getClass().equals(java.lang.Double.class)) {
updateDoubleDelta(ts, fullName, (Double)obj);
}
if(obj.getClass().equals(java.lang.Float.class)) {
Float f = (Float)obj;
updateDoubleDelta(ts, fullName, f.doubleValue());
}
}
}
public int getDataCalled() {
return getDataCalls;
}
......@@ -143,19 +193,17 @@ public class JmxMon {
long startTSns = System.nanoTime();
getDataCalls++;
// Do the update of data
Object mb;
mb = mbsc.getAttribute(new ObjectName("java.lang:type=OperatingSystem"), "ProcessCpuTime");
System.out.println("Class: " + mb.getClass());
System.out.println("toString: " + mb.toString());
mb = mbsc.getAttribute(new ObjectName("java.lang:type=OperatingSystem"), "AvailableProcessors");
System.out.println("Class: " + mb.getClass());
System.out.println("toString: " + mb.toString());
mb = mbsc.getAttribute(new ObjectName("java.lang:type=OperatingSystem"), "OpenFileDescriptorCount");
System.out.println("Class: " + mb.getClass());
System.out.println("toString: " + mb.toString());
mb = mbsc.getAttribute(new ObjectName("java.lang:type=Runtime"), "Uptime");
System.out.println("Class: " + mb.getClass());
System.out.println("toString: " + mb.toString());
numCpus = (Integer)getAttributeValue("java.lang:type=OperatingSystem", "AvailableProcessors");
fetchUpdateAttribute("java.lang:type=OperatingSystem", "ProcessCpuTime");
fetchUpdateAttribute("java.lang:type=OperatingSystem", "OpenFileDescriptorCount");
fetchUpdateAttribute("java.lang:type=OperatingSystem", "MaxFileDescriptorCount");
fetchUpdateAttribute("java.lang:type=OperatingSystem", "TotalSwapSpaceSize");
fetchUpdateAttribute("java.lang:type=OperatingSystem", "FreeSwapSpaceSize");
fetchUpdateAttribute("java.lang:type=OperatingSystem", "FreePhysicalMemorySize");
fetchUpdateAttribute("java.lang:type=OperatingSystem", "TotalPhysicalMemorySize");
fetchUpdateAttribute("java.lang:type=OperatingSystem", "SystemLoadAverage");
fetchUpdateAttribute("java.lang:type=Threading", "ThreadCount");
fetchUpdateAttribute("java.lang:type=ClassLoading", "LoadedClassCount");
// Finished updating data
getDataSucess++;
lastFetchTSns = System.nanoTime();
......@@ -175,6 +223,59 @@ public class JmxMon {
return numCpus;
}
public long getLoadedClassCount() {
LongDelta ld = getAttributeLongDelta("java.lang:type=ClassLoading", "LoadedClassCount");
return ld.getCurrentValue();
}
public double getCPUPercent() {
LongDelta ld = getAttributeLongDelta("java.lang:type=OperatingSystem", "ProcessCpuTime");
double cpuDeltaMS = (double)ld.getDelta() / (1000D*1000D);
double spentMS = ld.getMilliSeconds();
double perCPUMS = cpuDeltaMS / (double)numCpus;
double cpuPct = (perCPUMS / spentMS) * 100D;
return cpuPct;
}
public double getAverageLoad() {
DoubleDelta dd = getAttributeDoubleDelta("java.lang:type=OperatingSystem", "SystemLoadAverage");
return dd.getCurrentValue();
}
public double getAverageLoadPerCpu() {
DoubleDelta dd = getAttributeDoubleDelta("java.lang:type=OperatingSystem", "SystemLoadAverage");
return dd.getCurrentValue()/(double)numCpus;
}
public long getThreads() {
LongDelta ld = getAttributeLongDelta("java.lang:type=Threading", "ThreadCount");
return ld.getCurrentValue();
}
public double getSystemMemoryPercentUsed() {
LongDelta ldf = getAttributeLongDelta("java.lang:type=OperatingSystem", "FreePhysicalMemorySize");
LongDelta ldt = getAttributeLongDelta("java.lang:type=OperatingSystem", "TotalPhysicalMemorySize");
double free = ldf.getCurrentValue();
double total = ldt.getCurrentValue();
return ((total - free)/(total)) * 100D;
}
public double getSystemSwapPercentUsed() {
LongDelta ldf = getAttributeLongDelta("java.lang:type=OperatingSystem", "FreeSwapSpaceSize");
LongDelta ldt = getAttributeLongDelta("java.lang:type=OperatingSystem", "TotalSwapSpaceSize");
double free = ldf.getCurrentValue();
double total = ldt.getCurrentValue();
return ((total - free)/(total)) * 100D;
}
public double getSystemFileDescriptorsPercentUsed() {
LongDelta ldo = getAttributeLongDelta("java.lang:type=OperatingSystem", "OpenFileDescriptorCount");
LongDelta ldm = getAttributeLongDelta("java.lang:type=OperatingSystem", "MaxFileDescriptorCount");
double open = ldo.getCurrentValue();
double max = ldm.getCurrentValue();
return (open/max) * 100D;
}
public JmxMon() {
}
......
......@@ -32,6 +32,13 @@ class LongDelta {
return 0;
}
}
public long getDelta() {
if(lastValue != null && curValue != null) {
return curValue - lastValue;
} else {
return 0;
}
}
public long getCurrentValue() {
if(this.curValue != null) return this.curValue;
else return 0;
......
......@@ -19,31 +19,38 @@ public class TestRunner {
Long ts1 = System.currentTimeMillis();
JmxMon mon1 = new JmxMon("service:jmx:rmi:///jndi/iiop://u30128:23032/weblogic.management.mbeanservers.runtime"); // 10.3.6.0.161018.2
//JmxMon mon1 = new JmxMon("service:jmx:rmi:///jndi/iiop://u30009:26732/weblogic.management.mbeanservers.runtime"); // 10.3.6.0.12.1
JmxMon mon1 = new JmxMon("service:jmx:iiop:///jndi/iiop://u30457:29722/weblogic.management.mbeanservers.runtime"); // 10.3.6.0.161018.2
//JmxMon mon1 = new JmxMon("service:jmx:rmi:///jndi/iiop://u30128:23032/weblogic.management.mbeanservers.runtime"); // 10.3.6.0.161018.2
//JmxMon mon2 = new JmxMon("service:jmx:rmi:///jndi/iiop://u30009:26732/weblogic.management.mbeanservers.runtime"); // 10.3.6.0.12.1
mon1.open();
//mon2.open();
jmxList.add(mon1);
//jmxList.add(mon2);
Long time = System.currentTimeMillis() - ts1;
System.out.println(
"Open called on " + mon1.getConString()
"Open called on all jmx monitors"
+ "\nTime: " + time + "ms\n"
);
System.out.println("Connected to server " + mon1.getServerName() + " on domain " + mon1.getDomainName() + " and machine " + mon1.getRuntimeName());
//mon1.listAllMBeans();
//mon1.dumpAllMBeans("java.lang:*");
/*
int times = 0;
int times = 10;
while(times-- > 0) {
for(JmxMon mon : jmxList) {
ts1 = System.currentTimeMillis();
long age = mon1.getAgeTs();
mon.getData();
time = System.currentTimeMillis() - ts1;
System.out.println(mon.getServerName() + " called in " + time + "ms");
System.out.println(mon.getServerName() + " Process CPU %: " + mon.getCPUPercent());
System.out.println(mon.getServerName() + " Process Threads #: " + mon.getThreads());
System.out.println(mon.getServerName() + " Process Loaded Classes #: " + mon.getLoadedClassCount());
System.out.println(mon.getServerName() + " Process File Descriptors %: " + mon.getSystemFileDescriptorsPercentUsed());
System.out.println(mon.getServerName() + " System Load (per cpu): " + mon.getAverageLoadPerCpu());
System.out.println(mon.getServerName() + " System Load (total): " + mon.getAverageLoad());
System.out.println(mon.getServerName() + " System Memory %: " + mon.getSystemMemoryPercentUsed());
System.out.println(mon.getServerName() + " System Swap %: " + mon.getSystemSwapPercentUsed());
System.out.println();
}
Thread.sleep(15000);
}
......@@ -53,6 +60,6 @@ public class TestRunner {
mon.close();
System.out.println("Monitor closed. " + mon.getDataCalled() + " calls and " + mon.getDataSucceeded() + " succeeded and " + mon.getDataFailed() + " failed.");
}
*/
}
}
......