CollTomcat5.java 10.5 KB
package com.sitech.ismp.coll.middleware.tomcat.impl;

import java.lang.management.MemoryUsage;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.util.logging.Logger;

import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeDataSupport;

import com.sitech.ismp.coll.CollBase;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.Formater;

public class CollTomcat5 extends CollTomcat implements ICollTomcat {
	
	private Logger logger = Logger.getLogger("COLL");
	private static String KBP_CLASS = "10-12-13";

	public CollTomcat5() {
	}

	public Vector<TblATO_KPIDETAIL> getMemory(HashMap<String, String> params) {
		CollBase collResult = new CollBase();
		init(params);
		String domain_name = params.get("DOMAIN_NAME");
		String UNIT_ID = KBP_CLASS + "-12:" + Formater.neatenunitid(domain_name) + "-MEMORY";
		try {
			MemoryUsage heapMemoryUsage = MemoryUsage.from((CompositeDataSupport) this.execute("java.lang:type=Memory", "HeapMemoryUsage"));
			// 表示可以用于内存管理的最大内存量(以字节为单位)
			double maxMemory = heapMemoryUsage.getMax() / 1024.0;
			// 表示保证可以由 Java 虚拟机使用的内存量(以字节为单位)
			double commitMemory = heapMemoryUsage.getCommitted() / 1024.0;
			// 表示当前已经使用的内存量(以字节为单位)
			double usedMemory = heapMemoryUsage.getUsed() / 1024.0;
			double heapUsage = (double) usedMemory * 100 / commitMemory;
			collResult.addKPI(UNIT_ID, "PM-00-04-004-01", Formater.getByScale(String.valueOf(maxMemory), 4));
			collResult.addKPI(UNIT_ID, "PM-00-04-004-02", Formater.getByScale(String.valueOf(commitMemory), 4));
			collResult.addKPI(UNIT_ID, "PM-00-04-004-03", Formater.getByScale(String.valueOf(usedMemory), 4));
			collResult.addKPI(UNIT_ID, "PM-00-04-004-04", Formater.getByScale(String.valueOf(heapUsage), 2));
			logger.info("[" + maxMemory + "][" + commitMemory + "][" + usedMemory + "][" + heapUsage);
			
			MemoryUsage permGenUsage = MemoryUsage.from((CompositeDataSupport) this.execute("java.lang:type=MemoryPool,name=PS Perm Gen", "Usage"));
			double maxd = permGenUsage.getMax() / 1024.0;
			double committed = permGenUsage.getCommitted() / 1024.0;
			double used = permGenUsage.getUsed() / 1024.0;
			double permUsage = (double) used * 100 / committed;
			collResult.addKPI(UNIT_ID, "PM-00-04-004-05", Formater.getByScale(String.valueOf(maxd), 4));
			collResult.addKPI(UNIT_ID, "PM-00-04-004-06", Formater.getByScale(String.valueOf(committed), 4));
			collResult.addKPI(UNIT_ID, "PM-00-04-004-07", Formater.getByScale(String.valueOf(used), 4));
			collResult.addKPI(UNIT_ID, "PM-00-04-004-08", Formater.getByScale(String.valueOf(permUsage), 2));
			logger.info("[" + committed + "][" + used + "][" + permUsage + "]");
		} catch (NullPointerException e1) {
			e1.printStackTrace();
		} finally {
			this.release();
		}
		return collResult.KPISet;
	}

