Search in sources :

Example 1 with BusiLogField

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

the class QueryBusiLogField method exec.

public BusiLogFieldResponse exec(BusiLogField request) {
    BusiLogFieldResponse response = new BusiLogFieldResponse();
    ArrayList<BusiLogField> fields = new ArrayList<BusiLogField>();
    String msg = doGetBusiLogField(request, fields);
    if (msg == null || !msg.equals("success")) {
        response.setMessage(msg);
        response.setStatus(100);
    } else {
        response.setField_list(fields);
        response.setMessage("success");
        response.setStatus(0);
    }
    return response;
}
Also used : BusiLogField(beans.request.BusiLogField) ArrayList(java.util.ArrayList) BusiLogFieldResponse(beans.response.BusiLogFieldResponse)

Example 2 with BusiLogField

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

the class QueryBusiLogField method doGetBusiLogField.

private String doGetBusiLogField(BusiLogField request, ArrayList<BusiLogField> fields) {
    Logger logger = Logger.getLogger(QueryBusinessLog.class);
    String sJson = String.format("{\"getFieldsReq\":{\"appName\":null}}");
    logger.info("to log srv:" + sJson);
    String lens = String.format("%10d", sJson.length());
    ArrayList<IPPortPair> ipPortPairs = QueryBusinessLog.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[10240];
        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) {
            logger.error("response json is too long" + jsonLen);
            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;
        }
        logger.info(String.format("read logserver response bytes number:%d", total_len));
        String jsonStr = new String(buf, 0, total_len);
        logger.info("from log srv:" + jsonStr);
        JSONObject jsonObject = new JSONObject(jsonStr);
        jsonObject = jsonObject.getJSONObject("getFieldsRsp");
        int status = jsonObject.getInt("ret");
        if (status != 0) {
            String message = jsonObject.getString("errmsg");
            logger.error("log server returns errcode, msg:" + message);
            return message;
        }
        JSONArray infoArray = jsonObject.getJSONArray("fieldsInfo");
        for (int i = 0; i < infoArray.length(); i++) {
            JSONObject info = infoArray.getJSONObject(i);
            BusiLogField field = new BusiLogField();
            field.setField_type(info.getString("field_type"));
            field.setField_name(info.getString("field_name"));
            logger.info(String.format("field%d:%s,%s", i, field.getField_name(), field.getField_type()));
            fields.add(field);
        }
        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 : BusiLogField(beans.request.BusiLogField) InetSocketAddress(java.net.InetSocketAddress) OutputStream(java.io.OutputStream) JSONArray(org.json.JSONArray) Logger(org.apache.log4j.Logger) IPPortPair(beans.request.IPPortPair) JSONObject(org.json.JSONObject) Socket(java.net.Socket)

Aggregations

BusiLogField (beans.request.BusiLogField)2 IPPortPair (beans.request.IPPortPair)1 BusiLogFieldResponse (beans.response.BusiLogFieldResponse)1 OutputStream (java.io.OutputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 Socket (java.net.Socket)1 ArrayList (java.util.ArrayList)1 Logger (org.apache.log4j.Logger)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1