Skip to content

Commit

Permalink
add running and total processes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhang Yu committed Nov 6, 2018
1 parent fc9a1b8 commit a433026
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions loadavg.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package nux

import (
"errors"
"fmt"
"github.com/toolkits/file"
"strconv"
"strings"
)

type Loadavg struct {
Avg1min float64
Avg5min float64
Avg15min float64
Avg1min float64
Avg5min float64
Avg15min float64
RunningProcesses int64
TotalProcesses int64
}

func (this *Loadavg) String() string {
return fmt.Sprintf("<1min:%f, 5min:%f, 15min:%f>", this.Avg1min, this.Avg5min, this.Avg15min)
func (load *Loadavg) String() string {
return fmt.Sprintf("<1min:%f, 5min:%f, 15min:%f, processes:%d/%d>", load.Avg1min, load.Avg5min, load.Avg15min, load.RunningProcesses, load.TotalProcesses)
}

func LoadAvg() (*Loadavg, error) {
Expand All @@ -36,6 +39,16 @@ func LoadAvg() (*Loadavg, error) {
if loadAvg.Avg15min, err = strconv.ParseFloat(L[2], 64); err != nil {
return nil, err
}
processes := strings.SplitN(L[3], "/", 2)
if len(processes) != 2 {
return nil, errors.New("invalid loadavg " + data)
}
if loadAvg.RunningProcesses, err = strconv.ParseInt(processes[0], 10, 64); err != nil {
return nil, err
}
if loadAvg.TotalProcesses, err = strconv.ParseInt(processes[1], 10, 64); err != nil {
return nil, err
}

return &loadAvg, nil
}

0 comments on commit a433026

Please sign in to comment.