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