From 9b8632257388e9b4c132b4e847f3f50beaf6220b Mon Sep 17 00:00:00 2001 From: Ahmed Elsabbahy Date: Fri, 3 Jul 2020 12:10:54 -0700 Subject: [PATCH 1/2] Migrate from go-ps to gopsutil for better process detection --- docs/manual.md | 2 -- go.mod | 5 ++++- go.sum | 10 ++++++++-- system/process.go | 21 ++++++++++++++------- system/system.go | 7 ++++--- 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/docs/manual.md b/docs/manual.md index cc561e36e..2121ff1f6 100644 --- a/docs/manual.md +++ b/docs/manual.md @@ -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 -o comm`. - ### service Validates the state of a service. diff --git a/go.mod b/go.mod index bd686efc2..cfcb64759 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 ) diff --git a/go.sum b/go.sum index 026e3ea2b..c5dcf93ca 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= @@ -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= diff --git a/system/process.go b/system/process.go index a88a97e36..7479e85fc 100644 --- a/system/process.go +++ b/system/process.go @@ -1,8 +1,10 @@ package system import ( - "github.com/aelsabbahy/go-ps" + "fmt" + "github.com/aelsabbahy/goss/util" + "github.com/shirou/gopsutil/process" ) type Process interface { @@ -14,7 +16,7 @@ type Process interface { type DefProcess struct { executable string - procMap map[string][]ps.Process + procMap map[string][]*process.Process err error } @@ -39,7 +41,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 } @@ -54,14 +56,19 @@ 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 p.Pid == 14657 { + fmt.Println(p.Name()) + } + if pExe, err := p.Name(); err == nil { + pmap[pExe] = append(pmap[pExe], p) + } } return pmap, nil diff --git a/system/system.go b/system/system.go index 9573367e0..161850c0d 100644 --- a/system/system.go +++ b/system/system.go @@ -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" ) @@ -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 } @@ -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() { From 915d460a92bbeb903edf2e1f3d4fd2cb89b5699c Mon Sep 17 00:00:00 2001 From: Ahmed Elsabbahy Date: Fri, 3 Jul 2020 12:30:15 -0700 Subject: [PATCH 2/2] Remove debugging code --- system/process.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/system/process.go b/system/process.go index 7479e85fc..8edddbdde 100644 --- a/system/process.go +++ b/system/process.go @@ -1,8 +1,6 @@ package system import ( - "fmt" - "github.com/aelsabbahy/goss/util" "github.com/shirou/gopsutil/process" ) @@ -63,9 +61,6 @@ func GetProcs() (map[string][]*process.Process, error) { return pmap, err } for _, p := range processes { - if p.Pid == 14657 { - fmt.Println(p.Name()) - } if pExe, err := p.Name(); err == nil { pmap[pExe] = append(pmap[pExe], p) }