Ping.java 4.8 KB
package com.sitech.ismp.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sitech.util.Formater;

/**
 * Created by IntelliJ IDEA. User: LJ Date: 2010-2-23
 */
public class Ping {
	private static Logger logger = Logger.getLogger(Ping.class);

	public static Map<String, String> ping(String hostName) throws Exception {
		logger.info("-- Begin ping " + hostName);

		String strResult = execute(hostName);
		logger.info("-- Ping " + hostName + ", Result=" + strResult);

		String strOS = System.getProperty("os.name").toLowerCase();

		logger.info("-- OS = " + strOS);

		Map<String, String> result = null;
		if (strOS.indexOf("windows") != -1) {
			result = getWindowsData(strResult);
		} else {
			result = getHPUnixData(strResult);
		}
		return result;
	}

	public static String execute(String hostName) throws Exception {
		String s = "";
		String s1 = "";
		String s2 = "";
		String s3 = (new PingTraceUtil()).getResult(hostName,
				PingTraceUtil.Ping);
		String s4 = System.getProperty("os.name").toLowerCase();

		if (s4.indexOf("windows") == -1) {
			int i = s3.indexOf("---");
			if (i != -1) {
				s1 = s3.substring(i);
				s = s3.substring(0, i);
			}
			i = s1.lastIndexOf("---");
			if (i != -1) {
				s2 = s1.substring(i + 3);
				s1 = s1.substring(0, i + 3);
			}
			s3 = s + "<br/><br/>" + s1 + "<br/><br/>" + s2;
		}
		return s3;
	}

	private static Map<String, String> getWindowsData(String result) {
		Map<String, String> hash = new HashMap<String, String>();
		String STATE = "";
		String MINRTT = "";
		String AVGRTT = "";
		String MAXRTT = "";
		try {
			if (result != null && result.indexOf("100% loss") != -1) {
				STATE = "DOWN";
				MINRTT = "-1";
				AVGRTT = "-1";
				MAXRTT = "-1";
			} else {
				STATE = "UP";
				ByteArrayInputStream stream = new ByteArrayInputStream(result
						.getBytes());
				BufferedReader in = new BufferedReader(new InputStreamReader(
						stream));
				String line = "";
				while ((line = in.readLine()) != null) {
					if (line != null
							&& line.toLowerCase().indexOf("average") > 0) {
						String rttArray[] = line.toLowerCase().split(",");
						MINRTT = rttArray[0].substring(
								rttArray[0].indexOf("=") + 1, rttArray[0]
										.indexOf("ms"));
						MAXRTT = rttArray[1].substring(
								rttArray[1].indexOf("=") + 1, rttArray[1]
										.indexOf("ms"));
						AVGRTT = rttArray[2].substring(
								rttArray[2].indexOf("=") + 1, rttArray[2]
										.indexOf("ms"));
						break;
					}
				}
			}
			logger.info("Ping getWindowsData:STATE,MINRTT,AVGRTT,MAXRTT:"
					+ STATE + "," + MINRTT.trim() + "," + AVGRTT.trim() + ","
					+ MAXRTT.trim() + "," + Formater.getNowTime());

			hash.put("STATE", STATE);
			hash.put("MINRTT", MINRTT.trim());
			hash.put("AVGRTT", AVGRTT.trim());
			hash.put("MAXRTT", MAXRTT.trim());
		} catch (Exception e) {
			logger.error("Exception while getWindowsData:" + result, e);
		}
		return hash;
	}

	/**
	 * PING 172.21.1.71 (172.21.1.71) 56(84) bytes of data. 64 bytes from
	 * 172.21.1.71: icmp_seq=1 ttl=255 time=2.42 ms
	 * 
	 * <br/><br/>--- 172.21.1.71 ping statistics ---<br/><br/> 1 packets
	 * transmitted, 1 received, 0% packet loss, time 2ms rtt min/avg/max/mdev =
	 * 2.426/2.426/2.426/0.000 ms
	 * 
	 * @param strResult
	 * @return
	 */
	private static Map<String, String> getHPUnixData(String result) {
		Map<String, String> hash = new HashMap<String, String>();
		String STATE = "";
		String MINRTT = "";
		String AVGRTT = "";
		String MAXRTT = "";
		try {
			String[] line = result.split("\n");
			for (int i = 0; i < line.length; i++) {
				logger.info("-- line" + i + ":" + line[i]);

				if (line[i] != null
						&& line[i].indexOf("100% packet loss") != -1) {
					STATE = "DOWN";
					MINRTT = "-1";
					AVGRTT = "-1";
					MAXRTT = "-1";
					break;
				} else if (line[i] != null && line[i].indexOf("avg") != -1) {
					String total = line[i].substring(line[i].indexOf("=") + 1)
							.trim();
					String arr[] = total.split("/");
					MINRTT = arr[0];
					AVGRTT = arr[1];
					MAXRTT = arr[2];
					STATE = "UP";
					break;
				}
			}

			logger.info("Ping result:STATE,MINRTT,AVGRTT,MAXRTT:" + STATE + ","
					+ MINRTT.trim() + "," + AVGRTT.trim() + "," + MAXRTT.trim()
					+ "," + Formater.getNowTime());

			hash.put("STATE", STATE);
			hash.put("MINRTT", MINRTT.trim());
			hash.put("AVGRTT", AVGRTT.trim());
			hash.put("MAXRTT", MAXRTT.trim());

		} catch (Exception e) {
			logger.error("Exception while getHPUnixData:" + result, e);
		}
		return hash;
	}

	public static void main(String[] args) {
		String ip = args[0];

		try {
			Map<String, String> map = Ping.ping(ip);
			System.out.println("Ping " + ip + ":\t" + map.get("STATE"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}