CollNginxWithCMD.java 9.14 KB
package com.sitech.ismp.coll.middleware.nginx;

import com.sitech.ismp.coll.CollBase;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.DES3;
import com.sitech.util.Formater;
import com.sitech.util.upload.RomoteController;
import com.sitech.util.upload.SSHThread;
import com.sitech.util.upload.TelnetThread;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;

/**
 * Created with IntelliJ IDEA.
 * User: mooker
 * Date: 14-7-4
 * Time: 上午10:07
 * To change this template use File | Settings | File Templates.
 */
public class CollNginxWithCMD {

    private Logger logger = Logger.getLogger(CollNginxWithCMD.class);
    private static String KBP_CLASS = "10-12-15";

    String hostip = "";
    String username = "";
    String password = "";
    String authname  = "";
    String authpass = "";
    String protocol = "";
    String devicename = "";
    String neat_nginx_name = "";
    String credit = "";

    RomoteController tt = null;
    boolean isConnect = false;

    //String nginx_port = "80";
    //String status_url = "nginx_status";

    String nginx_port = "8091";
    String status_url = "NginxStatus";

    public CollNginxWithCMD() {
    }

    public void init(HashMap params) {
        hostip = (String) params.get("IP_ADDR");
        username = (String) params.get("USERNAME");
        password = (String) params.get("PASSWORD");
        password = DES3.decrypt(password);
        authname = (String) params.get("AUTHNAME");
        authpass = (String) params.get("AUTHPASS");
        protocol = (String) params.get("PROTOCOL");
        devicename = (String) params.get("DEVICENAME");
        this.neat_nginx_name = Formater.neatenunitid(devicename);
        String protocolPort = null;
        if (params.get("PROTOCOL_PORT") != null) {
            protocolPort = ((String) params.get("PROTOCOL_PORT")).trim();
        }
        if (null != params.get("STATUSURL") && params.get("STATUSURL").toString().length() > 0) {
            status_url = (String) params.get("STATUSURL");
        }
        if (null != params.get("NGINXPORT") && params.get("NGINXPORT").toString().length() > 0) {
            nginx_port = (String) params.get("NGINXPORT");
        }
        if (protocol != null && protocol.equals("telnet")) {
            if (null != protocolPort && !"".equals(protocolPort)) {
                tt = new TelnetThread(hostip, Integer.parseInt(protocolPort), username, password);
            } else {
                tt = new TelnetThread(hostip, 23, username, password);
            }
        } else {
            if (null != protocolPort && !"".equals(protocolPort)) {
                tt = new SSHThread(hostip, Integer.parseInt(protocolPort), username, password);
            } else {
                tt = new SSHThread(hostip, 22, username, password);
            }
        }
        if(null !=authname && !"".equals(authname)
                &&null !=authpass && !"".equals(authpass)){//如果nginx验证用户名和密码都不为空,就构造这个指令
            credit = "-u "+authname+":"+authpass;
        }
        tt.initial();
        this.isConnect = tt.isAuthorized();
        logger.info("isConnect is " + this.isConnect);
    }

