2017-10-23 09:08:19 +00:00
|
|
|
package stat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProcessStat struct {
|
2017-10-24 07:09:17 +00:00
|
|
|
User,
|
|
|
|
Pid,
|
|
|
|
Pcpu,
|
|
|
|
Pmem,
|
|
|
|
Cmd string
|
2017-10-23 09:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *ProcessStat) Command() string {
|
2017-10-24 09:23:56 +00:00
|
|
|
return "ps -eo user,pid,pcpu,pmem,comm --sort=-pmem c"
|
2017-10-23 09:08:19 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 09:23:56 +00:00
|
|
|
func (ps *ProcessStat) Read(r io.Reader) (*[]ProcessStat, error) {
|
2017-10-23 09:08:19 +00:00
|
|
|
var (
|
|
|
|
psStats = []ProcessStat{}
|
|
|
|
scanner = bufio.NewScanner(r)
|
|
|
|
)
|
|
|
|
scanner.Scan()
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
parts := strings.Fields(line)
|
|
|
|
psStats = append(psStats, ProcessStat{
|
2017-10-24 07:09:17 +00:00
|
|
|
User: parts[0],
|
|
|
|
Pid: parts[1],
|
2017-10-24 09:23:56 +00:00
|
|
|
Pcpu: parts[2],
|
|
|
|
Pmem: parts[3],
|
|
|
|
Cmd: parts[4],
|
2017-10-23 09:08:19 +00:00
|
|
|
})
|
|
|
|
}
|
2017-10-24 09:23:56 +00:00
|
|
|
|
|
|
|
|
2017-10-23 09:08:19 +00:00
|
|
|
return &psStats, scanner.Err()
|
|
|
|
}
|