From a55a098983c06b2da7663272467c9942c6b0c9d5 Mon Sep 17 00:00:00 2001 From: YuLuo Date: Mon, 12 Aug 2024 08:00:17 +0800 Subject: [PATCH] [improve] Replacing characters with http constants (#2500) Signed-off-by: yuluo-yx Co-authored-by: tomsun28 --- .../service/impl/AlertDefineServiceImpl.java | 10 ++-- .../collector/collect/AbstractCollect.java | 1 - .../collect/database/JdbcCommonCollect.java | 2 +- .../collect/http/HttpCollectImpl.java | 37 +++++++------- .../http/SslCertificateCollectImpl.java | 2 +- .../collector/collect/httpsd/HttpsdImpl.java | 2 +- .../collect/icmp/IcmpCollectImpl.java | 2 +- .../memcached/MemcachedCollectImpl.java | 3 +- .../mongodb/MongodbSingleCollectImpl.java | 2 +- .../nebulagraph/NebulaGraphCollectImpl.java | 3 +- .../collect/nebulagraph/NgqlCollectImpl.java | 2 +- .../collect/nginx/NginxCollectImpl.java | 25 +++++----- .../collector/collect/ntp/NtpCollectImpl.java | 2 +- .../collect/pop3/Pop3CollectImpl.java | 2 +- .../prometheus/PrometheusAutoCollectImpl.java | 20 ++++---- .../collect/push/PushCollectImpl.java | 16 +++--- .../collect/redfish/RedfishClient.java | 25 ++++++---- .../redfish/RedfishConnectSession.java | 19 +++---- .../collect/script/ScriptCollectImpl.java | 4 +- .../collect/smtp/SmtpCollectImpl.java | 2 +- .../collect/snmp/SnmpCollectImpl.java | 2 +- .../collector/collect/ssh/SshCollectImpl.java | 2 +- .../collect/telnet/TelnetCollectImpl.java | 2 +- .../collector/collect/udp/UdpCollectImpl.java | 2 +- .../websocket/WebsocketCollectImpl.java | 2 +- .../constants/CollectorConstants.java | 34 ++++++------- .../collect/nginx/NginxCollectImplTest.java | 4 +- .../common/constants/NetworkConstants.java | 50 +++++++++++++++++++ .../common/constants/SignConstants.java | 4 ++ .../hertzbeat/common/util/IpDomainUtil.java | 6 +-- .../hertzbeat/common/util/FileUtilTest.java | 5 +- .../common/util/IpDomainUtilTest.java | 17 +++---- .../impl/WeChatAlertNotifyHandlerImpl.java | 4 +- .../service/impl/MonitorServiceImpl.java | 23 +++++---- 34 files changed, 201 insertions(+), 137 deletions(-) rename {common/src/main/java/org/apache/hertzbeat/common => collector/src/main/java/org/apache/hertzbeat/collector}/constants/CollectorConstants.java (80%) create mode 100644 common/src/main/java/org/apache/hertzbeat/common/constants/NetworkConstants.java diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineServiceImpl.java b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineServiceImpl.java index 0923695c17a..e37fdb86ce8 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineServiceImpl.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineServiceImpl.java @@ -38,6 +38,7 @@ import org.apache.hertzbeat.alert.service.AlertDefineImExportService; import org.apache.hertzbeat.alert.service.AlertDefineService; import org.apache.hertzbeat.common.constants.ExportFileConstants; +import org.apache.hertzbeat.common.constants.SignConstants; import org.apache.hertzbeat.common.entity.alerter.AlertDefine; import org.apache.hertzbeat.common.entity.alerter.AlertDefineMonitorBind; import org.apache.hertzbeat.common.entity.manager.Monitor; @@ -49,6 +50,7 @@ import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; @@ -73,6 +75,8 @@ public class AlertDefineServiceImpl implements AlertDefineService { private final Map alertDefineImExportServiceMap = new HashMap<>(); + private static final String CONTENT_TYPE = MediaType.APPLICATION_OCTET_STREAM_VALUE + SignConstants.SINGLE_MARK + "charset=" + StandardCharsets.UTF_8; + public AlertDefineServiceImpl(List alertDefineImExportServiceList) { alertDefineImExportServiceList.forEach(it -> alertDefineImExportServiceMap.put(it.type(), it)); } @@ -229,10 +233,10 @@ public void export(List ids, String type, HttpServletResponse res) throws throw new IllegalArgumentException("not support export type: " + type); } var fileName = imExportService.getFileName(); - res.setHeader("content-type", "application/octet-stream;charset=UTF-8"); - res.setContentType("application/octet-stream;charset=UTF-8"); + res.setHeader(HttpHeaders.CONTENT_TYPE, CONTENT_TYPE); + res.setContentType(CONTENT_TYPE); res.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8)); - res.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); + res.setHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION); imExportService.exportConfig(res.getOutputStream(), ids); } diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/AbstractCollect.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/AbstractCollect.java index 82caa001248..455b584d86b 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/AbstractCollect.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/AbstractCollect.java @@ -17,7 +17,6 @@ package org.apache.hertzbeat.collector.collect; - import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.message.CollectRep; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/database/JdbcCommonCollect.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/database/JdbcCommonCollect.java index 22943694cd8..b3f25518afb 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/database/JdbcCommonCollect.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/database/JdbcCommonCollect.java @@ -31,9 +31,9 @@ import org.apache.hertzbeat.collector.collect.common.cache.CacheIdentifier; import org.apache.hertzbeat.collector.collect.common.cache.ConnectionCommonCache; import org.apache.hertzbeat.collector.collect.common.cache.JdbcConnect; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; import org.apache.hertzbeat.collector.util.CollectUtil; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.JdbcProtocol; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImpl.java index c2c48f48ead..99dfe3f9b94 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/HttpCollectImpl.java @@ -48,12 +48,14 @@ import org.apache.hertzbeat.collector.collect.http.promethus.PrometheusParseCreator; import org.apache.hertzbeat.collector.collect.http.promethus.exporter.ExporterParser; import org.apache.hertzbeat.collector.collect.http.promethus.exporter.MetricFamily; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; import org.apache.hertzbeat.collector.util.CollectUtil; import org.apache.hertzbeat.collector.util.JsonPathParser; import org.apache.hertzbeat.collector.util.TimeExpressionUtil; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; +import org.apache.hertzbeat.common.constants.NetworkConstants; +import org.apache.hertzbeat.common.constants.SignConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.HttpProtocol; import org.apache.hertzbeat.common.entity.message.CollectRep; @@ -81,6 +83,7 @@ import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import org.w3c.dom.Document; @@ -115,7 +118,7 @@ public void collect(CollectRep.MetricsData.Builder builder, httpProtocol.setUrl(StringUtils.hasText(url) ? RIGHT_DASH + url.trim() : RIGHT_DASH); } if (CollectionUtils.isEmpty(httpProtocol.getSuccessCodes())) { - httpProtocol.setSuccessCodes(List.of("200")); + httpProtocol.setSuccessCodes(List.of(HttpStatus.SC_OK + "")); } HttpContext httpContext = createHttpContext(metrics.getHttp()); @@ -126,7 +129,7 @@ public void collect(CollectRep.MetricsData.Builder builder, log.debug("http response status: {}", statusCode); if (!isSuccessInvoke) { builder.setCode(CollectRep.Code.FAIL); - builder.setMsg("StatusCode " + statusCode); + builder.setMsg(NetworkConstants.STATUS_CODE + SignConstants.BLANK + statusCode); return; } // todo This code converts an InputStream directly to a String. For large data in Prometheus exporters, @@ -229,7 +232,7 @@ private void addColumnFromHeader(CollectRep.ValueRow.Builder valueRowBuilder, St } private void addColumnForSummary(Long responseTime, CollectRep.ValueRow.Builder valueRowBuilder, int keywordNum, String alias) { - if (CollectorConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) { + if (NetworkConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) { valueRowBuilder.addColumns(responseTime.toString()); } else if (CollectorConstants.KEYWORD.equalsIgnoreCase(alias)) { valueRowBuilder.addColumns(Integer.toString(keywordNum)); @@ -304,14 +307,14 @@ private void parseResponseBySiteMap(String resp, List aliasFields, long responseTime = System.currentTimeMillis() - startTime; CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder(); for (String alias : aliasFields) { - if (CollectorConstants.URL.equalsIgnoreCase(alias)) { + if (NetworkConstants.URL.equalsIgnoreCase(alias)) { valueRowBuilder.addColumns(siteUrl); - } else if (CollectorConstants.STATUS_CODE.equalsIgnoreCase(alias)) { + } else if (NetworkConstants.STATUS_CODE.equalsIgnoreCase(alias)) { valueRowBuilder.addColumns(statusCode == null ? CommonConstants.NULL_VALUE : String.valueOf(statusCode)); - } else if (CollectorConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) { + } else if (NetworkConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) { valueRowBuilder.addColumns(String.valueOf(responseTime)); - } else if (CollectorConstants.ERROR_MSG.equalsIgnoreCase(alias)) { + } else if (NetworkConstants.ERROR_MSG.equalsIgnoreCase(alias)) { valueRowBuilder.addColumns(errorMsg); } else { valueRowBuilder.addColumns(CommonConstants.NULL_VALUE); @@ -360,7 +363,7 @@ private void parseResponseByJsonPath(String resp, List aliasFields, Http } else if (objectValue instanceof String stringValue) { CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder(); for (String alias : aliasFields) { - if (CollectorConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) { + if (NetworkConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) { valueRowBuilder.addColumns(responseTime.toString()); } else if (CollectorConstants.KEYWORD.equalsIgnoreCase(alias)) { valueRowBuilder.addColumns(Integer.toString(keywordNum)); @@ -511,8 +514,8 @@ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) { } // The default request header can be overridden if customized // keep-alive - requestBuilder.addHeader(HttpHeaders.CONNECTION, "keep-alive"); - requestBuilder.addHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36"); + requestBuilder.addHeader(HttpHeaders.CONNECTION, NetworkConstants.KEEP_ALIVE); + requestBuilder.addHeader(HttpHeaders.USER_AGENT, NetworkConstants.USER_AGENT); // headers The custom request header is overwritten here Map headers = httpProtocol.getHeaders(); if (headers != null && !headers.isEmpty()) { @@ -526,11 +529,11 @@ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) { // add accept if (DispatchConstants.PARSE_DEFAULT.equals(httpProtocol.getParseType()) || DispatchConstants.PARSE_JSON_PATH.equals(httpProtocol.getParseType())) { - requestBuilder.addHeader(HttpHeaders.ACCEPT, "application/json"); + requestBuilder.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE); } else if (DispatchConstants.PARSE_XML_PATH.equals(httpProtocol.getParseType())) { - requestBuilder.addHeader(HttpHeaders.ACCEPT, "text/xml,application/xml"); + requestBuilder.addHeader(HttpHeaders.ACCEPT, MediaType.TEXT_HTML_VALUE + "," + MediaType.APPLICATION_XML_VALUE); } else { - requestBuilder.addHeader(HttpHeaders.ACCEPT, "*/*"); + requestBuilder.addHeader(HttpHeaders.ACCEPT, MediaType.ALL_VALUE); } if (httpProtocol.getAuthorization() != null) { @@ -560,14 +563,14 @@ public HttpUriRequest createHttpRequest(HttpProtocol httpProtocol) { requestBuilder.setUri(httpProtocol.getHost() + ":" + httpProtocol.getPort() + uri); } else { String ipAddressType = IpDomainUtil.checkIpAddressType(httpProtocol.getHost()); - String baseUri = CollectorConstants.IPV6.equals(ipAddressType) + String baseUri = NetworkConstants.IPV6.equals(ipAddressType) ? String.format("[%s]:%s%s", httpProtocol.getHost(), httpProtocol.getPort(), uri) : String.format("%s:%s%s", httpProtocol.getHost(), httpProtocol.getPort(), uri); boolean ssl = Boolean.parseBoolean(httpProtocol.getSsl()); if (ssl) { - requestBuilder.setUri(CollectorConstants.HTTPS_HEADER + baseUri); + requestBuilder.setUri(NetworkConstants.HTTPS_HEADER + baseUri); } else { - requestBuilder.setUri(CollectorConstants.HTTP_HEADER + baseUri); + requestBuilder.setUri(NetworkConstants.HTTP_HEADER + baseUri); } } diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/SslCertificateCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/SslCertificateCollectImpl.java index 36141ddc30e..0addb520641 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/SslCertificateCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/http/SslCertificateCollectImpl.java @@ -36,8 +36,8 @@ import javax.net.ssl.X509TrustManager; import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.collector.collect.AbstractCollect; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.HttpProtocol; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/httpsd/HttpsdImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/httpsd/HttpsdImpl.java index 9b7811f8eef..92d494ca7d8 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/httpsd/HttpsdImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/httpsd/HttpsdImpl.java @@ -30,8 +30,8 @@ import org.apache.hertzbeat.collector.collect.httpsd.discovery.DiscoveryClient; import org.apache.hertzbeat.collector.collect.httpsd.discovery.DiscoveryClientManagement; import org.apache.hertzbeat.collector.collect.httpsd.discovery.entity.ServerInfo; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.HttpsdProtocol; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/icmp/IcmpCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/icmp/IcmpCollectImpl.java index 0ab7ba5f5a8..7b31383f723 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/icmp/IcmpCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/icmp/IcmpCollectImpl.java @@ -22,8 +22,8 @@ import java.net.UnknownHostException; import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.collector.collect.AbstractCollect; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.IcmpProtocol; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/memcached/MemcachedCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/memcached/MemcachedCollectImpl.java index 09bcf059aa4..8af815fc99f 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/memcached/MemcachedCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/memcached/MemcachedCollectImpl.java @@ -32,8 +32,8 @@ import java.util.Objects; import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.collector.collect.AbstractCollect; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.MemcachedProtocol; @@ -48,7 +48,6 @@ public class MemcachedCollectImpl extends AbstractCollect { private static final String STATS = "stats"; private static final String STATS_SETTINGS = "stats settings"; - private static final String STATS_ITEMS = "stats items"; private static final String STATS_SIZES = "stats sizes"; private static final String STATS_END_RSP = "END"; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/mongodb/MongodbSingleCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/mongodb/MongodbSingleCollectImpl.java index 9471c1874e8..c5ec9913fcd 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/mongodb/MongodbSingleCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/mongodb/MongodbSingleCollectImpl.java @@ -34,8 +34,8 @@ import org.apache.hertzbeat.collector.collect.common.cache.CacheIdentifier; import org.apache.hertzbeat.collector.collect.common.cache.ConnectionCommonCache; import org.apache.hertzbeat.collector.collect.common.cache.MongodbConnect; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.MongodbProtocol; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/nebulagraph/NebulaGraphCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/nebulagraph/NebulaGraphCollectImpl.java index f61cccc8844..6874ee99843 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/nebulagraph/NebulaGraphCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/nebulagraph/NebulaGraphCollectImpl.java @@ -28,9 +28,9 @@ import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.collector.collect.AbstractCollect; import org.apache.hertzbeat.collector.collect.common.http.CommonHttpClient; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; import org.apache.hertzbeat.collector.util.CollectUtil; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.NebulaGraphProtocol; @@ -131,7 +131,6 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri } } - @Override public String supportProtocol() { return DispatchConstants.PROTOCOL_NEBULAGRAPH; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/nebulagraph/NgqlCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/nebulagraph/NgqlCollectImpl.java index fea98f30222..3b18aef89c6 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/nebulagraph/NgqlCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/nebulagraph/NgqlCollectImpl.java @@ -26,8 +26,8 @@ import java.util.stream.Stream; import org.apache.commons.lang3.StringUtils; import org.apache.hertzbeat.collector.collect.AbstractCollect; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.NgqlProtocol; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImpl.java index dfb4f606d0f..b016c75c037 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImpl.java @@ -37,8 +37,8 @@ import org.apache.hertzbeat.collector.collect.common.http.CommonHttpClient; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; import org.apache.hertzbeat.collector.util.CollectUtil; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; +import org.apache.hertzbeat.common.constants.NetworkConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.NginxProtocol; import org.apache.hertzbeat.common.entity.message.CollectRep; @@ -46,6 +46,7 @@ import org.apache.hertzbeat.common.util.IpDomainUtil; import org.apache.http.HttpHeaders; import org.apache.http.HttpHost; +import org.apache.http.HttpStatus; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpUriRequest; @@ -53,6 +54,7 @@ import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; +import org.springframework.http.MediaType; /** * nginx collect @@ -60,7 +62,6 @@ @Slf4j public class NginxCollectImpl extends AbstractCollect { - private static final int SUCCESS_CODE = 200; private static final String NGINX_STATUS_NAME = "nginx_status"; private static final String REQ_STATUS_NAME = "req_status"; private static final String AVAILABLE = "available"; @@ -97,9 +98,9 @@ public void collect(CollectRep.MetricsData.Builder builder, long monitorId, Stri try (CloseableHttpResponse response = CommonHttpClient.getHttpClient().execute(request, httpContext)){ // send an HTTP request and get the response data int statusCode = response.getStatusLine().getStatusCode(); - if (statusCode != SUCCESS_CODE) { + if (statusCode != HttpStatus.SC_OK) { builder.setCode(CollectRep.Code.FAIL); - builder.setMsg("StatusCode " + statusCode); + builder.setMsg(NetworkConstants.STATUS_CODE + statusCode); return; } String resp = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); @@ -145,21 +146,21 @@ private HttpUriRequest createHttpRequest(NginxProtocol nginxProtocol) { requestBuilder.setUri(host + ":" + portWithUri); } else { String ipAddressType = IpDomainUtil.checkIpAddressType(host); - String baseUri = CollectorConstants.IPV6.equals(ipAddressType) + String baseUri = NetworkConstants.IPV6.equals(ipAddressType) ? String.format("[%s]:%s", host, portWithUri) : String.format("%s:%s", host, portWithUri); boolean ssl = Boolean.parseBoolean(nginxProtocol.getSsl()); if (ssl){ - requestBuilder.setUri(CollectorConstants.HTTPS_HEADER + baseUri); + requestBuilder.setUri(NetworkConstants.HTTPS_HEADER + baseUri); } else { - requestBuilder.setUri(CollectorConstants.HTTP_HEADER + baseUri); + requestBuilder.setUri(NetworkConstants.HTTP_HEADER + baseUri); } } - requestBuilder.addHeader(HttpHeaders.CONNECTION, "keep-alive"); - requestBuilder.addHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36"); - requestBuilder.addHeader(HttpHeaders.ACCEPT, "text/plain"); + requestBuilder.addHeader(HttpHeaders.CONNECTION, NetworkConstants.KEEP_ALIVE); + requestBuilder.addHeader(HttpHeaders.USER_AGENT, NetworkConstants.USER_AGENT); + requestBuilder.addHeader(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN_VALUE); int timeout = Integer.parseInt(nginxProtocol.getTimeout()); if (timeout > 0) { @@ -197,7 +198,7 @@ private void parseNginxStatusResponse(CollectRep.MetricsData.Builder builder, St if (value != null) { valueRowBuilder.addColumns(String.valueOf(value)); } else { - if (CollectorConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) { + if (NetworkConstants.RESPONSE_TIME.equalsIgnoreCase(alias)) { valueRowBuilder.addColumns(responseTime.toString()); } else { valueRowBuilder.addColumns(CommonConstants.NULL_VALUE); @@ -235,7 +236,7 @@ private void parseReqStatusResponse(CollectRep.MetricsData.Builder builder, Stri for (ReqStatusResponse reqStatusResponse : reqStatusResponses) { CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder(); for (String alias : aliasFields) { - if (CollectorConstants.RESPONSE_TIME.equals(alias)) { + if (NetworkConstants.RESPONSE_TIME.equals(alias)) { valueRowBuilder.addColumns(String.valueOf(responseTime)); } else { try { diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/ntp/NtpCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/ntp/NtpCollectImpl.java index adc080cb73a..7f5ec63c563 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/ntp/NtpCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/ntp/NtpCollectImpl.java @@ -33,9 +33,9 @@ import org.apache.commons.net.ntp.TimeInfo; import org.apache.commons.net.ntp.TimeStamp; import org.apache.hertzbeat.collector.collect.AbstractCollect; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; import org.apache.hertzbeat.collector.util.CollectUtil; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.NtpProtocol; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/pop3/Pop3CollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/pop3/Pop3CollectImpl.java index 958170df8e4..c595a4f2e10 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/pop3/Pop3CollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/pop3/Pop3CollectImpl.java @@ -27,8 +27,8 @@ import org.apache.commons.net.pop3.POP3MessageInfo; import org.apache.commons.net.pop3.POP3SClient; import org.apache.hertzbeat.collector.collect.AbstractCollect; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.Pop3Protocol; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/prometheus/PrometheusAutoCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/prometheus/PrometheusAutoCollectImpl.java index d1dc2ad5a32..e1d7ab86bfb 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/prometheus/PrometheusAutoCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/prometheus/PrometheusAutoCollectImpl.java @@ -38,8 +38,9 @@ import org.apache.hertzbeat.collector.collect.prometheus.parser.TextParser; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; import org.apache.hertzbeat.collector.util.CollectUtil; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; +import org.apache.hertzbeat.common.constants.NetworkConstants; +import org.apache.hertzbeat.common.constants.SignConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.PrometheusProtocol; import org.apache.hertzbeat.common.entity.message.CollectRep; @@ -64,6 +65,7 @@ import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; +import org.springframework.http.MediaType; import org.springframework.util.StringUtils; /** @@ -95,7 +97,7 @@ public List collect(CollectRep.MetricsData.Builder build log.debug("http response status: {}", statusCode); if (!isSuccessInvoke) { builder.setCode(CollectRep.Code.FAIL); - builder.setMsg("StatusCode " + statusCode); + builder.setMsg(NetworkConstants.STATUS_CODE + SignConstants.BLANK + statusCode); return null; } // todo: The InputStream is directly converted to a String here @@ -252,8 +254,8 @@ public HttpUriRequest createHttpRequest(PrometheusProtocol protocol) { } // The default request header can be overridden if customized // keep-alive - requestBuilder.addHeader(HttpHeaders.CONNECTION, "keep-alive"); - requestBuilder.addHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36"); + requestBuilder.addHeader(HttpHeaders.CONNECTION, NetworkConstants.KEEP_ALIVE); + requestBuilder.addHeader(HttpHeaders.USER_AGENT, NetworkConstants.USER_AGENT); // headers The custom request header is overwritten here Map headers = protocol.getHeaders(); if (headers != null && !headers.isEmpty()) { @@ -265,7 +267,7 @@ public HttpUriRequest createHttpRequest(PrometheusProtocol protocol) { } } // add accept - requestBuilder.addHeader(HttpHeaders.ACCEPT, "*/*"); + requestBuilder.addHeader(HttpHeaders.ACCEPT, MediaType.TEXT_PLAIN_VALUE); if (protocol.getAuthorization() != null) { PrometheusProtocol.Authorization authorization = protocol.getAuthorization(); @@ -291,17 +293,17 @@ public HttpUriRequest createHttpRequest(PrometheusProtocol protocol) { String uri = CollectUtil.replaceUriSpecialChar(protocol.getPath()); if (IpDomainUtil.isHasSchema(protocol.getHost())) { - requestBuilder.setUri(protocol.getHost() + ":" + protocol.getPort() + uri); + requestBuilder.setUri(protocol.getHost() + SignConstants.DOUBLE_MARK + protocol.getPort() + uri); } else { String ipAddressType = IpDomainUtil.checkIpAddressType(protocol.getHost()); - String baseUri = CollectorConstants.IPV6.equals(ipAddressType) + String baseUri = NetworkConstants.IPV6.equals(ipAddressType) ? String.format("[%s]:%s%s", protocol.getHost(), protocol.getPort(), uri) : String.format("%s:%s%s", protocol.getHost(), protocol.getPort(), uri); boolean ssl = Boolean.parseBoolean(protocol.getSsl()); if (ssl) { - requestBuilder.setUri(CollectorConstants.HTTPS_HEADER + baseUri); + requestBuilder.setUri(NetworkConstants.HTTPS_HEADER + baseUri); } else { - requestBuilder.setUri(CollectorConstants.HTTP_HEADER + baseUri); + requestBuilder.setUri(NetworkConstants.HTTP_HEADER + baseUri); } } diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/push/PushCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/push/PushCollectImpl.java index 3757cf2d223..e4412a0daf1 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/push/PushCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/push/PushCollectImpl.java @@ -28,7 +28,8 @@ import org.apache.hertzbeat.collector.collect.common.http.CommonHttpClient; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; import org.apache.hertzbeat.collector.util.CollectUtil; -import org.apache.hertzbeat.common.constants.CollectorConstants; +import org.apache.hertzbeat.common.constants.NetworkConstants; +import org.apache.hertzbeat.common.constants.SignConstants; import org.apache.hertzbeat.common.entity.dto.Message; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.PushProtocol; @@ -46,6 +47,7 @@ import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; +import org.springframework.http.MediaType; /** * push style collect @@ -86,7 +88,7 @@ public void collect(CollectRep.MetricsData.Builder builder, int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != SUCCESS_CODE) { builder.setCode(CollectRep.Code.FAIL); - builder.setMsg("StatusCode " + statusCode); + builder.setMsg(NetworkConstants.STATUS_CODE + SignConstants.BLANK + statusCode); return; } String resp = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); @@ -124,19 +126,19 @@ private HttpUriRequest createHttpRequest(PushProtocol pushProtocol, Long monitor requestBuilder.setUri(pushProtocol.getHost() + ":" + pushProtocol.getPort() + uri); } else { String ipAddressType = IpDomainUtil.checkIpAddressType(pushProtocol.getHost()); - String baseUri = CollectorConstants.IPV6.equals(ipAddressType) + String baseUri = NetworkConstants.IPV6.equals(ipAddressType) ? String.format("[%s]:%s", pushProtocol.getHost(), pushProtocol.getPort() + uri) : String.format("%s:%s", pushProtocol.getHost(), pushProtocol.getPort() + uri); - requestBuilder.setUri(CollectorConstants.HTTP_HEADER + baseUri); + requestBuilder.setUri(NetworkConstants.HTTP_HEADER + baseUri); } - requestBuilder.addHeader(HttpHeaders.CONNECTION, "keep-alive"); - requestBuilder.addHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36"); + requestBuilder.addHeader(HttpHeaders.CONNECTION, NetworkConstants.KEEP_ALIVE); + requestBuilder.addHeader(HttpHeaders.USER_AGENT, NetworkConstants.USER_AGENT); requestBuilder.addParameter("id", String.valueOf(monitorId)); requestBuilder.addParameter("time", String.valueOf(startTime)); - requestBuilder.addHeader(HttpHeaders.ACCEPT, "application/json"); + requestBuilder.addHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE); //requestBuilder.setUri(pushProtocol.getUri()); diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/redfish/RedfishClient.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/redfish/RedfishClient.java index b813b6653c9..afc715ce4ec 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/redfish/RedfishClient.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/redfish/RedfishClient.java @@ -17,8 +17,10 @@ package org.apache.hertzbeat.collector.collect.redfish; +import java.nio.charset.StandardCharsets; import org.apache.hertzbeat.collector.collect.common.http.CommonHttpClient; -import org.apache.hertzbeat.common.constants.CollectorConstants; +import org.apache.hertzbeat.common.constants.NetworkConstants; +import org.apache.hertzbeat.common.constants.SignConstants; import org.apache.hertzbeat.common.entity.job.protocol.RedfishProtocol; import org.apache.hertzbeat.common.util.IpDomainUtil; import org.apache.http.HttpHeaders; @@ -30,6 +32,7 @@ import org.apache.http.client.methods.RequestBuilder; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.entity.StringEntity; +import org.springframework.http.MediaType; /** * redfish client impl @@ -66,20 +69,20 @@ public ConnectSession connect() throws Exception { requestBuilder.setUri(this.host + ":" + this.port + uri); } else { String ipAddressType = IpDomainUtil.checkIpAddressType(this.host); - String baseUri = CollectorConstants.IPV6.equals(ipAddressType) + String baseUri = NetworkConstants.IPV6.equals(ipAddressType) ? String.format("[%s]:%s", this.host, this.port + uri) : String.format("%s:%s", this.host, this.port + uri); - requestBuilder.setUri(CollectorConstants.HTTP_HEADER + baseUri); + requestBuilder.setUri(NetworkConstants.HTTP_HEADER + baseUri); } - requestBuilder.addHeader(HttpHeaders.CONNECTION, "Keep-Alive"); - requestBuilder.addHeader(HttpHeaders.CONTENT_TYPE, "application/json"); - requestBuilder.addHeader(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36"); - requestBuilder.addHeader(HttpHeaders.CONTENT_ENCODING, "UTF-8"); + requestBuilder.addHeader(HttpHeaders.CONNECTION, NetworkConstants.KEEP_ALIVE); + requestBuilder.addHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + requestBuilder.addHeader(HttpHeaders.USER_AGENT, NetworkConstants.USER_AGENT); + requestBuilder.addHeader(HttpHeaders.CONTENT_ENCODING, StandardCharsets.UTF_8 + ""); final String json = "{\"UserName\": \"" + this.username + "\", \"Password\": \"" + this.password + "\"}"; - StringEntity entity = new StringEntity(json, "UTF-8"); + StringEntity entity = new StringEntity(json, StandardCharsets.UTF_8); requestBuilder.setEntity(entity); if (this.timeout > 0) { @@ -97,10 +100,10 @@ public ConnectSession connect() throws Exception { try (CloseableHttpResponse response = CommonHttpClient.getHttpClient().execute(request, httpClientContext)) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_CREATED) { - throw new Exception("Http Status Code: " + statusCode); + throw new Exception(NetworkConstants.STATUS_CODE + SignConstants.BLANK + statusCode); } - String location = response.getFirstHeader("Location").getValue(); - String auth = response.getFirstHeader("X-Auth-Token").getValue(); + String location = response.getFirstHeader(NetworkConstants.LOCATION).getValue(); + String auth = response.getFirstHeader(NetworkConstants.X_AUTH_TOKEN).getValue(); session = new Session(auth, location, this.host, this.port); } catch (Exception e) { throw new Exception("Redfish session create error: " + e.getMessage()); diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/redfish/RedfishConnectSession.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/redfish/RedfishConnectSession.java index 9107c73312e..2abc3288e8c 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/redfish/RedfishConnectSession.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/redfish/RedfishConnectSession.java @@ -19,7 +19,8 @@ import java.nio.charset.StandardCharsets; import org.apache.hertzbeat.collector.collect.common.http.CommonHttpClient; -import org.apache.hertzbeat.common.constants.CollectorConstants; +import org.apache.hertzbeat.common.constants.NetworkConstants; +import org.apache.hertzbeat.common.constants.SignConstants; import org.apache.hertzbeat.common.util.IpDomainUtil; import org.apache.http.HttpStatus; import org.apache.http.client.methods.CloseableHttpResponse; @@ -51,12 +52,12 @@ public void close() throws Exception { this.active = false; String url = RedfishClient.REDFISH_SESSION_SERVICE + session.location(); HttpDelete httpDelete = new HttpDelete(url); - httpDelete.setHeader("X-Auth-Token", session.token()); - httpDelete.setHeader("Location", session.location()); + httpDelete.setHeader(NetworkConstants.X_AUTH_TOKEN, session.token()); + httpDelete.setHeader(NetworkConstants.LOCATION, session.location()); try (CloseableHttpResponse response = CommonHttpClient.getHttpClient().execute(httpDelete)) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { - throw new Exception("Http State code: " + statusCode); + throw new Exception(NetworkConstants.STATUS_CODE + SignConstants.BLANK + statusCode); } } catch (Exception e) { throw new Exception("Redfish session close error:" + e.getMessage()); @@ -75,18 +76,18 @@ public String getRedfishResource(String uri) throws Exception { url = this.session.host() + ":" + this.session.port() + uri; } else { String ipAddressType = IpDomainUtil.checkIpAddressType(this.session.host()); - String baseUri = CollectorConstants.IPV6.equals(ipAddressType) + String baseUri = NetworkConstants.IPV6.equals(ipAddressType) ? String.format("[%s]:%s", this.session.host(), this.session.port() + uri) : String.format("%s:%s", this.session.host(), this.session.port() + uri); - url = CollectorConstants.HTTPS_HEADER + baseUri; + url = NetworkConstants.HTTPS_HEADER + baseUri; } HttpGet httpGet = new HttpGet(url); - httpGet.setHeader("X-Auth-Token", session.token()); - httpGet.setHeader("Location", session.location()); + httpGet.setHeader(NetworkConstants.X_AUTH_TOKEN, session.token()); + httpGet.setHeader(NetworkConstants.LOCATION, session.location()); try (CloseableHttpResponse response = CommonHttpClient.getHttpClient().execute(httpGet)) { int statusCode = response.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { - throw new Exception("Http State code: " + statusCode); + throw new Exception(NetworkConstants.STATUS_CODE + SignConstants.BLANK + statusCode); } return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); } catch (Exception e) { diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/script/ScriptCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/script/ScriptCollectImpl.java index dd1c1e30a57..6ec696dd7fe 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/script/ScriptCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/script/ScriptCollectImpl.java @@ -30,8 +30,8 @@ import java.util.stream.Collectors; import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.collector.collect.AbstractCollect; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.ScriptProtocol; @@ -45,8 +45,6 @@ */ @Slf4j public class ScriptCollectImpl extends AbstractCollect { - public static final String WINDOWS_SCRIPT = "windows_script"; - public static final String LINUX_SCRIPT = "linux_script"; private static final String CMD = "cmd"; private static final String BASH = "bash"; private static final String POWERSHELL = "powershell"; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/smtp/SmtpCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/smtp/SmtpCollectImpl.java index 1631d5f9d9c..da285c0d947 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/smtp/SmtpCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/smtp/SmtpCollectImpl.java @@ -27,9 +27,9 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.net.smtp.SMTP; import org.apache.hertzbeat.collector.collect.AbstractCollect; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; import org.apache.hertzbeat.collector.util.CollectUtil; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.SmtpProtocol; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/snmp/SnmpCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/snmp/SnmpCollectImpl.java index 97cf4a5c8f7..361b762d648 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/snmp/SnmpCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/snmp/SnmpCollectImpl.java @@ -26,9 +26,9 @@ import java.util.concurrent.ExecutionException; import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.collector.collect.AbstractCollect; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; import org.apache.hertzbeat.collector.util.CollectUtil; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.SnmpProtocol; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/ssh/SshCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/ssh/SshCollectImpl.java index dfed8e7a6c3..7b9c0ec55ff 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/ssh/SshCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/ssh/SshCollectImpl.java @@ -40,10 +40,10 @@ import org.apache.hertzbeat.collector.collect.common.cache.SshConnect; import org.apache.hertzbeat.collector.collect.common.ssh.CommonSshBlacklist; import org.apache.hertzbeat.collector.collect.common.ssh.CommonSshClient; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; import org.apache.hertzbeat.collector.util.CollectUtil; import org.apache.hertzbeat.collector.util.PrivateKeyUtils; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.SshProtocol; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/telnet/TelnetCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/telnet/TelnetCollectImpl.java index dbbe8bcac58..7f2df24f401 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/telnet/TelnetCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/telnet/TelnetCollectImpl.java @@ -30,9 +30,9 @@ import org.apache.commons.lang3.StringUtils; import org.apache.commons.net.telnet.TelnetClient; import org.apache.hertzbeat.collector.collect.AbstractCollect; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; import org.apache.hertzbeat.collector.util.CollectUtil; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.TelnetProtocol; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/udp/UdpCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/udp/UdpCollectImpl.java index 21b47b595d7..4c340e2a10f 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/udp/UdpCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/udp/UdpCollectImpl.java @@ -26,9 +26,9 @@ import java.nio.charset.StandardCharsets; import lombok.extern.slf4j.Slf4j; import org.apache.hertzbeat.collector.collect.AbstractCollect; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; import org.apache.hertzbeat.collector.util.CollectUtil; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.UdpProtocol; diff --git a/collector/src/main/java/org/apache/hertzbeat/collector/collect/websocket/WebsocketCollectImpl.java b/collector/src/main/java/org/apache/hertzbeat/collector/collect/websocket/WebsocketCollectImpl.java index 112dd966ccd..4ffa0df5ad4 100644 --- a/collector/src/main/java/org/apache/hertzbeat/collector/collect/websocket/WebsocketCollectImpl.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/collect/websocket/WebsocketCollectImpl.java @@ -37,8 +37,8 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.hertzbeat.collector.collect.AbstractCollect; +import org.apache.hertzbeat.collector.constants.CollectorConstants; import org.apache.hertzbeat.collector.dispatch.DispatchConstants; -import org.apache.hertzbeat.common.constants.CollectorConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.entity.job.Metrics; import org.apache.hertzbeat.common.entity.job.protocol.WebsocketProtocol; diff --git a/common/src/main/java/org/apache/hertzbeat/common/constants/CollectorConstants.java b/collector/src/main/java/org/apache/hertzbeat/collector/constants/CollectorConstants.java similarity index 80% rename from common/src/main/java/org/apache/hertzbeat/common/constants/CollectorConstants.java rename to collector/src/main/java/org/apache/hertzbeat/collector/constants/CollectorConstants.java index ace1ea800bb..0dcfb201b0b 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/constants/CollectorConstants.java +++ b/collector/src/main/java/org/apache/hertzbeat/collector/constants/CollectorConstants.java @@ -15,36 +15,24 @@ * limitations under the License. */ -package org.apache.hertzbeat.common.constants; +package org.apache.hertzbeat.collector.constants; + +import org.apache.hertzbeat.common.constants.NetworkConstants; /** - * collector constant + * collector module constant. + * Extends {@link NetworkConstants} */ -public interface CollectorConstants { - String RESPONSE_TIME = "responseTime"; +public interface CollectorConstants extends NetworkConstants { String KEYWORD = "keyword"; - String STATUS_CODE = "statusCode"; - - String ERROR_MSG = "errorMsg"; - - String URL = "url"; - - String HTTP_HEADER = "http://"; - - String HTTPS_HEADER = "https://"; - /** * POSTGRESQL un reachable status code */ String POSTGRESQL_UN_REACHABLE_CODE = "08001"; - String IPV6 = "ipv6"; - - String IPV4 = "ipv4"; - /** * MongoDB Atlas model */ @@ -53,4 +41,12 @@ public interface CollectorConstants { String ZOOKEEPER_APP = "zookeeper"; String ZOOKEEPER_ENVI_HEAD = "Environment:"; -} + + String ERROR_MSG = "errorMsg"; + + + String RESPONSE_TIME = "responseTime"; + + String STATUS_CODE = "StatusCode"; + +} \ No newline at end of file diff --git a/collector/src/test/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImplTest.java b/collector/src/test/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImplTest.java index 6ca36483eb8..fb05011231b 100644 --- a/collector/src/test/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImplTest.java +++ b/collector/src/test/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImplTest.java @@ -307,7 +307,7 @@ public void testNginxStatusMatch() { 4 4 2 Reading: 0 Writing: 1 Waiting: 1"""; - // 使用正则表达式匹配并提取所需的键和对应的值 + // Use regular expressions to match and extract the required keys and corresponding values Pattern keyValuePattern = Pattern.compile("(\\w+): (\\d+)"); Matcher keyValueMatcher = keyValuePattern.matcher(status); @@ -318,7 +318,7 @@ public void testNginxStatusMatch() { System.out.println(key + ": " + value); } - // 使用正则表达式匹配并提取"accepts"、"handled"和"requests"的键和对应的值 + // Use regular expressions to match and extract the keys and corresponding values for "accepts", "handled", and "requests". Pattern valuesPattern = Pattern.compile("server\\s+(\\w+)\\s+(\\w+)\\s+(\\w+)"); Matcher valuesMatcher = valuesPattern.matcher(status); diff --git a/common/src/main/java/org/apache/hertzbeat/common/constants/NetworkConstants.java b/common/src/main/java/org/apache/hertzbeat/common/constants/NetworkConstants.java new file mode 100644 index 00000000000..17252765e91 --- /dev/null +++ b/common/src/main/java/org/apache/hertzbeat/common/constants/NetworkConstants.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hertzbeat.common.constants; + +/** + * Http Constants. + */ + +public interface NetworkConstants { + + String KEEP_ALIVE = "Keep-Alive"; + + String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36\")"; + + String IPV6 = "ipv6"; + + String IPV4 = "ipv4"; + + String ERROR_MSG = "errorMsg"; + + String URL = "url"; + + String HTTP_HEADER = "http://"; + + String HTTPS_HEADER = "https://"; + + String RESPONSE_TIME = "responseTime"; + + String STATUS_CODE = "StatusCode"; + + String X_AUTH_TOKEN = "X-Auth-Token"; + + String LOCATION = "Location"; + +} diff --git a/common/src/main/java/org/apache/hertzbeat/common/constants/SignConstants.java b/common/src/main/java/org/apache/hertzbeat/common/constants/SignConstants.java index f9dc8305ca5..1918ab45a16 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/constants/SignConstants.java +++ b/common/src/main/java/org/apache/hertzbeat/common/constants/SignConstants.java @@ -24,6 +24,8 @@ public interface SignConstants { String DOUBLE_MARK = ":"; + String SINGLE_MARK = ";"; + String WELL_NO = "#"; String LINE_FEED = "\n"; @@ -33,4 +35,6 @@ public interface SignConstants { String RIGHT_DASH = "/"; String COMMA = ","; + + String BLANK = " "; } diff --git a/common/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java b/common/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java index 4c4c1396bc4..8845fcfaf86 100644 --- a/common/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java +++ b/common/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java @@ -24,7 +24,7 @@ import java.util.Enumeration; import java.util.regex.Pattern; import lombok.extern.slf4j.Slf4j; -import org.apache.hertzbeat.common.constants.CollectorConstants; +import org.apache.hertzbeat.common.constants.NetworkConstants; import org.apache.http.conn.util.InetAddressUtils; import org.springframework.util.StringUtils; @@ -114,9 +114,9 @@ public static String getLocalhostIp() { */ public static String checkIpAddressType(String ipDomain){ if (StringUtils.hasText(ipDomain) && InetAddressUtils.isIPv6Address(ipDomain)) { - return CollectorConstants.IPV6; + return NetworkConstants.IPV6; } - return CollectorConstants.IPV4; + return NetworkConstants.IPV4; } /** diff --git a/common/src/test/java/org/apache/hertzbeat/common/util/FileUtilTest.java b/common/src/test/java/org/apache/hertzbeat/common/util/FileUtilTest.java index f6b4f26919f..a6a711bceda 100644 --- a/common/src/test/java/org/apache/hertzbeat/common/util/FileUtilTest.java +++ b/common/src/test/java/org/apache/hertzbeat/common/util/FileUtilTest.java @@ -20,6 +20,8 @@ import org.apache.hertzbeat.common.constants.ExportFileConstants; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; + +import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -30,7 +32,6 @@ class FileUtilTest { - private static final String JSON_TYPE = "application/json"; private static final String EXCEL_TYPE = "application/vnd.ms-excel"; private static final String YAML_TYPE = "application/x-yaml"; @@ -42,7 +43,7 @@ class FileUtilTest { @BeforeEach void setUp() { - jsonFile = new MockMultipartFile("file", "test.json", JSON_TYPE, "test content".getBytes()); + jsonFile = new MockMultipartFile("file", "test.json", MediaType.APPLICATION_JSON_VALUE, "test content".getBytes()); excelFile = new MockMultipartFile("file", "test.xlsx", EXCEL_TYPE, "test content".getBytes()); yamlFile = new MockMultipartFile("file", "test.yaml", YAML_TYPE, "test content".getBytes()); emptyFile = new MockMultipartFile("file", "", null, (byte[]) null); diff --git a/common/src/test/java/org/apache/hertzbeat/common/util/IpDomainUtilTest.java b/common/src/test/java/org/apache/hertzbeat/common/util/IpDomainUtilTest.java index a711d6515d8..ac7bee3bf86 100644 --- a/common/src/test/java/org/apache/hertzbeat/common/util/IpDomainUtilTest.java +++ b/common/src/test/java/org/apache/hertzbeat/common/util/IpDomainUtilTest.java @@ -23,8 +23,7 @@ import java.net.SocketException; import java.util.Collections; import java.util.Enumeration; - -import org.apache.hertzbeat.common.constants.CollectorConstants; +import org.apache.hertzbeat.common.constants.NetworkConstants; import org.junit.jupiter.api.Test; import org.mockito.MockedStatic; import org.mockito.Mockito; @@ -119,15 +118,15 @@ void testGetLocalhostIp() throws SocketException { @Test void testCheckIpAddressType() { - assertEquals(CollectorConstants.IPV4, IpDomainUtil.checkIpAddressType("192.168.1.1")); - assertEquals(CollectorConstants.IPV4, IpDomainUtil.checkIpAddressType("127.0.0.1")); + assertEquals(NetworkConstants.IPV4, IpDomainUtil.checkIpAddressType("192.168.1.1")); + assertEquals(NetworkConstants.IPV4, IpDomainUtil.checkIpAddressType("127.0.0.1")); - assertEquals(CollectorConstants.IPV6, IpDomainUtil.checkIpAddressType("2001:0db8:85a3:0000:0000:8a2e:0370:7334")); - assertEquals(CollectorConstants.IPV6, IpDomainUtil.checkIpAddressType("::1")); + assertEquals(NetworkConstants.IPV6, IpDomainUtil.checkIpAddressType("2001:0db8:85a3:0000:0000:8a2e:0370:7334")); + assertEquals(NetworkConstants.IPV6, IpDomainUtil.checkIpAddressType("::1")); - assertEquals(CollectorConstants.IPV4, IpDomainUtil.checkIpAddressType("")); - assertEquals(CollectorConstants.IPV4, IpDomainUtil.checkIpAddressType(null)); - assertEquals(CollectorConstants.IPV4, IpDomainUtil.checkIpAddressType("invalid-ip")); + assertEquals(NetworkConstants.IPV4, IpDomainUtil.checkIpAddressType("")); + assertEquals(NetworkConstants.IPV4, IpDomainUtil.checkIpAddressType(null)); + assertEquals(NetworkConstants.IPV4, IpDomainUtil.checkIpAddressType("invalid-ip")); } } diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WeChatAlertNotifyHandlerImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WeChatAlertNotifyHandlerImpl.java index 7132776b59f..27effc2fc4c 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WeChatAlertNotifyHandlerImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/component/alerter/impl/WeChatAlertNotifyHandlerImpl.java @@ -26,8 +26,10 @@ import org.apache.hertzbeat.common.entity.alerter.Alert; import org.apache.hertzbeat.common.entity.manager.NoticeReceiver; import org.apache.hertzbeat.common.entity.manager.NoticeTemplate; +import org.apache.http.HttpHeaders; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.MediaType; /** * Send alarm information through WeChat @@ -95,7 +97,7 @@ private void sendMessage(String accessToken, String messageContent) throws Excep HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(new URI(SEND_MESSAGE_URL + accessToken)) - .header("Content-Type", "application/json") + .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) .POST(HttpRequest.BodyPublishers.ofString(messageContent)) .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MonitorServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MonitorServiceImpl.java index fb62c740c38..8763db63d26 100644 --- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MonitorServiceImpl.java +++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/MonitorServiceImpl.java @@ -41,6 +41,8 @@ import org.apache.hertzbeat.collector.dispatch.DispatchConstants; import org.apache.hertzbeat.common.constants.CommonConstants; import org.apache.hertzbeat.common.constants.ExportFileConstants; +import org.apache.hertzbeat.common.constants.NetworkConstants; +import org.apache.hertzbeat.common.constants.SignConstants; import org.apache.hertzbeat.common.entity.job.Configmap; import org.apache.hertzbeat.common.entity.job.Job; import org.apache.hertzbeat.common.entity.job.Metrics; @@ -81,6 +83,7 @@ import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; @@ -96,10 +99,6 @@ public class MonitorServiceImpl implements MonitorService { private static final Long MONITOR_ID_TMP = 1000000000L; - public static final String HTTP = "http://"; - - public static final String HTTPS = "https://"; - public static final String BLANK = ""; public static final String PATTERN_HTTP = "(?i)http://"; public static final String PATTERN_HTTPS = "(?i)https://"; @@ -107,6 +106,8 @@ public class MonitorServiceImpl implements MonitorService { private static final int TAG_LENGTH = 2; + private static final String CONTENT_VALUE = MediaType.APPLICATION_OCTET_STREAM_VALUE + SignConstants.SINGLE_MARK + "charset=" + StandardCharsets.UTF_8; + @Autowired private AppService appService; @@ -303,10 +304,10 @@ public void export(List ids, String type, HttpServletResponse res) throws throw new IllegalArgumentException("not support export type: " + type); } var fileName = imExportService.getFileName(); - res.setHeader("content-type", "application/octet-stream;charset=UTF-8"); - res.setContentType("application/octet-stream;charset=UTF-8"); + res.setHeader(HttpHeaders.CONTENT_DISPOSITION, CONTENT_VALUE); + res.setContentType(CONTENT_VALUE); res.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8)); - res.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); + res.setHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION); imExportService.exportConfig(res.getOutputStream(), ids); } @@ -412,11 +413,11 @@ public void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgu break; case "host": String hostValue = param.getParamValue(); - if (hostValue.toLowerCase().contains(HTTP)) { - hostValue = hostValue.replaceAll(PATTERN_HTTP, BLANK); + if (hostValue.toLowerCase().contains(NetworkConstants.HTTP_HEADER)) { + hostValue = hostValue.replaceAll(PATTERN_HTTP, SignConstants.BLANK); } - if (hostValue.toLowerCase().contains(HTTPS)) { - hostValue = hostValue.replace(PATTERN_HTTPS, BLANK); + if (hostValue.toLowerCase().contains(NetworkConstants.HTTPS_HEADER)) { + hostValue = hostValue.replace(PATTERN_HTTPS, SignConstants.BLANK); } if (!IpDomainUtil.validateIpDomain(hostValue)) { throw new IllegalArgumentException("Params field " + field + " value "