	public Vector<TblATO_KPIDETAIL> getRuntime(HashMap<String, String> params) {
		CollBase collResult = new CollBase();
		init(params);
		String domain_name = params.get("DOMAIN_NAME");
//		String ip_iddr = params.get("IP_IDDR");
		String UNIT_ID = KBP_CLASS + "-10:" + Formater.neatenunitid(domain_name) + "-RUNTIME";
		try {
			HashMap<String, Object> resulMap = this.executeAttributeArray("java.lang:type=Runtime", new String[] { "StartTime", "Uptime", "VmVendor", "VmName", "VmVersion", "Name" });
			// Java 虚拟机的启动时间(以毫秒为单位)
			Date startTime = new Date(((Long) resulMap.get("StartTime")).longValue());
			// Java 虚拟机的正常运行时间(以毫秒为单位)
			Long upTime = (Long) resulMap.get("Uptime");
			// Java 虚拟机实现供应商
			String vmVendor = (String) resulMap.get("VmVendor");
			// Java 虚拟机实现名称
			String vmName = (String) resulMap.get("VmName");
			// Java 虚拟机实现版本
			String vmVersion = (String) resulMap.get("VmVersion");
			// 正在运行的 Java 虚拟机的名称
			String name = (String) resulMap.get("Name");
			collResult.addKPI(UNIT_ID, "CM-00-05-009-14", Formater.datetimeToString(startTime));
			collResult.addKPI(UNIT_ID, "CM-00-01-999-03", Formater.devDayHourSecond(upTime));
//			collResult.addKPI(UNIT_ID, "CM-00-04-004-03", ip_iddr);
			collResult.addKPI(UNIT_ID, "CM-00-05-009-34", vmVendor);
			collResult.addKPI(UNIT_ID, "CM-00-05-009-35", vmName);
			collResult.addKPI(UNIT_ID, "CM-00-05-009-36", vmVersion);
			collResult.addKPI(UNIT_ID, "CM-00-05-009-37", name);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				this.release();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return collResult.KPISet;
	}
	
	public Vector<TblATO_KPIDETAIL> getThreading(HashMap<String, String> params) {
		CollBase collResult = new CollBase();
		init(params);
		String domain_name = params.get("DOMAIN_NAME");
		String UNIT_ID = KBP_CLASS + "-10:" + Formater.neatenunitid(domain_name) + "-RUNTIME";
		try {
			HashMap<String, Object> resulMap = this.executeAttributeArray("java.lang:type=Threading", new String[] { "TotalStartedThreadCount", "DaemonThreadCount", "ThreadCount", "PeakThreadCount" });
			// 已启动的线程总数
			Long totalStartedThreadCount = (Long) resulMap.get("TotalStartedThreadCount");
			// 守护线程
			Integer daemonThreadCount = (Integer) resulMap.get("DaemonThreadCount");
			// 活动线程
			Integer threadCount = (Integer) resulMap.get("ThreadCount");
			// 峰值
			Integer peakThreadCount = (Integer) resulMap.get("PeakThreadCount");
			collResult.addKPI(UNIT_ID, "PM-00-05-009-48", String.valueOf(totalStartedThreadCount));
			collResult.addKPI(UNIT_ID, "PM-00-05-009-49", String.valueOf(daemonThreadCount));
			collResult.addKPI(UNIT_ID, "PM-00-05-009-50", String.valueOf(threadCount));
			collResult.addKPI(UNIT_ID, "PM-00-05-009-51", String.valueOf(peakThreadCount));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				this.release();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return collResult.KPISet;
	}
	
	public Vector<TblATO_KPIDETAIL> getThreadPool(HashMap<String, String> params) {
		CollBase collResult = new CollBase();
		init(params);
		String domain_name = params.get("DOMAIN_NAME");
		try {
			List<HashMap<String, Object>> exeResult = this.executQueryNames("Catalina:type=ThreadPool,*", new String[] { "maxThreads", "currentThreadCount", "currentThreadsBusy" });
			for (HashMap<String, Object> resulMap : exeResult) {
				// 线程池名
				String name = (String) resulMap.get("name");
				String UNIT_ID = KBP_CLASS + "-13:" + Formater.neatenunitid(domain_name) + "-" + Formater.neatenunitid(name);
				// 最大线程数
				String maxThreads = ((Integer) resulMap.get("maxThreads")).toString();
				// 当前线程数
				String currentThreadCount = ((Integer) resulMap.get("currentThreadCount")).toString();
				// 繁忙线程数
				String currentThreadsBusy = ((Integer) resulMap.get("currentThreadsBusy")).toString();
				collResult.addKPI(UNIT_ID, "CM-00-05-009-25", name);
				collResult.addKPI(UNIT_ID, "PM-00-04-009-19", maxThreads);
				collResult.addKPI(UNIT_ID, "PM-00-04-009-20", currentThreadCount);
				collResult.addKPI(UNIT_ID, "PM-00-04-009-21", currentThreadsBusy);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				this.release();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return collResult.KPISet;
	}
	
	public Vector<TblATO_KPIDETAIL> getSession(HashMap<String, String> params) {
		CollBase collResult = new CollBase();
		init(params);
		String domain_name = params.get("DOMAIN_NAME");
		try {
			Set<ObjectInstance> managerSet = this.executeQueryMbeans("Catalina:type=Manager,*", null);
			for (Object o : managerSet) {
				javax.management.ObjectInstance _1 = (javax.management.ObjectInstance) o;
				ObjectName obj = _1.getObjectName();
				// 应用名
				String applicationName = obj.getKeyProperty("path");
				String UNIT_ID = KBP_CLASS + "-11:" + Formater.neatenunitid(domain_name) + "-" + Formater.neatenunitid(applicationName);
				String objname = obj.getCanonicalName();
				// 最大会话数
				String maxActiveSessions = this.execute(objname, "maxActiveSessions").toString();
				// 会话数
				String currentActiveSession = (String) this.execute(objname, "activeSessions").toString();
				// 活动会话数
				String sessionCounter = (String) this.execute(objname, "sessionCounter").toString();
				collResult.addKPI(UNIT_ID, "CM-00-05-009-01", applicationName);
				collResult.addKPI(UNIT_ID, "PM-00-05-009-42", maxActiveSessions);
				collResult.addKPI(UNIT_ID, "PM-00-05-009-40", currentActiveSession);
				collResult.addKPI(UNIT_ID, "PM-00-05-009-43", sessionCounter);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			this.release();
		}
		return collResult.KPISet;
	}

	public Vector<TblATO_KPIDETAIL> getClassLoading(HashMap<String, String> params) {
		CollBase collResult = new CollBase();
		init(params);
		String domain_name = params.get("DOMAIN_NAME");
		String UNIT_ID = KBP_CLASS + "-14:" + Formater.neatenunitid(domain_name) + "-CLASSLOADING";
		try {
			HashMap<String, Object> resulMap = this.executeAttributeArray("java.lang:type=ClassLoading", new String[] { "LoadedClassCount", "TotalLoadedClassCount", "UnloadedClassCount" });
			// 当前类已装入
			Integer loadedClassCount = (Integer) resulMap.get("LoadedClassCount");
			// 已装入类的总数
			Long totalLoadedClassCount = (Long) resulMap.get("TotalLoadedClassCount");
			// 已卸载类的总数
			Long unloadedClassCount = (Long) resulMap.get("UnloadedClassCount");
			collResult.addKPI(UNIT_ID, "PM-00-05-009-44", String.valueOf(loadedClassCount));
			collResult.addKPI(UNIT_ID, "PM-00-05-009-45", String.valueOf(totalLoadedClassCount));
			collResult.addKPI(UNIT_ID, "PM-00-05-009-46", String.valueOf(unloadedClassCount));
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				this.release();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return collResult.KPISet;
	}
	
	public static void main(String[] args) {
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://172.21.136.110:1090/jmxrmi");
		params.put("DOMAIN_NAME", "local-tomcat5");
		CollTomcat5 ct = new CollTomcat5();
		Vector<TblATO_KPIDETAIL> result = ct.getClassLoading(params);
		for (TblATO_KPIDETAIL tk : result) {
			System.out.println("[" + tk.UNIT_ID + "][" + tk.KPI_ID + "][" + tk.KPI_VALUE + "][" + tk.CLL_TIME + "]");
		}
	}
}