added agent-events backend (#20012)
Costa Tsaousis committed
Mar 31, 2025 at 16:42 UTC
c1277e9689035293312b53f9789832b41e253f25
5 files changed
+108
packaging/tools/agent-events/.gitignore
new
+1
@@ -0,0 +1 @@
1
+server
packaging/tools/agent-events/agent-events.service
new
+16
@@ -0,0 +1,16 @@
1
+[Unit]
2
+Description=Netdata Agent Events
3
+After=network.target
4
+
5
+[Service]
6
+Type=simple
7
+User=nobody
8
+Group=nogroup
9
+LogNamespace=agent-events
10
+ExecStart=/opt/agent-events/run.sh
11
+Restart=always
12
+RestartSec=3
13
+StartLimitInterval=0
14
+
15
+[Install]
16
+WantedBy=multi-user.target
packaging/tools/agent-events/build.sh
new
+3
@@ -0,0 +1,3 @@
1
+#!/bin/sh
2
+
3
+go build server.go
packaging/tools/agent-events/run.sh
new
+31
@@ -0,0 +1,31 @@
1
+#!/usr/bin/env bash
2
+
3
+stdbuf -oL /opt/agent-events/server --port=30001 2>/dev/null \
4
+ | stdbuf -oL log2journal json \
5
+ --prefix 'AE_' \
6
+ --inject 'SYSLOG_IDENTIFIER=agent-events' \
7
+ --rename AE_AGENT_PROFILE_0=AE_AGENT_ND_PROFILE_0 \
8
+ --rename AE_AGENT_PROFILE_1=AE_AGENT_ND_PROFILE_1 \
9
+ --rename AE_AGENT_PROFILE_2=AE_AGENT_ND_PROFILE_2 \
10
+ --rename AE_STATUS=AE_AGENT_STATUS \
11
+ --rename AE_STATUS=AE_AGENT_ND_STATUS \
12
+ --rename AE_AGENT_EXIT_REASON_0=AE_AGENT_ND_EXIT_REASON_0 \
13
+ --rename AE_AGENT_EXIT_REASON_1=AE_AGENT_ND_EXIT_REASON_1 \
14
+ --rename AE_AGENT_EXIT_REASON_2=AE_AGENT_ND_EXIT_REASON_2 \
15
+ --rename AE_AGENT_EXIT_REASON_3=AE_AGENT_ND_EXIT_REASON_3 \
16
+ --rename AE_AGENT_NODE_ID=AE_AGENT_ND_NODE_ID \
17
+ --rename AE_AGENT_CLAIM_ID=AE_AGENT_ND_CLAIM_ID \
18
+ --rename AE_AGENT_INSTALL_TYPE=AE_AGENT_ND_INSTALL_TYPE \
19
+ --rename AE_AGENT_RESTARTS=AE_AGENT_ND_RESTARTS \
20
+ --rename AE_AGENT_DB_MODE=AE_AGENT_ND_DB_MODE \
21
+ --rename AE_AGENT_DB_TIERS=AE_AGENT_ND_DB_TIERS \
22
+ --rename AE_AGENT_KUBERNETES=AE_AGENT_ND_KUBERNETES \
23
+ --rename AE_AGENT_SENTRY_AVAILABLE=AE_AGENT_ND_SENTRY_AVAILABLE \
24
+ --rename AE_AGENT_SENTRY_AVAILABLE=AE_AGENT_ND_SENTRY \
25
+ --rename PRIORITY=AE_PRIORITY \
26
+ --rename MESSAGE=AE_MESSAGE \
27
+ --rewrite 'AE_FATAL_LINE=/0//' \
28
+ --rewrite 'AE_FATAL_THREAD_ID=/0//' \
29
+ --rewrite 'AE_OS_PLATFORM=/unknown/${AE_OS_FAMILY}/' \
30
+ --rewrite 'MESSAGE=/.*/${MESSAGE}\n${AE_FATAL_MESSAGE}/' \
31
+ | systemd-cat-native --newline=\\n
packaging/tools/agent-events/server.go
new
+57
@@ -0,0 +1,57 @@
1
+package main
2
+
3
+import (
4
+ "flag"
5
+ "fmt"
6
+ "io"
7
+ "log"
8
+ "net/http"
9
+ "os"
10
+ "strings"
11
+)
12
+
13
+func handler(w http.ResponseWriter, r *http.Request) {
14
+ if r.Method != http.MethodPost {
15
+ http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
16
+ log.Printf("Method not allowed: %s\n", r.Method)
17
+ return
18
+ }
19
+
20
+ // Read the entire request body.
21
+ body, err := io.ReadAll(r.Body)
22
+ if err != nil {
23
+ http.Error(w, "Error reading request", http.StatusInternalServerError)
24
+ log.Printf("Error reading request: %v\n", err)
25
+ return
26
+ }
27
+ defer r.Body.Close()
28
+
29
+ // Remove all newline characters.
30
+ cleaned := strings.ReplaceAll(string(body), "\n", "")
31
+ cleaned = strings.ReplaceAll(cleaned, "\r", "")
32
+
33
+ // Write to stdout with a single newline at the end.
34
+ fmt.Println(cleaned)
35
+
36
+ // Respond with OK.
37
+ if _, err := w.Write([]byte("OK")); err != nil {
38
+ log.Printf("Error writing response: %v\n", err)
39
+ return
40
+ }
41
+}
42
+
43
+func main() {
44
+ // Configure logging to write to stderr.
45
+ log.SetOutput(os.Stderr)
46
+ log.SetFlags(log.LstdFlags | log.Lshortfile)
47
+
48
+ // Parse the port from the command line.
49
+ port := flag.Int("port", 8080, "Port to listen on")
50
+ flag.Parse()
51
+
52
+ http.HandleFunc("/", handler)
53
+ log.Printf("Server listening on port %d\n", *port)
54
+ if err := http.ListenAndServe(fmt.Sprintf(":%d", *port), nil); err != nil {
55
+ log.Fatalf("Server failed: %v\n", err)
56
+ }
57
+}