CollJbossStatusWithHttp.java 5.57 KB
package com.sitech.ismp.coll.centercoll;

import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;

import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;

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

public class CollJbossStatusWithHttp implements CollJbossStatusMBean {
	private Logger logger = Logger.getLogger("COLL");
	private Vector result = new Vector();
	private String KBP_CLASS = "10-12-15";

	public CollJbossStatusWithHttp() {

	}

	public Vector collStatus(HashMap params) {
		result.clear();
		String url = (String) params.get("URL");
		String username = (String) params.get("USERNAME");
		String password = (String) params.get("PASSWORD");
		String hostname = (String) params.get("HOSTNAME");
		if (username == null) {
			username = "";
		}
		if (password == null) {
			password = "";
		}
		String UNIT_ID = "";
		String status_htm = this.request(url, username, password);
		if (status_htm != null && !status_htm.equals("")) {
			int index_jvm = 0;
			index_jvm = status_htm.indexOf("<h1>JVM</h1>");
			if (index_jvm > 1) {
				int index_jvm_end = status_htm.indexOf("</p>", index_jvm);
				String jvm_info = status_htm.substring(index_jvm
						+ "<h1>JVM</h1><p>".length(), index_jvm_end);
				String[] jvm_infos = jvm_info.trim().split(" ");
				try {
					UNIT_ID = this.KBP_CLASS + "-10:" + hostname;
					this.setResult(jvm_infos[2], UNIT_ID, "CM-00-04-003-02");
					this.setResult(jvm_infos[6], UNIT_ID, "CM-00-04-004-01");
					this.setResult(jvm_infos[10], UNIT_ID, "PM-00-04-004-02");

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			int index_http = 0;
			index_http = status_htm.indexOf("<h1>http-");
			if (index_http > 1) {
				int index_http_end = status_htm.indexOf("</h1>", index_http);
				String port = status_htm.substring(index_http
						+ "<h1>http-".length(), index_http_end);
				// System.out.println(port);
				// CM-00-04-003-07 10-12 ����Ķ˿�
				try {
					this.setResult(port, UNIT_ID, "CM-00-04-003-07");
				} catch (Exception e) {
					e.printStackTrace();
				}
				int index_http_info = status_htm.indexOf("<p>", index_http_end);
				int index_http_info_end = status_htm.indexOf("</p>",
						index_http_end);
				String http_info = status_htm.substring(index_http_info
						+ "<p>".length(), index_http_info_end);
				String[] http_infos = http_info.trim().split(" ");
				try {
					UNIT_ID = this.KBP_CLASS + "-11:" + hostname;
					this.setResult(http_infos[2], UNIT_ID, "PM-00-04-004-05");
					this.setResult(http_infos[6], UNIT_ID, "PM-00-04-004-08");
					this.setResult(http_infos[10], UNIT_ID, "PM-00-04-004-09");
					this.setResult(http_infos[14], UNIT_ID, "PM-00-04-004-06");
					this.setResult(http_infos[18].substring(0, http_infos[18]
							.indexOf("<")), UNIT_ID, "PM-00-04-004-07");
					this.setResult(http_infos[22], UNIT_ID, "PM-00-04-800-03");
					this.setResult(http_infos[26], UNIT_ID, "PM-00-04-800-04");
					this.setResult(http_infos[30], UNIT_ID, "PM-00-04-800-05");
					this.setResult(http_infos[33], UNIT_ID, "PM-00-04-800-06");
					this.setResult(http_infos[36], UNIT_ID, "PM-00-04-800-07");
					this.setResult(http_infos[40], UNIT_ID, "PM-00-04-800-08");

				} catch (Exception e) {
					e.printStackTrace();
				}
				try {
					UNIT_ID = this.KBP_CLASS + "-10:" + hostname;
					this.setResult("UP", UNIT_ID, "FM-00-04-001-01");
				} catch (Exception e) {
					e.printStackTrace();
				}

			} else {
				System.out.println("index_http <=0");
			}

		} else {
			try {
				UNIT_ID = this.KBP_CLASS + "-10:" + hostname;
				this.setResult("DOWN", UNIT_ID, "FM-00-04-001-01");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return result;

	}

	/**
	 * request "url" with username and password to get response as String
	 * 
	 * @param url
	 * @param username
	 * @param password
	 * @return String of response
	 */
	public String request(String url, String username, String password) {
		StringBuffer sb = new StringBuffer();
		try {

			HttpMethod method = null;
			HttpClient client = new HttpClient();
			method = new GetMethod(url);
			URL url1 = new URL(url);

			Credentials defaultcreds = new UsernamePasswordCredentials(
					username, password);
			client.getState().setCredentials(
					new AuthScope(url1.getHost(), url1.getPort(),
							AuthScope.ANY_REALM), defaultcreds);

			method.setDoAuthentication(true);
			method.setQueryString(url);
			client.executeMethod(method);
			int status = method.getStatusCode();
			// System.out.println("status="+status);
			/*
			 * Header[] headers = method.getResponseHeaders(); for(int i=0;i<headers.length;i++){
			 * System.out.println(headers[i].getName()
			 * +":"+headers[i].getValue()); }
			 */
			if (status == 200) {
				String s = method.getResponseBodyAsString();
				sb.append(s);
			} else {
				return null;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	private void setResult(String value, String unitID, String kpiID) {
		try {
			TblATO_KPIDETAIL record;
			record = new TblATO_KPIDETAIL();
			record.setCLL_TIME(new java.util.Date());
			record.setKPI_ID(kpiID);
			record.setKPI_VALUE(value);
			record.setUNIT_ID(unitID);
			record.setCLL_TIME(new Date());
			result.add(record);
			System.out.println("add record=" + record.KPI_VALUE);
		} catch (Exception e) {
			logger.error(e);
		}
	}

}