-
Notifications
You must be signed in to change notification settings - Fork 906
/
metrics.go
397 lines (310 loc) · 9.88 KB
/
metrics.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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package main
import (
"bufio"
"bytes"
"fmt"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/canonical/lxd/lxd/metrics"
"github.com/canonical/lxd/lxd/response"
"github.com/canonical/lxd/lxd/storage/filesystem"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/logger"
)
// These mountpoints are excluded as they are irrelevant for metrics.
// /var/lib/docker/* subdirectories are excluded for this reason: https://github.com/prometheus/node_exporter/pull/1003
var defMountPointsExcluded = regexp.MustCompile(`^/(?:dev|proc|sys|var/lib/docker/.+)(?:$|/)`)
var defFSTypesExcluded = []string{
"autofs", "binfmt_misc", "bpf", "cgroup", "cgroup2", "configfs", "debugfs", "devpts", "devtmpfs", "fusectl", "hugetlbfs", "iso9660", "mqueue", "nsfs", "overlay", "proc", "procfs", "pstore", "rpc_pipefs", "securityfs", "selinuxfs", "squashfs", "sysfs", "tracefs"}
var metricsCmd = APIEndpoint{
Path: "metrics",
Get: APIEndpointAction{Handler: metricsGet},
}
func metricsGet(d *Daemon, r *http.Request) response.Response {
out := metrics.Metrics{}
diskStats, err := getDiskMetrics(d)
if err != nil {
logger.Warn("Failed to get disk metrics", logger.Ctx{"err": err})
} else {
out.Disk = diskStats
}
filesystemStats, err := getFilesystemMetrics(d)
if err != nil {
logger.Warn("Failed to get filesystem metrics", logger.Ctx{"err": err})
} else {
out.Filesystem = filesystemStats
}
memStats, err := getMemoryMetrics(d)
if err != nil {
logger.Warn("Failed to get memory metrics", logger.Ctx{"err": err})
} else {
out.Memory = memStats
}
netStats, err := getNetworkMetrics(d)
if err != nil {
logger.Warn("Failed to get network metrics", logger.Ctx{"err": err})
} else {
out.Network = netStats
}
out.ProcessesTotal, err = getTotalProcesses(d)
if err != nil {
logger.Warn("Failed to get total processes", logger.Ctx{"err": err})
}
cpuStats, err := getCPUMetrics(d)
if err != nil {
logger.Warn("Failed to get CPU metrics", logger.Ctx{"err": err})
} else {
out.CPU = cpuStats
}
return response.SyncResponse(true, &out)
}
func getCPUMetrics(d *Daemon) (map[string]metrics.CPUMetrics, error) {
stats, err := os.ReadFile("/proc/stat")
if err != nil {
return nil, fmt.Errorf("Failed to read /proc/stat: %w", err)
}
out := map[string]metrics.CPUMetrics{}
scanner := bufio.NewScanner(bytes.NewReader(stats))
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
// Only consider CPU info, skip everything else. Skip aggregated CPU stats since there will
// be stats for each individual CPU.
if !strings.HasPrefix(fields[0], "cpu") || fields[0] == "cpu" {
continue
}
// Validate the number of fields only for lines starting with "cpu".
if len(fields) < 9 {
return nil, fmt.Errorf("Invalid /proc/stat content: %q", line)
}
stats := metrics.CPUMetrics{}
stats.SecondsUser, err = strconv.ParseFloat(fields[1], 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse %q: %w", fields[1], err)
}
stats.SecondsUser /= 100
stats.SecondsNice, err = strconv.ParseFloat(fields[2], 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse %q: %w", fields[2], err)
}
stats.SecondsNice /= 100
stats.SecondsSystem, err = strconv.ParseFloat(fields[3], 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse %q: %w", fields[3], err)
}
stats.SecondsSystem /= 100
stats.SecondsIdle, err = strconv.ParseFloat(fields[4], 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse %q: %w", fields[4], err)
}
stats.SecondsIdle /= 100
stats.SecondsIOWait, err = strconv.ParseFloat(fields[5], 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse %q: %w", fields[5], err)
}
stats.SecondsIOWait /= 100
stats.SecondsIRQ, err = strconv.ParseFloat(fields[6], 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse %q: %w", fields[6], err)
}
stats.SecondsIRQ /= 100
stats.SecondsSoftIRQ, err = strconv.ParseFloat(fields[7], 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse %q: %w", fields[7], err)
}
stats.SecondsSoftIRQ /= 100
stats.SecondsSteal, err = strconv.ParseFloat(fields[8], 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse %q: %w", fields[8], err)
}
stats.SecondsSteal /= 100
out[fields[0]] = stats
}
return out, nil
}
func getTotalProcesses(d *Daemon) (uint64, error) {
entries, err := os.ReadDir("/proc")
if err != nil {
return 0, fmt.Errorf("Failed to read dir %q: %w", "/proc", err)
}
pidCount := uint64(0)
for _, entry := range entries {
// Skip everything which isn't a directory
if !entry.IsDir() {
continue
}
name := entry.Name()
// Skip all non-PID directories
_, err := strconv.ParseUint(name, 10, 64)
if err != nil {
continue
}
cmdlinePath := filepath.Join("/proc", name, "cmdline")
cmdline, err := os.ReadFile(cmdlinePath)
if err != nil {
continue
}
if string(cmdline) == "" {
continue
}
pidCount++
}
return pidCount, nil
}
func getDiskMetrics(d *Daemon) (map[string]metrics.DiskMetrics, error) {
diskStats, err := os.ReadFile("/proc/diskstats")
if err != nil {
return nil, fmt.Errorf("Failed to read /proc/diskstats: %w", err)
}
out := map[string]metrics.DiskMetrics{}
scanner := bufio.NewScanner(bytes.NewReader(diskStats))
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
fields := strings.Fields(line)
if len(fields) < 10 {
return nil, fmt.Errorf("Invalid /proc/diskstats content: %q", line)
}
stats := metrics.DiskMetrics{}
stats.ReadsCompleted, err = strconv.ParseUint(fields[3], 10, 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse %q: %w", fields[3], err)
}
sectorsRead, err := strconv.ParseUint(fields[5], 10, 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse %q: %w", fields[3], err)
}
stats.ReadBytes = sectorsRead * 512
stats.WritesCompleted, err = strconv.ParseUint(fields[7], 10, 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse %q: %w", fields[3], err)
}
sectorsWritten, err := strconv.ParseUint(fields[9], 10, 64)
if err != nil {
return nil, fmt.Errorf("Failed to parse %q: %w", fields[3], err)
}
stats.WrittenBytes = sectorsWritten * 512
out[fields[2]] = stats
}
return out, nil
}
func getFilesystemMetrics(d *Daemon) (map[string]metrics.FilesystemMetrics, error) {
mounts, err := os.ReadFile("/proc/mounts")
if err != nil {
return nil, fmt.Errorf("Failed to read /proc/mounts: %w", err)
}
out := map[string]metrics.FilesystemMetrics{}
scanner := bufio.NewScanner(bytes.NewReader(mounts))
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) < 3 {
return nil, fmt.Errorf("Invalid /proc/mounts content: %q", line)
}
// Skip uninteresting mounts
if shared.ValueInSlice(fields[2], defFSTypesExcluded) || defMountPointsExcluded.MatchString(fields[1]) {
continue
}
stats := metrics.FilesystemMetrics{}
stats.Mountpoint = fields[1]
statfs, err := filesystem.StatVFS(stats.Mountpoint)
if err != nil {
return nil, fmt.Errorf("Failed to stat %s: %w", stats.Mountpoint, err)
}
fsType, err := filesystem.FSTypeToName(int32(statfs.Type))
if err == nil {
stats.FSType = fsType
}
stats.AvailableBytes = statfs.Bavail * uint64(statfs.Bsize)
stats.FreeBytes = statfs.Bfree * uint64(statfs.Bsize)
stats.SizeBytes = statfs.Blocks * uint64(statfs.Bsize)
out[fields[0]] = stats
}
return out, nil
}
func getMemoryMetrics(d *Daemon) (metrics.MemoryMetrics, error) {
content, err := os.ReadFile("/proc/meminfo")
if err != nil {
return metrics.MemoryMetrics{}, fmt.Errorf("Failed to read /proc/meminfo: %w", err)
}
out := metrics.MemoryMetrics{}
scanner := bufio.NewScanner(bytes.NewReader(content))
for scanner.Scan() {
line := scanner.Text()
fields := strings.Fields(line)
if len(fields) < 2 {
return metrics.MemoryMetrics{}, fmt.Errorf("Invalid /proc/meminfo content: %q", line)
}
fields[0] = strings.TrimRight(fields[0], ":")
value, err := strconv.ParseUint(fields[1], 10, 64)
if err != nil {
return metrics.MemoryMetrics{}, fmt.Errorf("Failed to parse %q: %w", fields[1], err)
}
// Multiply suffix (kB)
if len(fields) == 3 {
value *= 1024
}
// FIXME: Missing RSS
switch fields[0] {
case "Active":
out.ActiveBytes = value
case "Active(anon)":
out.ActiveAnonBytes = value
case "Active(file)":
out.ActiveFileBytes = value
case "Cached":
out.CachedBytes = value
case "Dirty":
out.DirtyBytes = value
case "HugePages_Free":
out.HugepagesFreeBytes = value
case "HugePages_Total":
out.HugepagesTotalBytes = value
case "Inactive":
out.InactiveBytes = value
case "Inactive(anon)":
out.InactiveAnonBytes = value
case "Inactive(file)":
out.InactiveFileBytes = value
case "Mapped":
out.MappedBytes = value
case "MemAvailable":
out.MemAvailableBytes = value
case "MemFree":
out.MemFreeBytes = value
case "MemTotal":
out.MemTotalBytes = value
case "Shmem":
out.ShmemBytes = value
case "SwapCached":
out.SwapBytes = value
case "Unevictable":
out.UnevictableBytes = value
case "Writeback":
out.WritebackBytes = value
}
}
return out, nil
}
func getNetworkMetrics(d *Daemon) (map[string]metrics.NetworkMetrics, error) {
out := map[string]metrics.NetworkMetrics{}
for dev, state := range networkState() {
stats := metrics.NetworkMetrics{}
stats.ReceiveBytes = uint64(state.Counters.BytesReceived)
stats.ReceiveDrop = uint64(state.Counters.PacketsDroppedInbound)
stats.ReceiveErrors = uint64(state.Counters.ErrorsReceived)
stats.ReceivePackets = uint64(state.Counters.PacketsReceived)
stats.TransmitBytes = uint64(state.Counters.BytesSent)
stats.TransmitDrop = uint64(state.Counters.PacketsDroppedOutbound)
stats.TransmitErrors = uint64(state.Counters.ErrorsSent)
stats.TransmitPackets = uint64(state.Counters.PacketsSent)
out[dev] = stats
}
return out, nil
}