JmxMon.java 4.69 KB
package se.lil.jm;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.management.JMX;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.MalformedObjectNameException;
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 void dumpAllMBeans() {
		dumpAllMBeans(null);
	}
	
	public void dumpAllMBeans(String query) {
		if(mbsc != null) {
			try {
				Set<ObjectName> res = mbsc.queryNames(query != null ? new ObjectName(query) : ObjectName.WILDCARD, null);
				for (ObjectName name : res) {
					System.out.println(name.getCanonicalName());
					MBeanInfo inf = mbsc.getMBeanInfo(name);
					MBeanAttributeInfo[] attribs = inf.getAttributes();
					for (MBeanAttributeInfo ainf : attribs) {
						System.out.println("(" + ainf.getType() + ") " + ainf.getName());
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	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);
	}
}