Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add process info #59

Merged
merged 1 commit into from
Feb 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
32 changes: 28 additions & 4 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 45 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ package main
import (
"bytes"
"fmt"
"log"
"os"
"regexp"
"strconv"
"strings"

"github.com/google/gops/goprocess"
"github.com/shirou/gopsutil/process"
)

const helpText = `gops is a tool to list and diagnose Go processes.

Usage:

gops [command [pid|address:port]]
gops <cmd> <pid|addr> ...
gops <pid> # displays process info

Commands:
stack Prints the stack trace.
Expand Down Expand Up @@ -49,12 +50,17 @@ func main() {
}

cmd := os.Args[1]

// See if it is a PID.
pid, err := strconv.Atoi(cmd)
if err == nil {
processInfo(pid)
return
}

if cmd == "help" {
usage("")
}
if len(os.Args) < 3 {
usage("missing PID or address")
}
fn, ok := cmds[cmd]
if !ok {
usage("unknown subcommand")
Expand Down Expand Up @@ -110,6 +116,39 @@ func processes() {
}
}

func processInfo(pid int) {
p, err := process.NewProcess(int32(pid))
if err != nil {
log.Fatalf("Cannot read process info: %v", err)
}
if v, err := p.Parent(); err == nil {
fmt.Printf("parent PID:\t%v\n", v.Pid)
}
if v, err := p.NumThreads(); err == nil {
fmt.Printf("threads:\t%v\n", v)
}
if v, err := p.MemoryPercent(); err == nil {
fmt.Printf("memory usage:\t%.3f%%\n", v)
}
if v, err := p.CPUPercent(); err == nil {
fmt.Printf("cpu usage:\t%.3f%%\n", v)
}
if v, err := p.Username(); err == nil {
fmt.Printf("username:\t%v\n", v)
}
if v, err := p.Cmdline(); err == nil {
fmt.Printf("cmd+args:\t%v\n", v)
}
if v, err := p.Connections(); err == nil {
if len(v) > 0 {
for _, conn := range v {
fmt.Printf("local/remote:\t%v:%v <-> %v:%v (%v)\n",
conn.Laddr.IP, conn.Laddr.Port, conn.Raddr.IP, conn.Raddr.Port, conn.Status)
}
}
}
}

var develRe = regexp.MustCompile(`devel\s+\+\w+`)

func shortenVersion(v string) string {
Expand Down
20 changes: 20 additions & 0 deletions vendor/github.com/StackExchange/wmi/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/StackExchange/wmi/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading