This is an automated email from the ASF dual-hosted git repository.

zhaoqingran pushed a commit to branch log
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git

commit bccf821adfc442ff8e181e19efca7c75873f8627
Author: zqr10159 <[email protected]>
AuthorDate: Sun Apr 28 01:15:35 2024 +0800

    [feature]Support Log monitor.
---
 collector/pom.xml                                  |  6 ++
 .../collect/log/impl/FileLogCollectImpl.java       | 65 ++++++++++++++++++++++
 .../collect/log/impl/SshLogCollectImpl.java        | 48 ++++++++++++++++
 .../collector/collect/log/model/FileLog.java       | 40 +++++++++++++
 .../hertzbeat/collector/collect/log/model/Log.java | 41 ++++++++++++++
 .../collector/dispatch/DispatchConstants.java      |  8 +++
 .../hertzbeat/common/entity/job/Metrics.java       |  5 ++
 .../entity/job/protocol/log/FileLogProtocol.java   | 42 ++++++++++++++
 .../entity/job/protocol/log/LogProtocol.java       | 44 +++++++++++++++
 9 files changed, 299 insertions(+)

diff --git a/collector/pom.xml b/collector/pom.xml
index f4887a954..9c2a91356 100644
--- a/collector/pom.xml
+++ b/collector/pom.xml
@@ -193,6 +193,12 @@
             <artifactId>nacos-client</artifactId>
             <version>2.2.1</version>
         </dependency>
+        <!-- commons-io -->
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.16.1</version>
+        </dependency>
     </dependencies>
     
     <profiles>
diff --git 
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/log/impl/FileLogCollectImpl.java
 
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/log/impl/FileLogCollectImpl.java
new file mode 100644
index 000000000..dbc2a27e1
--- /dev/null
+++ 
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/log/impl/FileLogCollectImpl.java
@@ -0,0 +1,65 @@
+/*
+ * 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.collector.collect.log.impl;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.LineIterator;
+import org.apache.hertzbeat.collector.collect.AbstractCollect;
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
+
+import java.io.File;
+import java.io.IOException;
+
+@Slf4j
+public class FileLogCollectImpl extends AbstractCollect {
+    /**
+     * Real acquisition implementation interface
+     *
+     * @param builder   response builder
+     * @param monitorId monitor id
+     * @param app       monitor type
+     * @param metrics   metric configuration
+     */
+    @Override
+    public void collect(CollectRep.MetricsData.Builder builder, long 
monitorId, String app, Metrics metrics) {
+        File file = new File("");
+
+        try (LineIterator it = FileUtils.lineIterator(file, "UTF-8")) {
+            while (it.hasNext()) {
+                String line = it.nextLine();
+                // 处理每一行数据
+                System.out.println(line);
+            }
+        } catch (IOException e) {
+            log.error("Error reading file:{}", e.getMessage());
+        }
+    }
+
+    /**
+     * the protocol this collect instance support
+     *
+     * @return protocol str
+     */
+    @Override
+    public String supportProtocol() {
+        return DispatchConstants.PROTOCOL_FILE_LOG;
+    }
+}
diff --git 
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/log/impl/SshLogCollectImpl.java
 
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/log/impl/SshLogCollectImpl.java
new file mode 100644
index 000000000..0a8a51a79
--- /dev/null
+++ 
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/log/impl/SshLogCollectImpl.java
@@ -0,0 +1,48 @@
+/*
+ * 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.collector.collect.log.impl;
+
+import org.apache.hertzbeat.collector.collect.AbstractCollect;
+import org.apache.hertzbeat.collector.dispatch.DispatchConstants;
+import org.apache.hertzbeat.common.entity.job.Metrics;
+import org.apache.hertzbeat.common.entity.message.CollectRep;
+
+public class SshLogCollectImpl extends AbstractCollect {
+    /**
+     * Real acquisition implementation interface
+     *
+     * @param builder   response builder
+     * @param monitorId monitor id
+     * @param app       monitor type
+     * @param metrics   metric configuration
+     */
+    @Override
+    public void collect(CollectRep.MetricsData.Builder builder, long 
monitorId, String app, Metrics metrics) {
+
+    }
+
+    /**
+     * the protocol this collect instance support
+     *
+     * @return protocol str
+     */
+    @Override
+    public String supportProtocol() {
+        return DispatchConstants.PROTOCOL_SSH_LOG;
+    }
+}
diff --git 
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/log/model/FileLog.java
 
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/log/model/FileLog.java
new file mode 100644
index 000000000..911fc8ba9
--- /dev/null
+++ 
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/log/model/FileLog.java
@@ -0,0 +1,40 @@
+/*
+ * 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.collector.collect.log.model;
+
+import lombok.*;
+
+@Getter
+@Setter
+@ToString
+public class FileLog extends Log{
+    /**
+     * File name
+     */
+    private String fileName;
+
+    /**
+     * Log File create time
+     */
+    private Long logTime;
+
+    /**
+     * Offset in the log file
+     */
+    private Long offset;
+}
diff --git 
a/collector/src/main/java/org/apache/hertzbeat/collector/collect/log/model/Log.java
 
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/log/model/Log.java
new file mode 100644
index 000000000..4436221ef
--- /dev/null
+++ 
b/collector/src/main/java/org/apache/hertzbeat/collector/collect/log/model/Log.java
@@ -0,0 +1,41 @@
+/*
+ * 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.collector.collect.log.model;
+
+import lombok.*;
+
+@Getter
+@Setter
+@EqualsAndHashCode
+@ToString
+public class Log {
+    /**
+     * Time when an event occurred
+     */
+    private Long eventTime;
+
+    /**
+     * Log Collect time
+     */
+    private Long collectTime;
+
+    /**
+     * Log content
+     */
+    private String content;
+}
diff --git 
a/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/DispatchConstants.java
 
