shirou_gopsutil/load/load_linux.go

79 lines
1.4 KiB
Go
Raw Normal View History

2014-04-18 15:34:47 +08:00
// +build linux
2014-12-30 21:09:05 +08:00
package load
2014-04-18 15:34:47 +08:00
import (
"io/ioutil"
"strconv"
"strings"
"github.com/shirou/gopsutil/internal/common"
2014-04-18 15:34:47 +08:00
)
func Avg() (*AvgStat, error) {
filename := common.HostProc("loadavg")
2014-04-18 15:34:47 +08:00
line, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
2014-04-18 15:34:47 +08:00
}
values := strings.Fields(string(line))
load1, err := strconv.ParseFloat(values[0], 64)
if err != nil {
return nil, err
2014-04-18 15:34:47 +08:00
}
load5, err := strconv.ParseFloat(values[1], 64)
if err != nil {
return nil, err
2014-04-18 15:34:47 +08:00
}
load15, err := strconv.ParseFloat(values[2], 64)
if err != nil {
return nil, err
2014-04-18 15:34:47 +08:00
}
ret := &AvgStat{
2014-04-18 15:34:47 +08:00
Load1: load1,
Load5: load5,
Load15: load15,
}
return ret, nil
}
2016-02-20 22:17:20 +08:00
// Misc returnes miscellaneous host-wide statistics.
// Note: the name should be changed near future.
func Misc() (*MiscStat, error) {
filename := common.HostProc("stat")
2016-02-20 22:03:32 +08:00
out, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
2016-02-20 22:03:32 +08:00
ret := &MiscStat{}
lines := strings.Split(string(out), "\n")
for _, line := range lines {
fields := strings.Fields(line)
if len(fields) != 2 {
continue
}
v, err := strconv.ParseInt(fields[1], 10, 64)
if err != nil {
continue
}
switch fields[0] {
case "procs_running":
2016-02-20 22:03:32 +08:00
ret.ProcsRunning = int(v)
case "procs_blocked":
2016-02-20 22:03:32 +08:00
ret.ProcsBlocked = int(v)
case "ctxt":
2016-02-20 22:03:32 +08:00
ret.Ctxt = int(v)
default:
continue
}
}
return ret, nil
}