Skip to content

Commit

Permalink
Migrate from go-ps to gopsutil for better process detection (#597)
Browse files Browse the repository at this point in the history
* Migrate from go-ps to gopsutil for better process detection

* Remove debugging code
  • Loading branch information
aelsabbahy authored Sep 7, 2020
1 parent d1e9f59 commit 85ce2c8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
2 changes: 0 additions & 2 deletions docs/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,6 @@ process:
skip: false
```

**NOTE:** This check is inspecting the name of the binary, not the name of the process. For example, a process with the name `nginx: master process /usr/sbin/nginx` would be checked with the process `nginx`. To discover the binary of a pid run `ps -p <PID> -o comm`.

### service
Validates the state of a service.

Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ require (
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/achanda/go-sysctl v0.0.0-20160222034550-6be7678c45d2
github.com/aelsabbahy/GOnetstat v0.0.0-20160428114218-edf89f784e08
github.com/aelsabbahy/go-ps v0.0.0-20170721000941-443386855ca1
github.com/blang/semver v3.5.1+incompatible
github.com/cheekybits/genny v1.0.0
github.com/docker/docker v1.13.1
github.com/fatih/color v1.9.0
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/huandu/xstrings v1.3.0 // indirect
github.com/imdario/mergo v0.3.8 // indirect
Expand All @@ -20,8 +21,10 @@ require (
github.com/onsi/gomega v1.9.0
github.com/opencontainers/runc v0.0.0-20161107232042-8779fa57eb4a
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/shirou/gopsutil v2.20.6+incompatible
github.com/stretchr/testify v1.4.0
github.com/urfave/cli v0.0.0-20161102131801-d86a009f5e13
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
gopkg.in/yaml.v2 v2.2.8
)

Expand Down
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3Q
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/sprig v2.22.0+incompatible h1:z4yfnGrZ7netVz+0EDJ0Wi+5VZCSYp4Z0m2dk6cEM60=
github.com/Masterminds/sprig v2.22.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/achanda/go-sysctl v0.0.0-20160222034550-6be7678c45d2 h1:NYoPVh1XuUB5VBWLXRKoqzQhl4bajIxh+XuURbJ0uwc=
github.com/achanda/go-sysctl v0.0.0-20160222034550-6be7678c45d2/go.mod h1:DCNKSpXhum14Y258jSbRmJvcesbzEdBPincz7yJUx3k=
github.com/aelsabbahy/GOnetstat v0.0.0-20160428114218-edf89f784e08 h1:oD15ssIOuFLi64zhkPRsaIDvhx4PeZb2QdQoR/wKY2g=
github.com/aelsabbahy/GOnetstat v0.0.0-20160428114218-edf89f784e08/go.mod h1:FETZSu2VGNDJbGfeRExaz/SNbX0TTaqJEMo1yvsKoZ8=
github.com/aelsabbahy/go-ps v0.0.0-20170721000941-443386855ca1 h1:s4dvLggvQOov0YFdv8XQvX+72TAFzfJg+6SgoXiIaq4=
github.com/aelsabbahy/go-ps v0.0.0-20170721000941-443386855ca1/go.mod h1:70tSBushy/POz6cCR294bKno4BNAC7XWVdkkxWQ1N6E=
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/cheekybits/genny v1.0.0 h1:uGGa4nei+j20rOSeDeP5Of12XVm7TGUd4dJA9RDitfE=
Expand All @@ -22,6 +22,8 @@ github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-ole/go-ole v1.2.4 h1:nNBDSCOigTSiarFpYE9J/KtEA1IOW4CNeqT9TQDqCxI=
github.com/go-ole/go-ole v1.2.4/go.mod h1:XCwSNxSkXRo4vlyPy93sltvi/qJq0jqQhjqQNIwKuxM=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
Expand Down Expand Up @@ -55,6 +57,8 @@ github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaR
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shirou/gopsutil v2.20.6+incompatible h1:P37G9YH8M4vqkKcwBosp+URN5O8Tay67D2MbR361ioY=
github.com/shirou/gopsutil v2.20.6+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand All @@ -79,6 +83,8 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae h1:Ih9Yo4hSPImZOpfGuA4bR/ORKTAbhZo2AbWNRCnevdo=
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
Expand Down
16 changes: 9 additions & 7 deletions system/process.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package system

import (
"github.com/aelsabbahy/go-ps"
"github.com/aelsabbahy/goss/util"
"github.com/shirou/gopsutil/process"
)

type Process interface {
Expand All @@ -14,7 +14,7 @@ type Process interface {

type DefProcess struct {
executable string
procMap map[string][]ps.Process
procMap map[string][]*process.Process
err error
}

Expand All @@ -39,7 +39,7 @@ func (p *DefProcess) Pids() ([]int, error) {
return pids, p.err
}
for _, proc := range p.procMap[p.executable] {
pids = append(pids, proc.Pid())
pids = append(pids, int(proc.Pid))
}
return pids, nil
}
Expand All @@ -54,14 +54,16 @@ func (p *DefProcess) Running() (bool, error) {
return false, nil
}

func GetProcs() (map[string][]ps.Process, error) {
pmap := make(map[string][]ps.Process)
processes, err := ps.Processes()
func GetProcs() (map[string][]*process.Process, error) {
pmap := make(map[string][]*process.Process)
processes, err := process.Processes()
if err != nil {
return pmap, err
}
for _, p := range processes {
pmap[p.Executable()] = append(pmap[p.Executable()], p)
if pExe, err := p.Name(); err == nil {
pmap[pExe] = append(pmap[pExe], p)
}
}

return pmap, nil
Expand Down
7 changes: 4 additions & 3 deletions system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"sync"

"github.com/aelsabbahy/GOnetstat"
"github.com/shirou/gopsutil/process"

// This needs a better name
"github.com/aelsabbahy/go-ps"

util2 "github.com/aelsabbahy/goss/util"
)
Expand All @@ -37,7 +38,7 @@ type System struct {
NewHTTP func(string, *System, util2.Config) HTTP
ports map[string][]GOnetstat.Process
portsOnce sync.Once
procMap map[string][]ps.Process
procMap map[string][]*process.Process
procOnce sync.Once
}

Expand All @@ -48,7 +49,7 @@ func (s *System) Ports() map[string][]GOnetstat.Process {
return s.ports
}

func (s *System) ProcMap() (map[string][]ps.Process, error) {
func (s *System) ProcMap() (map[string][]*process.Process, error) {
var err error

s.procOnce.Do(func() {
Expand Down

0 comments on commit 85ce2c8

Please sign in to comment.