    public void release() {
        if (tt != null) {
            tt.close();
            tt = null;
        }
        System.gc();
    }

    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 Vector<TblATO_KPIDETAIL> getConfig(HashMap<String, String> params) {
        logger.info("begin getConfig");
        System.out.println("begin getConfig");
        CollBase collResult = new CollBase();
        init(params);
        String pro_PRE_UNITID = KBP_CLASS + "-10";
        try {
            // 查看进程信息
            String cmd = "ps -ef | grep nginx | grep -v grep";
//          ps -ef | grep nginx | grep -v grep
//          root      7078     1  0 Jul01 ?        00:00:00 nginx: master process ./nginx -c /usr/local/nginx/conf/nginx.conf
//          root     30520  7078  0 Jul02 ?        00:00:00 nginx: worker process
//          root     30521  7078  0 Jul02 ?        00:00:07 nginx: worker process
            Vector resstr = tt.getResultAsVector(cmd);
            if (resstr == null || resstr.equals("") || resstr.equals("null")) {
                // 进程状态 PM-00-01-005-02
                collResult.addKPI(pro_PRE_UNITID + ":" + neat_nginx_name + "-total", "PM-00-01-005-02", "DOWN");
                // 进程个数 PM-00-01-005-12
                collResult.addKPI(pro_PRE_UNITID + ":" + neat_nginx_name + "-total", "PM-00-01-005-12", "0");
                return collResult.getKPISet();
            } else {
                int count = resstr.size();
                // 进程状态 PM-00-01-005-02
                collResult.addKPI(pro_PRE_UNITID + ":" + neat_nginx_name + "-total", "PM-00-01-005-02", "UP");
                // 进程个数 PM-00-01-005-12
                collResult.addKPI(pro_PRE_UNITID + ":" + neat_nginx_name + "-total", "PM-00-01-005-12", count + "");
                for (Object commands : resstr) {
                    String _commands = (String) commands;
                    if (_commands.contains("master process")) {
                        String command = _commands.substring(_commands.indexOf("master process") + 15);
                        // 主进程启动用命令行 CM-00-01-005-08
                        collResult.addKPI(pro_PRE_UNITID + ":" + neat_nginx_name + "-total", "CM-00-01-005-08", command);
                        String[] configFile = _commands.split(" ");
                        for (String _configFile : configFile) {
                            if (_configFile.contains("nginx.conf")) {
                                // 资源文件 CM-00-05-009-07
                                collResult.addKPI(pro_PRE_UNITID + ":" + neat_nginx_name + "-total", "CM-00-05-009-07", _configFile);
                            }
                        }
                    }
                }
            }
            // 监听地址 CM-00-05-009-02
            collResult.addKPI(KBP_CLASS + "-10:" + neat_nginx_name + "-total", "CM-00-05-009-02", hostip);
            // 监听端口 CM-00-05-009-03
            collResult.addKPI(KBP_CLASS + "-10:" + neat_nginx_name + "-total", "CM-00-05-009-03", nginx_port);
        } catch (Exception e) {
            logger.error("coll nginx config error", e);
        } finally {
            release();
        }
        return collResult.KPISet;
    }

    public Vector<TblATO_KPIDETAIL> getPerfor(HashMap<String, String> params) {
        logger.info("begin getPerfor");
        CollBase collResult = new CollBase();
        init(params);
        String pro_PRE_UNITID = KBP_CLASS + "-11";
        try {
            // 查看进程信息
            String cmd = "/usr/bin/curl "+credit+" \"http://" + hostip + ":" + nginx_port + "/" + status_url + "\"";
            System.out.println("check nginx status url is: " + cmd);
//            /usr/bin/curl "http://172.21.0.203:90/nginx_status"
//            Active connections: 1
//            server accepts handled requests
//            31 31 98
//            Reading: 0 Writing: 1 Waiting: 0
            Vector resstr = tt.getResultAsVector(cmd);
            int i = 1;
            if (resstr == null || resstr.equals("") || resstr.equals("null")) {
                logger.info(cmd + " has no info");
                return collResult.getKPISet();
            } else {
                String activeConnections = (String) resstr.elementAt(0);
                // 正处理的活动连接数
                collResult.addKPI(KBP_CLASS + "-11:" + neat_nginx_name + "-perfor", "PM-00-05-009-55", split(activeConnections, 2));
                String serverAHR = (String) resstr.elementAt(2);
                // 总共处理连接数 成功处理连接数 总共处理连接请求数
                collResult.addKPI(KBP_CLASS + "-11:" + neat_nginx_name + "-perfor", "PM-00-05-009-56", split(serverAHR, 0));
                collResult.addKPI(KBP_CLASS + "-11:" + neat_nginx_name + "-perfor", "PM-00-05-009-57", split(serverAHR, 1));
                collResult.addKPI(KBP_CLASS + "-11:" + neat_nginx_name + "-perfor", "PM-00-05-009-58", split(serverAHR, 2));
                String readWriteWait = (String) resstr.elementAt(3);
                // 读取客户端Header数 返回客户端Header数 等待请求驻留连接
                collResult.addKPI(KBP_CLASS + "-11:" + neat_nginx_name + "-perfor", "PM-00-05-009-59", split(readWriteWait, 1));
                collResult.addKPI(KBP_CLASS + "-11:" + neat_nginx_name + "-perfor", "PM-00-05-009-60", split(readWriteWait, 3));
                collResult.addKPI(KBP_CLASS + "-11:" + neat_nginx_name + "-perfor", "PM-00-05-009-61", split(readWriteWait, 5));
            }
        } catch (Exception e) {
            logger.error("coll nginx config error", e);
        } finally {
            release();
        }
        return collResult.KPISet;
    }

}