forked from cppla/ServerStatus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ping.go
208 lines (200 loc) · 4.76 KB
/
ping.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"net"
"sync"
"time"
)
type PingValue struct {
ping uint64
status uint
lostRate float64
stop chan struct{}
mtx sync.Mutex
}
func NewPingValue() *PingValue {
return &PingValue{
status: 0,
ping: 0.0,
lostRate: 0.0,
stop: make (chan struct{}),
}
}
func (pingValue *PingValue) RunCU() {
go func() {
t1 := time.Duration(INTERVAL) * time.Second
t := time.NewTicker(t1)
var lostPacket = 0
var allPacket = 0
var lostConnect = false
startTime := time.Now()
defaulttimeout := 1 * time.Second
for {
select {
case <- pingValue.stop:
t.Stop()
return
case <-t.C:
pingValue.mtx.Lock()
t := time.Now()
conn , err := net.DialTimeout("tcp",CU_ADDR,defaulttimeout)
if err != nil {
logger.Alertf("[ping]Error try to connect China Unicom :", err)
//fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [ping]Error try to connect China unicom :", err)
lostConnect = true
lostPacket += 1
}
tcpconn, ok := conn.(*net.TCPConn)
if ok {
tcpconn.SetLinger(0)
}
if conn != nil {
conn.Close()
}
diffTime := time.Since(t)
//TODO:三网延迟和丢包率算法存在问题
//fmt.Println(diffTime)
allPacket += 1
if allPacket > 100 {
pingValue.lostRate = float64(lostPacket/allPacket) * 100
}
//fmt.Println("ALL LOST RATE")
//fmt.Printf("%10d %10d %10f\n",allPacket,lostPacket,pingValue.lostRate)
if lostConnect {
pingValue.ping = 0
pingValue.status = 1
} else {
pingValue.ping = uint64(diffTime/time.Millisecond)
pingValue.status = 0
}
lostConnect = false
resetTime := uint64(time.Since(startTime) / time.Second)
if resetTime > 3600 {
lostPacket = 0
allPacket = 0
startTime = time.Now()
}
pingValue.mtx.Unlock()
}
}
}()
}
func (pingValue *PingValue) RunCT() {
go func() {
t1 := time.Duration(INTERVAL) * time.Second
t := time.NewTicker(t1)
var lostPacket = 0
var allPacket = 0
var lostConnect = false
startTime := time.Now()
defaulttimeout := 1 * time.Second
for {
select {
case <- pingValue.stop:
t.Stop()
return
case <-t.C:
pingValue.mtx.Lock()
t := time.Now()
conn , err := net.DialTimeout("tcp",CT_ADDR,defaulttimeout)
if err != nil {
logger.Alertf("[ping]Error try to connect China Telecom :", err)
//fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [ping]Error try to connect China Telecom :", err)
lostConnect = true
lostPacket += 1
}
tcpconn, ok := conn.(*net.TCPConn)
if ok {
tcpconn.SetLinger(0)
}
if conn != nil {
conn.Close()
}
diffTime := time.Since(t)
allPacket += 1
if allPacket > 100 {
pingValue.lostRate = float64(lostPacket/allPacket) * 100
}
if lostConnect {
pingValue.ping = 0
pingValue.status = 1
} else {
pingValue.ping = uint64(diffTime/time.Millisecond)
pingValue.status = 0
}
lostConnect = false
resetTime := uint64(time.Since(startTime) / time.Second)
if resetTime > 3600 {
lostPacket = 0
allPacket = 0
startTime = time.Now()
}
pingValue.mtx.Unlock()
}
}
}()
}
func (pingValue *PingValue) RunCM() {
go func() {
t1 := time.Duration(INTERVAL) * time.Second
t := time.NewTicker(t1)
var lostPacket = 0
var allPacket = 0
var lostConnect = false
startTime := time.Now()
defaulttimeout := 1 * time.Second
for {
select {
case <- pingValue.stop:
t.Stop()
return
case <-t.C:
pingValue.mtx.Lock()
t := time.Now()
conn , err := net.DialTimeout("tcp",CM_ADDR,defaulttimeout)
if err != nil {
logger.Alertf("[ping]Error try to connect China Mobile :", err)
//fmt.Println(time.Now().Format("2006-01-02 15:04:05")," [ping]Error try to connect China mobile :", err)
lostConnect = true
lostPacket += 1
}
tcpconn, ok := conn.(*net.TCPConn)
if ok {
tcpconn.SetLinger(0)
}
if conn != nil {
conn.Close()
}
diffTime := time.Since(t)
allPacket += 1
if allPacket > 100 {
pingValue.lostRate = float64(lostPacket/allPacket) * 100
}
if lostConnect {
pingValue.ping = 0
pingValue.status = 1
} else {
pingValue.ping = uint64(diffTime/time.Millisecond)
pingValue.status = 0
}
lostConnect = false
resetTime := uint64(time.Since(startTime) / time.Second)
if resetTime > 3600 {
lostPacket = 0
allPacket = 0
startTime = time.Now()
}
pingValue.mtx.Unlock()
}
}
}()
}
func (pingValue *PingValue) Stop() {
close(pingValue.stop)
}
func (pingValue *PingValue) Get() (float64,uint64,uint) {
pingValue.mtx.Lock()
defer pingValue.mtx.Unlock()
status := pingValue.status
pingValue.status = 0
return pingValue.lostRate,pingValue.ping,status
}