-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (74 loc) · 1.81 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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"strings"
"time"
)
var status struct {
Ok bool `json:"ok"`
Msg string `json:"msg"`
LastUpdate time.Time `json:"last_update"`
}
var serveAddr = flag.String("addr", ":80", "the address to listen on")
var updateFreqMinutes = flag.Duration("duration", time.Minute*1, "the duration between two detects, in minutes")
func init() {
flag.Parse()
}
func main() {
checkConfiguration()
initStatus()
go startCheckerServer()
startHttpServer()
}
func initStatus() {
status.Ok = true
status.Msg = "监控探针已经启动"
status.LastUpdate = time.Now()
}
func checkConfiguration() {
if *updateFreqMinutes >= 365*24*60*time.Minute {
log.Fatalln("The specified duration is too long!")
}
}
func startCheckerServer() {
for now := range time.Tick(*updateFreqMinutes) {
log.Printf("Checking network status.")
checkHttpConnection()
status.LastUpdate = now
}
}
func checkHttpConnection() {
resp, err := http.Get("http://www.baidu.com/")
if err == nil {
if resp.StatusCode == 200 {
status.Ok = true
status.Msg = "一切正常"
} else {
status.Ok = false
status.Msg = "似乎不能正常连接到百度"
}
} else {
if strings.Contains(err.Error(), ": EOF") {
status.Ok = false
status.Msg = "外网连接被网络中心阻断"
} else {
status.Ok = false
status.Msg = fmt.Sprintf("访问失败:%s", err.Error())
}
}
log.Println(status)
}
func startHttpServer() {
http.Handle("/", http.FileServer(http.Dir("./static")))
http.HandleFunc("/api/status", func(writer http.ResponseWriter, request *http.Request) {
encoder := json.NewEncoder(writer)
_ = encoder.Encode(status)
})
if http.ListenAndServe(*serveAddr, nil) != nil {
log.Fatalf("Unable to listen on address `%s`.\n", *serveAddr)
}
}