CollBackUp.java 7.82 KB
package com.sitech.ismp.coll;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.Vector;

import org.apache.log4j.Logger;

import com.sitech.ismp.app.coll.RPCTarget;

public class CollBackUp implements CollBackUpMBean {

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

	String UnitID = "10-15";

	RPCTarget rpctarget = null;

	String ipHostName = "127.0.0.1:BACKUP";

	String runDate = "1";

	public void init(HashMap params) {
		rpctarget = new RPCTarget();
		rpctarget.setRpcCmd("lexe_hp");
		ipHostName = (String) params.get("IPHOSTNAME");
		runDate = (String) params.get("RUNDATE");
	}

	public Vector collMonth(HashMap params) {
		init(params);
		if (isSameDay(runDate)) {
			String sinceSession = formatDate(1);
			String untilSession = formatDate();
			Vector sessionID = rpctarget.getKPISet("omnidb -session -since "
					+ sinceSession + " -until " + untilSession
					+ " |grep -v SessionID|grep -v ==|awk '{ print $1}'");

			if (sessionID != null && sessionID.size() > 0) {
				for (int j = 0; j < sessionID.size(); j++) {
					String SessionID = (String) sessionID.elementAt(j);
					Vector backUp = rpctarget.getKPISet("omnidb -rpt "
							+ SessionID + " -detail");
					if (backUp != null && backUp.size() > 0) {
						for (int i = 0; i < backUp.size(); i++) {

						}
					}
				}
			}
		}
		// String SessionID = formatDate();
		// CollBase collResult = new CollBase();
		// Vector backUp = rpctarget.getKPISet("omnidb -session -since "
		// + SessionID + " |grep -v SessionID|grep -v ==");
		// if (backUp != null && backUp.size() > 0) {
		// for (int i = 0; i < backUp.size(); i++) {
		//
		// String omnidb = (String) backUp.elementAt(i);
		// String sessionID = split(omnidb, 0);
		// String status = split(omnidb, 2);
		// collResult.addKPI(UnitID + ":" + ipHostName, "FM-00-06-001-01",
		// status);
		// collResult.addKPI(UnitID + ":" + ipHostName, "FM-00-06-001-02",
		// status);
		// }
		// }
		return null;
	}

	public Vector collDay(HashMap params) {
		init(params);
		CollBase collResult = new CollBase();
		try {
			String backUpStatus = "Completed";
			String backUpResult = "Completed";
			ArrayList backUpTime = new ArrayList();
			ArrayList backUpVelocity = new ArrayList();
			long failsTime = 0;
			long allTime = 0;

			String sinceSession = formatDate(1);
			String untilSession = formatDate();
			Vector sessionID = rpctarget.getKPISet("omnidb -session -since "
					+ sinceSession + " -until " + untilSession
					+ " |grep -v SessionID|grep -v ==|awk '{ print $1}'");

			if (sessionID != null && sessionID.size() > 0) {
				for (int j = 0; j < sessionID.size(); j++) {
					String SessionID = (String) sessionID.elementAt(j);
					Vector backUp = rpctarget.getKPISet("omnidb -rpt "
							+ SessionID + " -detail");
					if (backUp != null && backUp.size() > 0) {
						for (int i = 0; i < backUp.size(); i++) {
							Date start = null;
							Date finish = null;
							long sessionSize = 0;
							String status = "";
							String element = (String) backUp.elementAt(i);
							if (element.indexOf(":") > 0) {
								ArrayList list = split(element, ":");
								String key = ((String) list.get(0)).trim();
								String value = ((String) list.get(1)).trim();
								System.out.println(key + "--" + value);
								if (key.equals("Started")) {
									start = stringToDateTime(value);
								} else if (key.equals("Finished")) {
									finish = stringToDateTime(value);
								} else if (key.equals("Status")) {
									status = value;
									if (value.equals("Failed")) {
										backUpStatus = "Failed";
										backUpResult = "Failed";
									}
									if (value.equals("Completed/Failure")) {
										backUpResult = "Failed";
									}

								} else if (key.equals("Session size")) {
									sessionSize = Long.parseLong(value
											.substring(0, value.length() - 5));
								}
							}
							long bakTime = finish.getTime() - start.getTime();
							if (!status.equals("Completed")) {
								failsTime = failsTime + bakTime;
							}
							allTime = allTime + bakTime;
							backUpTime.add(new Long(bakTime));
							backUpVelocity.add(new Long(sessionSize / bakTime));
						}
					}

				}
			}
			long bakVe = 0;
			for (int i = 0; i < backUpVelocity.size(); i++) {
				bakVe = bakVe + ((Long) backUpVelocity.get(i)).longValue();
			}
			collResult.addKPI(UnitID + ":" + ipHostName, "PM-00-06-800-01",
					(String) Collections.max(backUpTime));
			collResult.addKPI(UnitID + ":" + ipHostName, "PM-00-06-800-02",
					String.valueOf(bakVe / backUpVelocity.size()));
			collResult.addKPI(UnitID + ":" + ipHostName, "PM-00-06-800-03",
					String.valueOf(1 - (failsTime / allTime)));
			collResult.addKPI(UnitID + ":" + ipHostName, "FM-00-06-001-01",
					backUpStatus);
			collResult.addKPI(UnitID + ":" + ipHostName, "FM-00-06-001-02",
					backUpResult);
		} catch (Exception e) {

			logger.debug(e);
		}

		return collResult.getKPISet();
	}

