Authored by qinchao

支持tcp探测

... ... @@ -4,13 +4,12 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.model.JavaApiInfo;
import com.model.MObjectDetails;
import com.model.MObjectInfo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.client.RestTemplate;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.Socket;
import java.util.concurrent.Callable;
/**
... ... @@ -36,7 +35,73 @@ public class JavaApiTask implements Callable<JavaApiStatics> {
@Override
public JavaApiStatics call() throws Exception {
if("tcp".equals(javaApiInfo.getRequestType())){
return dealTcp();
}else{
//默认是http
return dealHttp();
}
}
private JavaApiStatics dealTcp(){
JavaApiStatics apiStatics = new JavaApiStatics();
apiStatics.setStartTime(System.currentTimeMillis());
apiStatics.setHasException(false);
apiStatics.setJavaApiInfo(this.javaApiInfo);
apiStatics.setMObjectDetails(this.mObjectInfo);
String socketPath=null;
try{
socketPath = createReqTcpPath();
if(StringUtils.isBlank(socketPath)){
return null;
}
String[] ary =socketPath.split(":");
if(ary.length!=2){
log.warn("socketPath {} is not valid ", socketPath);
return null;
}
String host=ary[0];
int port;
try{
port = Integer.parseInt(ary[1]);
}catch (Exception e){
log.warn("socketPath {} port is not valid ", socketPath);
return null;
}
Socket s = new Socket(host, port);
s.close();
}catch(Exception e){
log.warn("socketPath {} exception {} ", socketPath, e);
apiStatics.setHasException(true);
apiStatics.setException(e);
} finally {
apiStatics.setEndTime(System.currentTimeMillis());
apiStatics.setResponse(null);//没有错误
}
return apiStatics;
}
private String createReqTcpPath() {
if (javaApiInfo.getApiUrlCustom() != null && javaApiInfo.getApiUrlCustom() == 1 && javaApiInfo.getApiUrl() != null) {
return javaApiInfo.getApiUrl(); //custom url
}
if (mObjectInfo.getMoHostIp() == null || mObjectInfo.getMoHostIp() == null || javaApiInfo.getApiUrl() == null) {
log.warn("get url failed has null prop. ");
return null;
}
String url = mObjectInfo.getMoHostIp();
if (mObjectInfo.getMoTags() != null && !mObjectInfo.getMoTags().equals("")) {
url += ":" + mObjectInfo.getMoTags();
}
return url;
}
private JavaApiStatics dealHttp(){
String url = createReqUrl();
// log.info("url: {}", url);
if (url == null)
... ...
package com.monitor.javaserver.handle.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.monitor.javaserver.common.JavaApiStatics;
import com.monitor.javaserver.handle.IJavaApiHadnler;
import com.monitor.javaserver.util.YohoSearchCheckUtil;
... ...
... ... @@ -15,6 +15,9 @@ public class JavaApiInfoReq extends PageRequest {
private String apiName;
//请求类型 http ,tcp
private String requestType;
private String apiUrl;
private String apiData;
... ...
... ... @@ -11,6 +11,9 @@ public class JavaApiInfo {
private String apiName;
//请求类型 http ,tcp
private String requestType;
private String apiUrl;
private String apiData;
... ...
... ... @@ -5,6 +5,7 @@
<id column="service_id" property="serviceId" jdbcType="INTEGER"/>
<result column="service_type" property="serviceType" jdbcType="INTEGER"/>
<result column="api_name" property="apiName" jdbcType="VARCHAR"/>
<result column="request_type" property="requestType" jdbcType="VARCHAR"/>
<result column="api_url" property="apiUrl" jdbcType="VARCHAR"/>
<result column="api_data" property="apiData" jdbcType="VARCHAR"/>
<result column="api_toggle" property="apiToggle" jdbcType="INTEGER"/>
... ... @@ -15,7 +16,7 @@
<result column="special_deal_type" property="specialDealType" jdbcType="VARCHAR"/>
</resultMap>
<sql id="Base_Column_List">
service_id, service_type, api_name, api_url, api_data, api_toggle, api_req_method,api_warn_trigger,api_error_check,api_url_custom,special_deal_type
service_id, service_type, api_name,request_type, api_url, api_data, api_toggle, api_req_method,api_warn_trigger,api_error_check,api_url_custom,special_deal_type
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
select
... ... @@ -29,10 +30,10 @@
</delete>
<insert id="insert" parameterType="com.model.JavaApiInfo">
insert into java_api_info (service_id, service_type, api_name,
api_url, api_data, api_toggle,
request_type,api_url, api_data, api_toggle,
api_req_method,api_warn_trigger,api_error_check,api_url_custom)
values (#{serviceId,jdbcType=INTEGER}, #{serviceType,jdbcType=INTEGER}, #{apiName,jdbcType=VARCHAR},
#{apiUrl,jdbcType=VARCHAR}, #{apiData,jdbcType=VARCHAR}, #{apiToggle,jdbcType=INTEGER},
#{requestType,jdbcType=VARCHAR},#{apiUrl,jdbcType=VARCHAR}, #{apiData,jdbcType=VARCHAR}, #{apiToggle,jdbcType=INTEGER},
#{apiReqMethod,jdbcType=INTEGER},#{apiWarnTrigger,jdbcType=INTEGER},
#{apiErrorCheck,jdbcType=INTEGER},#{apiUrlCustom,jdbcType=INTEGER})
</insert>
... ... @@ -48,6 +49,9 @@
<if test="apiName != null">
api_name,
</if>
<if test="requestType != null">
request_type,
</if>
<if test="apiUrl != null">
api_url,
</if>
... ... @@ -80,6 +84,9 @@
<if test="apiName != null">
#{apiName,jdbcType=VARCHAR},
</if>
<if test="requestType != null">
#{requestType,jdbcType=VARCHAR},
</if>
<if test="apiUrl != null">
#{apiUrl,jdbcType=VARCHAR},
</if>
... ... @@ -112,6 +119,9 @@
<if test="apiName != null">
api_name = #{apiName,jdbcType=VARCHAR},
</if>
<if test="requestType != null">
request_type = #{requestType,jdbcType=VARCHAR},
</if>
<if test="apiUrl != null">
api_url = #{apiUrl,jdbcType=VARCHAR},
</if>
... ... @@ -140,6 +150,7 @@
update java_api_info
set service_type = #{serviceType,jdbcType=INTEGER},
api_name = #{apiName,jdbcType=VARCHAR},
request_type = #{requestType,jdbcType=VARCHAR},
api_url = #{apiUrl,jdbcType=VARCHAR},
api_data = #{apiData,jdbcType=VARCHAR},
api_toggle = #{apiToggle,jdbcType=INTEGER},
... ...