Search in sources :

Example 1 with BusinessLogResult

use of beans.dbaccess.BusinessLogResult in project MSEC by Tencent.

the class QueryBusinessLog method doQueryLog.

private String doQueryLog(BusinessLog request) {
    Logger logger = Logger.getLogger(QueryBusinessLog.class);
    String svcname = request.getService_name();
    //if (svcname == null || svcname.length() < 1) { return "svcname field invalid";}
    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 where = "";
    if (request.getRequest_id() != null && request.getRequest_id().length() > 0) {
        if (where.length() > 0) {
            where += " and ";
        }
        where += " ReqID='" + request.getRequest_id() + "'";
    }
    if (request.getLog_ip() != null && request.getLog_ip().length() > 0) {
        if (where.length() > 0) {
            where += " and ";
        }
        where += " LocalIP='" + request.getLog_ip() + "' ";
    }
    if (request.getMore_condition() != null && request.getMore_condition().length() > 0) {
        if (where.length() > 0) {
            where += " and ";
        }
        where += request.getMore_condition();
    }
    String sJson = String.format("{\"queryLogReq\":{\"appName\":\"%s\",  \"logLevel\":\"DEBUG\"," + "\"filterFieldList\": null,\"maxRetNum\":1000, \"startDate\":\"%s\"," + " \"endDate\":\"%s\", \"startTime\":\"%s\", \"endTime\":\"%s\", " + "\"whereCondition\":\"%s\"}}", svcname, begin_dt, end_dt, begin_tm, end_tm, where);
    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);
        //String jsonStr = "{'queryLogRsp':{'ret':0, 'errmsg':'ok', 'records':[ ['b\"i{e:r}s\\'on', '1122[8]490', '36岁'],['babamama', '2202020302', '37岁']  ],heads:['name', 'uin', 'age']}}";
        logger.info("the length of log json string:" + jsonStr.length());
        // logger.info("log resp:"+jsonStr);
        JSONObject jsonObject = new JSONObject(jsonStr);
        jsonObject = jsonObject.getJSONObject("queryLogRsp");
        int status = jsonObject.getInt("ret");
        if (status != 0) {
            String message = jsonObject.getString("errmsg");
            return message;
        }
        ArrayList<String> columns = new ArrayList<String>();
        ArrayList<HashMap<String, String>> record_map_list = new ArrayList<HashMap<String, String>>();
        //返回的json字符串
        // {queryLogRsp:{ret:0, errmsg:'ok', records:[ ['fieldvalue1', 'fieldvalue2', 'fieldvalue3'], ...  ],heads:['ReqID', 'instime', ....]} }
        // 注意:引号要转义
        // 例如:{'queryLogRsp':{'ret':0, 'errmsg':'ok', 'records':[ ['b"i{e:r}s\'on', '1122[8]490', '36岁'],['babamama', '2202030202', '37岁']  ],heads:['name', 'uin', 'age']}}
        // json array里元素的顺序是否在序列化前后或者反序列化前后保持不变呢?
        JSONArray heads = jsonObject.getJSONArray("heads");
        StringBuffer logstr = new StringBuffer();
        for (int i = 0; i < heads.length(); i++) {
            columns.add(heads.getString(i));
            logstr.append(heads.getString(i));
            logstr.append(";");
        }
        logger.info("log head:" + logstr);
        JSONArray records = jsonObject.getJSONArray("records");
        for (int i = 0; i < records.length(); ++i) {
            JSONArray values = records.getJSONArray(i);
            if (values.length() != columns.size()) {
                return "data returned by log serv is invalid.";
            }
            HashMap<String, String> record_map = new HashMap<String, String>();
            for (int j = 0; j < values.length(); j++) {
                String value = values.getString(j);
                record_map.put(columns.get(j), value);
            }
            record_map_list.add(record_map);
        }
        logger.info("log record number:" + record_map_list.size());
        BusinessLogResult businessLogResult = new BusinessLogResult();
        businessLogResult.setColumn_names(columns);
        businessLogResult.setLog_records(record_map_list);
        this.getHttpRequest().getSession().setAttribute(BUSI_LOG_RESULT_IN_SESSION, businessLogResult);
        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) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) Logger(org.apache.log4j.Logger) IPPortPair(beans.request.IPPortPair) JSONObject(org.json.JSONObject) Socket(java.net.Socket)

Example 2 with BusinessLogResult

use of beans.dbaccess.BusinessLogResult 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)

Aggregations

BusinessLogResult (beans.dbaccess.BusinessLogResult)2 IPPortPair (beans.request.IPPortPair)2 OutputStream (java.io.OutputStream)2 InetSocketAddress (java.net.InetSocketAddress)2 Socket (java.net.Socket)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 Logger (org.apache.log4j.Logger)2 JSONObject (org.json.JSONObject)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JSONArray (org.json.JSONArray)1