summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/ping/collect.go
blob: c162a2b158f6a69800084c3119866ffa062f7a84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// SPDX-License-Identifier: GPL-3.0-or-later

package ping

import (
	"fmt"
	"sync"
)

func (p *Ping) collect() (map[string]int64, error) {
	mu := &sync.Mutex{}
	mx := make(map[string]int64)
	var wg sync.WaitGroup

	for _, v := range p.Hosts {
		wg.Add(1)
		go func(v string) { defer wg.Done(); p.pingHost(v, mx, mu) }(v)
	}
	wg.Wait()

	return mx, nil
}

func (p *Ping) pingHost(host string, mx map[string]int64, mu *sync.Mutex) {
	stats, err := p.prober.ping(host)
	if err != nil {
		p.Error(err)
		return
	}

	mu.Lock()
	defer mu.Unlock()

	if !p.hosts[host] {
		p.hosts[host] = true
		p.addHostCharts(host)
	}

	px := fmt.Sprintf("host_%s_", host)
	if stats.PacketsRecv != 0 {
		mx[px+"min_rtt"] = stats.MinRtt.Microseconds()
		mx[px+"max_rtt"] = stats.MaxRtt.Microseconds()
		mx[px+"avg_rtt"] = stats.AvgRtt.Microseconds()
		mx[px+"std_dev_rtt"] = stats.StdDevRtt.Microseconds()
	}
	mx[px+"packets_recv"] = int64(stats.PacketsRecv)
	mx[px+"packets_sent"] = int64(stats.PacketsSent)
	mx[px+"packet_loss"] = int64(stats.PacketLoss * 1000)
}