-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsysstats_linux.go
100 lines (83 loc) · 2.6 KB
/
sysstats_linux.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
// Copyright 2020 Albert "Drauthius" Diserholt. All rights reserved.
// Licensed under the MIT License.
// +build linux
// Get system status from /proc/ and /sys/ (Linux edition)
package main
import (
"log"
"math"
"time"
linuxproc "github.com/c9s/goprocinfo/linux"
)
// Get system statistics at the specified interval.
// This will get the current CPU, memory, swap, and disk usage in fractions (0.0-1.0)
func SystemStats(interval time.Duration, results chan []float64, quit chan bool) {
var prevIdle, prevTotal, prevIOTicks uint64
var prevUptime float64
defer close(results)
for {
var cpu, mem, swap, disk float64
stats, err := linuxproc.ReadStat("/proc/stat")
if err != nil {
log.Println("Failed to retrieve stat information:", err)
} else {
idle := stats.CPUStatAll.Idle + stats.CPUStatAll.IOWait
nonIdle := stats.CPUStatAll.User + stats.CPUStatAll.Nice + stats.CPUStatAll.System + stats.CPUStatAll.IRQ + stats.CPUStatAll.SoftIRQ + stats.CPUStatAll.Steal
total := idle + nonIdle
if prevIdle != 0 && prevTotal != 0 {
totalDelta := total - prevTotal
idleDelta := idle - prevIdle
cpu = math.Max(float64(totalDelta-idleDelta)/float64(totalDelta), 0)
if *gArgs.debug {
log.Println("CPU%: ", cpu*100)
}
}
prevIdle = idle
prevTotal = total
}
meminfo, err := linuxproc.ReadMemInfo("/proc/meminfo")
if err != nil {
log.Println("Failed to retrieve meminfo information:", err)
} else {
mem = math.Max(float64(meminfo.MemTotal-meminfo.MemAvailable)/float64(meminfo.MemTotal), 0)
swap = math.Max(float64(meminfo.SwapTotal-meminfo.SwapFree)/float64(meminfo.SwapTotal), 0)
if math.IsInf(swap, 0) {
// In case there is no swap.
swap = 0
}
if *gArgs.debug {
log.Println("Mem%: ", mem*100)
log.Println("Swap%:", swap*100)
}
}
uptime, err := linuxproc.ReadUptime("/proc/uptime")
if err != nil {
log.Println("Failed to retrieve uptime information:", err)
} else {
diskStats, err := linuxproc.ReadDiskStats("/proc/diskstats")
if err != nil {
log.Println("Failed to retrieve disk status information:", err)
} else {
for _, diskStat := range diskStats {
if diskStat.Name == *gArgs.sysStatDisk {
if prevIOTicks != 0 {
disk = math.Max(float64(diskStat.IOTicks-prevIOTicks)/(uptime.Total-prevUptime)/1000, 0)
if *gArgs.debug {
log.Println("Disk%:", disk*100)
}
}
prevIOTicks = diskStat.IOTicks
break
}
}
}
prevUptime = uptime.Total
}
results <- []float64{cpu, mem, swap, disk}
select {
case <-quit:
return
case <-time.After(interval):
}
}
}