CollHost.java 11.7 KB
package com.sitech.ismp.coll.tivoli.linux;

import java.util.*;

import com.sitech.ismp.coll.CollBase;
import com.sitech.util.JSONUtil;
import org.apache.log4j.Logger;

import com.sitech.util.Formater;
 
/**
 * 该类只采集 规范中的 ibm提供采集方法的 主机相关指标
 * 
 * @author winnerbao
 * 
 */
public class CollHost {

	private boolean debug = false;

	private SoapClient soapClient = null;

	private String pre_unitid = "";

	private Logger logger = Logger.getLogger("COLL");

	/**
	 * 
	 * @param linux采集
	 * */
	public CollHost(String pre_unit) {
		this.pre_unitid = pre_unit;

		// 
		String url = SoapParam.getString("SOAP_URL");
		soapClient = new SoapClient(url);
	}
     
	//cpu  及内存使用率指标采集
	public Vector getPerformance_Ten_Minute(java.util.HashMap params)
			throws Exception {
		String host_name = (String) params.get("HOST_NAME");
		String neat_host_name = Formater.neatenunitid(host_name);
		String soap_host_name = SOAPITCMNameHelper.getSoapHostName(host_name);
		com.sitech.ismp.coll.CollBase collResult = new com.sitech.ismp.coll.CollBase();
		String Used_Real_Mem_Pct = "";
		String Swap_Used_Pct="";
		try {
			// 采集PM-00-01-01-01/02/03/04/05
			// object=SMP_CPU
			System.out.println(new Date());
			String soap_cpu_SMPCPU = CollHostSoapHelper
					.getSoap_cpu_SMPCPU(soap_host_name); // soap消息
			String soap_cpu_SMPCPU_res = soapClient
					.getSoapStringResponse(soap_cpu_SMPCPU);
			System.out.println(new Date());
			if (debug)
				System.out.println(new Date() + soap_cpu_SMPCPU_res);
			if (soap_cpu_SMPCPU_res.indexOf("faultstring") != -1) {
				collResult.addKPI("10-10-991:" + neat_host_name + "-kux",
						"FM-10-10-991-01", "DOWN");
			} else {
				collResult.addKPI("10-10-991:" + neat_host_name + "-kux",
						"FM-10-10-991-01", "UP");
			}

			List cpu_results = CollHostXmlHelper
					.parseSoapResponse(soap_cpu_SMPCPU_res);

			Map cpu_aggregate = null;
			// 得到 CPU_ID=aggregate 的那个 aggregate
			for (int i = 0; i < cpu_results.size(); i++) {
				Map cpu = (Map) cpu_results.get(i);
				String cpu_id = (String) cpu.get("CPU_ID");
				if (cpu_id.equals("Aggregate")) {
					cpu_aggregate = cpu;
					break;
				}
			}
			if (cpu_aggregate == null) {
				System.out
						.println("@tivoli , no cpu info got by tivoli in collCpu "
								+ new Date());
			} else {
				collResult.addKPI(
						pre_unitid + "-11:" + neat_host_name + "-cpu",
						"PM-00-01-001-05", (String) cpu_aggregate
								.get("Busy_CPU"));
			}

		} catch (Exception e) {
			logger.error("collhost cpu error");
			e.printStackTrace();
		}
		
		try {
			String soap_filesystem_System = CollHostSoapHelper
					.getSoap_Unix_Memory(soap_host_name); // soap消息
			String soap_filesystem_System_res = soapClient
					.getSoapStringResponse(soap_filesystem_System);
			List system_results = CollHostXmlHelper
					.parseSoapResponse(soap_filesystem_System_res);
			if (system_results == null || system_results.size() < 1) {
				System.out
						.println("@tivoli , no Unix_Memory info got by tivoli in collFileSystem"
								+ new Date());

			} else {

				// 该结果中,肯定只有一条记录,这是由tivoli机制所保证的
				Map system_map = (Map) system_results.get(0);
			
				Used_Real_Mem_Pct = (String) system_map
						.get("Memory_Used_Pct");
				Swap_Used_Pct = (String) system_map
				.get("Swap_Used_Pct");
				
				collResult.addKPI(pre_unitid + "-12:" + neat_host_name
						+ "-memory", "PM-00-01-002-01", getByScale(String
						.valueOf(Used_Real_Mem_Pct)));
				collResult.addKPI(pre_unitid + "-10:" + neat_host_name
						+ "-total", "PM-00-01-004-02", Formater
						.formatDecimalKpivalue(String
								.valueOf(Swap_Used_Pct)));

			}

		} catch (Exception e) {
			logger.error("collhost memory error");
			e.printStackTrace();
		}

		return collResult.getKPISet();
	}

