-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.go
45 lines (35 loc) · 912 Bytes
/
cpu.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
package main
/**
* CPU Information
*/
import (
linuxproc "github.com/c9s/goprocinfo/linux"
)
type CPUInfo struct {
NumProcessors int
Load1Min float64
Load5Min float64
Load1MinPercentage float64
Load5MinPercentage float64
}
func NewCPUInfo() *CPUInfo {
info := &CPUInfo{}
// Read /proc/stat for overall CPU information
stats, statErr := linuxproc.ReadStat("/proc/stat")
if statErr == nil {
// How many processors do we have?
info.NumProcessors = len(stats.CPUStats)
}
// Read load average
loadavg, loadErr := linuxproc.ReadLoadAvg("/proc/loadavg")
if loadErr == nil {
info.Load1Min = loadavg.Last1Min
info.Load5Min = loadavg.Last5Min
}
// Calculate percentages
if info.NumProcessors > 0 {
info.Load1MinPercentage = info.Load1Min / float64(info.NumProcessors)
info.Load5MinPercentage = info.Load5Min / float64(info.NumProcessors)
}
return info
}