/
stats.go
72 lines (66 loc) · 1.89 KB
/
stats.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package stats
import (
"os"
"runtime"
"time"
"github.com/TheThingsNetwork/ttn/api"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/load"
"github.com/shirou/gopsutil/mem"
"github.com/shirou/gopsutil/process"
)
var startTime = time.Now()
// GetSystem gets statistics about the system
func GetSystem() *api.SystemStats {
status := new(api.SystemStats)
if load, err := load.Avg(); err == nil {
status.Load = &api.SystemStats_Loadstats{
Load1: float32(load.Load1),
Load5: float32(load.Load5),
Load15: float32(load.Load15),
}
}
if cpu, err := cpu.Times(false); err == nil && len(cpu) == 1 {
status.Cpu = &api.SystemStats_CPUStats{
User: float32(cpu[0].User),
System: float32(cpu[0].System),
Idle: float32(cpu[0].Idle),
}
}
if mem, err := mem.VirtualMemory(); err == nil {
status.Memory = &api.SystemStats_MemoryStats{
Total: mem.Total,
Available: mem.Available,
Used: mem.Used,
}
}
return status
}
// GetComponent gets statistics about this component
func GetComponent() *api.ComponentStats {
status := new(api.ComponentStats)
status.Uptime = uint64(time.Now().Sub(startTime).Seconds())
process, err := process.NewProcess(int32(os.Getpid()))
if err == nil {
if memory, err := process.MemoryInfo(); err == nil {
status.Memory = &api.ComponentStats_MemoryStats{
Memory: memory.RSS,
Swap: memory.Swap,
}
}
if cpu, err := process.Times(); err == nil {
status.Cpu = &api.ComponentStats_CPUStats{
User: float32(cpu.User),
System: float32(cpu.System),
Idle: float32(cpu.Idle),
}
}
}
status.Goroutines = uint64(runtime.NumGoroutine())
memstats := new(runtime.MemStats)
runtime.ReadMemStats(memstats)
status.GcCpuFraction = float32(memstats.GCCPUFraction)
return status
}