修改系统获取方式

This commit is contained in:
wangjian 2023-03-01 11:36:19 +08:00
parent e03f012a6c
commit 27b3bfacbd
3 changed files with 15 additions and 63 deletions

View File

@ -22,7 +22,11 @@ var (
func must(err error) { func must(err error) {
if err != nil { 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) os.Exit(1)
} }
} }
@ -36,8 +40,6 @@ func NewStartCmd() *cobra.Command {
cfg *config.AgentConfig cfg *config.AgentConfig
err error err error
) )
//ctx, cancel := context.WithCancel(context.Background())
//defer cancel()
configFileFlag, err := cmd.Flags().GetString("c") configFileFlag, err := cmd.Flags().GetString("c")
if err != nil { if err != nil {
fmt.Println("get local config err: ", err) fmt.Println("get local config err: ", err)
@ -62,35 +64,26 @@ func NewStartCmd() *cobra.Command {
) )
err = ap.Connect() err = ap.Connect()
must(err) must(err)
//defer ap.Close()
//for _, v := range cfg.Funcs {
// ap.SetDataTag(v.DataTag)
//}
ap.SetDataTag(18) ap.SetDataTag(18)
node := monitor.GetHost() node := monitor.GetHost()
byteNode := node.ToByte() byteNode := node.ToByte()
_ = generateAndSendData(ap, byteNode) _ = 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 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 { for {
select { select {
case <-ticker.C: case <-statTicker.C:
stat := monitor.GetState(node.NodeName).ToByte() stat := monitor.GetState(node.NodeName).ToByte()
go func() { go func() {
_ = generateAndSendData(ap, stat) _ = generateAndSendData(ap, stat)
}() }()
case <-nodeTicker.C:
nodeInfo := monitor.GetHost().ToByte()
go func() {
_ = generateAndSendData(ap, nodeInfo)
}()
case errs := <-exitChannel: case errs := <-exitChannel:
count++ count++
if count > 3 { if count > 3 {

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"environmentCaptureAgent/config/cmd" "environmentCaptureAgent/cmd"
"fmt" "fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"os" "os"
@ -21,7 +21,7 @@ func init() {
func main() { func main() {
if err := rootCmd.Execute(); err != nil { if err := rootCmd.Execute(); err != nil {
fmt.Fprint(os.Stderr, err.Error()) _, _ = fmt.Fprint(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)
} }
} }

View File

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