44 lines
667 B
Go
44 lines
667 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()
|
|
}
|
|
|