ssh_crawler/stat/process.go
insanity 3134350172 ing
2017-10-24 18:23:56 +09:00

42 lines
633 B
Go

package stat
import (
"bufio"
"io"
"strings"
)
type ProcessStat struct {
User,
Pid,
Pcpu,
Pmem,
Cmd string
}
func (ps *ProcessStat) Command() string {
return "ps -eo user,pid,pcpu,pmem,comm --sort=-pmem c"
}
func (ps *ProcessStat) Read(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],
Pcpu: parts[2],
Pmem: parts[3],
Cmd: parts[4],
})
}
return &psStats, scanner.Err()
}