    /**
     * 获得用户关注的文件系统,需要对这些文件系统的挂载状态监控
     * @param params
     * @return
     */
    private Vector collFsMountState(HashMap params, Map<String, String> fsUnitIdMap) {
        CollBase kpis = new CollBase();

        try{
            if (null == params.get("FS_FILTER")) {
                return kpis.getKPISet();
            }

            List<String> fsConfList = (List<String>) JSONUtil.fromJSON((String) params.get("FS_FILTER"));

            if (null != fsConfList && !fsConfList.isEmpty()) {
                for (String fsUnitId : fsConfList) {
                    if (fsUnitIdMap.containsKey(fsUnitId)) {
                        kpis.addKPI(fsUnitId, "FM-00-01-004-01",
                                "OK");
                    } else {
                        kpis.addKPI(fsUnitId, "FM-00-01-004-01",
                                "unmount");
                    }
                }
            }
        }catch (Exception e){
            logger.error("Exception while collFsMountState", e);
        }

        return  kpis.getKPISet();
    }

	public Vector getPerformance_Fifteen_Minute(
			java.util.HashMap params) throws Exception {
		String host_name = (String) params.get("HOST_NAME");
		String ua_host_name = UaHostHelper.getUaHostName(host_name);
		String neat_host_name = Formater.neatenunitid(host_name);
		String soap_host_name = SOAPITCMNameHelper.getSoapHostName(host_name);
		com.sitech.ismp.coll.CollBase collResult = new com.sitech.ismp.coll.CollBase();

		try {
			// 得到soap请求
			String soap_filesystem_Disk = CollHostSoapHelper
					.getSoap_filesystem_Disk(soap_host_name); // soap消息
			// 发送soap请求,并返回结果
			String soap_filesystem_Disk_res = soapClient
					.getSoapStringResponse(soap_filesystem_Disk);
			// 分析返回结果,得到list
			List disk_results = CollHostXmlHelper
					.parseSoapResponse(soap_filesystem_Disk_res);

			// 获取每个文件系统的kpi值,顺便计算总的文件系统大小和总的已使用文件系统大小
			long total_filesystem = 0;
			long used_filesystem = 0;
			if (disk_results == null || disk_results.size() < 1) {
				System.out
						.println("@tivoli , no System info got by tivoli in disk : "
								+ new Date());
				if (soap_filesystem_Disk_res.indexOf("faultstring") == -1) {
					collResult.addKPI("10-10-991:" + neat_host_name + "-Disk",
							"FM-10-10-03", "0");
				}
			} else {
				collResult.addKPI("10-10-991:" + neat_host_name + "-Disk",
						"FM-10-10-03", "1");

                // 保存本次采集到的文件系统UNIT_ID
                Map<String, String> fsUnitIdMap = new HashMap<String, String>();

				for (int i = 0; i < disk_results.size(); i++) {
					Map diskMap = (Map) disk_results.get(i);
					String Mount_Point = (String) diskMap.get("Mount_Point");
					String Space_Used_Percent = (String) diskMap
							.get("Disk_Used_Percent");
					

					if (Mount_Point == null || "".equals(Mount_Point.trim())) {
						continue;
					}
					String neatMount_Point = Formater.neatenunitid(Mount_Point);

                    String unitId = pre_unitid + "-14:" + neat_host_name
                            + "-" + neatMount_Point;
					collResult.addKPI(unitId, "PM-00-01-004-03",
							Space_Used_Percent);

                    fsUnitIdMap.put(unitId, unitId);
					
				}

                // 采集文件系统挂载状态
                collResult.addKPISet(collFsMountState(params, fsUnitIdMap));
			}

		} catch (Exception e) {
			logger.error("collhost total error == " + host_name, e);
			e.printStackTrace();
		}

				

		return collResult.getKPISet();
	}

