12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- package pinglog
- import (
- "github.com/gogf/gf/v2/database/gdb"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/os/glog"
- "golang.org/x/net/context"
- "nodeMonitor/internal/dao"
- "nodeMonitor/internal/model"
- "nodeMonitor/internal/model/do"
- "nodeMonitor/internal/service"
- "time"
- )
- type (
- sPing struct{}
- )
- func init() {
- service.RegisterPing(New())
- }
- func New() *sPing {
- return &sPing{}
- }
- // Create 创建延迟日志
- func (c *sPing) Create(ctx context.Context, ping model.PingSt, host string) error {
- return dao.Pinglog.Transaction(ctx, func(ctx context.Context, tx *gdb.TX) error {
- _, err := dao.Pinglog.Ctx(ctx).Data(do.Pinglog{
- Maxdelay: ping.MaxDelay,
- Mindelay: ping.MinDelay,
- Avgdelay: ping.AvgDelay,
- Losspk: ping.LossPk,
- Host: host,
- }).Insert()
- return err
- })
- }
- // GetStatus 查询节点状态
- // SELECT * FROM pinglog WHERE create_at >= DATE_SUB(NOW(),INTERVAL 10 MINUTE) and avgdelay = 0 and losspk >= 0 and maxdelay = 0 and `host` = 'kdvkr-04.xyz'
- func (c *sPing) GetStatus(ctx context.Context, host string) (bool, error) {
- //查询的数据超过3次就是不ok了
- startTime, err := g.Cfg().Get(ctx, "node.startTime")
- if err != nil {
- glog.Debug(ctx, err.Error())
- return false, err
- }
- loos, err := g.Cfg().Get(ctx, "node.loos")
- if err != nil {
- glog.Debug(ctx, err.Error())
- return false, err
- }
- timeStartStr := time.Unix(time.Now().Unix()-startTime.Int64(), 0)
- count, err := dao.Pinglog.Ctx(ctx).Where("create_at > ?", timeStartStr).Where("avgdelay = 0").Where("losspk > ?", loos.Int()).Where("maxdelay = 0 ").Where("host = ?", host).Count()
- return count >= 3, err
- }
|