	public static ArrayList split(String str, String sp) {
		int ip = 0;
		ArrayList al = new ArrayList();
		ip = str.indexOf(sp);
		if (ip != -1) {
			al.add(str.substring(0, ip));
			str = str.substring(ip + sp.length());
			if (str.length() != 0) {
				al.addAll(split(str, sp));
			}
		} else {
			al.add(str);
		}
		return al;
	}

	public String formatDate1(int dayOfMonth) {

		SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd");
		GregorianCalendar cal = new GregorianCalendar();
		cal.add(Calendar.DAY_OF_MONTH, dayOfMonth);
		Date d = cal.getTime();

		String s = fmt.format(d);
		return s;
	}

	public String formatDate(int dayOfMonth) {

		SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd");
		GregorianCalendar cal = new GregorianCalendar();
		cal.add(Calendar.DAY_OF_MONTH, dayOfMonth);
		Date d = cal.getTime();

		String s = fmt.format(d);
		return s;
	}

	public String formatDate() {

		SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd");
		GregorianCalendar cal = new GregorianCalendar();
		Date d = cal.getTime();
		String s = fmt.format(d);
		return s;
	}

	public final static Date stringToDateTime(String cDate) {
		try {
			SimpleDateFormat formatter1 = new SimpleDateFormat(
					"EEE MMM dd HH:mm:ss yyyy", Locale.ENGLISH);
			return formatter1.parse(cDate);
		} catch (ParseException e) {
			return null;
		}
	}

	public final static String stringToDateTime(Date cDate) {
		try {
			SimpleDateFormat formatter1 = new SimpleDateFormat(
					"EEE MMM dd HH:mm:ss yyyy", Locale.ENGLISH);
			return formatter1.format(cDate);
		} catch (Exception e) {
			return null;
		}
	}

	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 boolean isSameDay(String date) {

		String DATE_FORMAT = "d";
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				DATE_FORMAT);
		String thisDate = sdf.format(new Date());
		System.out.println(thisDate);
		return thisDate.equals(date);

	}

	public static void main(String[] args) {
		/*
		 * System.out.println("main!"); CollBackUpMBean cbu = new CollBackUp();
		 * HashMap params = new HashMap(); Vector a = cbu.coll(params); for (int
		 * i = 0; i < a.size(); i++) { TblATO_KPIDETAIL record =
		 * (TblATO_KPIDETAIL) a.get(i); System.out.println(record.KPI_VALUE + " " +
		 * record.UNIT_ID + " " + record.KPI_ID); }
		 */
		// System.out.println(CollBackUp
		// .stringToDateTime("Wed May 24 23:34:12 2006"));
		// System.out.println(CollBackUp.stringToDateTime(new Date()));
		//	
		CollBackUp c = new CollBackUp();
		System.out.println(c.isSameDay("07"));

	}

}