-
Notifications
You must be signed in to change notification settings - Fork 77
/
task.go
231 lines (220 loc) · 6.07 KB
/
task.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package icmpping
import (
"github.com/WeBankPartners/open-monitor/monitor-agent/ping_exporter/funcs"
"log"
"net"
"sync"
"time"
)
func StartTask() {
timeout = 2
interval := funcs.Config().Interval
if interval < 30 {
log.Println("interval refresh to 30s")
interval = 30
}
if timeout > interval/12 {
log.Printf("timeout %d > interval/12,max 3 try and 4 packages,reset to interval/12=%d \n", timeout, interval/12)
timeout = interval / 12
}
InitIcmpBytes() // 初始化ICMP数据包的bytes
localIp = getIntranetIp() // 初始化本机IP列表
if funcs.Config().OpenFalcon.Enabled {
funcs.InitTransfer()
}
if TestModel {
doTask()
doPacketLossCheck()
return
}
go startPacketLossTask()
t := time.NewTicker(time.Second * time.Duration(interval)).C
for {
go doTask()
<-t
}
}
func startPacketLossTask() {
t := time.NewTicker(time.Minute * 30).C
for {
doPacketLossCheck()
<-t
}
}
func doPacketLossCheck() {
log.Println("start do packet loss check")
startTime := time.Now()
wg := sync.WaitGroup{}
ipList := funcs.GetIpList()
clearLossResultMap(ipList)
for _, ip := range ipList {
funcs.DebugLog("start loss packent ping check %s ", ip)
if containString(ip, localIp) {
funcs.DebugLog("%s is local ip", ip)
writeLossResultMap(ip, 0)
continue
}
wg.Add(1)
go func(ip string) {
funcs.DebugLog("start loss packent ping %s ", ip)
lossPercent := StartPingLossPacket(ip)
writeLossResultMap(ip, lossPercent)
funcs.DebugLog("end loss packent ping %s result %.0f ", ip, lossPercent)
wg.Done()
}(ip)
}
wg.Wait()
useTime := float64(time.Now().Sub(startTime).Nanoseconds()) / 1e6
log.Printf("end packet loss check, check ip num %d, use time %.3f ms \n", len(ipList), useTime)
result := readLossResultMap()
funcs.UpdateLossPingExportMetric(result)
}
func doTask() {
// 清空上次数据,把resultMap数据保存到lastResultMap
//ClearRetryIp()
ClearSuccessIp()
//ClearRetryMap()
lastResultMap := readResultMap()
//lastResultMap := make(map[string]funcs.PingResultObj)
//for k,v := range readResultMap(){
// lastResultMap[k] = v
//}
log.Println("start")
startTime := time.Now()
wg := sync.WaitGroup{}
var successCounter int
var lastValue int
var lastExist bool
ipList := funcs.GetIpList()
clearResultMap(ipList)
// first check
for _, ip := range ipList {
if containString(ip, localIp) {
funcs.DebugLog("%s is local ip", ip)
writeResultMap(ip, 0, 0)
successCounter += 1
continue
}
if lv, ok := lastResultMap[ip]; ok {
lastValue = lv.UpDown
lastExist = true
} else {
lastValue = 0
lastExist = false
}
wg.Add(1)
go func(ip string, timeout int, lv int, le bool) {
d, ut, isConfused := StartPing(ip, timeout)
if isConfused {
if le == true && lv == 1 {
d = 1
} else {
addSuccessIp(ip)
}
}
writeResultMap(ip, d, ut)
funcs.DebugLog("ping %s result %d ", ip, d)
wg.Done()
}(ip, timeout, lastValue, lastExist)
}
wg.Wait()
// 对第一次执行有异常的IP执行第二次检测
//retryIp := GetRetryIp()
//retryLength := len(retryIp) // 重试IP的数量,如果第二次检测成功则数量减1,如果第二次检测完数量还大于0,则进行第三次检测
//if len(retryIp) > 0 {
// funcs.DebugLog("start second round, retry ip num : %d ", len(retryIp))
// wgs := sync.WaitGroup{}
// for _,v:= range retryIp{
// wgs.Add(1)
// go func(ip string,timeout int) {
// defer wgs.Done()
// funcs.DebugLog("second round , retry ip %s ", ip)
// d,ut := StartPing(ip, timeout)
// if d==0 {
// retryLength = retryLength - 1
// }
// writeResultMap(ip, d, ut)
// }(v,timeout)
// }
// wgs.Wait()
// funcs.DebugLog("end second round ")
//}
// 第三次,最后一次检测,如果前面两次检测都还有需要重试的IP,说明此时的网络环境不稳定,需要对比上次检测的结果进行一次最后的重试
//if retryLength>0 {
// funcs.DebugLog("start third round, retry ip num : %d ", retryLength)
// ClearRetryIp()
// tmpRMap := readResultMap()
// for _,v := range ipList{
// if lv,ok := lastResultMap[v]; ok {
// if lv!=tmpRMap[v]{ // 上次结果和这次的不一致,最后再检查一次,一般这种IP比较少
// addRetryIp(v)
// funcs.DebugLog("%s last result is different this : %d, last %d ", v, tmpRMap[v], lv)
// if tmpRMap[v].UpDown==0{
// successCounter = successCounter - 1
// }
// continue
// }
// }
// if tmpRMap[v].UpDown >= 2{
// funcs.DebugLog("%s retry is still not work ", v)
// addRetryIp(v)
// }
// }
// finalRetryIp := GetRetryIp()
// if len(finalRetryIp) > 0 {
// funcs.DebugLog("final retry ip num : %d ", len(finalRetryIp))
// wgt := sync.WaitGroup{}
// for _,v := range finalRetryIp{
// wgt.Add(1)
// go func(ip string,timeout int) {
// defer wgt.Done()
// d,ut := StartPing(ip, timeout)
// writeResultMap(ip, d, ut)
// funcs.DebugLog("ping %s result %d ", ip ,d)
// }(v,timeout)
// }
// wgt.Wait()
// }
//}
endTime := time.Now()
useTime := float64(endTime.Sub(startTime).Nanoseconds()) / 1e6
successIp := GetSuccessIp()
successCounter = successCounter + len(successIp)
log.Printf("end ping, success num %d, fail num %d, use time %.3f ms \n", successCounter, len(ipList)-successCounter, useTime)
dealResult(successCounter)
}
func dealResult(successCounter int) {
result := readResultMap()
if funcs.Config().Prometheus.Enabled {
go funcs.UpdatePingExportMetric(result, successCounter)
}
if funcs.Config().OpenFalcon.Enabled {
go funcs.HandleTransferResult(result, successCounter)
}
if TestModel {
successOutput := "alive ip : \n"
for k, v := range result {
if v.UpDown == 0 {
successOutput = successOutput + k + ","
}
}
log.Println(successOutput)
}
}
func getIntranetIp() []string {
addrs, err := net.InterfaceAddrs()
re := []string{}
if err != nil {
log.Println(err)
return re
}
for _, address := range addrs {
// 检查ip地址判断是否回环地址
if ipNet, ok := address.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if ipNet.IP.To4() != nil {
re = append(re, ipNet.IP.String())
}
}
}
return re
}