-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmonitor.go
More file actions
127 lines (118 loc) · 3.26 KB
/
monitor.go
File metadata and controls
127 lines (118 loc) · 3.26 KB
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
package monitor
import (
"github.com/bar-counter/monitor/v3/debug"
"github.com/bar-counter/monitor/v3/pprof"
"github.com/bar-counter/monitor/v3/status"
"github.com/gin-gonic/gin"
)
const (
DefaultStatusPrefix = "/status"
// DefaultVarsPrefix url prefix of /status/hardware
DefaultStatusHardwarePrefix = "/hardware"
DefaultDebugPrefix = "/debug"
// DefaultVarsPrefix url prefix of /debug/vars
DefaultVarsPrefix = "/vars"
// DefaultPrefix url prefix of /debug/pprof
DefaultPPROFPrefix = "/pprof"
)
type Cfg struct {
APIBase string
Status bool
StatusMiddleware gin.HandlerFunc
StatusPrefix string
StatusHardware bool
StatusHardwarePrefix string
Debug bool
DebugPrefix string
DebugMiddleware gin.HandlerFunc
VarsPrefix string
PProf bool
PProfPrefix string
}
// DefaultCfg
// config for monitor
var DefaultCfg *Cfg
func initDefaultCfg() *Cfg {
cfg := Cfg{
APIBase: "",
Status: false,
StatusPrefix: DefaultStatusPrefix,
StatusHardware: false,
StatusHardwarePrefix: DefaultStatusHardwarePrefix,
Debug: false,
DebugPrefix: DefaultDebugPrefix,
VarsPrefix: DefaultVarsPrefix,
PProf: false,
PProfPrefix: DefaultPPROFPrefix,
}
return &cfg
}
func Register(r *gin.Engine, cfg *Cfg) error {
checkCfg(cfg)
mGroup := r.Group(cfg.APIBase)
{
if cfg.Status {
statusGroup := mGroup.Group(cfg.StatusPrefix)
{
statusGroup.GET("/health", status.HealthCheck)
if cfg.StatusHardware {
if cfg.StatusMiddleware == nil {
statusHardwareGroup := statusGroup.Group(cfg.StatusHardwarePrefix)
{
statusHardwareGroup.GET("/disk", status.DiskCheck)
statusHardwareGroup.GET("/ram", status.RAMCheck)
statusHardwareGroup.GET("/cpu", status.CPUCheck)
statusHardwareGroup.GET("/cpu_info", status.CPUInfo)
}
} else {
statusHardwareGroup := statusGroup.Group(cfg.StatusHardwarePrefix, cfg.StatusMiddleware)
{
statusHardwareGroup.GET("/disk", status.DiskCheck)
statusHardwareGroup.GET("/ram", status.RAMCheck)
statusHardwareGroup.GET("/cpu", status.CPUCheck)
statusHardwareGroup.GET("/cpu_info", status.CPUInfo)
}
}
}
}
}
if cfg.Debug {
debug.PublishExpVarDebug()
var debugGroup *gin.RouterGroup
if cfg.DebugMiddleware != nil {
debugGroup = mGroup.Group(cfg.DebugPrefix, cfg.DebugMiddleware)
} else {
debugGroup = mGroup.Group(cfg.DebugPrefix)
}
{
debugGroup.GET(cfg.VarsPrefix, debug.GetMonitorRunningStats)
}
if cfg.PProf {
pprof.OnBind(debugGroup, cfg.PProfPrefix)
}
}
}
return nil
}
func checkCfg(cfg *Cfg) {
defaultCfg := initDefaultCfg()
if cfg.APIBase == "" {
cfg.APIBase = defaultCfg.APIBase
}
if cfg.StatusPrefix == "" {
cfg.StatusPrefix = defaultCfg.StatusPrefix
}
if cfg.StatusHardwarePrefix == "" {
cfg.StatusHardwarePrefix = defaultCfg.StatusHardwarePrefix
}
if cfg.DebugPrefix == "" {
cfg.DebugPrefix = defaultCfg.DebugPrefix
}
if cfg.VarsPrefix == "" {
cfg.VarsPrefix = defaultCfg.VarsPrefix
}
if cfg.PProfPrefix == "" {
cfg.PProfPrefix = defaultCfg.PProfPrefix
}
DefaultCfg = cfg
}