From 2a22781b46957eeccd5ff9dfa4942fcc2ed6cae0 Mon Sep 17 00:00:00 2001 From: wangjian Date: Thu, 18 May 2023 10:57:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/server.go | 5 +++-- config/config.go | 14 ++++++++------ config/config.yaml | 6 ++++-- go.mod | 35 ++++++++++++++++++----------------- internal/monitor/monitor.go | 10 +++++++--- model/node.go | 3 +++ 6 files changed, 43 insertions(+), 30 deletions(-) diff --git a/cmd/server.go b/cmd/server.go index 957d3c7..6e2aff8 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -65,7 +65,7 @@ func NewStartCmd() *cobra.Command { err = ap.Connect() must(err) ap.SetDataTag(18) - node := monitor.GetHost() + node := monitor.GetHost(cfg) byteNode := node.ToByte() _ = generateAndSendData(ap, byteNode) @@ -81,7 +81,7 @@ func NewStartCmd() *cobra.Command { _ = generateAndSendData(ap, stat) }() case <-nodeTicker.C: - nodeInfo := monitor.GetHost().ToByte() + nodeInfo := monitor.GetHost(cfg).ToByte() go func() { _ = generateAndSendData(ap, nodeInfo) }() @@ -102,6 +102,7 @@ func NewStartCmd() *cobra.Command { } func generateAndSendData(stream hpds_node.AccessPoint, data []byte) error { + logger.With(zap.String("agent", "发送数据")).Info("数据报文", zap.ByteString("原文", data)) _, err := stream.Write(data) if err != nil { return err diff --git a/config/config.go b/config/config.go index 31c201d..8414b6f 100644 --- a/config/config.go +++ b/config/config.go @@ -8,12 +8,14 @@ import ( ) type AgentConfig struct { - Name string `yaml:"name,omitempty"` - Mode string `yaml:"mode,omitempty"` - Delay int `yaml:"delay"` - Logging LogOptions `yaml:"logging"` - Node HpdsNode `yaml:"node,omitempty"` - Funcs []FuncConfig `yaml:"functions,omitempty"` + Name string `yaml:"name,omitempty"` + Mode string `yaml:"mode,omitempty"` + Point string `yaml:"point"` + NodeType int `yaml:"nodeType"` + Delay int `yaml:"delay"` + Logging LogOptions `yaml:"logging"` + Node HpdsNode `yaml:"node,omitempty"` + Funcs []FuncConfig `yaml:"functions,omitempty"` } type FuncConfig struct { diff --git a/config/config.yaml b/config/config.yaml index 72cdfba..92bb6c2 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -1,9 +1,11 @@ name: capture-agent mode: dev +point: 开发机 +nodeType: 1 delay: 15 node: - host: 114.55.236.153 - port: 9188 + host: 127.0.0.1 + port: 27188 token: 06d36c6f5705507dae778fdce90d0767 logging: path: ./logs diff --git a/go.mod b/go.mod index cff24ea..78bffab 100644 --- a/go.mod +++ b/go.mod @@ -4,38 +4,35 @@ go 1.19 require ( git.hpds.cc/Component/logging v0.0.0-20230106105738-e378e873921b - git.hpds.cc/pavement/hpds_node v0.0.0-20221023053316-37f7ba99eab3 + git.hpds.cc/pavement/hpds_node v0.0.0-20230405153516-9403c4d01e12 github.com/Erope/goss v0.0.0-20211230093305-df3c03fd1ed4 - github.com/robfig/cron/v3 v3.0.1 github.com/shirou/gopsutil/v3 v3.23.1 github.com/spf13/cobra v1.6.1 github.com/spf13/viper v1.15.0 - github.com/stretchr/testify v1.8.1 + go.uber.org/zap v1.23.0 ) require ( git.hpds.cc/Component/mq_coder v0.0.0-20221010064749-174ae7ae3340 // indirect - git.hpds.cc/Component/network v0.0.0-20221012021659-2433c68452d5 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect + git.hpds.cc/Component/network v0.0.0-20230405135741-a4ea724bab76 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect github.com/golang/mock v1.6.0 // indirect + github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.0.1 // indirect - github.com/lucas-clemente/quic-go v0.29.1 // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect github.com/magiconair/properties v1.8.7 // indirect - github.com/marten-seemann/qtls-go1-18 v0.1.2 // indirect - github.com/marten-seemann/qtls-go1-19 v0.1.0 // indirect github.com/matoous/go-nanoid/v2 v2.0.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect - github.com/nxadm/tail v1.4.8 // indirect - github.com/onsi/ginkgo v1.16.4 // indirect + github.com/onsi/ginkgo/v2 v2.2.0 // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/pkg/errors v0.9.1 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect + github.com/quic-go/qtls-go1-19 v0.2.1 // indirect + github.com/quic-go/qtls-go1-20 v0.1.1 // indirect + github.com/quic-go/quic-go v0.33.0 // indirect github.com/spf13/afero v1.9.3 // indirect github.com/spf13/cast v1.5.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect @@ -46,16 +43,20 @@ require ( github.com/yusufpapurcu/wmi v1.2.2 // indirect go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.8.0 // indirect - go.uber.org/zap v1.23.0 // indirect - golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect - golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect - golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect + golang.org/x/crypto v0.4.0 // indirect + golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect + golang.org/x/mod v0.6.0 // indirect golang.org/x/net v0.4.0 // indirect golang.org/x/sys v0.4.0 // indirect golang.org/x/text v0.5.0 // indirect - golang.org/x/tools v0.1.12 // indirect + golang.org/x/tools v0.2.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect - gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) + +// +//replace ( +// git.hpds.cc/Component/network => ../network +// git.hpds.cc/pavement/hpds_node => ../hpds_node +//) diff --git a/internal/monitor/monitor.go b/internal/monitor/monitor.go index 9aad7a0..32d6590 100644 --- a/internal/monitor/monitor.go +++ b/internal/monitor/monitor.go @@ -1,6 +1,7 @@ package monitor import ( + "environmentCaptureAgent/config" "environmentCaptureAgent/model" "fmt" "github.com/shirou/gopsutil/v3/disk" @@ -35,7 +36,7 @@ var ( Version string ) -func GetHost() *model.Node { +func GetHost(cfg *config.AgentConfig) *model.Node { hi, _ := host.Info() var cpuType string if hi.VirtualizationSystem != "" { @@ -68,7 +69,9 @@ func GetHost() *model.Node { } return &model.Node{ - NodeName: hi.HostID, + NodeGuid: hi.HostID, + NodeName: cfg.Point, + NodeType: cfg.NodeType, Platform: hi.OS, PlatformVersion: hi.PlatformVersion, CPU: cpus, @@ -86,7 +89,7 @@ func GetHost() *model.Node { func GetState(nodeName string) *model.NodeState { procs, _ := process.Pids() - + hi, _ := host.Info() mv, _ := mem.VirtualMemory() var swapMemUsed uint64 @@ -132,6 +135,7 @@ func GetState(nodeName string) *model.NodeState { } return &model.NodeState{ + NodeGuid: hi.HostID, NodeName: nodeName, CPU: cpuPercent, MemUsed: mv.Total - mv.Available, diff --git a/model/node.go b/model/node.go index 2cc31f7..2ebe4e3 100644 --- a/model/node.go +++ b/model/node.go @@ -4,7 +4,9 @@ import "encoding/json" // Node 节点信息 type Node struct { + NodeGuid string `json:"nodeGuid"` NodeName string `json:"nodeName"` + NodeType int `json:"nodeType"` Platform string `json:"platform,omitempty"` PlatformVersion string `json:"platformVersion,omitempty"` CPU []string `json:"cpu,omitempty"` @@ -29,6 +31,7 @@ func (n Node) ToByte() []byte { // NodeState 节点状态信息 type NodeState struct { + NodeGuid string `json:"nodeGuid"` NodeName string `json:"nodeName"` CPU float64 `json:"cpu,omitempty"` MemUsed uint64 `json:"memUsed,omitempty"`