CollTimestenWithCMD.java 10.9 KB
package com.sitech.ismp.coll;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Vector;

import org.apache.log4j.Logger;

public class CollTimestenWithCMD {
	
	private Logger logger = Logger.getLogger("COLL");
	private String KBP_ID = "10-11-34";
	private String TTSERVER;
	private String CMD_DIR;
	private String IP_ADDR;
	private String PORT;
	private String USERNAME;
	private String PASSWORD;
	private String LOG_DIR;
	
	private boolean IS_DIR = true;
	private boolean IS_LOG_DIR = true;

	/**
	 * 初始化数据库链接
	 */
	public void initParams(HashMap params) {
		this.TTSERVER = (String) params.get("TTSERVER");
		this.CMD_DIR = (String) params.get("CMD_DIR");
		this.IP_ADDR = (String) params.get("IP_ADDR");
		this.PORT = (String) params.get("PORT");
		this.USERNAME = (String) params.get("USERNAME");
		this.PASSWORD = (String) params.get("PASSWORD");
		this.LOG_DIR = (String) params.get("LOG_DIR");
		File file = new File(CMD_DIR);
		if (!file.exists() || !file.isDirectory()) {
			logger.info("CMD_DIR is not a directory");
			this.IS_DIR = false;
		}
		File logsfile = new File(LOG_DIR);
		if (!logsfile.exists() || !logsfile.isDirectory()) {
			logger.info("LOG_DIR is not a directory");
			this.IS_LOG_DIR = false;
		}
	}
	
	/**
	 * 执行命令
	 */ 
	private Vector execute(String shellCommand) {
		logger.info("cmd: " + shellCommand);
		Vector vResult = null;
		try {
			Process process = Runtime.getRuntime().exec(shellCommand);
			vResult = new Vector();
			BufferedReader reader = null;
			reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			String line = reader.readLine();
			logger.info("line: " + line);
			while (line != null) {
				vResult.addElement(line);
				line = reader.readLine();
			}
			reader.close();
			return vResult;
		} catch (Exception e) {
			logger.info("在本机上可能没有 " + shellCommand + " 命令!");
		} finally {
			Runtime.getRuntime().freeMemory();
			Runtime.getRuntime().gc();
		}
		return vResult;
	}

