-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (70 loc) · 1.49 KB
/
main.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
package main
import (
"fmt"
"net/http"
"sync"
"time"
"github.com/go-ping/ping"
)
type CheckResult struct {
Up bool
Status string
Latency time.Duration
}
var wg sync.WaitGroup
const VERBOSE = false
func main() {
if VERBOSE {
fmt.Printf("[%v] Starting upcheck...\n", time.Now().Format("2006-01-02 15:04:05"))
}
ch := make(chan CheckResult)
wg.Add(2)
go httpUp(ch)
go pingUp(ch)
go func() {
wg.Wait()
close(ch)
}()
for res := range ch {
if !res.Up {
fmt.Printf("[%v] Outage detected status: %s. Latency:%v\n", time.Now().Format("2006-01-02 15:04:05"), res.Status, res.Latency)
return
}
if VERBOSE {
fmt.Printf("[%v] Successful CheckResult: %s. Latency:%v\n", time.Now().Format("2006-01-02 15:04:05"), res.Status, res.Latency)
}
}
}
func httpUp(c chan CheckResult) {
defer wg.Done()
res, err := http.Get("https://icanhazip.com/")
result := CheckResult{}
if err != nil {
result.Up = false
return
}
result.Up = true
result.Status = res.Status + " icanhazip.com"
c <- result
}
func pingUp(c chan CheckResult) {
defer wg.Done()
checkResult := CheckResult{}
pinger, err := ping.NewPinger("8.8.8.8")
if err != nil {
checkResult.Up = false
checkResult.Status = err.Error()
return
}
pinger.Count = 3
pinger.Timeout = time.Second * 10
pingerErr := pinger.Run()
if pingerErr != nil {
panic(err)
}
stats := pinger.Statistics()
checkResult.Up = true
checkResult.Status = "Ping recv"
checkResult.Latency = stats.AvgRtt
c <- checkResult
}