CollStorageWithCMD.java 5.08 KB
package com.sitech.ismp.coll;

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

import org.apache.log4j.Logger;

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

public class CollStorageWithCMD {
	/**
	 * 				case 1:
					this.os_type = "AIX";
					this.kbp_class = "10-14-12";
					case 2:
					this.os_type ="HP-UX";
					this.kbp_class = "10-14-11";
					case 3:
					this.os_type = "Linux";
					this.kbp_class = "10-14-13";
				
	 */
	
	private Logger logger = Logger.getLogger("COLL");
	public String kbp_class="";
	public String os_type ="";
	public String host_name="";
	
	
	public Vector collDiskStatus(HashMap params){
		CollBase collResult = new CollBase();
		//初始化参数
		initkbpclass();
		String net_host_name = Formater.neatenunitid(host_name);
		if(this.os_type.equals("AIX")){
			String cmd = "/usr/sbin/lsdev -Cc disk";
			Vector result = this.execute(cmd);
			if(result!=null&&result.size()>0){
				for(int i=0;i<result.size();i++){
					String disk_info = (String)result.get(i);
					String diskname = split(disk_info,0);
					String diskstatus = split(disk_info,1);
					String unit_id = this.kbp_class+"-10:"+net_host_name+"-"+diskname;
					//CM-00-05-002-04磁盘标识
					//PM-00-05-800-02磁盘状态 
					//PM-00-05-800-03采集时间   
					collResult.addKPI(unit_id,
							"CM-00-05-002-04", diskname);
					collResult.addKPI(unit_id,
							"PM-00-05-800-02", diskstatus);
					collResult.addKPI(unit_id,
							"PM-00-05-800-03", Formater.getNowTime());
				}
			}
		}else if(this.os_type.equals("HP-UX")){
		   String cmd =	"/sbin/ioscan -fknC disk";
		   Vector result = this.execute(cmd);
			if(result!=null&&result.size()>0){
				for(int i=0;i<result.size();i++){
					String disk_info = (String)result.get(i);
					if(disk_info!=null&&disk_info.indexOf("disk")==0){
						String diskname = split(disk_info,1);
						String diskstatus = split(disk_info,4);
						String unit_id = this.kbp_class+"-10:"+net_host_name+"-disk"+diskname;
						//CM-00-05-002-04磁盘标识
						//PM-00-05-800-02磁盘状态 
						//PM-00-05-800-03采集时间   
						collResult.addKPI(unit_id,
								"CM-00-05-002-04", "disk"+diskname);
						collResult.addKPI(unit_id,
								"PM-00-05-800-02", diskstatus);
						collResult.addKPI(unit_id,
								"PM-00-05-800-03", Formater.getNowTime());
					}
				}
			}
		}else if(this.os_type.equals("Linux")){
			
		}
		return collResult.getKPISet();
	}
	
	private void initkbpclass(){
		this.getHostOS_TYPE();
		this.getHostName();
	}
	private void getHostOS_TYPE(){
		try{
			String cmd = "uname -a";
			Vector result = this.execute(cmd);
			if(result!=null&&result.size()>0){
				String s=(String) result.get(0);
				//s = s.toLowerCase();
				if(s.indexOf("AIX")>=0){
					this.os_type = "AIX";
					this.kbp_class = "10-14-12";
				}else if(s.indexOf("HP-UX")>=0){
					this.os_type ="HP-UX";
					this.kbp_class = "10-14-11";
				}else if(s.indexOf("Linux")>=0){
					this.os_type = "Linux";
					this.kbp_class = "10-14-13";
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e);
		}
	}
	private void getHostName(){
		try{
			String cmd = "hostname";
			Vector result = this.execute(cmd);
			if(result!=null&&result.size()>0){
				String s=(String) result.get(0);
				this.host_name = s.trim();
			}
		}catch(Exception e){
			e.printStackTrace();
			logger.error(e);
		}
	}
	
	
//	执行命令开始
	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();
			while (line != null) {
				if (shellCommand.indexOf("cqitsm") > 0) {
					logger.info("line: " + line);
				}
				vResult.addElement(line);
				line = reader.readLine();
			}
			reader.close();
			return vResult;
		} catch (Exception e) {
			logger.error("在本机上可能没有 " + shellCommand + " 命令!");
		} finally {
			Runtime.getRuntime().freeMemory();
			Runtime.getRuntime().gc();
		}
		return vResult;
	}
	
	public String split(String str, int pos) {
		StringTokenizer st = new StringTokenizer(str);
		Vector values = new Vector();
		while (st.hasMoreTokens()) {
			values.add(st.nextToken());
		}
		if (pos >= 0 && pos < values.size()) {
			return (String) values.elementAt(pos);
		}
		return "";
	}
	
	public static void main(String[] arg){
		HashMap params = new HashMap();
		CollStorageWithCMD coll = new CollStorageWithCMD();
		coll.collDiskStatus(params);
		Vector lst = null;
		try {
			lst = coll.collDiskStatus(params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("lst.size() is " + lst.size());
		for (int i = 0; i < lst.size(); i++) {
			TblATO_KPIDETAIL kpidetail = (TblATO_KPIDETAIL) lst.get(i);
			System.out.println(kpidetail.UNIT_ID + "|" + kpidetail.KPI_ID + "|"
					+ kpidetail.CLL_TIME + "|" + kpidetail.KPI_VALUE);
		}
	}
}