Search in sources :

Example 21 with IPPortPair

use of beans.request.IPPortPair in project MSEC by Tencent.

the class QueryBusinessLog method doGetCallRelationGraph.

//获取调用关系图
private String doGetCallRelationGraph(BusinessLog request) {
    Logger logger = Logger.getLogger(QueryBusinessLog.class);
    String dt;
    dt = request.getDt_begin();
    if (dt == null || dt.length() < 14) {
        return "time field invalid";
    }
    String begin_dt = dt.substring(0, 4) + "-" + dt.substring(4, 6) + "-" + dt.substring(6, 8);
    String begin_tm = dt.substring(9);
    dt = request.getDt_end();
    if (dt == null || dt.length() < 14) {
        return "time field invalid";
    }
    String end_dt = dt.substring(0, 4) + "-" + dt.substring(4, 6) + "-" + dt.substring(6, 8);
    String end_tm = dt.substring(9);
    String ReqID = request.getRequest_id();
    String sJson = String.format("{\"callGraphReq\":{\"reqId\":\"%s\", \"filterFieldList\": null,  \"startDate\":\"%s\"," + " \"endDate\":\"%s\", \"startTime\":\"%s\", \"endTime\":\"%s\"}}", ReqID, begin_dt, end_dt, begin_tm, end_tm);
    logger.info("request to  log server:" + sJson);
    String lens = String.format("%10d", sJson.length());
    ArrayList<IPPortPair> ipPortPairs = getBusiLogSrvIPPort();
    if (ipPortPairs == null || ipPortPairs.size() < 1) {
        return "get log server ip failed";
    }
    log_srv_ip = ipPortPairs.get(0).getIp();
    log_srv_port = ipPortPairs.get(0).getPort();
    Socket socket = new Socket();
    try {
        socket.setSoTimeout(5000);
        socket.connect(new InetSocketAddress(log_srv_ip, log_srv_port), 3000);
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(lens.getBytes());
        outputStream.write(sJson.getBytes());
        socket.shutdownOutput();
        //收应答包
        // length field 10bytes
        byte[] buf = new byte[1024 * 1024];
        int total_len = 0;
        while (total_len < 10) {
            int len = socket.getInputStream().read(buf, total_len, 10 - total_len);
            if (len <= 0) {
                return "receive json len failed.";
            }
            total_len += len;
        }
        int jsonLen = new Integer(new String(buf, 0, total_len).trim()).intValue();
        logger.info(String.format("response json string len:%d", jsonLen));
        if (jsonLen > buf.length) {
            return "response json is too long";
        }
        total_len = 0;
        while (total_len < jsonLen) {
            int len = socket.getInputStream().read(buf, total_len, jsonLen - total_len);
            if (len <= 0) {
                return "receive json string failed.";
            }
            total_len += len;
        }
        String jsonStr = new String(buf, 0, total_len);
        logger.info("the length of log json string:" + jsonStr.length());
        logger.info("log resp:" + jsonStr);
        JSONObject jsonObject = new JSONObject(jsonStr);
        jsonObject = jsonObject.getJSONObject("callGraphRsp");
        int status = jsonObject.getInt("ret");
        if (status != 0) {
            String message = jsonObject.getString("errmsg");
            return message;
        }
        String dotLang = jsonObject.getString("graph");
        if (!(dotLang == null || dotLang.equals(""))) {
            String filename = getGraphFilename(ReqID);
            Graphviz.drawGraph(dotLang, filename);
            BusinessLogResult businessLogResult = (BusinessLogResult) (this.getHttpRequest().getSession().getAttribute(BUSI_LOG_RESULT_IN_SESSION));
            if (businessLogResult == null) {
                return "failed to get log result from session.";
            }
            businessLogResult.setCall_relation_graph(ReqID);
        }
        return "success";
    } catch (Exception e) {
        e.printStackTrace();
        logger.error(e.getMessage());
        return e.getMessage();
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (Exception e) {
            }
            ;
        }
    }
}
Also used : BusinessLogResult(beans.dbaccess.BusinessLogResult) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) Logger(org.apache.log4j.Logger) IPPortPair(beans.request.IPPortPair) JSONObject(org.json.JSONObject) Socket(java.net.Socket)

Example 22 with IPPortPair

use of beans.request.IPPortPair in project MSEC by Tencent.

the class QueryConfigInLB method exec.

public QueryConfigInLBResponse exec(QueryConfigInLBRequest request) {
    QueryConfigInLBResponse response = new QueryConfigInLBResponse();
    Logger logger = Logger.getLogger(QueryConfigInLB.class);
    String svc = request.getFirst_level_service_name() + "/" + request.getSecond_level_service_name();
    AccessZooKeeper azk = new AccessZooKeeper();
    try {
        //读LB系统里的配置信息
        byte[] ret = LoadBalance.readOneServiceConfigInfo(azk, svc);
        if (ret == null) {
            response.setStatus(100);
            response.setMessage("read failed.");
            return response;
        }
        String s = new String(ret, Charset.forName("UTF-8"));
        logger.error(s);
        //json格式解析
        ArrayList<IPPortPair> ip_list = new ArrayList<>();
        JSONObject obj = new JSONObject(s);
        JSONArray arr = obj.getJSONArray("IPInfo");
        for (//每一个IP
        int i = 0; //每一个IP
        i < arr.length(); //每一个IP
        ++i) {
            obj = arr.getJSONObject(i);
            JSONArray arr2 = obj.getJSONArray("ports");
            for (int j = 0; j < arr2.length(); j++) {
                //该IP上的每一个端口
                IPPortPair pair = new IPPortPair();
                pair.setPort(arr2.getInt(j));
                pair.setIp(obj.getString("IP"));
                ip_list.add(pair);
            }
        }
        response.setIp_list(ip_list);
        response.setStatus(0);
        response.setMessage("success");
        return response;
    } catch (Exception e) {
        e.printStackTrace();
        response.setStatus(100);
        response.setMessage(e.getMessage());
        return response;
    } finally {
        azk.disconnect();
    }
}
Also used : IPPortPair(beans.request.IPPortPair) JSONObject(org.json.JSONObject) QueryConfigInLBResponse(beans.response.QueryConfigInLBResponse) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Logger(org.apache.log4j.Logger) AccessZooKeeper(ngse.org.AccessZooKeeper)

Aggregations

IPPortPair (beans.request.IPPortPair)22 Logger (org.apache.log4j.Logger)18 ArrayList (java.util.ArrayList)12 DBUtil (ngse.org.DBUtil)12 AccessZooKeeper (ngse.org.AccessZooKeeper)7 JSONObject (org.json.JSONObject)7 OutputStream (java.io.OutputStream)5 InetSocketAddress (java.net.InetSocketAddress)5 Socket (java.net.Socket)5 ServletOutputStream (javax.servlet.ServletOutputStream)5 JsonRPCResponseBase (ngse.org.JsonRPCResponseBase)4 SecondLevelService (beans.dbaccess.SecondLevelService)3 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)3 JSONArray (org.json.JSONArray)3 BusinessLogResult (beans.dbaccess.BusinessLogResult)2 OddSecondLevelService (beans.dbaccess.OddSecondLevelService)2 SQLException (java.sql.SQLException)2 CapacityBaseInfo (beans.dbaccess.CapacityBaseInfo)1 CapacityDetailInfo (beans.dbaccess.CapacityDetailInfo)1 BusiLogField (beans.request.BusiLogField)1