	/**
	 * 获取配置数据
	 */
	public Vector getConfig(HashMap params) {
		initParams(params);
		// 保存采集结果,并返回值
		CollBase collResult = new CollBase();
		String config_PRE_UNITID = this.KBP_ID + "-10";
		if (IS_DIR) {
			// 采集数据库版本和位数	ttVersion
			String timesten_version_cmd = this.CMD_DIR
					+ System.getProperty("file.separator") + "ttVersion";
			/**
			 * TimesTen Release 7.0.6.2.0 (32 bit NT) (tt70_32:17000) 2010-06-16T07:34:37Z
			 * Instance admin: mooker
			 * Instance home directory: F:\TimesTen\tt70_32
			 * Daemon home directory: F:\TimesTen\tt70_32\srv\info
			 * Access control enabled.
			 */
			Vector version = this.execute(timesten_version_cmd);
			String vesrion = "unkown";
			String bitinfo = "32";
			if (version != null && version.size() > 0) {
				logger.info("size is not 0");
				for (int i = 0; i < version.size(); i++) {
					String res = (String) version.get(i);
					if (res.indexOf("Release") >= 0) {
						String[] ver = res.split(" ");
						if (ver != null && ver.length > 0) {
							vesrion = ver[2];
							bitinfo = ver[3].substring(1);
						}
					}
				}
			} else {
				logger.info("size is  0");
			}
			// CM-00-03-001-01 数据库名
			collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total", 
					"CM-00-03-001-01", this.TTSERVER);
			// CM-00-03-001-02 数据库版本
			collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total", 
					"CM-00-03-001-02", vesrion);
			// CM-00-03-001-03 数据库位数
			collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total", 
					"CM-00-03-001-03", bitinfo + "bit");
		}
		return collResult.getKPISet();
	}

	/**
	 * 获取性能数据
	 * 
	 * @param params
	 * @return
	 */
	public Vector getPerformance(HashMap params) {
		return null;
	}
	
	/**
	 * 监控锁
	 */
	public Vector getLockInfo(HashMap params) {
		initParams(params);
		// 保存采集结果,并返回值
		CollBase collResult = new CollBase();
		String config_PRE_UNITID = this.KBP_ID + "-11";
		if (IS_DIR) {
			String timesten_lock_cmd = this.CMD_DIR
					+ System.getProperty("file.separator") + "ttxact.sh";
			logger.info("timesten_lock_cmd is " + timesten_lock_cmd);
			Vector lock_v = this.execute(timesten_lock_cmd);
			// 共享锁数
			int alock_total = 0;
			// 更新锁数
			int block_total = 0;
			// 独占锁数
			int clock_total = 0;
			// 系统锁
			int dlock_total = 0;
			if (lock_v != null && lock_v.size() > 0) {
				for (int i = 0; i < lock_v.size(); i++) {
					String res = (String) lock_v.get(i);
					if (res.indexOf("Command") >= 0) {
						String type = collResult.split(res, 2);
						if (type.equals("S")) {
							alock_total++;
						}
					}
					if (res.indexOf("Row") >= 0) {
						block_total++;
					}
					if (res.indexOf("Xn") >= 0) {
						String type = collResult.split(res, 0);
						String flag = collResult.split(res, 2);
						String info = collResult.split(res, 3);
						clock_total++;
						logger.info("type+flag+info: " + type + " " + flag + " " + info);
					}
					if (res.indexOf("SYS.TABLES") >= 0) {
						dlock_total++;
					}
				}
			}
//			logger.info("alock_total " + alock_total);
//			logger.info("block_total " + block_total);
//			logger.info("clock_total " + clock_total);
//			logger.info("dlock_total " + dlock_total);
			// PM-00-03-008-01 总锁数
			collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total", 
					"PM-00-03-008-01", String.valueOf(block_total + clock_total + dlock_total));
			// PM-00-03-008-17 共享锁数
			collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total", 
					"PM-00-03-008-17", String.valueOf(alock_total));
			// PM-00-03-008-18 更新锁数
			collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total", 
					"PM-00-03-008-18", String.valueOf(block_total));
			// PM-00-03-008-19 独占锁数
			collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total", 
					"PM-00-03-008-19", String.valueOf(clock_total));
			// PM-00-03-008-20 系统锁
			collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total", 
					"PM-00-03-008-20", String.valueOf(dlock_total));
		}
		return collResult.getKPISet();
	}
	
	/**
	 * 实时连接数 ttstatus | grep Server | wc -l
	 */
	public Vector getLinkedNum(HashMap params) {
		initParams(params);
		CollBase collResult = new CollBase();
		String config_PRE_UNITID = this.KBP_ID + "-10";
		int linknum = 0;
		if (IS_DIR) {
			String timesten_link_cmd = this.CMD_DIR
					+ System.getProperty("file.separator") + "ttstatus";
			logger.info("timesten_link_cmd: " + timesten_link_cmd);
			Vector linknum_v = this.execute(timesten_link_cmd);
			if (linknum_v != null && linknum_v.size() > 0) {
				for (int i = 0; i < linknum_v.size(); i++) {
					String res = (String) linknum_v.get(i);
					if (res.indexOf("Server") >= 0) {
						linknum++;
					}
				}
			}
		}
		logger.info(String.valueOf(linknum));
		// FM-00-03-001-09 实时连接数
		collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total", 
				"FM-00-03-001-09", String.valueOf(linknum));
		return collResult.getKPISet();
	}
	
	/**
	 * 事务采集
	 */
	public Vector getTransaction(HashMap params) {
		initParams(params);
		// 保存采集结果,并返回值
		CollBase collResult = new CollBase();
		String config_PRE_UNITID = this.KBP_ID + "-12";
		if (IS_DIR) {
			String timesten_trans_cmd = this.CMD_DIR 
					+ System.getProperty("file.separator") + "ttlogholds.sh";
			logger.info("timesten_trans_cmd is " + timesten_trans_cmd);
			Vector trans_v = this.execute(timesten_trans_cmd);
			if (trans_v != null && trans_v.size() > 0) {
				int rowid = 1;
				for (int i = 0; i < trans_v.size(); i++) {
					String res = (String) trans_v.get(i);
					if (res.indexOf("<") == 0) {
						String[] info = res.split(",");
						String index = (String) info[0].substring(1).trim();
						String seq = (String) info[1].trim();
						String tranname = (String) info[2].trim();
						String xid = (String) info[3].substring(0, info[3].length() - 1).trim();
						logger.info("<>" + index + " " + seq + " " + tranname + " " + xid);
						// FM-00-04-003-04 日志序列号
						collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-" + rowid, 
								"FM-00-04-003-04", index);
						// FM-00-04-003-05 事务类型
						collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-" + rowid, 
								"FM-00-04-003-05", tranname);
						// FM-00-04-003-06 事务说明
						collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-" + rowid, 
								"FM-00-04-003-06", xid);
						rowid++;
					}
				}
			}
		}
		return collResult.getKPISet();
	}
	
	/**
	 * standby复制监测
	 */
	public Vector getStandby(HashMap params) {
		initParams(params);
		TTSQLTarget collectiondbbase = new TTSQLTarget();
		collectiondbbase.init(IP_ADDR, PORT, USERNAME, PASSWORD, TTSERVER);
		getStandby(collectiondbbase);
		Vector v = collectiondbbase.getKPISet();
		collectiondbbase.releaseResources();
		return v;
	}
	
	private void getStandby(TTSQLTarget collectiondbbase) {
		String config_PRE_UNITID = this.KBP_ID + "-10";
		Vector v = collectiondbbase.getSQLKPIInfos("{call ttRepStateGet}");
		for (int i = 0; i < v.size(); i++) {
			Vector row = (Vector) v.get(i);
			String repstate = (String) row.get(0);
			logger.info(repstate);
			// 复制状态
			collectiondbbase.addKPI(config_PRE_UNITID, 
					"PM-00-03-900-01", repstate);
		}
	}
	
	/**
	 * 复制异常
	 */
	public Vector getDSLog(HashMap params) {
		logger.info("collect error");
		initParams(params);
		CollBase collResult = new CollBase();
		String config_PRE_UNITID = this.KBP_ID + "-10";
		int logsnum = 0;
		logger.info("IS_LOG_DIR" + this.LOG_DIR + " " + IS_LOG_DIR);
		if (IS_LOG_DIR) {
			String timesten_log_cmd = "ls " + this.LOG_DIR;
			logger.info("timesten_log_cmd: " + timesten_log_cmd);
			Vector linknum_v = this.execute(timesten_log_cmd);
			for (int i = 0; i < linknum_v.size(); i++) {
				String row = (String) linknum_v.get(i);
				logger.info("tempinfo: " + row);
				if (row.indexOf("log") != -1) {
					logsnum++;
				}
			}
//			logsnum = linknum_v.size();
		}
		logger.info("大于2告警: " + logsnum);
		// PM-00-03-900-02 复制异常
		collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total", 
				"PM-00-03-900-02", String.valueOf(logsnum));
		return collResult.getKPISet();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HashMap params = new HashMap();
//		params.put("TTSERVER", "test");
//		params.put("CMD_DIR", "F:\\TimesTen\\tt70_32\\bin");
//		params.put("ID_ADDR", "127.0.0.1");
//		params.put("LOG_DIR", "F:\\TimesTen\\ds");
		params.put("TTSERVER", "abm");
		params.put("CMD_DIR", "/ibnms/masteragent/data");
		params.put("ID_ADDR", "192.168.10.61");
		params.put("LOG_DIR", "/tt/DS");

		CollTimestenWithCMD cmd = new CollTimestenWithCMD();
		cmd.getTransaction(params);
//		cmd.getStandby(params);
//		cmd.getLinkedNum(params);
//		cmd.getLockInfo(params);
//		cmd.getDSLog(params);
	}

}