CollUnionWorker_NEW.java 4.81 KB
/**
 * 功能:并发监控agent所在主机的主机状态,实现ping并生成指标文件
 * 作者:毛志荣
 * 日期:2008-5-2 12:34
 * 版本:v1.00
 */
package com.sitech.ismp.coll;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;

public class CollUnionWorker_NEW implements Runnable {
	private String host;

	private String unit_id;

	private String flag;

	private int count = 0;

	public CollUnionWorker_NEW(String host, String unit_id, String flag) {
		this.host = host;
		this.unit_id = unit_id;
		this.flag = flag;
	}

	/** * -- 构造ping命令 --- * */
	public synchronized void run() {
		String cmd = "";
		System.out.println();
		if (CollUnion_NEW.host_type.equalsIgnoreCase("hp")) {
			cmd = "ping " + this.host + " -n " + CollUnion_NEW.times;
		}
		if (CollUnion_NEW.host_type.equalsIgnoreCase("aix")) {
			cmd = "ping -c " + CollUnion_NEW.times + " " + this.host;
		}
		if (CollUnion_NEW.host_type.equalsIgnoreCase("sun")) {
			cmd = "ping  -s   " + this.host + " " + CollUnion_NEW.times + " "
					+ CollUnion_NEW.count;
		}
		if (CollUnion_NEW.host_type.equalsIgnoreCase("linux")) {
			cmd = "ping -c " + CollUnion_NEW.times + " " + this.host;
		}
		if (CollUnion_NEW.host_type.equalsIgnoreCase("windows")) {
			cmd = "ping  -n  " + CollUnion_NEW.times + " " + this.host;
		}
		if (CollUnion_NEW.host_type.equalsIgnoreCase("sco")) {

		}
		System.out
				.println("[begin moni " + new Date() + " " + this.host + "] ");
		String loss = ping(cmd);
		if (flag.equals("0") && loss.equals("100")) {
			loss = "UP";
		} else if (flag.equals("1") && loss.equals("100")) {

			// Thread.sleep(5000L);
			// String lossother = ping(cmd);
			if (loss.equals("100")) {
				loss = "DOWN";
			} else {
				loss = "UP";
			}

		} else if (flag.equals("1") && loss.equals("0")) {
			System.out.print(loss.equals("falg=1的主机可以PING0"));
			loss = "UP";
		} else if (flag.equals("0") && loss.equals("0")) {
			System.out.print(loss.equals("falg=1的主机可以PING0"));
			loss = "DOWN";
		}
		System.out.println("[end " + this.host + new Date() + " ,restult="
				+ loss + "]");
		create_kpi_files(this.unit_id, loss, this.host);

	}

	/**
	 * 执行ping命令
	 * 
	 * @param cmd
	 * @return
	 */
	private synchronized String ping(String cmd) {
		BufferedReader br = null;
		String loss = "";
		try {
			System.out.println(cmd);
			Process p = Runtime.getRuntime().exec(cmd);

			br = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String line = br.readLine();
			System.out.println("读出数据data" + line);
			while ((line = br.readLine()) != null) {

				if (line.indexOf("%") != -1) { // 根据不同主机类型取出不同丢包率
					System.out.println(line);
					if (CollUnion_NEW.host_type.equalsIgnoreCase("hp")) {
						loss = line.substring(line.indexOf("received") + 9,
								line.indexOf("%")).trim();
					}
					if (CollUnion_NEW.host_type.equalsIgnoreCase("sun")) {
						loss = line.substring(line.indexOf("received") + 9,
								line.indexOf("%")).trim();
						System.out.println("ping sun=" + loss);
					}
					if (CollUnion_NEW.host_type.equalsIgnoreCase("linux")) {
						loss = line.substring(line.indexOf("received") + 9,
								line.indexOf("%")).trim();
					}
					if (CollUnion_NEW.host_type.equalsIgnoreCase("windows")) {
						loss = line.substring(line.indexOf("(") + 1,
								line.indexOf("%")).trim();
					}
					if (CollUnion_NEW.host_type.equalsIgnoreCase("aix")) {
						loss = line.substring(line.indexOf("received") + 9,
								line.indexOf("%")).trim();
						System.out.println("ping aix=" + loss);
					}
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("error:" + e.toString());
		} finally {
			try {
				br.close();
				Runtime.getRuntime().gc();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		// if (!loss.equals("100")) {
		// return loss;
		// }
		//
		// while (this.count < 2) {
		// try {
		// Thread.sleep(1000 * 3); // 停留5秒,再继续
		// } catch (InterruptedException e) {
		// e.printStackTrace();
		// }
		// this.count++;
		// System.out.println(" Again " + this.count + " " + this.host
		// + " ping]");
		// ping(cmd);
		// }
		return loss;
	}

	/**
	 * 生成指标文件
	 */
	private synchronized boolean create_kpi_files(String unit_id, String value,
			String host_ip) {
		String cmd = "";
		String kpi_cmd = "";
		cmd = CollUnion_NEW.swap_up_cmd;
		kpi_cmd = cmd + " " + unit_id + " " + CollUnion_NEW.kpi_id + " "
				+ value;
		System.out.println("--" + kpi_cmd);
		try {
			Runtime.getRuntime().exec(kpi_cmd);
		} catch (Exception e) {
			System.out.println("error:" + e.toString());
			return false;
		} finally {
			try {
				Runtime.getRuntime().gc();
				System.gc();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		return true;
	}
}