42 lines
663 B
Go
42 lines
663 B
Go
package stat
|
|
|
|
import (
|
|
"bufio"
|
|
"io"
|
|
"strings"
|
|
)
|
|
|
|
type ProcessStat struct {
|
|
User,
|
|
Pid,
|
|
Size,
|
|
Pcpu,
|
|
Pmem,
|
|
Cmd string
|
|
}
|
|
|
|
func (ps *ProcessStat) Command() string {
|
|
return "ps -eo user,pid,size,pcpu,pmem,comm --sort=-pmem c"
|
|
}
|
|
|
|
func (ps *ProcessStat) Parse(r io.Reader) (*[]ProcessStat, error) {
|
|
var (
|
|
psStats = []ProcessStat{}
|
|
scanner = bufio.NewScanner(r)
|
|
)
|
|
scanner.Scan()
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
parts := strings.Fields(line)
|
|
psStats = append(psStats, ProcessStat{
|
|
User: parts[0],
|
|
Pid: parts[1],
|
|
Size: parts[2],
|
|
Pcpu: parts[3],
|
|
Pmem: parts[4],
|
|
Cmd: parts[5],
|
|
})
|
|
}
|
|
return &psStats, scanner.Err()
|
|
}
|