-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhardware_linux.go
More file actions
218 lines (193 loc) · 4.32 KB
/
Copy pathhardware_linux.go
File metadata and controls
218 lines (193 loc) · 4.32 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//go:build linux
package pulse
import (
"bufio"
"os"
"path/filepath"
"strconv"
"strings"
"sync"
"syscall"
)
type hardwareCollector struct {
mu sync.Mutex
prevIdle uint64
prevTotal uint64
havePrev bool
}
func newHardwareCollector() *hardwareCollector {
return &hardwareCollector{}
}
func (h *hardwareCollector) Collect() map[string]float64 {
out := make(map[string]float64)
cpuPercent := h.readCPUPercent()
if cpuPercent >= 0 {
out["hw.cpu_percent"] = cpuPercent
}
totalMem, availMem, okMem := readMemInfo()
if okMem {
out["hw.mem_total_bytes"] = float64(totalMem)
out["hw.mem_available_bytes"] = float64(availMem)
if totalMem > 0 {
used := totalMem - availMem
out["hw.mem_used_percent"] = (float64(used) / float64(totalMem)) * 100.0
}
}
totalDisk, usedDisk, okDisk := readDiskUsage("/")
if okDisk {
out["hw.disk_total_bytes"] = float64(totalDisk)
out["hw.disk_used_bytes"] = float64(usedDisk)
if totalDisk > 0 {
out["hw.disk_used_percent"] = (float64(usedDisk) / float64(totalDisk)) * 100.0
}
}
l1, l5, l15, okLoad := readLoadAvg()
if okLoad {
out["hw.load1"] = l1
out["hw.load5"] = l5
out["hw.load15"] = l15
}
if tempC, okTemp := readCPUTempCelsius(); okTemp {
out["hw.cpu_temp_celsius"] = tempC
}
return out
}
func (h *hardwareCollector) readCPUPercent() float64 {
idle, total, ok := readProcStatCPU()
if !ok {
return -1
}
h.mu.Lock()
defer h.mu.Unlock()
if !h.havePrev {
h.prevIdle = idle
h.prevTotal = total
h.havePrev = true
return 0
}
deltaIdle := idle - h.prevIdle
deltaTotal := total - h.prevTotal
h.prevIdle = idle
h.prevTotal = total
if deltaTotal == 0 {
return 0
}
usage := (1.0 - float64(deltaIdle)/float64(deltaTotal)) * 100.0
if usage < 0 {
return 0
}
if usage > 100 {
return 100
}
return usage
}
func readProcStatCPU() (idle uint64, total uint64, ok bool) {
f, err := os.Open("/proc/stat")
if err != nil {
return 0, 0, false
}
defer f.Close()
s := bufio.NewScanner(f)
if !s.Scan() {
return 0, 0, false
}
line := s.Text()
fields := strings.Fields(line)
if len(fields) < 5 || fields[0] != "cpu" {
return 0, 0, false
}
vals := make([]uint64, 0, len(fields)-1)
for _, f := range fields[1:] {
v, err := strconv.ParseUint(f, 10, 64)
if err != nil {
return 0, 0, false
}
vals = append(vals, v)
}
var sum uint64
for _, v := range vals {
sum += v
}
idleVal := vals[3]
if len(vals) > 4 {
idleVal += vals[4] // include iowait
}
return idleVal, sum, true
}
func readMemInfo() (total uint64, available uint64, ok bool) {
b, err := os.ReadFile("/proc/meminfo")
if err != nil {
return 0, 0, false
}
lines := strings.Split(string(b), "\n")
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) < 2 {
continue
}
switch fields[0] {
case "MemTotal:":
v, err := strconv.ParseUint(fields[1], 10, 64)
if err == nil {
total = v * 1024
}
case "MemAvailable:":
v, err := strconv.ParseUint(fields[1], 10, 64)
if err == nil {
available = v * 1024
}
}
}
if total == 0 {
return 0, 0, false
}
if available > total {
available = total
}
return total, available, true
}
func readDiskUsage(path string) (total uint64, used uint64, ok bool) {
var st syscall.Statfs_t
if err := syscall.Statfs(path, &st); err != nil {
return 0, 0, false
}
totalBytes := st.Blocks * uint64(st.Bsize)
usedBytes := (st.Blocks - st.Bfree) * uint64(st.Bsize)
return totalBytes, usedBytes, true
}
func readLoadAvg() (l1 float64, l5 float64, l15 float64, ok bool) {
b, err := os.ReadFile("/proc/loadavg")
if err != nil {
return 0, 0, 0, false
}
fields := strings.Fields(string(b))
if len(fields) < 3 {
return 0, 0, 0, false
}
v1, err1 := strconv.ParseFloat(fields[0], 64)
v5, err2 := strconv.ParseFloat(fields[1], 64)
v15, err3 := strconv.ParseFloat(fields[2], 64)
if err1 != nil || err2 != nil || err3 != nil {
return 0, 0, 0, false
}
return v1, v5, v15, true
}
func readCPUTempCelsius() (float64, bool) {
paths, err := filepath.Glob("/sys/class/thermal/thermal_zone*/temp")
if err != nil || len(paths) == 0 {
return 0, false
}
for _, path := range paths {
b, err := os.ReadFile(path)
if err != nil {
continue
}
raw := strings.TrimSpace(string(b))
value, err := strconv.ParseFloat(raw, 64)
if err != nil {
continue
}
return value / 1000.0, true
}
return 0, false
}