From c515f6ed03eba6e25d8b3ca61145ad60bf0bdebf Mon Sep 17 00:00:00 2001 From: attacker Date: Wed, 24 Jun 2026 14:14:06 +0000 Subject: [PATCH] =?UTF-8?q?refactor(agent):=20output=20=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E5=90=88=E5=B9=B6=E7=AB=AF=E5=8F=A3=E5=8F=91=E7=8E=B0=E2=80=94?= =?UTF-8?q?=E2=80=94=E4=B8=80=E8=A1=8C=20ports.discovery[*]=20=E8=BE=93?= =?UTF-8?q?=E5=87=BA=20LLD=20JSON?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 去掉 discover 子命令,全部合并到 output - 新增 ports.discovery[*] 行(Zabbix LLD JSON) - 新增 system.hostname(同 agent.hostname) - 一次采集,全部输出 --- agent/main.go | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/agent/main.go b/agent/main.go index 984f355..c4d896b 100644 --- a/agent/main.go +++ b/agent/main.go @@ -82,9 +82,6 @@ func main() { case "output": runOutput(cfg) return - case "discover": - runDiscover() - return } // ── 正常采集上报模式 ── @@ -394,6 +391,7 @@ func runOutput(cfg Config) { // Agent 身份 ps("agent.hostname", report.Hostname) + ps("system.hostname", report.Hostname) ps("agent.version", report.AgentVersion) ps("agent.ip", report.IP) @@ -439,6 +437,21 @@ func runOutput(cfg Config) { } pu("net.listen.count", len(report.Ports)) + // 端口发现 Zabbix LLD JSON(合并为一行) + if len(report.Ports) > 0 { + type lldItem struct { + PName string `json:"{#PNAME}"` + PPort string `json:"{#PPORT}"` + } + items := make([]lldItem, 0, len(report.Ports)) + for _, pt := range report.Ports { + items = append(items, lldItem{PName: pt.Process, PPort: strconv.Itoa(pt.Port)}) + } + if jsonBytes, err := json.Marshal(map[string]interface{}{"data": items}); err == nil { + ps("ports.discovery[*]", string(jsonBytes)) + } + } + // 网络 TCP pu("net.tcp.established", report.Network.TCPEstablished) pu("net.tcp.time_wait", report.Network.TCPTimeWait) @@ -489,30 +502,6 @@ func runOutput(cfg Config) { pu64("agent.heartbeat.report_seq", int64(report.Heartbeat.ReportSeq)) } -// ── runDiscover: Zabbix LLD 端口发现 ── - -func runDiscover() { - port := collector.NewPortCollector() - ports, err := port.Collect() - if err != nil { - fmt.Println(`{"data":[]}`) - return - } - type lldItem struct { - PName string `json:"{#PNAME}"` - PPort string `json:"{#PPORT}"` - } - items := make([]lldItem, 0, len(ports)) - for _, p := range ports { - items = append(items, lldItem{ - PName: p.Process, - PPort: strconv.Itoa(p.Port), - }) - } - out, _ := json.Marshal(map[string]interface{}{"data": items}) - fmt.Println(string(out)) -} - // detectLocalIP 探测本机主 IP func detectLocalIP() string { conn, err := net.Dial("udp", "8.8.8.8:53")