Skip to content

Commit

Permalink
Make it capable of detecting process automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed Nov 1, 2020
1 parent 13a8fb8 commit a4f9933
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 31 deletions.
24 changes: 14 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,31 @@ func (c *cli) run(args []string) int {
return 1
}
if c.list {
ps, err := process.List()
ps, err := process.FindAll()
if err != nil {
fmt.Fprintf(c.stderr, "failed to list processes: %w", err)
fmt.Fprintf(c.stderr, "failed to list processes: %w\n", err)
return 1
}
fmt.Fprintf(c.stderr, "%v", ps)
return 0
}

// Try to run diagnoser
var addr *net.TCPAddr
var pid string
if len(args) == 0 {
// TODO: Make sure to use the process where the agent runs on.
addr = &net.TCPAddr{}
} else {
var err error
addr, err = targetToAddr(args[0])
// Automatically finds the process where the agent runs on if no args given.
p, err := process.FindOne()
if err != nil {
fmt.Fprintf(c.stderr, "failed to convert args into addresses: %v\n", err)
fmt.Fprintln(c.stderr, err)
return 1
}
pid = strconv.Itoa(p.PID)
} else {
pid = args[0]
}
addr, err := targetToAddr(pid)
if err != nil {
fmt.Fprintf(c.stderr, "failed to convert args into addresses: %v\n", err)
return 1
}
if err := diagnoser.Run(addr, c.scrapeInterval); err != nil {
fmt.Fprintf(c.stderr, "failed to start diagnoser: %s\n", err.Error())
Expand Down
63 changes: 42 additions & 21 deletions process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,39 +55,60 @@ func (ps Processes) String() string {
return b.String()
}

// List gives back all processes where the agent runs on.
func List() (Processes, error) {
// FindAll gives back all processes where the agent runs on.
func FindAll() (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
}
pidfile, err := PIDFile(pid)
process, err := newProcess(p)
if err != nil {
return nil, fmt.Errorf("failed to find pid file: %w", err)
}
if _, err := os.Stat(pidfile); 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)
}
return processes, nil
}

// FindOne finds processes where the agent runs on and gives back the first one it found.
func FindOne() (*Process, error) {
ps, err := ps.Processes()
if err != nil {
return nil, err
}
for _, p := range ps {
process, err := newProcess(p)
if err == nil {
return process, nil
}
}
return nil, fmt.Errorf("no process where the agent runs found")
}

processes = append(processes, Process{
PID: pid,
Executable: p.Executable(),
Path: path,
})
func newProcess(p ps.Process) (*Process, error) {
pid := p.Pid()
if pid == 0 {
return nil, fmt.Errorf("system process given")
}
return processes, nil
pidfile, err := PIDFile(pid)
if err != nil {
return nil, fmt.Errorf("failed to find pid file: %w", err)
}
if _, err := os.Stat(pidfile); err != nil {
return nil, fmt.Errorf("given process isn't the process where the agent doesn't run on")
}
path, err := p.Path()
if err != nil {
return nil, fmt.Errorf("failed to detect full path to the executable: %w", err)
}
return &Process{
PID: pid,
Executable: p.Executable(),
Path: path,
}, nil

}

func pad(s string, total int) string {
Expand Down

0 comments on commit a4f9933

Please sign in to comment.