-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpinger_system.go
99 lines (83 loc) · 2.08 KB
/
pinger_system.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"bufio"
"fmt"
"log"
"net"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"time"
"github.com/google/shlex"
)
type SystemPingWrapper struct {
host string
ip *net.IPAddr
hstring string
stats *PWStats
cmd *exec.Cmd
ping_options string
}
var time_extractor = regexp.MustCompile(`time[=<]([\d\.]+) *(.?s)`)
var time_extractor_non_local = regexp.MustCompile(`[=<]([\d\.]+) *(.?s)`)
func (w *SystemPingWrapper) Start() {
w.hstring = fmt.Sprintf("%s (%s)", w.host, w.ip.String())
w.stats.hrepr = w.host
w.stats.iprepr = w.ip.String()
var path string
// Looks like an ipv6 ? search for ping6
// Some systems doesn't have ping6 because ping handle both v4 and v6
// so not finding ping6 is not necessarily a problem
if strings.Contains(w.ip.String(), ":") {
path, _ = exec.LookPath("ping6")
}
if path == "" {
var err error
path, err = exec.LookPath("ping")
if err != nil {
log.Fatal(err)
}
}
args, err := shlex.Split(w.ping_options)
if err != nil {
log.Fatal(err)
}
extractor := time_extractor
if runtime.GOOS == "windows" {
args = append(args, "-t")
extractor = time_extractor_non_local
}
args = append(args, w.ip.String())
w.cmd = exec.Command(path, args...)
w.cmd.Env = append(w.cmd.Environ(), "LANG=C")
w.stats = &PWStats{
state: true,
}
r, _ := w.cmd.StdoutPipe()
scanner := bufio.NewScanner(r)
go func() {
// Read line by line and process it
for scanner.Scan() {
line := scanner.Text()
extracted := extractor.FindAllStringSubmatch(line, -1)
if len(extracted) > 0 {
w.stats.lastrecv = time.Now().UnixNano()
w.stats.lastrtt_as_string = extracted[0][1] + extracted[0][2]
}
}
w.stats.error_message = fmt.Sprintf("%v exited code %v", w.cmd.String(), w.cmd.ProcessState.ExitCode())
}()
w.cmd.Start()
}
func (w *SystemPingWrapper) Stop() {
w.cmd.Process.Signal(os.Interrupt)
}
func (w *SystemPingWrapper) Host() string {
return w.hstring
}
func (w *SystemPingWrapper) CalcStats(timeout_threshold int64) PWStats {
w.stats.ComputeState(timeout_threshold)
return *w.stats
}