From 9715668586b837e5d830e4d445349804dad4ed56 Mon Sep 17 00:00:00 2001 From: pytimer Date: Sun, 5 May 2019 10:29:20 +0800 Subject: [PATCH] add ProcsTotal in load --- load/load.go | 1 + load/load_linux.go | 30 ++++++++++++++++++++++++++---- load/load_test.go | 3 ++- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/load/load.go b/load/load.go index 9085889..86d9114 100644 --- a/load/load.go +++ b/load/load.go @@ -20,6 +20,7 @@ func (l AvgStat) String() string { } type MiscStat struct { + ProcsTotal int `json:"procsTotal"` ProcsRunning int `json:"procsRunning"` ProcsBlocked int `json:"procsBlocked"` Ctxt int `json:"ctxt"` diff --git a/load/load_linux.go b/load/load_linux.go index 63c26a2..3489eba 100644 --- a/load/load_linux.go +++ b/load/load_linux.go @@ -16,14 +16,11 @@ func Avg() (*AvgStat, error) { } func AvgWithContext(ctx context.Context) (*AvgStat, error) { - filename := common.HostProc("loadavg") - line, err := ioutil.ReadFile(filename) + values, err := readLoadAvgFromFile() if err != nil { return nil, err } - values := strings.Fields(string(line)) - load1, err := strconv.ParseFloat(values[0], 64) if err != nil { return nil, err @@ -83,5 +80,30 @@ func MiscWithContext(ctx context.Context) (*MiscStat, error) { } + procsTotal, err := getProcsTotal() + if err != nil { + return ret, err + } + ret.ProcsTotal = int(procsTotal) + return ret, nil } + +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 +} diff --git a/load/load_test.go b/load/load_test.go index d6855de..0aa4de9 100644 --- a/load/load_test.go +++ b/load/load_test.go @@ -46,11 +46,12 @@ func TestMisc(t *testing.T) { func TestMiscStatString(t *testing.T) { v := MiscStat{ + ProcsTotal: 4, ProcsRunning: 1, ProcsBlocked: 2, Ctxt: 3, } - e := `{"procsRunning":1,"procsBlocked":2,"ctxt":3}` + e := `{"procsTotal":4,"procsRunning":1,"procsBlocked":2,"ctxt":3}` if e != fmt.Sprintf("%v", v) { t.Errorf("TestMiscString string is invalid: %v", v) }