134 lines
3.2 KiB
Go
134 lines
3.2 KiB
Go
package cmd
|
|
|
|
import (
|
|
"environmentCaptureAgent/config"
|
|
"environmentCaptureAgent/internal/monitor"
|
|
"fmt"
|
|
"git.hpds.cc/pavement/hpds_node"
|
|
"go.uber.org/zap"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"git.hpds.cc/Component/logging"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
ConfigFileFlag string = "./config/config.yaml"
|
|
logger *logging.Logger
|
|
)
|
|
|
|
func must(err error) {
|
|
if err != nil {
|
|
fmt.Fprint(os.Stderr, err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func NewStartCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "start",
|
|
Short: "Start hpds environment capture agent",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
var (
|
|
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)
|
|
return
|
|
}
|
|
|
|
must(err)
|
|
cfg, err = config.ParseConfigByFile(configFileFlag)
|
|
must(err)
|
|
logger = LoadLoggerConfig(cfg.Logging)
|
|
exitChannel := make(chan os.Signal)
|
|
defer close(exitChannel)
|
|
|
|
// 退出信号监听
|
|
go func(c chan os.Signal) {
|
|
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
|
|
}(exitChannel)
|
|
ap := hpds_node.NewAccessPoint(
|
|
cfg.Name,
|
|
hpds_node.WithMqAddr(fmt.Sprintf("%s:%d", cfg.Node.Host, cfg.Node.Port)),
|
|
hpds_node.WithCredential(cfg.Node.Token),
|
|
)
|
|
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)
|
|
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:
|
|
stat := monitor.GetState(node.NodeName).ToByte()
|
|
go func() {
|
|
_ = generateAndSendData(ap, stat)
|
|
}()
|
|
case errs := <-exitChannel:
|
|
count++
|
|
if count > 3 {
|
|
logger.With(
|
|
zap.String("agent", "服务退出"),
|
|
).Info(errs.String())
|
|
return
|
|
}
|
|
}
|
|
}
|
|
},
|
|
}
|
|
cmd.Flags().StringVar(&ConfigFileFlag, "c", "./config/config.yaml", "The configuration file path")
|
|
return cmd
|
|
}
|
|
|
|
func generateAndSendData(stream hpds_node.AccessPoint, data []byte) error {
|
|
_, err := stream.Write(data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
time.Sleep(1000 * time.Millisecond)
|
|
return nil
|
|
}
|
|
|
|
func LoadLoggerConfig(opt config.LogOptions) *logging.Logger {
|
|
return logging.NewLogger(
|
|
logging.SetPath(opt.Path),
|
|
logging.SetPrefix(opt.Prefix),
|
|
logging.SetDevelopment(opt.Development),
|
|
logging.SetDebugFileSuffix(opt.DebugFileSuffix),
|
|
logging.SetWarnFileSuffix(opt.WarnFileSuffix),
|
|
logging.SetErrorFileSuffix(opt.ErrorFileSuffix),
|
|
logging.SetInfoFileSuffix(opt.InfoFileSuffix),
|
|
logging.SetMaxAge(opt.MaxAge),
|
|
logging.SetMaxBackups(opt.MaxBackups),
|
|
logging.SetMaxSize(opt.MaxSize),
|
|
logging.SetLevel(logging.LogLevel["debug"]),
|
|
)
|
|
}
|