From 27b3bfacbdd66613d538f4ddff38bc5b0f6d0e2e Mon Sep 17 00:00:00 2001 From: wangjian Date: Wed, 1 Mar 2023 11:36:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=B3=BB=E7=BB=9F=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {config/cmd => cmd}/server.go | 33 +++++++++++++-------------------- main.go | 4 ++-- pkg/utils/utils_test.go | 41 ----------------------------------------- 3 files changed, 15 insertions(+), 63 deletions(-) rename {config/cmd => cmd}/server.go (81%) delete mode 100644 pkg/utils/utils_test.go diff --git a/config/cmd/server.go b/cmd/server.go similarity index 81% rename from config/cmd/server.go rename to cmd/server.go index fc0f81d..634f4f4 100644 --- a/config/cmd/server.go +++ b/cmd/server.go @@ -22,7 +22,11 @@ var ( func must(err error) { if err != nil { - fmt.Fprint(os.Stderr, err) + if logger != nil { + logger.With(zap.String("capture agent", "错误信息")).Error("启动错误", zap.Error(err)) + } else { + _, _ = fmt.Fprint(os.Stderr, err) + } os.Exit(1) } } @@ -36,8 +40,6 @@ func NewStartCmd() *cobra.Command { cfg *config.AgentConfig err error ) - //ctx, cancel := context.WithCancel(context.Background()) - //defer cancel() configFileFlag, err := cmd.Flags().GetString("c") if err != nil { fmt.Println("get local config err: ", err) @@ -62,35 +64,26 @@ func NewStartCmd() *cobra.Command { ) err = ap.Connect() must(err) - //defer ap.Close() - //for _, v := range cfg.Funcs { - // ap.SetDataTag(v.DataTag) - //} ap.SetDataTag(18) node := monitor.GetHost() byteNode := node.ToByte() _ = generateAndSendData(ap, byteNode) - ticker := time.NewTicker(time.Duration(cfg.Delay) * time.Second) + statTicker := time.NewTicker(time.Duration(cfg.Delay) * time.Second) + nodeTicker := time.NewTicker(1 * time.Hour) count := 0 - //c := cron.New() - //spec := fmt.Sprintf("*/%d * * * * *", cfg.Delay) - //_, err = c.AddFunc(spec, func() { - // stat := monitor.GetState().ToByte() - // _ = generateAndSendData(ap, stat) - // logger.With( - // zap.String("agent", "发送状态信息"), - // ) - //}) - //must(err) - //c.Start() for { select { - case <-ticker.C: + case <-statTicker.C: stat := monitor.GetState(node.NodeName).ToByte() go func() { _ = generateAndSendData(ap, stat) }() + case <-nodeTicker.C: + nodeInfo := monitor.GetHost().ToByte() + go func() { + _ = generateAndSendData(ap, nodeInfo) + }() case errs := <-exitChannel: count++ if count > 3 { diff --git a/main.go b/main.go index 106a933..5e88dcf 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,7 @@ package main import ( - "environmentCaptureAgent/config/cmd" + "environmentCaptureAgent/cmd" "fmt" "github.com/spf13/cobra" "os" @@ -21,7 +21,7 @@ func init() { func main() { if err := rootCmd.Execute(); err != nil { - fmt.Fprint(os.Stderr, err.Error()) + _, _ = fmt.Fprint(os.Stderr, err.Error()) os.Exit(1) } } diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go deleted file mode 100644 index 688a703..0000000 --- a/pkg/utils/utils_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package utils - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -type testSt struct { - input string - output string -} - -func TestNotification(t *testing.T) { - cases := []testSt{ - { - input: "103.80.236.249/d5ce:d811:cdb8:067a:a873:2076:9521:9d2d", - output: "103.****.249/d5ce:d811:****:9521:9d2d", - }, - { - input: "3.80.236.29/d5ce::cdb8:067a:a873:2076:9521:9d2d", - output: "3.****.29/d5ce::****:9521:9d2d", - }, - { - input: "3.80.236.29/d5ce::cdb8:067a:a873:2076::9d2d", - output: "3.****.29/d5ce::****::9d2d", - }, - { - input: "3.80.236.9/d5ce::cdb8:067a:a873:2076::9d2d", - output: "3.****.9/d5ce::****::9d2d", - }, - { - input: "3.80.236.9/d5ce::cdb8:067a:a873:2076::9d2d", - output: "3.****.9/d5ce::****::9d2d", - }, - } - - for _, c := range cases { - assert.Equal(t, IPDesensitize(c.input), c.output) - } -}