-
Notifications
You must be signed in to change notification settings - Fork 3
/
psutil.go
39 lines (30 loc) · 817 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
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,
}
sw, _ := mem.SwapMemoryWithContext(ctx)
vi, _ := mem.VirtualMemoryWithContext(ctx)
r["mem"] = map[string]any{
"swap": sw,
"virtual": vi,
}
resp.Header().Add("Content-Type", "application/json")
b, _ := json.Marshal(r)
resp.Write(b)
}
mux.HandleFunc("/debug/psutil", psutil)
}