Merge pull request #682 from pytimer/add-load-procstotal

add ProcsTotal in load
This commit is contained in:
shirou 2019-05-05 21:37:46 +09:00 committed by GitHub
commit 01487156ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 5 deletions

View File

@ -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"`

View File

@ -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
}

View File

@ -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)
}