b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/DispatchConstants.java
index 81357b569..2e7d36792 100644
--- 
a/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/DispatchConstants.java
+++ 
b/collector/src/main/java/org/apache/hertzbeat/collector/dispatch/DispatchConstants.java
@@ -119,6 +119,14 @@ public interface DispatchConstants {
      * protocol http_sd
      */
     String PROTOCOL_HTTP_SD = "httpsd";
+    /**
+     * protocol ssh log
+     */
+    String PROTOCOL_SSH_LOG = "sshLog";
+    /**
+     * protocol file log
+     */
+    String PROTOCOL_FILE_LOG = "fileLog";
 
     // Protocol type related - end
     // 协议类型相关 - end //
diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/entity/job/Metrics.java 
b/common/src/main/java/org/apache/hertzbeat/common/entity/job/Metrics.java
index 6bb96574e..edbaab62d 100644
--- a/common/src/main/java/org/apache/hertzbeat/common/entity/job/Metrics.java
+++ b/common/src/main/java/org/apache/hertzbeat/common/entity/job/Metrics.java
@@ -51,6 +51,7 @@ import 
org.apache.hertzbeat.common.entity.job.protocol.SshProtocol;
 import org.apache.hertzbeat.common.entity.job.protocol.TelnetProtocol;
 import org.apache.hertzbeat.common.entity.job.protocol.UdpProtocol;
 import org.apache.hertzbeat.common.entity.job.protocol.WebsocketProtocol;
+import org.apache.hertzbeat.common.entity.job.protocol.log.LogProtocol;
 import org.apache.hertzbeat.common.entity.message.CollectRep;
 
 /**
@@ -206,6 +207,10 @@ public class Metrics {
      * Monitoring configuration information using the public http_sd protocol
      */
     private HttpsdProtocol httpsd;
+    /**
+     * Monitoring configuration information using the public log protocol
+     */
+    private LogProtocol log;
 
     /**
      * collector use - Temporarily store subTask metrics response data
diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/log/FileLogProtocol.java
 
b/common/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/log/FileLogProtocol.java
new file mode 100644
index 000000000..c6ece35b0
--- /dev/null
+++ 
b/common/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/log/FileLogProtocol.java
@@ -0,0 +1,42 @@
+/*
+ * 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.entity.job.protocol.log;
+
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+@Getter
+@Setter
+@ToString
+public class FileLogProtocol extends LogProtocol {
+    /**
+     * File name
+     */
+    private String fileName;
+
+    /**
+     * Log File create time
+     */
+    private Long logTime;
+
+    /**
+     * Offset in the log file
+     */
+    private Long offset;
+}
diff --git 
a/common/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/log/LogProtocol.java
 
b/common/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/log/LogProtocol.java
new file mode 100644
index 000000000..82c794994
--- /dev/null
+++ 
b/common/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/log/LogProtocol.java
@@ -0,0 +1,44 @@
+/*
+ * 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.entity.job.protocol.log;
+
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.Setter;
+import lombok.ToString;
+
+@Getter
+@Setter
+@EqualsAndHashCode
+@ToString
+public class LogProtocol {
+    /**
+     * Time when an event occurred
+     */
+    private Long eventTime;
+
+    /**
+     * Log Collect time
+     */
+    private Long collectTime;
+
+    /**
+     * Log content
+     */
+    private String content;
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to