	public Vector getFailure(java.util.HashMap params)
			throws Exception {
		String host_name = (String) params.get("HOST_NAME");
		String ha_host_name = HaHostHelper.getHaHostName(host_name);
		String neat_host_name = Formater.neatenunitid(host_name);
		com.sitech.ismp.coll.CollBase collResult = new com.sitech.ismp.coll.CollBase();
		int count1 = 0;
		int count2 = 0;

		String status3 = "DOWN";
		try {

			String bnms1_status = CollHostSoapHelper
					.getSoap_PINGSERVERSPINGSTATS00_From_bnms1(ha_host_name);
			String bnms2_status = CollHostSoapHelper
					.getSoap_PINGSERVERSPINGSTATS00_From_bnms2(ha_host_name);
			String bnms3_status = CollHostSoapHelper
					.getSoap_PINGSERVERSPINGSTATS00_From_bnms3(ha_host_name);
			System.out.println(new Date());
			String bnms1_status_res = soapClient
					.getSoapStringResponse(bnms1_status);
			String bnms2_status_res = soapClient
					.getSoapStringResponse(bnms2_status);
			String bnms3_status_res = soapClient
					.getSoapStringResponse(bnms3_status);
			List bnms1_status_result31 = CollHostXmlHelper
					.parseSoapResponse(bnms1_status_res);
			List bnms2_status_result32 = CollHostXmlHelper
					.parseSoapResponse(bnms2_status_res);
			List bnms3_status_result33 = CollHostXmlHelper
					.parseSoapResponse(bnms3_status_res);
			System.out.println(new Date());
			if (bnms1_status_result31 == null
					|| bnms1_status_result31.size() == 0) {
				collResult.addKPI("10-10-991:bnas1_" + host_name + "-total",
						"FM-10-10-991-02", "bnas1 PING " + host_name
								+ " NULL RESPONSE");
				count1++;
			} else {
				Map statusMap = (Map) bnms1_status_result31.get(0);
				status3 = ((String) statusMap.get("Status")).toUpperCase();
				if (status3.equalsIgnoreCase("DOWN")) {
					count2++;
				}

			}

			if (bnms2_status_result32 == null
					|| bnms2_status_result32.size() == 0) {
				collResult.addKPI("10-10-991:bnas2_" + host_name + "-total",
						"FM-10-10-991-02", "bnas2 PING " + host_name
								+ " NULL RESPONSE");
				count1++;

			} else {
				Map statusMap = (Map) bnms2_status_result32.get(0);
				status3 = ((String) statusMap.get("Status")).toUpperCase();
				if (status3.equalsIgnoreCase("DOWN")) {
					count2++;
				}
			}

			if (bnms3_status_result33 == null
					|| bnms3_status_result33.size() == 0) {
				collResult.addKPI("10-10-991:bnas3_" + host_name + "-total",
						"FM-10-10-991-02", "bnas3 PING " + host_name
								+ " NULL RESPONSE");
				count1++;
			} else {
				Map statusMap = (Map) bnms3_status_result33.get(0);
				status3 = ((String) statusMap.get("Status")).toUpperCase();
				if (status3.equalsIgnoreCase("DOWN")) {
					count2++;
				}
			}
			System.out.println(count1);
			if (count1 < 3) {
				if (count2 == (3 - count1)) {
					// down
					collResult.addKPI(pre_unitid + "-10:" + neat_host_name
							+ "-total", "FM-00-01-001-01", "DOWN");
					System.out.println("down1");
				} else {
					// up
					collResult.addKPI(pre_unitid + "-10:" + neat_host_name
							+ "-total", "FM-00-01-001-01", "UP");
					System.out.println("up1");
				}
			} else {
				System.out.println("up2");
			}

		} catch (Exception e) {
			logger.error("collhost getFailure error");
			e.printStackTrace();
		}
		
		return collResult.getKPISet();
	}

	/**
	 * 
	 * 得到指定精度的double类型数值 精度设定为4
	 * 
	 * @param double_vlaue
	 * @return
	 */
	private String getByScale(String double_value) {
		int scale = 4;
		return Formater.getByScale(double_value, 2);
	}

	public static void main(String[] args) throws Exception {
		CollHost aix = new CollHost("10-10-20");
		java.util.HashMap params = new java.util.HashMap();
		params.put("HOST_NAME", "linux1");

		Vector v = new Vector();
		String aa = "aaaaaasoap_cpu_SMPCPU_resaaa";
		aix.getFailure(params);
		System.out.println(aa.indexOf("wq"));
		// v.addAll(aix.getConfiguration_One_Day(params));
		//v.addAll(aix.getPerformance_Ten_Minute(params));
		//v.addAll(aix.getPerformance_Ten_Minute(params));
		// v.addAll(aix.getPerformance_Fifteen_Minute(params));
		// v.addAll(aix.getPerformance_One_Hour(params));
		// v.addAll(aix.getPerformance_Ten_Minute(params));
		// String temp[][] = CheckKPIHelper.Check_AIX_HOST_KPI(v);
		// for(int i=0;i<temp.length;i++)
		// {
		// System.out.println("KPI_ID : " + temp[i][0] + "---" + temp[i][1]);
		//}
		//String filename = "test1.xls";
		//CheckKPIHelper.Write_KPIValue2File(v, filename);
	}

}