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) {
}
;
}
}
}
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();
}
}
Aggregations