2018-01-31 17:05:54 +00:00
|
|
|
// +build linux
|
2017-06-14 13:11:40 +00:00
|
|
|
|
2017-06-12 14:12:03 +00:00
|
|
|
package collector
|
2017-06-09 17:15:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
linuxproc "github.com/c9s/goprocinfo/linux"
|
|
|
|
)
|
|
|
|
|
|
|
|
var sysMemTotal = getSysMemTotal()
|
|
|
|
|
2020-10-25 14:22:47 +00:00
|
|
|
const (
|
|
|
|
clockTicksPerSecond uint64 = 100
|
|
|
|
nanoSecondsPerSecond = 1e9
|
|
|
|
)
|
2017-06-09 17:15:12 +00:00
|
|
|
|
|
|
|
func getSysMemTotal() int64 {
|
|
|
|
stat, err := linuxproc.ReadMemInfo("/proc/meminfo")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error reading system stats: %s", err)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return int64(stat.MemTotal * 1024)
|
|
|
|
}
|
|
|
|
|
2017-06-10 13:00:54 +00:00
|
|
|
// return cumulative system cpu usage in nanoseconds
|
2017-06-09 17:15:12 +00:00
|
|
|
func getSysCPUUsage() uint64 {
|
|
|
|
stat, err := linuxproc.ReadStat("/proc/stat")
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("error reading system stats: %s", err)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
sum := stat.CPUStatAll.User +
|
|
|
|
stat.CPUStatAll.Nice +
|
|
|
|
stat.CPUStatAll.System +
|
|
|
|
stat.CPUStatAll.Idle +
|
|
|
|
stat.CPUStatAll.IOWait +
|
|
|
|
stat.CPUStatAll.IRQ +
|
|
|
|
stat.CPUStatAll.SoftIRQ +
|
|
|
|
stat.CPUStatAll.Steal +
|
|
|
|
stat.CPUStatAll.Guest +
|
|
|
|
stat.CPUStatAll.GuestNice
|
|
|
|
|
|
|
|
return (sum * nanoSecondsPerSecond) / clockTicksPerSecond
|
|
|
|
}
|