-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_handlers.go
More file actions
65 lines (51 loc) · 1.68 KB
/
system_handlers.go
File metadata and controls
65 lines (51 loc) · 1.68 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
package main
import (
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/mem"
"github.com/valyala/fasthttp"
"runtime"
"time"
"fmt"
)
var handel_get_system_status = func(ctx *fasthttp.RequestCtx) {
cpuUsage, _ := cpu.Percent(time.Second, false) // Error ignored for simplicity
cpuCounts, _ := cpu.Counts(true) // Error ignored for simplicity
var memory runtime.MemStats
runtime.ReadMemStats(&memory)
virtualMem, _ := mem.VirtualMemory() // Error ignored for simplicity
uptimeStr,uptimeInt := GetUptime()
result := map[string]interface{}{
"cpu_usage": cpuUsage[0],
"cpu_count": cpuCounts,
"mem_usage": virtualMem.UsedPercent,
"mem_size": float64(virtualMem.Total) / float64(1024*1024),
"uptime_str": uptimeStr,
"uptime_int": uptimeInt,
}
json_response, err := Json(result)
ctx_error_handler(ctx, err)
fmt.Fprintf(ctx, json_response)
}
var handel_get_system_mem = func(ctx *fasthttp.RequestCtx) {
var memory runtime.MemStats
runtime.ReadMemStats(&memory)
virtualMem, _ := mem.VirtualMemory() // Error ignored for simplicity
result := map[string]interface{}{
"mem_total_alloc": memory.TotalAlloc,
"mem_heap_alloc": memory.HeapAlloc,
"mem_alloc": memory.Alloc,
"mem_swap": virtualMem.SwapTotal,
"mem_sys": memory.Sys,
"mem_size": float64(virtualMem.Total) / float64(1024*1024),
}
json_response, err := Json(result)
ctx_error_handler(ctx, err)
fmt.Fprintf(ctx, json_response)
}
var handel_get_system_disk = func(ctx *fasthttp.RequestCtx) {
result, err := GetDiskUsage()
ctx_error_handler(ctx, err)
json_response, err := Json(result)
ctx_error_handler(ctx, err)
fmt.Fprintf(ctx, json_response)
}