-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
114 lines (107 loc) · 3.53 KB
/
monitor.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
package middleware
import (
"fmt"
"github.com/bridgewwater/gin-api-swagger-temple/internal/zlog"
"net/http"
"time"
"github.com/bar-counter/monitor/v2"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
// monitorAPI
//
// use monitor https://github.com/bar-counter/monitor
//
// config.yml like
//
// monitor: # monitor
// status: true # api status use {monitor.health}
// health: /status/health # api health
// retryCount: 10 # ping api health retry count
// hardware: true # hardware true or false
// status_hardware:
// disk: /status/hardware/disk # hardware api disk
// cpu: /status/hardware/cpu # hardware api cpu
// ram: /status/hardware/ram # hardware api ram
// debug: true # debug true or false
// pprof: true # security true or false
// security: false # debug and security true or false
// securityUser:
// admin: pwd # admin:pwd
func monitorAPI(g *gin.Engine) {
var monitorCfg *monitor.Cfg
isSecurity := viper.GetBool("monitor.security")
if isSecurity {
monitorCfg = &monitor.Cfg{
Status: viper.GetBool("monitor.status"),
StatusHardware: viper.GetBool("monitor.hardware"),
Debug: viper.GetBool("monitor.debug"),
DebugMiddleware: gin.BasicAuth(gin.Accounts{
"admin": viper.GetString("monitor.securityUser.admin"),
}),
PProf: viper.GetBool("monitor.pprof"),
}
} else {
monitorCfg = &monitor.Cfg{
Status: viper.GetBool("monitor.status"),
StatusHardware: viper.GetBool("monitor.status_hardware"),
Debug: viper.GetBool("monitor.debug"),
PProf: viper.GetBool("monitor.pprof"),
}
}
err := monitor.Register(g, monitorCfg)
if err != nil {
zlog.S().Errorf("monitor Register err %v", err)
}
}
// checkPingServer
//
// ping the server to make sure the router is working.
// use config.yml as
//
// apiBaseURL load by github.com/spf13/viper
//
// viper config.yml
//
// monitor: # monitor
// status: true # api status use {monitor.health}
// health: /status/health # api health
// retryCount: 10 # ping api health retry count
func checkPingServer(apiBaseURL string) {
// Ping the server to make sure the router is working.
go func() {
if err := pingServer(apiBaseURL, viper.GetString("monitor.health")); err != nil {
zlog.S().Errorf("The router has no response, or it might took too long to start up. err %v", err)
}
zlog.S().Info("The router has been deployed successfully.")
}()
}
// PingServer
//
// ping server pings the http server
//
// apiBaseURL load by github.com/spf13/viper
// checkRouter monitor.health by github.com/spf13/viper
//
// viper config.yml
// monitor: # monitor
// status: true # api status use {monitor.health}
// health: /status/health # api health
// retryCount: 10 # ping api health retry count
func pingServer(apiBaseURL, checkRouter string) error {
pingApi := apiBaseURL + checkRouter
zlog.S().Infof("pingServer test api : %v", pingApi)
for i := 0; i < viper.GetInt("monitor.retryCount"); i++ {
// Ping the server by sending a GET request to `/health`.
resp, err := http.Get(pingApi)
if err == nil && resp.StatusCode == 200 {
zlog.S().Infof("pingServer test pass api at: %v", pingApi)
return nil
}
// sleep for a second to continue the next ping.
zlog.S().Warnf("Waiting for the router, retry in 1 second. Check URL: %v", pingApi)
time.Sleep(time.Second)
}
//noinspection ALL
return fmt.Errorf("Can not connect to the router %v.", pingApi)
}