forked from gofiber/fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
129 lines (106 loc) · 2.92 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package monitor
import (
"os"
"sync"
"sync/atomic"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/internal/gopsutil/cpu"
"github.com/gofiber/fiber/v2/internal/gopsutil/load"
"github.com/gofiber/fiber/v2/internal/gopsutil/mem"
"github.com/gofiber/fiber/v2/internal/gopsutil/net"
"github.com/gofiber/fiber/v2/internal/gopsutil/process"
)
type stats struct {
PID statsPID `json:"pid"`
OS statsOS `json:"os"`
}
type statsPID struct {
CPU float64 `json:"cpu"`
RAM uint64 `json:"ram"`
Conns int `json:"conns"`
}
type statsOS struct {
CPU float64 `json:"cpu"`
RAM uint64 `json:"ram"`
TotalRAM uint64 `json:"total_ram"`
LoadAvg float64 `json:"load_avg"`
Conns int `json:"conns"`
}
var (
monitPidCpu atomic.Value
monitPidRam atomic.Value
monitPidConns atomic.Value
monitOsCpu atomic.Value
monitOsRam atomic.Value
monitOsTotalRam atomic.Value
monitOsLoadAvg atomic.Value
monitOsConns atomic.Value
)
var (
mutex sync.RWMutex
once sync.Once
data = &stats{}
)
// New creates a new middleware handler
func New(config ...Config) fiber.Handler {
// Set default config
cfg := configDefault(config...)
// Start routine to update statistics
once.Do(func() {
p, _ := process.NewProcess(int32(os.Getpid()))
updateStatistics(p)
go func() {
for {
time.Sleep(cfg.Refresh)
updateStatistics(p)
}
}()
})
// Return new handler
return func(c *fiber.Ctx) error {
// Don't execute middleware if Next returns true
if cfg.Next != nil && cfg.Next(c) {
return c.Next()
}
if c.Method() != fiber.MethodGet {
return fiber.ErrMethodNotAllowed
}
if c.Get(fiber.HeaderAccept) == fiber.MIMEApplicationJSON || cfg.APIOnly {
mutex.Lock()
data.PID.CPU = monitPidCpu.Load().(float64)
data.PID.RAM = monitPidRam.Load().(uint64)
data.PID.Conns = monitPidConns.Load().(int)
data.OS.CPU = monitOsCpu.Load().(float64)
data.OS.RAM = monitOsRam.Load().(uint64)
data.OS.TotalRAM = monitOsTotalRam.Load().(uint64)
data.OS.LoadAvg = monitOsLoadAvg.Load().(float64)
data.OS.Conns = monitOsConns.Load().(int)
mutex.Unlock()
return c.Status(fiber.StatusOK).JSON(data)
}
c.Set(fiber.HeaderContentType, fiber.MIMETextHTMLCharsetUTF8)
return c.Status(fiber.StatusOK).SendString(cfg.index)
}
}
func updateStatistics(p *process.Process) {
pidCpu, _ := p.CPUPercent()
monitPidCpu.Store(pidCpu / 10)
if osCpu, _ := cpu.Percent(0, false); len(osCpu) > 0 {
monitOsCpu.Store(osCpu[0])
}
if pidMem, _ := p.MemoryInfo(); pidMem != nil {
monitPidRam.Store(pidMem.RSS)
}
if osMem, _ := mem.VirtualMemory(); osMem != nil {
monitOsRam.Store(osMem.Used)
monitOsTotalRam.Store(osMem.Total)
}
if loadAvg, _ := load.Avg(); loadAvg != nil {
monitOsLoadAvg.Store(loadAvg.Load1)
}
pidConns, _ := net.ConnectionsPid("tcp", p.Pid)
monitPidConns.Store(len(pidConns))
osConns, _ := net.Connections("tcp")
monitOsConns.Store(len(osConns))
}