github.com/netdata/go.d.plugin@v0.58.1/modules/ping/collect.go (about)

     1  // SPDX-License-Identifier: GPL-3.0-or-later
     2  
     3  package ping
     4  
     5  import (
     6  	"fmt"
     7  	"sync"
     8  )
     9  
    10  func (p *Ping) collect() (map[string]int64, error) {
    11  	mu := &sync.Mutex{}
    12  	mx := make(map[string]int64)
    13  	var wg sync.WaitGroup
    14  
    15  	for _, v := range p.Hosts {
    16  		wg.Add(1)
    17  		go func(v string) { defer wg.Done(); p.pingHost(v, mx, mu) }(v)
    18  	}
    19  	wg.Wait()
    20  
    21  	return mx, nil
    22  }
    23  
    24  func (p *Ping) pingHost(host string, mx map[string]int64, mu *sync.Mutex) {
    25  	stats, err := p.prober.ping(host)
    26  	if err != nil {
    27  		p.Error(err)
    28  		return
    29  	}
    30  
    31  	mu.Lock()
    32  	defer mu.Unlock()
    33  
    34  	if !p.hosts[host] {
    35  		p.hosts[host] = true
    36  		p.addHostCharts(host)
    37  	}
    38  
    39  	px := fmt.Sprintf("host_%s_", host)
    40  	if stats.PacketsRecv != 0 {
    41  		mx[px+"min_rtt"] = stats.MinRtt.Microseconds()
    42  		mx[px+"max_rtt"] = stats.MaxRtt.Microseconds()
    43  		mx[px+"avg_rtt"] = stats.AvgRtt.Microseconds()
    44  		mx[px+"std_dev_rtt"] = stats.StdDevRtt.Microseconds()
    45  	}
    46  	mx[px+"packets_recv"] = int64(stats.PacketsRecv)
    47  	mx[px+"packets_sent"] = int64(stats.PacketsSent)
    48  	mx[px+"packet_loss"] = int64(stats.PacketLoss * 1000)
    49  }