ping

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DoPing = func(ctx context.Context, resMap map[string]map[string]*Info, t *BatchPingTool) map[string]map[string]*Info {

	p := fastping.NewPinger()
	nameMapping := make(map[string]map[string]struct{})
	ipsCount := 0
	for target, infoMap := range resMap {
		for _, info := range infoMap {
			ra, ok := info.Addr.(*net.IPAddr)
			if ok {
				p.AddIPAddr(ra)

				_, ok := nameMapping[ra.IP.String()]
				if !ok {
					nameMapping[ra.IP.String()] = map[string]struct{}{target: {}}
				} else {
					nameMapping[ra.IP.String()][target] = struct{}{}
				}
				ipsCount++
			}
		}
	}

	totalCount := 0
	jobCtx, jobCancel := context.WithCancel(ctx)

	p.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {

		ipStr := addr.IP.String()
		targets, ok := nameMapping[ipStr]
		if !ok {
			logger.Warnf("get unexpected ip:%s", addr.IP.String())
			return
		}

		for target := range targets {
			pingInfo, ok := resMap[target][ipStr]
			if ok {
				logger.Debugf("target:%s,received icmp package", target)
				pingInfo.RecvCount++

				rttMillSecond := rtt.Seconds() * 1000

				if rttMillSecond > pingInfo.MaxRTT {
					pingInfo.MaxRTT = rttMillSecond
				}

				if pingInfo.RecvCount == 1 {
					pingInfo.MinRTT = rttMillSecond
				}
				if pingInfo.MinRTT > rttMillSecond {
					pingInfo.MinRTT = rttMillSecond
				}
				pingInfo.TotalRTT += rttMillSecond

				resMap[target][ipStr] = pingInfo

				totalCount++

				if totalCount == ipsCount {
					jobCancel()
					totalCount = 0
				}
			} else {
				logger.Errorf("missing inited pingInfo,target:%s", target)
			}
		}
	}

	p.OnIdle = func() {
		jobCancel()
		totalCount = 0
	}

	p.Size = t.size
	p.MaxRTT = t.maxRTT
	p.Debug = false
	_, err := p.Source("")
	if err != nil {
		logger.Errorf("pingtool source failed,error:%v", err)
		return resMap
	}

	loopChannel := make(chan int, 1)

	go func() {
		defer func() {
			logger.Info("loop end,send signal")
			loopChannel <- 1
		}()
		for i := 0; i < t.totalCount; i++ {
			logger.Debugf("ping start,times:%d", i)
			p.RunLoop()
			select {
			case <-jobCtx.Done():
				p.Stop()
				jobCtx, jobCancel = context.WithCancel(ctx)
			case <-ctx.Done():
				p.Stop()
				return
			}
			<-p.Done()

		}
	}()

	select {
	case <-ctx.Done():
		logger.Infof("get ctx done,reason:%v", ctx.Err())
		break
	case <-loopChannel:
		logger.Info("get loop end signal,return result")
		break
	}

	return resMap
}

DoPing 实际进行ping的方法,使用fastping包

View Source
var InitTargets = func(targets []*configs.Target) []Target {
	targetList := make([]Target, len(targets))
	for index, target := range targets {
		targetList[index] = target
	}
	return targetList
}

InitTargets 初始化targets

View Source
var NewBatchPingTool = func(
	ctx context.Context, targetList []Target, totalNum int, maxRTT string, size int, batchSize int,
	targetIPType configs.IPType, dnsCheckMode configs.CheckMode, s tasks.Semaphore,
) (Tool, error) {
	var err error
	pingTool := new(BatchPingTool)
	pingTool.ctx = ctx
	pingTool.targetList = targetList
	pingTool.totalCount = totalNum
	pingTool.batchSize = batchSize
	pingTool.size = size
	pingTool.targetIPType = targetIPType
	pingTool.dnsCheckMode = dnsCheckMode
	pingTool.s = s

	pingTool.maxRTT, err = time.ParseDuration(maxRTT)
	if err != nil {
		logger.Errorf("init max_rtt failed,err:%v", err)
		return nil, err
	}
	return pingTool, nil
}

NewBatchPingTool :

Functions

func New

func New(globalConfig define.Config, taskConfig define.TaskConfig) define.Task

New :

Types

type BatchPingTool

type BatchPingTool struct {
	// contains filtered or unexported fields
}

BatchPingTool 批量Ping

func (*BatchPingTool) Ping

func (t *BatchPingTool) Ping(doFunc DoFunc) error

Ping 开始分批ping

type DoFunc

type DoFunc func(resMap map[string]map[string]*Info, wg *sync.WaitGroup)

DoFunc 处理数据的接口方法

type Gather

type Gather struct {
	tasks.BaseTask
}

Gather :

func (*Gather) Run

func (g *Gather) Run(ctx context.Context, e chan<- define.Event)

Run :

type Info

type Info struct {
	Name       string
	Type       string
	Addr       net.Addr
	RecvCount  int
	TotalCount int
	MaxRTT     float64
	MinRTT     float64
	TotalRTT   float64
	Code       define.NamedCode
}

Info ping的信息记录表

type Target

type Target interface {
	GetTarget() string
	GetTargetType() string
}

Target 接口,任何实现该接口的数据都可传入

type Tool

type Tool interface {
	// 单次全ping
	Ping(doFunc DoFunc) error
}

Tool ping接口

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL