From 4d31312f8c305fbda51123df33262f1f37fe8c4e Mon Sep 17 00:00:00 2001 From: Carpe-Wang Date: Fri, 23 Aug 2024 20:55:21 -0400 Subject: [PATCH 1/2] refactor: parse response by xml path in http collect --- .../collect/http/HttpCollectImpl.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) 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 99dfe3f9b94..c8695cf10cf 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 @@ -326,8 +326,38 @@ private void parseResponseBySiteMap(String resp, List aliasFields, private void parseResponseByXmlPath(String resp, List aliasFields, HttpProtocol http, CollectRep.MetricsData.Builder builder) { + try { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builderFactory = factory.newDocumentBuilder(); + + Document document = builderFactory.parse(new ByteArrayInputStream(resp.getBytes(StandardCharsets.UTF_8))); + document.getDocumentElement().normalize(); + + for (String alias : aliasFields) { + NodeList nodeList = document.getElementsByTagName(alias); + if (nodeList.getLength() > 0) { + Node node = nodeList.item(0); + String value = node.getTextContent(); + + CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder(); + valueRowBuilder.addColumns(value); + builder.addValues(valueRowBuilder.build()); + } else { + CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder(); + valueRowBuilder.addColumns(CommonConstants.NULL_VALUE); + builder.addValues(valueRowBuilder.build()); + } + } + + } catch (Exception e) { + String errorMsg = "Error parsing XML response: " + e.getMessage(); + log.error(errorMsg, e); + builder.setCode(CollectRep.Code.FAIL); + builder.setMsg(errorMsg); + } } + private void parseResponseByJsonPath(String resp, List aliasFields, HttpProtocol http, CollectRep.MetricsData.Builder builder, Long responseTime) { List results = JsonPathParser.parseContentWithJsonPath(resp, http.getParseScript()); From 8e36967d7d2c61ebc1e0e03c3f97d588ce62a20c Mon Sep 17 00:00:00 2001 From: Carpe-Wang Date: Sat, 24 Aug 2024 09:47:44 -0400 Subject: [PATCH 2/2] refactor: parse response by xml path in http collect --- .../collect/http/HttpCollectImpl.java | 1 - .../main/resources/define/app-springboot3.yml | 50 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) 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 c8695cf10cf..5f094713b6b 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 @@ -357,7 +357,6 @@ private void parseResponseByXmlPath(String resp, List aliasFields, HttpP } } - private void parseResponseByJsonPath(String resp, List aliasFields, HttpProtocol http, CollectRep.MetricsData.Builder builder, Long responseTime) { List results = JsonPathParser.parseContentWithJsonPath(resp, http.getParseScript()); diff --git a/manager/src/main/resources/define/app-springboot3.yml b/manager/src/main/resources/define/app-springboot3.yml index 90ffe25f426..6cd1d7df0c5 100644 --- a/manager/src/main/resources/define/app-springboot3.yml +++ b/manager/src/main/resources/define/app-springboot3.yml @@ -172,6 +172,56 @@ metrics: parseType: jsonPath parseScript: '$.availableTags[?(@.tag == "state")].values[*]' + - name: status + i18n: + zh-CN: 状态 + en-US: Status + priority: 6 + fields: + - field: activeThreads + i18n: + zh-CN: 活跃线程数 + en-US: Active Threads + type: 0 + - field: inactiveThreads + i18n: + zh-CN: 非活跃线程数 + en-US: Inactive Threads + type: 0 + - field: usedMemory + i18n: + zh-CN: 已用内存 + en-US: Used Memory + type: 0 + unit: MB + - field: totalMemory + i18n: + zh-CN: 总内存 + en-US: Total Memory + type: 0 + unit: MB + protocol: http + http: + host: ^_^host^_^ + port: ^_^port^_^ + url: ^_^base_path^_^/metrics/status.xml + method: GET + ssl: ^_^ssl^_^ + authorization: + type: Basic Auth + basicAuthUsername: ^_^username^_^ + basicAuthPassword: ^_^password^_^ + parseType: xmlPath + parseScript: + - alias: activeThreads + path: /status/threads/active + - alias: inactiveThreads + path: /status/threads/inactive + - alias: usedMemory + path: /status/memory/used + - alias: totalMemory + path: /status/memory/total + - name: threads i18n: zh-CN: 线程