package task import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/os/glog" "golang.org/x/net/context" "net" "nodeMonitor/internal/model" "nodeMonitor/internal/service" "nodeMonitor/library/nettools" "sync" "time" ) func Ping(ctx context.Context) { glog.Debug(ctx, "start ping .....") var wg sync.WaitGroup //获取节点数据 nodeList, err := service.Node().GetNode(ctx) if err != nil { glog.Debug(ctx, err.Error()) return } for _, target := range nodeList { wg.Add(1) //glog.Debug(ctx, target) go PingTask(ctx, target.Host, &wg, target.PingType, target.Port) } wg.Wait() go CheckNodeStatus(ctx) } func PingTask(ctx context.Context, t string, wg *sync.WaitGroup, pingType int, hostPort int) { //var ipSlice []string //ipSlice = append(ipSlice, "kdvkr-02.xyz") //ipSlice = append(ipSlice, "kdvkr-04.xyz") pingCount, err := g.Cfg().Get(ctx, "node.pingCount") if err != nil { glog.Debug(ctx, err.Error()) return } stat := model.PingSt{} stat.MinDelay = -1 lossPK := 0 addr, err := net.ResolveIPAddr("ip", t) if err == nil { for i := 0; i < pingCount.Int(); i++ { starttime := time.Now().UnixNano() var rest nettools.IPingResult if pingType == 0 { //icmp icmping := nettools.NewIcmpPing(addr.String(), time.Second*4) rest = icmping.Ping() } else if pingType == 1 { tcpping := nettools.NewTcpPing(addr.String(), hostPort, time.Second*4) rest = tcpping.Ping() } if rest.Error() == nil { delay := rest.Result() //seelog.Info("[func:IcmpPing] Finish Addr:", addr, delay, "ms") stat.AvgDelay = stat.AvgDelay + rest.Result() if stat.MaxDelay < delay { stat.MaxDelay = delay } if stat.MinDelay == -1 || stat.MinDelay > delay { stat.MinDelay = delay } stat.RevcPk = stat.RevcPk + 1 } else { glog.Debug(ctx, "[func:StartPing IcmpPing] ID:", i, " IP:", addr, "| err:", rest.Error()) lossPK = lossPK + 1 } stat.SendPk = stat.SendPk + 1 stat.LossPk = int((float64(lossPK) / float64(stat.SendPk)) * 100) duringtime := time.Now().UnixNano() - starttime time.Sleep(time.Duration(3000*1000000-duringtime) * time.Nanosecond) } if stat.RevcPk > 0 { stat.AvgDelay = stat.AvgDelay / stat.RevcPk } else { stat.AvgDelay = 0.0 } glog.Debug(ctx, "[func:IcmpPing] Finish Addr:", addr, " MaxDelay:", stat.MaxDelay, " MinDelay:", stat.MinDelay, " AvgDelay:", stat.AvgDelay, " Revc:", stat.RevcPk, " LossPK:", stat.LossPk) } else { stat.AvgDelay = 0.00 stat.MinDelay = 0.00 stat.MaxDelay = 0.00 stat.SendPk = 0 stat.RevcPk = 0 stat.LossPk = 100 glog.Debug(ctx, "[func:IcmpPing] Finish Addr:", addr, " Unable to resolve destination host") } //添加到数据库 AddPingLog(ctx, stat, t) wg.Done() } func AddPingLog(ctx context.Context, pingres model.PingSt, addr string) { err := service.Ping().Create(ctx, pingres, addr) if err != nil { glog.Debug(ctx, err.Error()) return } return } func CheckNodeStatus(ctx context.Context) { nodeList, err := service.Node().GetNode(ctx) if err != nil { glog.Debug(ctx, err.Error()) return } for _, target := range nodeList { glog.Debug(ctx, "start url req .....") //获取不通的IP进程url请求 status, err := service.Ping().GetStatus(ctx, target.Host) if err != nil { glog.Error(ctx, err.Error()) return } if status { if target.UrlStatus == 200 { return } r, err := g.Client().Get(ctx, target.Url) if err != nil { glog.Error(ctx, err.Error()) return } defer r.Close() glog.Debug(ctx, "req :", target.Url, "status :", r.Status) if r.StatusCode == 200 { err := service.Node().UpdateNodeUrlStatus(ctx, model.NodeCreateInput{Host: target.Host, UrlStatus: r.StatusCode}) if err != nil { glog.Debug(ctx, err.Error()) return } } } } }