Skip to content

Commit

Permalink
Make it possible to list processes (#3)
Browse files Browse the repository at this point in the history
* Make it possible to list processes

* Add comment
  • Loading branch information
nakabonne authored Nov 1, 2020
1 parent 3e1a8dc commit 977ab06
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 96 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ apt install ./gosivy_0.1.0_linux_amd64.deb
```

#### Go
Note that you may have a problem because it downloads an untagged binary.
Required: >= 1.10. Note that you may have a problem because it downloads an untagged binary.
```
go get github.com/nakabonne/gosivy
```
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.15
require (
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/keybase/go-ps v0.0.0-20190827175125-91aafc93ba19
github.com/mum4k/termdash v0.12.2
github.com/shirou/gopsutil v2.20.9+incompatible
github.com/sirupsen/logrus v1.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ github.com/gdamore/tcell v1.3.0 h1:r35w0JBADPZCVQijYebl6YMWWtHRqVEGt7kL2eBADRM=
github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM=
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/keybase/go-ps v0.0.0-20190827175125-91aafc93ba19 h1:WjT3fLi9n8YWh/Ih8Q1LHAPsTqGddPcHqscN+PJ3i68=
github.com/keybase/go-ps v0.0.0-20190827175125-91aafc93ba19/go.mod h1:hY+WOq6m2FpbvyrI93sMaypsttvaIL5nhVR92dTMUcQ=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.0.2 h1:mCMFu6PgSozg9tDNMMK3g18oJBX7oYGrC09mS6CXfO4=
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/nakabonne/gosivy/diagnoser"
"github.com/nakabonne/gosivy/pidfile"
"github.com/nakabonne/gosivy/process"
)

const defaultScrapeInterval = time.Second
Expand Down Expand Up @@ -102,7 +103,12 @@ func (c *cli) run(args []string) int {
return 1
}
if c.list {
c.listProcesses()
ps, err := process.List()
if err != nil {
fmt.Fprintf(c.stderr, "failed to list processes: %w", err)
return 1
}
fmt.Fprintf(c.stderr, "%v", ps)
return 0
}

Expand Down
94 changes: 0 additions & 94 deletions process.go

This file was deleted.

107 changes: 107 additions & 0 deletions process/process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package process

import (
"fmt"
"os"
"strconv"
"strings"

"github.com/keybase/go-ps"

"github.com/nakabonne/gosivy/pidfile"
)

// Process represents an OS process.
type Process struct {
PID int
Executable string
// Full path to the executable.
Path string
}

type Processes []Process

// String formats as:
//
// PID Exec Path
// 15788 foo /path/to/src/foo
// 14054 main /private/var/folders/sy/5rwqjr1j3kl5r3kwxfgntkfr0000gn/T/go-build227076651/b001/exe/main
func (ps Processes) String() string {
var (
b strings.Builder
pidTitle = "PID"
execTitle = "Exec"
pathTitle = "Path"
maxPIDLen = len(pidTitle)
maxExecLen = len(execTitle)
)
// Take the maximum length to align the width of each column.
for _, p := range ps {
maxPIDLen = max(maxPIDLen, len(strconv.Itoa(p.PID)))
maxExecLen = max(maxExecLen, len(p.Executable))
}

b.WriteString(fmt.Sprintf("%s %s %s\n",
pad(pidTitle, maxPIDLen),
pad(execTitle, maxExecLen),
pathTitle,
))

for _, p := range ps {
b.WriteString(fmt.Sprintf("%s %s %s\n",
pad(strconv.Itoa(p.PID), maxPIDLen),
pad(p.Executable, maxExecLen),
p.Path,
))
}
return b.String()
}

// List gives back all processes where the agent runs on.
func List() (Processes, error) {
processes := make(Processes, 0)
ps, err := ps.Processes()
if err != nil {
return nil, err
}
for _, p := range ps {
pid := p.Pid()
if pid == 0 {
// Ignore system process.
continue
}
pf, err := pidfile.PIDFile(pid)
if err != nil {
return nil, fmt.Errorf("failed to find pid file: %w", err)
}
if _, err := os.Stat(pf); err != nil {
// Ignore ps where the agent doesn't run on.
continue
}
path, err := p.Path()
if err != nil {
return nil, fmt.Errorf("failed to detect full path to the executable: %w", err)
}

processes = append(processes, Process{
PID: pid,
Executable: p.Executable(),
Path: path,
})
}
return processes, nil
}

func pad(s string, total int) string {
if len(s) >= total {
return s
}
return s + strings.Repeat(" ", total-len(s))
}

func max(i, j int) int {
if i > j {
return i
}
return j
}

0 comments on commit 977ab06

Please sign in to comment.