[monitor] 支持PING监控类型
This commit is contained in:
@@ -74,7 +74,7 @@ public class HttpCollectImpl extends AbstractCollect {
|
|||||||
if (statusCode < HttpStatus.SC_OK || statusCode >= HttpStatus.SC_BAD_REQUEST) {
|
if (statusCode < HttpStatus.SC_OK || statusCode >= HttpStatus.SC_BAD_REQUEST) {
|
||||||
// 1XX 3XX 4XX 5XX 状态码 失败
|
// 1XX 3XX 4XX 5XX 状态码 失败
|
||||||
builder.setCode(CollectRep.Code.FAIL);
|
builder.setCode(CollectRep.Code.FAIL);
|
||||||
builder.setMsg("statusCode: " + statusCode);
|
builder.setMsg("StatusCode " + statusCode);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
// 2xx 状态码 成功
|
// 2xx 状态码 成功
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
package com.usthe.collector.collect.icmp;
|
||||||
|
|
||||||
|
import com.usthe.collector.collect.AbstractCollect;
|
||||||
|
import com.usthe.collector.util.CollectorConstants;
|
||||||
|
import com.usthe.common.entity.job.Metrics;
|
||||||
|
import com.usthe.common.entity.job.protocol.IcmpProtocol;
|
||||||
|
import com.usthe.common.entity.message.CollectRep;
|
||||||
|
import com.usthe.common.util.CommonConstants;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetAddress;
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* icmp协议采集实现 - ping
|
||||||
|
* @author tom
|
||||||
|
* @date 2021/12/4 12:32
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class IcmpCollectImpl extends AbstractCollect {
|
||||||
|
|
||||||
|
private IcmpCollectImpl(){}
|
||||||
|
|
||||||
|
public static IcmpCollectImpl getInstance() {
|
||||||
|
return IcmpCollectImpl.Singleton.INSTANCE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void collect(CollectRep.MetricsData.Builder builder, long appId, String app, Metrics metrics) {
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
// 简单校验必有参数
|
||||||
|
if (metrics == null || metrics.getIcmp() == null) {
|
||||||
|
builder.setCode(CollectRep.Code.FAIL);
|
||||||
|
builder.setMsg("ICMP collect must has icmp params");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
IcmpProtocol icmp = metrics.getIcmp();
|
||||||
|
// 超时时间默认300毫秒
|
||||||
|
int timeout = 300;
|
||||||
|
try {
|
||||||
|
timeout = Integer.parseInt(metrics.getIcmp().getTimeout());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn(e.getMessage());
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
// todo 需要配置java虚拟机root权限从而使用ICMP,否则是判断telnet对端7号端口是否开通
|
||||||
|
// https://stackoverflow.com/questions/11506321/how-to-ping-an-ip-address
|
||||||
|
boolean status = InetAddress.getByName(icmp.getHost()).isReachable(timeout);
|
||||||
|
long responseTime = System.currentTimeMillis() - startTime;
|
||||||
|
if (status) {
|
||||||
|
CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder();
|
||||||
|
for (String alias : metrics.getAliasFields()) {
|
||||||
|
if (CollectorConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) {
|
||||||
|
valueRowBuilder.addColumns(Long.toString(responseTime));
|
||||||
|
} else {
|
||||||
|
valueRowBuilder.addColumns(CommonConstants.NULL_VALUE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
builder.addValues(valueRowBuilder.build());
|
||||||
|
} else {
|
||||||
|
builder.setCode(CollectRep.Code.UN_REACHABLE);
|
||||||
|
builder.setMsg("对端不可达,Timeout " + timeout + "ms");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch (UnknownHostException unknownHostException) {
|
||||||
|
builder.setCode(CollectRep.Code.UN_REACHABLE);
|
||||||
|
builder.setMsg("UnknownHost " + unknownHostException.getMessage());
|
||||||
|
return;
|
||||||
|
} catch (IOException ioException) {
|
||||||
|
builder.setCode(CollectRep.Code.UN_REACHABLE);
|
||||||
|
builder.setMsg("IOException " + ioException.getMessage());
|
||||||
|
return;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e.getMessage(), e);
|
||||||
|
builder.setCode(CollectRep.Code.FAIL);
|
||||||
|
builder.setMsg("IllegalArgument " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class Singleton {
|
||||||
|
private static final IcmpCollectImpl INSTANCE = new IcmpCollectImpl();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import com.googlecode.aviator.AviatorEvaluator;
|
|||||||
import com.googlecode.aviator.Expression;
|
import com.googlecode.aviator.Expression;
|
||||||
import com.usthe.collector.collect.AbstractCollect;
|
import com.usthe.collector.collect.AbstractCollect;
|
||||||
import com.usthe.collector.collect.http.HttpCollectImpl;
|
import com.usthe.collector.collect.http.HttpCollectImpl;
|
||||||
|
import com.usthe.collector.collect.icmp.IcmpCollectImpl;
|
||||||
import com.usthe.collector.dispatch.timer.Timeout;
|
import com.usthe.collector.dispatch.timer.Timeout;
|
||||||
import com.usthe.collector.dispatch.timer.WheelTimerTask;
|
import com.usthe.collector.dispatch.timer.WheelTimerTask;
|
||||||
import com.usthe.common.entity.job.Job;
|
import com.usthe.common.entity.job.Job;
|
||||||
@@ -98,6 +99,8 @@ public class MetricsCollect implements Runnable, Comparable<MetricsCollect> {
|
|||||||
case DispatchConstants.PROTOCOL_HTTP:
|
case DispatchConstants.PROTOCOL_HTTP:
|
||||||
abstractCollect = HttpCollectImpl.getInstance();
|
abstractCollect = HttpCollectImpl.getInstance();
|
||||||
break;
|
break;
|
||||||
|
case DispatchConstants.PROTOCOL_ICMP:
|
||||||
|
abstractCollect = IcmpCollectImpl.getInstance();
|
||||||
// todo
|
// todo
|
||||||
default: break;
|
default: break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,9 +15,15 @@ import lombok.NoArgsConstructor;
|
|||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class IcmpProtocol {
|
public class IcmpProtocol {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 对端主机ip或域名
|
* 对端主机ip或域名
|
||||||
*/
|
*/
|
||||||
private String host;
|
private String host;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 超时时间
|
||||||
|
*/
|
||||||
|
private String timeout;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# 监控应用类型名称(与文件名保持一致) eg: linux windows tomcat mysql aws...
|
# 监控应用类型名称(与文件名保持一致) eg: linux windows tomcat mysql aws...
|
||||||
app: exapmle_type
|
app: exapmle
|
||||||
# 参数映射map. type是参数类型: 0-number数字, 1-string明文字符串, 2-secret加密字符串
|
# 参数映射map. type是参数类型: 0-number数字, 1-string明文字符串, 2-secret加密字符串
|
||||||
# 强制固定必须参数 - host
|
# 强制固定必须参数 - host
|
||||||
configmap:
|
configmap:
|
||||||
|
|||||||
28
manager/src/main/resources/define/app/ping.yml
Normal file
28
manager/src/main/resources/define/app/ping.yml
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# 监控应用类型名称(与文件名保持一致) eg: linux windows tomcat mysql aws...
|
||||||
|
app: ping
|
||||||
|
# 参数映射map. type是参数类型: 0-number数字, 1-string明文字符串, 2-secret加密字符串
|
||||||
|
# 强制固定必须参数 - host
|
||||||
|
configmap:
|
||||||
|
- key: host
|
||||||
|
type: 1
|
||||||
|
- key: timeout
|
||||||
|
type: 0
|
||||||
|
metrics:
|
||||||
|
# 第一个监控指标组 cpu
|
||||||
|
# 注意:内置监控指标有 (responseTime - 响应时间)
|
||||||
|
- name: summary
|
||||||
|
# 指标组调度优先级(0-127)越小优先级越高,优先级低的指标组会等优先级高的指标组采集完成后才会被调度,相同优先级的指标组会并行调度采集
|
||||||
|
# 优先级为0的指标组为可用性指标组,即它会被首先调度,采集成功才会继续调度其它指标组,采集失败则中断调度
|
||||||
|
priority: 0
|
||||||
|
# 指标组中的具体监控指标
|
||||||
|
fields:
|
||||||
|
# 指标信息 包括 field名称 type字段类型:0-number数字,1-string字符串 instance是否为实例主键 unit:指标单位
|
||||||
|
- field: responseTime
|
||||||
|
type: 0
|
||||||
|
unit: ms
|
||||||
|
# 监控采集使用协议 eg: sql, ssh, http, telnet, wmi, snmp, sdk
|
||||||
|
protocol: icmp
|
||||||
|
# 当protocol为ping协议时具体的采集配置
|
||||||
|
icmp:
|
||||||
|
host: ^_^host^_^
|
||||||
|
timeout: ^_^timeout^_^
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
# 监控应用类型名称(与文件名保持一致) eg: linux windows tomcat mysql aws...
|
# 监控应用类型名称(与文件名保持一致) eg: linux windows tomcat mysql aws...
|
||||||
app: exapmle_type
|
app: exapmle
|
||||||
# 强制固定必须参数 - host(ipv4,ipv6,域名)
|
# 强制固定必须参数 - host(ipv4,ipv6,域名)
|
||||||
param:
|
param:
|
||||||
# field-字段名称标识符
|
# field-字段名称标识符
|
||||||
|
|||||||
@@ -24,10 +24,18 @@ param:
|
|||||||
required: true
|
required: true
|
||||||
- field: method
|
- field: method
|
||||||
name: 请求方式
|
name: 请求方式
|
||||||
type: text
|
type: radio
|
||||||
# 当type为text时,用limit表示字符串限制大小
|
|
||||||
limit: 20
|
|
||||||
required: true
|
required: true
|
||||||
|
# 当type为radio单选框,checkbox复选框时,option表示可选项值列表 {name1:value1,name2:value2}
|
||||||
|
options:
|
||||||
|
- label: GET请求
|
||||||
|
value: GET
|
||||||
|
- label: POST请求
|
||||||
|
value: POST
|
||||||
|
- label: PUT请求
|
||||||
|
value: PUT
|
||||||
|
- label: DELETE请求
|
||||||
|
value: DELETE
|
||||||
- field: username
|
- field: username
|
||||||
name: 用户名
|
name: 用户名
|
||||||
type: text
|
type: text
|
||||||
@@ -40,8 +48,6 @@ param:
|
|||||||
required: false
|
required: false
|
||||||
- field: ssl
|
- field: ssl
|
||||||
name: 使用SSL
|
name: 使用SSL
|
||||||
|
# 当type为boolean时,前端用switch展示开关
|
||||||
type: boolean
|
type: boolean
|
||||||
required: true
|
required: true
|
||||||
# 当type为boolean时,前端用switch展示开关
|
|
||||||
# 当type为radio单选框,checkbox复选框时,option表示可选项值列表
|
|
||||||
# option: Yes,No
|
|
||||||
20
manager/src/main/resources/define/param/ping.yml
Normal file
20
manager/src/main/resources/define/param/ping.yml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# 监控应用类型名称(与文件名保持一致) eg: linux windows tomcat mysql aws...
|
||||||
|
app: ping
|
||||||
|
# 强制固定必须参数 - host(ipv4,ipv6,域名)
|
||||||
|
param:
|
||||||
|
# field-字段名称标识符
|
||||||
|
- field: host
|
||||||
|
# name-参数字段显示名称
|
||||||
|
name: 主机Host
|
||||||
|
# type-字段类型,样式(大部分映射input标签type属性)
|
||||||
|
type: host
|
||||||
|
# 是否是必输项 true-必填 false-可选
|
||||||
|
required: true
|
||||||
|
- field: timeout
|
||||||
|
name: Ping超时时间
|
||||||
|
type: number
|
||||||
|
# 当type为number时,用range表示范围
|
||||||
|
range: '[0,100000]'
|
||||||
|
required: true
|
||||||
|
placeholder: '请输入超时时间,单位毫秒'
|
||||||
|
defaultValue: 3000
|
||||||
@@ -42,7 +42,7 @@
|
|||||||
{
|
{
|
||||||
"text": "api",
|
"text": "api",
|
||||||
"link": "/monitors?app=api",
|
"link": "/monitors?app=api",
|
||||||
"i18n": "monitor.app.http"
|
"i18n": "monitor.app.api"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"text": "ping",
|
"text": "ping",
|
||||||
@@ -51,7 +51,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"text": "telnet",
|
"text": "telnet",
|
||||||
"link": "/monitors",
|
"link": "/monitors?app=telnet",
|
||||||
"i18n": "monitor.app.telnet"
|
"i18n": "monitor.app.telnet"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user