/
probe.go
105 lines (92 loc) · 1.81 KB
/
probe.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
100
101
102
103
104
105
package subcmd
import (
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/urfave/cli"
)
func probe(c *cli.Context) {
host := c.String("host")
port := c.Int("port")
device := c.String("interface")
idleTime := c.Int("idleTime")
retranCount := c.Int("retranCount")
interval := 3
ch := make(chan int)
cap := make(chan Capture)
filter := fmt.Sprintf("host %s and tcp and port %d", host, port)
go doHeartbeat(host, port, idleTime, retranCount, interval, ch)
time.Sleep(time.Duration(idleTime) * time.Second)
go doTcpDump(device, filter, ch, cap)
sig := make(chan os.Signal)
signal.Notify(sig, os.Interrupt, os.Kill, syscall.SIGINT, syscall.SIGUSR1, syscall.SIGUSR2)
res := "fail"
getOne := false
L:
for {
select {
case s := <-sig:
log.Println("退出信号", s)
break L
case d := <-cap:
if tcp, ok := d.(*CaptureTcpData); ok {
fmt.Printf(tcp.ToString())
// req
if int(tcp.GetDport()) == port {
getOne = true
} else {
if getOne && int(tcp.GetSport()) == port {
res = "ok"
}
break L
}
}
case c := <-ch:
if c == 1 {
break L
} else if c == -1 {
fmt.Printf("get error")
break L
}
}
}
fmt.Printf("tcp heartbeat %s", res)
return
}
func Probe() cli.Command {
return cli.Command{
Name: "probe",
Usage: "Check whether the server supports heartbeat",
Flags: []cli.Flag{
cli.StringFlag{
Name: "interface,i",
Value: "eth0",
Usage: "interface",
},
cli.StringFlag{
Name: "host,H",
Value: "127.0.0.1",
Usage: "host",
},
cli.IntFlag{
Name: "port, P",
Value: 80,
Usage: "port",
},
cli.IntFlag{
Name: "idleTime",
Value: 3,
Usage: "idleTime",
},
cli.IntFlag{
Name: "retranCount",
Value: 10,
Usage: "retranCount",
},
},
Action: probe,
}
}