CollMySQL.java 6.21 KB
package com.sitech.ismp.coll;

import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.DES3;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Scanner;
import java.util.Vector;

/**
 * Created with IntelliJ IDEA.
 * User: mooker
 * Date: 14-3-20
 * Time: 下午1:27
 * To change this template use File | Settings | File Templates.
 */
public class CollMySQL implements CollMySQLMBean {
    private Logger logger = Logger.getLogger(CollMySQL.class);

    @Override
    public Vector<TblATO_KPIDETAIL> getSystem(HashMap<String, String> params) {
        try {
            CollMySQLBySql collector = new CollMySQLBySql();
            return collector.getSystem(params);
        } catch (Exception e) {
            logger.error("Exception while getSystem", e);
            return new Vector<TblATO_KPIDETAIL>();
        }
    }

    /**
     * 获取数据库jdbc的连接状态。
     * @param params
     * @return
     */
    @Override
    public Vector<TblATO_KPIDETAIL> getStatus(HashMap<String, String> params) {
        try {
            CollMySQLBySql collector = new CollMySQLBySql();
            return collector.getStatus(params);
        } catch (Exception e) {
            logger.error("Exception while getStatus", e);
            return new Vector<TblATO_KPIDETAIL>();
        }
    }

    /**
     * 获取数据库ping状态。
     * @param params
     * @return
     */
    @Override
    public Vector<TblATO_KPIDETAIL> getPingStatus(HashMap<String, String> params) {
        try {
            CollMySQLBySql collector = new CollMySQLBySql();
            return collector.getPingStatus(params);
        } catch (Exception e) {
            logger.error("Exception while getPingStatus", e);
            return new Vector<TblATO_KPIDETAIL>();
        }
    }

    @Override
    public Vector<TblATO_KPIDETAIL> getPerformance(HashMap<String, String> params) {
        try {
            CollMySQLBySql collector = new CollMySQLBySql();
            return collector.getPerformance(params);
        } catch (Exception e) {
            logger.error("Exception while getPerformance", e);
            return new Vector<TblATO_KPIDETAIL>();
        }
    }

    @Override
    public Vector<TblATO_KPIDETAIL> getInnodb(HashMap<String, String> params) {
        try {
            CollMySQLBySql collector = new CollMySQLBySql();
            return collector.getInnodb(params);
        } catch (Exception e) {
            logger.error("Exception while getInnodb", e);
            return new Vector<TblATO_KPIDETAIL>();
        }
    }

    public Vector<TblATO_KPIDETAIL> getSlave(HashMap<String, String> params) {
        try {
            CollMySQLBySql collector = new CollMySQLBySql();
            return collector.getSlave(params);
        } catch (Exception e) {
            logger.error("Exception while getSlave", e);
            return new Vector<TblATO_KPIDETAIL>();
        }
    }

    public Vector<TblATO_KPIDETAIL> getTableSpaceIO(HashMap<String, String> params) {
        try {
            CollMySQLBySql collector = new CollMySQLBySql();
            return collector.getTableSpaceIO(params);
        } catch (Exception e) {
            logger.error("Exception while getTableSpaceIO", e);
            return new Vector<TblATO_KPIDETAIL>();
        }
    }

    private static String read(String prompt) {
        Scanner scanner = new Scanner(System.in);
        System.out.print(prompt);
        return scanner.nextLine();
    }

    public static void main(String[] args) {
        System.out.println("***************MySQL Collect Test Begin*********************");
//        String db_url = read("DB_URL(jdbc:main://172.21.0.67:3306):");
//        String username = read("USERNAME:");
//        String password = read("PASSWORD:");
//        String deviceId = read("DEVICE_ID:");
//        String methodStr = read("METHOD(0:getAll, 1:getSystem, 2:getStatus, 3:getInnodb, 4:getSlaveStatus):");
        String db_url = "jdbc:mysql://172.21.0.67:3306/bnms";
        String username = "bnms";
        String password = "bnms";
        String deviceId = "bnms";
        String methodStr = "5";

        HashMap params = new HashMap();
        params.put("DB_URL", db_url);
        params.put("USERNAME", username);
        params.put("PASSWORD", DES3.encrypt(password));
        params.put("DEVICE_ID", deviceId);

        CollMySQL coll = new CollMySQL();
        Vector lst = new Vector<TblATO_KPIDETAIL>();

        int method = Integer.parseInt(methodStr);
        switch (method) {
            case 0:
                lst.addAll(coll.getSystem(params));
                lst.addAll(coll.getStatus(params));
                lst.addAll(coll.getInnodb(params));
                lst.addAll(coll.getSlave(params));
                lst.addAll(coll.getTableSpaceIO(params));
                break;
            case 1:
                lst.addAll(coll.getSystem(params));
                break;
            case 2:
                lst.addAll(coll.getStatus(params));
                break;
            case 3:
                lst.addAll(coll.getInnodb(params));
                break;
            case 4:
                lst.addAll(coll.getSlave(params));
                break;
            case 5:
                lst.addAll(coll.getTableSpaceIO(params));
                break;
            default:
                break;
        }
        System.out.println("***************MySQL Collect Test End*********************");

        System.out.println("lst.size() is " + lst.size());
        for (int i = 0; i < lst.size(); i++) {
            TblATO_KPIDETAIL record = (TblATO_KPIDETAIL) lst.get(i);
            System.out.println(record.UNIT_ID + "\t" + record.KPI_ID + "\t" + record.KPI_VALUE);
//            String insSql = "INSERT INTO TB_ATO_KPIDETAIL (KPI_ID, UNIT_ID, CLL_TIME, KPI_VALUE, EXT_INFO, DB_TIME, EXT_UNIT_ID, INTERVAL) " +
//                    "VALUES ('#KPI_ID#', '#UNIT_ID#', sysdate, '#KPI_VALUE#', 'null', sysdate, '10-11-35:bnms', #INTERVAL#);";
//            insSql = insSql.replace("#KPI_ID#", record.KPI_ID);
//            insSql = insSql.replace("#UNIT_ID#", record.UNIT_ID);
//            insSql = insSql.replace("#KPI_VALUE#", record.KPI_VALUE);
//            insSql = insSql.replace("#INTERVAL#", record.INTERVAL);
//            System.out.println(insSql);
        }
    }
}