Commit d6ba81ae d6ba81ae282c3397fb62dd34954a88ca935aa184 by Christian Gerdes

New library: LILJM for JMX Monitoring

1 parent 2cf7063d
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path=""/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="output" path=""/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>LILJM Library</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.6
/JmxMon.class
/Collector.class
/DoubleDelta.class
/LongDelta.class
/Registry.class
/TestRunner.class
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
package se.lil.jm;
public class DoubleDelta {
public String name = null;
public Double curValue = null;
public Double lastValue = null;
public Long lastFetch = null;
public Long curFetch = null;
public DoubleDelta() {
}
public DoubleDelta(Long timestamp, Double value) {
this.curFetch = timestamp;
this.curValue = value;
}
public void update(Long timestamp, Double value) {
this.lastFetch = this.curFetch;
this.curFetch = timestamp;
this.lastValue = this.curValue;
this.curValue = value;
}
public double getPerSecondValue() {
if(this.curValue != null && this.lastValue != null && this.lastFetch != null && this.curFetch != null) {
// We have values, calculate the number of gets per second
double numMilliSeconds = this.curFetch - this.lastFetch;
double delta = this.curValue - this.lastValue;
double perMilli = delta/numMilliSeconds;
return perMilli * 1000;
} else {
return 0;
}
}
public double getCurrentValue() {
if(this.curValue != null) return this.curValue;
else return 0;
}
public double getSeconds() {
if(this.lastFetch != null && this.curFetch != null) {
double numSeconds = this.curFetch - this.lastFetch;
return numSeconds/1000;
}
return 0;
}
public long getMilliSeconds() {
if(this.lastFetch != null && this.curFetch != null) {
return this.curFetch - this.lastFetch;
}
return 0;
}
}
package se.lil.jm;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import javax.management.JMX;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.management.openmbean.CompositeData;
public class JmxMon {
String conUser = "system";
String conPass = "passw0rd";
String domain = null;
JMXServiceURL url = null;
JMXConnector jmxc = null;
MBeanServerConnection mbsc = null;
Collector col = new Collector();
int getDataCalls = 0;
int getDataSucess = 0;
public int getDataCalled() {
return getDataCalls;
}
public int getDataSucceeded() {
return getDataSucess;
}
public int getDataFailed() {
return getDataCalls - getDataSucess;
}
public String getConString() {
if(this.url != null) return this.url.toString();
else return null;
}
public void open() throws IOException {
if(url != null) {
jmxc = JMXConnectorFactory.connect(url);
mbsc = jmxc.getMBeanServerConnection();
domain = mbsc.getDefaultDomain();
}
}
public void open(String serviceURL) throws IOException, MalformedURLException {
url = new JMXServiceURL(serviceURL);
open();
}
public void close() throws IOException {
if(jmxc != null) jmxc.close();
jmxc = null;
mbsc = null;
}
long lastRTns = 0;
public long getLastRTms() {
return lastRTns / (1000 * 1000);
}
long lastFetchTSns = 0;
public boolean getData(long ageTs) throws Throwable {
if (getAgeTs() > ageTs ) return getData();
else return false;
}
public long getAgeTs() {
if(lastFetchTSns > 0) return (System.nanoTime() - lastFetchTSns)/(1000*1000*1000);
else return 0;
}
Lock lock = new ReentrantLock();
public boolean getData() throws Throwable {
// Start thread safe
// Try to get a lock
boolean haveLock = lock.tryLock();
if( haveLock == false) {
// We could not get the lock, someone else is updating. Wait for it to complete by waiting for a lock, then unlock and return.
try {
lock.lock();
} finally {
lock.unlock();
}
return false;
}
// 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.
try {
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());
// Finished updating data
getDataSucess++;
lastFetchTSns = System.nanoTime();
lastRTns = lastFetchTSns - startTSns;
return true;
} catch (Throwable e) {
throw (e);
} finally {
lock.unlock();
}
// End thread safe
}
//NUM_CPUS
int numCpus = 0;
public long getNumberOfCPUs() {
return numCpus;
}
public JmxMon() {
}
public JmxMon(String serviceURL) throws MalformedURLException {
this.url = new JMXServiceURL(serviceURL);
}
}
package se.lil.jm;
class LongDelta {
public String name = null;
public Long curValue = null;
public Long lastValue = null;
public Long lastFetch = null;
public Long curFetch = null;
public LongDelta() {
}
public LongDelta(Long timestamp, Long value) {
this.curFetch = timestamp;
this.curValue = value;
}
public void update(Long timestamp, Long value) {
this.lastFetch = this.curFetch;
this.curFetch = timestamp;
this.lastValue = this.curValue;
this.curValue = value;
}
public double getPerSecondValue() {
if(this.curValue != null && this.lastValue != null && this.lastFetch != null && this.curFetch != null) {
// We have values, calculate the number of gets per second
double numMilliSeconds = this.curFetch - this.lastFetch;
double delta = this.curValue - this.lastValue;
double perMilli = delta/numMilliSeconds;
return perMilli * 1000;
} else {
return 0;
}
}
public long getCurrentValue() {
if(this.curValue != null) return this.curValue;
else return 0;
}
public double getSeconds() {
if(this.lastFetch != null && this.curFetch != null) {
double numSeconds = this.curFetch - this.lastFetch;
return numSeconds/1000;
}
return 0;
}
public long getMilliSeconds() {
if(this.lastFetch != null && this.curFetch != null) {
return this.curFetch - this.lastFetch;
}
return 0;
}
}
\ No newline at end of file
package se.lil.jm;
import java.net.MalformedURLException;
import java.util.ArrayList;
public class Registry {
private static ArrayList<JmxMon> jmxList = null;
public static boolean test = true;
public static synchronized ArrayList<JmxMon> getList() {
if (jmxList == null) {
jmxList = new ArrayList<JmxMon>();
}
return jmxList;
}
public static synchronized JmxMon findOrCreate(String conStr) throws MalformedURLException {
for (JmxMon item : getList()) {
if(item.getConString().equals(conStr)) {
return item;
}
}
// Not found, create it
JmxMon monitor = new JmxMon(conStr);
jmxList.add(monitor);
return monitor;
}
}
package se.lil.jm;
import java.util.ArrayList;
public class TestRunner {
public TestRunner() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) throws Throwable{
// TODO Auto-generated method stub
//ArrayList<OraMon> oraList = new ArrayList<OraMon>();
ArrayList<JmxMon> jmxList = Registry.getList();
Long ts1 = System.currentTimeMillis();
JmxMon mon1 = new JmxMon("service:jmx:iiop:///jndi/iiop://u01892.ef.kap.rsv.se:17020/weblogic.management.mbeanservers.runtime");
mon1.open();
jmxList.add(mon1);
Long time = System.currentTimeMillis() - ts1;
System.out.println(
"Open called on " + mon1.getConString()
+ "\nTime: " + time + "ms\n"
);
int times = 3;
while(times-- > 0) {
for(JmxMon mon : jmxList) {
ts1 = System.currentTimeMillis();
long age = mon1.getAgeTs();
mon.getData();
time = System.currentTimeMillis() - ts1;
System.out.println(
"GetData called on " + mon.domain + " (age was " + age + "s)"
+ "\nTime: " + time + "ms\n\n"
);
}
Thread.sleep(15000);
}
// Close all mon objects
for(JmxMon mon : jmxList) {
mon.close();
System.out.println("Monitor closed. " + mon.getDataCalled() + " calls and " + mon.getDataSucceeded() + " succeeded and " + mon.getDataFailed() + " failed.");
}
}
}