-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindows-funcs.go
75 lines (63 loc) · 1.94 KB
/
windows-funcs.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
//go:build windows
// +build windows
package main
import (
"context"
"fmt"
"os/exec"
"strconv"
"strings"
)
// getResponseTime extracts time value from Ping output
// and tells if this is a failure message or not.
// -1 means the output is not a successful reply.
// true means the output states for a ping failure.
// false means to ignore the output (statistics data).
// On Windows the Ping output entry looks like below:
// <Reply from 8.8.8.8: bytes=32 time=1160ms TTL=56>
// <Reply from 127.0.0.1: bytes=32 time<1ms TTL=128>
func getResponseTime(output string) (int, bool) {
indexT := strings.Index(output, "time=")
if indexT > 0 {
indexM := strings.Index(output, "ms")
value := output[(indexT + 5):indexM]
response, _ := strconv.Atoi(value)
return response, false
} else {
indexT := strings.Index(output, "time<")
if indexT > 0 {
indexM := strings.Index(output, "ms")
value := output[(indexT + 5):indexM]
response, _ := strconv.Atoi(value)
return response, false
}
}
// ignore these outputs entries.
if strings.HasPrefix(output, "Pinging") || strings.HasPrefix(output, "Ping") ||
strings.HasPrefix(output, "Packets") || strings.HasPrefix(output, "Approximate") ||
strings.HasPrefix(output, "Minimum") {
return -1, false
}
return -1, true
}
// buildPingCommand constructs full command to run. The ping should
// run indefinitely by default unless a requests is defined.
func buildPingCommand(ip string, ctx context.Context) (string, *exec.Cmd) {
cfg := dbs.getConfig(ip)
cfg.start = getCurrentTime()
var cmd *exec.Cmd
syntax := fmt.Sprintf("ping %s", ip)
if cfg.requests > 0 {
syntax = syntax + fmt.Sprintf(" -n %d", cfg.requests)
} else {
syntax = syntax + " -t"
}
if cfg.timeout > 0 {
syntax = syntax + fmt.Sprintf(" -w %d", cfg.timeout)
}
if cfg.size > 0 {
syntax = syntax + fmt.Sprintf(" -l %d", cfg.size)
}
cmd = exec.CommandContext(ctx, "cmd", "/C", syntax)
return strconv.Itoa(cfg.threshold), cmd
}