system/process.go
package system
import (
"github.com/aelsabbahy/go-ps"
"github.com/aelsabbahy/goss/util"
)
type Process interface {
Executable() string
Exists() (bool, error)
Running() (bool, error)
Pids() ([]int, error)
}
type DefProcess struct {
executable string
procMap map[string][]ps.Process
}
func NewDefProcess(executable string, system *System, config util.Config) (Process, error) {
pmap, err := system.ProcMap()
if err != nil {
return nil, err
}
return &DefProcess{
executable: executable,
procMap: pmap,
}, nil
}
func (p *DefProcess) Executable() string {
return p.executable
}
func (p *DefProcess) Exists() (bool, error) { return p.Running() }
func (p *DefProcess) Pids() ([]int, error) {
var pids []int
for _, proc := range p.procMap[p.executable] {
pids = append(pids, proc.Pid())
}
return pids, nil
}
func (p *DefProcess) Running() (bool, error) {
if _, ok := p.procMap[p.executable]; ok {
return true, nil
}
return false, nil
}
func GetProcs() (map[string][]ps.Process, error) {
pmap := make(map[string][]ps.Process)
processes, err := ps.Processes()
if err != nil {
return pmap, err
}
for _, p := range processes {
pmap[p.Executable()] = append(pmap[p.Executable()], p)
}
return pmap, nil
}