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 (
|
2017-12-31 14:25:49 +08:00
|
|
|
"context"
|
2014-04-18 15:34:47 +08:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
func Avg() (*AvgStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return AvgWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func AvgWithContext(ctx context.Context) (*AvgStat, error) {
|
2019-05-05 10:29:20 +08:00
|
|
|
values, err := readLoadAvgFromFile()
|
2014-04-18 15:34:47 +08:00
|
|
|
if err != nil {
|
2014-05-01 11:01:30 +08:00
|
|
|
return nil, err
|
2014-04-18 15:34:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-22 22:09:12 +08:00
|
|
|
ret := &AvgStat{
|
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
|
|
|
|
2016-02-20 22:17:20 +08:00
|
|
|
// Misc returnes miscellaneous host-wide statistics.
|
|
|
|
// Note: the name should be changed near future.
|
2016-02-20 21:52:16 +08:00
|
|
|
func Misc() (*MiscStat, error) {
|
2017-12-31 14:25:49 +08:00
|
|
|
return MiscWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func MiscWithContext(ctx context.Context) (*MiscStat, error) {
|
2016-02-20 21:52:16 +08:00
|
|
|
filename := common.HostProc("stat")
|
2016-02-20 22:03:32 +08:00
|
|
|
out, err := ioutil.ReadFile(filename)
|
2016-02-20 21:52:16 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-02-20 22:03:32 +08:00
|
|
|
ret := &MiscStat{}
|
|
|
|
lines := strings.Split(string(out), "\n")
|
2016-02-20 21:52:16 +08:00
|
|
|
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)
|
2016-02-20 21:52:16 +08:00
|
|
|
case "procs_blocked":
|
2016-02-20 22:03:32 +08:00
|
|
|
ret.ProcsBlocked = int(v)
|
2016-02-20 21:52:16 +08:00
|
|
|
case "ctxt":
|
2016-02-20 22:03:32 +08:00
|
|
|
ret.Ctxt = int(v)
|
2016-02-20 21:52:16 +08:00
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-05-05 10:29:20 +08:00
|
|
|
procsTotal, err := getProcsTotal()
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
ret.ProcsTotal = int(procsTotal)
|
|
|
|
|
2016-02-20 21:52:16 +08:00
|
|
|
return ret, nil
|
|
|
|
}
|
2019-05-05 10:29:20 +08:00
|
|
|
|
|
|
|
func getProcsTotal() (int64, error) {
|
|
|
|
values, err := readLoadAvgFromFile()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return strconv.ParseInt(strings.Split(values[3], "/")[1], 10, 64)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readLoadAvgFromFile() ([]string, error) {
|
|
|
|
loadavgFilename := common.HostProc("loadavg")
|
|
|
|
line, err := ioutil.ReadFile(loadavgFilename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
values := strings.Fields(string(line))
|
|
|
|
return values, nil
|
|
|
|
}
|