-
Notifications
You must be signed in to change notification settings - Fork 3
/
psutil.go
41 lines (32 loc) · 878 Bytes
/
psutil.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
package debug
import (
"net/http"
"github.com/alexfalkowski/go-service/marshaller"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/mem"
)
// RegisterPprof for debug.
func RegisterPsutil(mux *http.ServeMux, json *marshaller.JSON) {
psutil := func(resp http.ResponseWriter, req *http.Request) {
ctx := req.Context()
r := make(map[string]any)
i, _ := cpu.InfoWithContext(ctx)
t, _ := cpu.TimesWithContext(ctx, true)
r["cpu"] = map[string]any{
"info": i,
"times": t,
}
sm, _ := mem.SwapMemoryWithContext(ctx)
sd, _ := mem.SwapDevicesWithContext(ctx)
vm, _ := mem.VirtualMemoryWithContext(ctx)
r["mem"] = map[string]any{
"swap": sm,
"devices": sd,
"virtual": vm,
}
resp.Header().Add("Content-Type", "application/json")
b, _ := json.Marshal(r)
resp.Write(b)
}
mux.HandleFunc("/debug/psutil", psutil)
}