|
@@ -24,12 +24,14 @@ func Ping(ctx context.Context) {
|
|
for _, target := range nodeList {
|
|
for _, target := range nodeList {
|
|
wg.Add(1)
|
|
wg.Add(1)
|
|
//glog.Debug(ctx, target)
|
|
//glog.Debug(ctx, target)
|
|
- go PingTask(ctx, target.Host, &wg)
|
|
|
|
|
|
+ go PingTask(ctx, target.Host, &wg, target.PingType, target.Port)
|
|
}
|
|
}
|
|
wg.Wait()
|
|
wg.Wait()
|
|
|
|
+
|
|
|
|
+ go CheckNodeStatus(ctx)
|
|
}
|
|
}
|
|
|
|
|
|
-func PingTask(ctx context.Context, t string, wg *sync.WaitGroup) {
|
|
|
|
|
|
+func PingTask(ctx context.Context, t string, wg *sync.WaitGroup, pingType int, hostPort int) {
|
|
//var ipSlice []string
|
|
//var ipSlice []string
|
|
//ipSlice = append(ipSlice, "kdvkr-02.xyz")
|
|
//ipSlice = append(ipSlice, "kdvkr-02.xyz")
|
|
//ipSlice = append(ipSlice, "kdvkr-04.xyz")
|
|
//ipSlice = append(ipSlice, "kdvkr-04.xyz")
|
|
@@ -46,9 +48,16 @@ func PingTask(ctx context.Context, t string, wg *sync.WaitGroup) {
|
|
if err == nil {
|
|
if err == nil {
|
|
for i := 0; i < pingCount.Int(); i++ {
|
|
for i := 0; i < pingCount.Int(); i++ {
|
|
starttime := time.Now().UnixNano()
|
|
starttime := time.Now().UnixNano()
|
|
- //tcpping := nettools.NewTcpPing(addr.String(), 22, time.Second*4)
|
|
|
|
- icmping := nettools.NewIcmpPing(addr.String(), time.Second*4)
|
|
|
|
- rest := icmping.Ping()
|
|
|
|
|
|
+
|
|
|
|
+ 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 {
|
|
if rest.Error() == nil {
|
|
delay := rest.Result()
|
|
delay := rest.Result()
|
|
//seelog.Info("[func:IcmpPing] Finish Addr:", addr, delay, "ms")
|
|
//seelog.Info("[func:IcmpPing] Finish Addr:", addr, delay, "ms")
|
|
@@ -60,16 +69,14 @@ func PingTask(ctx context.Context, t string, wg *sync.WaitGroup) {
|
|
stat.MinDelay = delay
|
|
stat.MinDelay = delay
|
|
}
|
|
}
|
|
stat.RevcPk = stat.RevcPk + 1
|
|
stat.RevcPk = stat.RevcPk + 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)
|
|
|
|
} else {
|
|
} else {
|
|
- //seelog.Debug("[func:StartPing IcmpPing] ID:", i, " IP:", addr, "| err:", rest.Error())
|
|
|
|
|
|
+ glog.Debug(ctx, "[func:StartPing IcmpPing] ID:", i, " IP:", addr, "| err:", rest.Error())
|
|
lossPK = lossPK + 1
|
|
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 {
|
|
if stat.RevcPk > 0 {
|
|
stat.AvgDelay = stat.AvgDelay / stat.RevcPk
|
|
stat.AvgDelay = stat.AvgDelay / stat.RevcPk
|
|
@@ -88,11 +95,11 @@ func PingTask(ctx context.Context, t string, wg *sync.WaitGroup) {
|
|
}
|
|
}
|
|
|
|
|
|
//添加到数据库
|
|
//添加到数据库
|
|
- AddPinglog(ctx, stat, t)
|
|
|
|
|
|
+ AddPingLog(ctx, stat, t)
|
|
wg.Done()
|
|
wg.Done()
|
|
}
|
|
}
|
|
|
|
|
|
-func AddPinglog(ctx context.Context, pingres model.PingSt, addr string) {
|
|
|
|
|
|
+func AddPingLog(ctx context.Context, pingres model.PingSt, addr string) {
|
|
err := service.Ping().Create(ctx, pingres, addr)
|
|
err := service.Ping().Create(ctx, pingres, addr)
|
|
if err != nil {
|
|
if err != nil {
|
|
glog.Debug(ctx, err.Error())
|
|
glog.Debug(ctx, err.Error())
|
|
@@ -103,4 +110,41 @@ func AddPinglog(ctx context.Context, pingres model.PingSt, addr string) {
|
|
|
|
|
|
func CheckNodeStatus(ctx context.Context) {
|
|
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
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
}
|
|
}
|