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"
|
2015-10-17 01:58:17 +08:00
|
|
|
|
2015-10-19 11:40:01 +08:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2014-04-18 15:34:47 +08:00
|
|
|
)
|
|
|
|
|
2014-05-01 11:01:30 +08:00
|
|
|
func LoadAvg() (*LoadAvgStat, error) {
|
2015-10-19 11:40:01 +08:00
|
|
|
filename := common.HostProc("loadavg")
|
2014-04-18 15:34:47 +08:00
|
|
|
line, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
2014-05-01 11:01:30 +08:00
|
|
|
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 {
|
2014-05-01 11:01:30 +08:00
|
|
|
return nil, err
|
2014-04-18 15:34:47 +08:00
|
|
|
}
|
|
|
|
load5, err := strconv.ParseFloat(values[1], 64)
|
|
|
|
if err != nil {
|
2014-05-01 11:01:30 +08:00
|
|
|
return nil, err
|
2014-04-18 15:34:47 +08:00
|
|
|
}
|
|
|
|
load15, err := strconv.ParseFloat(values[2], 64)
|
|
|
|
if err != nil {
|
2014-05-01 11:01:30 +08:00
|
|
|
return nil, err
|
2014-04-18 15:34:47 +08:00
|
|
|
}
|
|
|
|
|
2014-05-01 11:01:30 +08:00
|
|
|
ret := &LoadAvgStat{
|
2014-04-18 15:34:47 +08:00
|
|
|
Load1: load1,
|
|
|
|
Load5: load5,
|
|
|
|
Load15: load15,
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
2016-02-20 21:52:16 +08:00
|
|
|
|
|
|
|
func Misc() (*MiscStat, error) {
|
|
|
|
filename := common.HostProc("stat")
|
|
|
|
lines, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := &Misc{
|
|
|
|
ProcsRunning: pr,
|
|
|
|
ProcsBlocked: pb,
|
|
|
|
Ctxt: ctxt,
|
|
|
|
}
|
|
|
|
|
|
|
|
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":
|
|
|
|
ret.ProcessRunning = v
|
|
|
|
case "procs_blocked":
|
|
|
|
ret.ProcessBlocked = v
|
|
|
|
case "ctxt":
|
|
|
|
ret.Ctxt = v
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|