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")