Skip to content

Commit

Permalink
Merge branch 'master' into httpsd-test
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsun28 authored Apr 25, 2024
2 parents 71980f9 + 41de1b9 commit 8cece92
Show file tree
Hide file tree
Showing 39 changed files with 1,646 additions and 268 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,41 +41,40 @@ public IcmpCollectImpl(){}
@Override
public void collect(CollectRep.MetricsData.Builder builder, long monitorId, String app, Metrics metrics) {
long startTime = System.currentTimeMillis();
// 简单校验必有参数
// Simple validation requires mandatory parameters
if (metrics == null || metrics.getIcmp() == null) {
builder.setCode(CollectRep.Code.FAIL);
builder.setMsg("ICMP collect must has icmp params");
return;
}
IcmpProtocol icmp = metrics.getIcmp();
// 超时时间默认6000毫秒
// The default timeout is 6000 milliseconds
int timeout = 6000;
try {
timeout = Integer.parseInt(icmp.getTimeout());
} catch (Exception e) {
log.warn(e.getMessage());
}
try {
// todo need root java jcm to use ICMP, else it telnet the peer server 7 port available
// todo 需要配置java虚拟机root权限从而使用ICMP,否则是判断telnet对端7号端口是否开通
// todo requires Java JVM with root permissions to use ICMP, otherwise check if telnet is available on peer server's port 7
// todo requires configuring Java JVM with root permissions to use ICMP, otherwise check if telnet is available on the peer's port 7
// todo 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 {
if (!status) {
builder.setCode(CollectRep.Code.UN_REACHABLE);
builder.setMsg("Un Reachable, Timeout " + timeout + "ms");
return;
}
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());
} catch (UnknownHostException unknownHostException) {
String errorMsg = CommonUtil.getMessageFromThrowable(unknownHostException);
builder.setCode(CollectRep.Code.UN_REACHABLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.apache.hertzbeat.common.entity.job.protocol.JmxProtocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.apache.hertzbeat.common.util.CommonUtil;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
Expand All @@ -76,8 +77,8 @@ public JmxCollectImpl() {
public void collect(CollectRep.MetricsData.Builder builder, long monitorId, String app, Metrics metrics) {

try {
JmxProtocol jmxProtocol = metrics.getJmx();
validateParams(metrics);
JmxProtocol jmxProtocol = metrics.getJmx();

// Create a jndi remote connection
JMXConnector jmxConnector = getConnectSession(jmxProtocol);
Expand Down Expand Up @@ -161,14 +162,12 @@ private Map<String, String> extractAttributeValue(AttributeList attributeList) {
return attributeValueMap;
}

private void validateParams(Metrics metrics) throws IllegalArgumentException {
if (metrics == null || metrics.getJmx() == null) {
throw new IllegalArgumentException("JMX collect must has jmx params");
}
if (StringUtils.hasText(metrics.getJmx().getUrl())) {
if (metrics.getJmx().getUrl().contains(IGNORED_STUB)) {
throw new IllegalArgumentException("JMX url prohibit contains stub, please check");
}
private void validateParams(Metrics metrics) {
Assert.isTrue(metrics != null && metrics.getJmx() != null, "JMX collect must have JMX params");

String url = metrics.getJmx().getUrl();
if (StringUtils.hasText(url)) {
Assert.doesNotContain(url, IGNORED_STUB, "JMX url prohibit contains stub, please check");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.hertzbeat.common.entity.job.protocol.WebsocketProtocol;
import org.apache.hertzbeat.common.entity.message.CollectRep;
import org.apache.hertzbeat.common.util.CommonUtil;
import org.springframework.util.Assert;

/**
* Websocket Collect
Expand All @@ -62,6 +63,11 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri
return;
}
WebsocketProtocol websocketProtocol = metrics.getWebsocket();
// Compatible with monitoring templates without path parameters
if (StringUtils.isBlank(websocketProtocol.getPath())) {
websocketProtocol.setPath("/");
}
checkParam(websocketProtocol);
String host = websocketProtocol.getHost();
String port = websocketProtocol.getPort();
Socket socket = null;
Expand All @@ -74,13 +80,12 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri
long responseTime = System.currentTimeMillis() - startTime;
OutputStream out = socket.getOutputStream();
InputStream in = socket.getInputStream();


send(out);

send(out, websocketProtocol);
Map<String, String> resultMap = readHeaders(in);
resultMap.put(CollectorConstants.RESPONSE_TIME, Long.toString(responseTime));

// 关闭输出流和Socket连接
// Close the output stream and socket connection
in.close();
out.close();
socket.close();
Expand Down Expand Up @@ -118,10 +123,10 @@ public String supportProtocol() {
return DispatchConstants.PROTOCOL_WEBSOCKET;
}

private static void send(OutputStream out) throws IOException {
private static void send(OutputStream out, WebsocketProtocol websocketProtocol) throws IOException {
byte[] key = generateRandomKey();
String base64Key = base64Encode(key);
String requestLine = "GET / HTTP/1.1\r\n";
String requestLine = "GET " + websocketProtocol.getPath() + " HTTP/1.1\r\n";
out.write(requestLine.getBytes());
String hostName = InetAddress.getLocalHost().getHostAddress();
out.write(("Host:" + hostName + "\r\n").getBytes());
Expand All @@ -135,7 +140,7 @@ private static void send(OutputStream out) throws IOException {
out.flush();
}

// 读取响应头
// Read response headers
private static Map<String, String> readHeaders(InputStream in) throws IOException {

Map<String, String> map = new HashMap<>(8);
Expand All @@ -147,19 +152,19 @@ private static Map<String, String> readHeaders(InputStream in) throws IOExceptio
if (separatorIndex != -1) {
String key = line.substring(0, separatorIndex).trim();
String value = line.substring(separatorIndex + 1).trim();
// 首字母小写化
// Lowercase first letter
map.put(StringUtils.uncapitalize(key), value);
} else {
// 切割HTTP/1.1, 101, Switching Protocols
// Cut HTTP/1.1, 101, Switching Protocols
String[] parts = line.split("\\s+", 3);
if (parts.length == 3) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].startsWith("HTTP")) {
map.put("httpVersion", parts[i]);
} else if (Character.isDigit(parts[i].charAt(0))) {
map.put("responseCode", parts[i]);
for (String part : parts) {
if (part.startsWith("HTTP")) {
map.put("httpVersion", part);
} else if (StringUtils.isNotBlank(part) && Character.isDigit(part.charAt(0))) {
map.put("responseCode", part);
} else {
map.put("statusMessage", parts[i]);
map.put("statusMessage", part);
}
}
}
Expand All @@ -175,6 +180,12 @@ private static byte[] generateRandomKey() {
return key;
}

private void checkParam(WebsocketProtocol protocol) {
Assert.hasText(protocol.getHost(), "Websocket Protocol host is required.");
Assert.hasText(protocol.getPort(), "Websocket Protocol port is required.");
Assert.hasText(protocol.getPath(), "Websocket Protocol path is required.");
}

private static String base64Encode(byte[] data) {
return Base64.getEncoder().encodeToString(data);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,6 @@ private void setNewThreadName(long monitorId, String app, long startTime, Metric

@Override
public int compareTo(MetricsCollect collect) {
return runPriority - collect.runPriority;
return collect.runPriority - this.runPriority;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public class WebsocketProtocol {
* Port number
*/
private String port;

/**
* The path to the websocket endpoint
*/
private String path;
}
101 changes: 101 additions & 0 deletions home/docs/help/debian.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
id: debian
title: Monitoring Debian System Monitoring
sidebar_label: Debian
keywords: [Open Source Monitoring System, Operating System Monitoring, Debian Monitoring]
---
> Collect and monitor general performance metrics of the Debian system.
## Configuration Parameters


| Parameter Name | Metric help description |
| ----------------------- | ----------------------------------------------------------------------------------------------------------------- |
| Target Host | The monitored destination IPV4, IPV6, or domain name. Note: no protocol header (e.g., https://, http://). |
| Task Name | A unique name to identify this monitoring task. |
| Port | SSH port of the Debian system, default: 22 |
| Timeout | Timeout for the connection, in milliseconds, default: 6000 milliseconds. |
| Connection Reuse | Whether to reuse the SSH connection, default: false. False means a new connection will be created for each query. |
| Username | Server username |
| Password | Server password |
| Collector | Configure which collector to use for scheduling this monitoring. |
| Monitoring Period | The interval for periodically collecting data, in seconds, with a minimum interval of 30 seconds. |
| Binding Tags | Used for categorizing and managing monitoring resources. |
| Metric help description | Additional notes and Metric help descriptions for this monitoring, users can add notes here. |
| Key | Key required to connect to the server. |

### Monitoring Metrics

#### Metric Set: Basic System Information


| Metric Name | Metric Unit | Metric help description |
| -------------- | ----------- | ------------------------ |
| Host Name | N/A | Host name |
| System Version | N/A | Operating system version |
| Uptime | N/A | Boot time |

#### Metric Set: CPU Information


| Metric Name | Metric Unit | Metric help description |
| -------------- | ----------- | ----------------------- |
| Info | N/A | Model |
| Cores | N/A | Number of cores |
| Interrupt | N/A | Number of interrupts |
| Load | N/A | Load |
| Context Switch | N/A | Context switches |
| Usage | % | Usage rate |

#### Metric Set: Memory Information


| Metric Name | Metric Unit | Metric help description |
| ------------------- | ----------- | ---------------------------- |
| Total Memory | Mb | Total memory capacity |
| User Program Memory | Mb | Memory used by user programs |
| Free Memory | Mb | Free memory capacity |
| Buff Cache Memory | Mb | Memory used by cache |
| Available Memory | Mb | Available memory |
| Memory Usage | % | Memory usage rate |

#### Metric Set: Disk Information


| Metric Name | Metric Unit | Metric help description |
| ------------- | ----------- | ----------------------------- |
| Disk Num | N/A | Total number of disks |
| Partition Num | N/A | Total number of partitions |
| Block Write | N/A | Number of disk blocks written |
| Block Read | N/A | Number of disk blocks read |
| Write Rate | iops | Disk write rate |

#### Metric Set: Network Interface Information

Statistics for all network interface cards, including interface name, incoming data traffic, and outgoing data traffic.
Metric Unit: Mb

#### Metric Set: File System

Statistics for all mounted file systems. Statistics include: file system, usage, available space, usage rate, mount point.
Metric Unit:

- Usage: Mb
- Available Space: Mb
- Usage Rate: %

#### Metric Set: Top 10 CPU Processes

Statistics for the top 10 processes by CPU usage. Statistics include: process ID, CPU usage rate, memory usage rate, command being executed.
Metric Unit:

- CPU Usage Rate: %
- Memory Usage Rate: %

#### Metric Set: Top 10 Memory Processes

Statistics for the top 10 processes by memory usage. Statistics include: process ID, memory usage rate, CPU usage rate, command being executed.
Metric Unit:

- Memory Usage Rate: %
- CPU Usage Rate: %
1 change: 1 addition & 0 deletions home/docs/help/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ sidebar_label: Help Center
&emsp;&#x1F449;&emsp;[OpenGauss database monitoring](opengauss) <br />
&emsp;&#x1F449;&emsp;[IoTDB database monitoring](iotdb) <br />
&emsp;&#x1F449;&emsp;[TiDB database monitoring](tidb) <br />
&emsp;&#x1F449;&emsp;[MongoDB database monitoring](mongodb) <br />

### Operating system monitoring

Expand Down
Loading

0 comments on commit 8cece92

Please sign in to comment.