refactor(agent): output 命令合并端口发现——一行 ports.discovery[*] 输出 LLD JSON

- 去掉 discover 子命令,全部合并到 output
- 新增 ports.discovery[*] 行(Zabbix LLD JSON)
- 新增 system.hostname(同 agent.hostname)
- 一次采集,全部输出
This commit is contained in:
attacker committed 2026-06-24 14:14:06 +00:00
1 parent 2c170589c6
commit c515f6ed03
1 file changed
+16 -27
+16 -27
View File
@@ -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")