pass through arguments from command line

This commit is contained in:
Dominic Breuker
2018-02-13 21:41:41 +01:00
parent c92dd9992f
commit 4495ae7c96
8 changed files with 60 additions and 23 deletions

View File

@@ -6,3 +6,19 @@ build-dev:
dev: dev:
docker run -it --rm -v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy $(DEV_IMAGE) docker run -it --rm -v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy $(DEV_IMAGE)
release:
docker run -it \
--rm \
-v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \
--env CGO_ENABLED=0 \
--env GOOS=linux \
--env GOARCH=386 \
$(DEV_IMAGE) go build -a -ldflags '-extldflags "-static"' -o pspy/bin/pspy32 pspy/main.go
docker run -it \
--rm \
-v $(PROJECT_DIR):/go/src/github.com/dominicbreuker/pspy \
--env CGO_ENABLED=0 \
--env GOOS=linux \
--env GOARCH=amd64 \
$(DEV_IMAGE) go build -a -ldflags '-extldflags "-static"' -o pspy/bin/pspy64 pspy/main.go

BIN
bin/pspy32 Executable file

Binary file not shown.

BIN
bin/pspy64 Executable file

Binary file not shown.

View File

@@ -2,6 +2,7 @@ package cmd
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"strings" "strings"
@@ -39,18 +40,29 @@ var rootCmd = &cobra.Command{
var logPS, logFS bool var logPS, logFS bool
var rDirs, dirs []string var rDirs, dirs []string
var defaultRDirs = []string{
"/usr",
"/tmp",
"/etc",
"/home",
"/var",
"/opt",
}
var defaultDirs = []string{}
func init() { func init() {
rootCmd.PersistentFlags().BoolVarP(&logPS, "procevents", "p", true, "print new processes to stdout") rootCmd.PersistentFlags().BoolVarP(&logPS, "procevents", "p", true, "print new processes to stdout")
rootCmd.PersistentFlags().BoolVarP(&logFS, "fsevents", "f", true, "print file system events to stdout") rootCmd.PersistentFlags().BoolVarP(&logFS, "fsevents", "f", false, "print file system events to stdout")
rootCmd.PersistentFlags().StringArrayVarP(&rDirs, "recursive_dirs", "r", []string{"/tmp"}, "watch these dirs recursively") rootCmd.PersistentFlags().StringArrayVarP(&rDirs, "recursive_dirs", "r", defaultRDirs, "watch these dirs recursively")
rootCmd.PersistentFlags().StringArrayVarP(&dirs, "dirs", "d", []string{}, "watch these dirs") rootCmd.PersistentFlags().StringArrayVarP(&dirs, "dirs", "d", defaultDirs, "watch these dirs")
log.SetOutput(os.Stdout)
} }
func root(cmd *cobra.Command, args []string) { func root(cmd *cobra.Command, args []string) {
fmt.Printf("Watching recursively : %+v (%d)\n", rDirs, len(rDirs)) fmt.Printf("Watching recursively : %+v (%d)\n", rDirs, len(rDirs))
fmt.Printf("Watching non-recursively: %+v (%d)\n", dirs, len(dirs)) fmt.Printf("Watching non-recursively: %+v (%d)\n", dirs, len(dirs))
fmt.Printf("Printing: processes=%t file-system events:%t\n", logPS, logFS) fmt.Printf("Printing: processes=%t file-system events=%t\n", logPS, logFS)
pspy.Watch(rDirs, dirs, logPS, logFS) pspy.Watch(rDirs, dirs, logPS, logFS)
} }

View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"log" "log"
"strconv" "strconv"
"time"
"unsafe" "unsafe"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
@@ -55,7 +56,13 @@ func newEvent(name string, mask uint32) Event {
return e return e
} }
func eventLogger(i *Inotify, buffers chan bufRead) { func eventLogger(i *Inotify, buffers chan bufRead, print bool) {
// enable printing only after delay since setting up watchers causes flood of events
printEnabled := false
go func() {
<-time.After(1 * time.Second)
printEnabled = print
}()
for bf := range buffers { for bf := range buffers {
n := bf.n n := bf.n
buf := bf.buf buf := bf.buf
@@ -82,7 +89,9 @@ func eventLogger(i *Inotify, buffers chan bufRead) {
} }
ev := newEvent(name, sys.Mask) ev := newEvent(name, sys.Mask)
if printEnabled {
log.Printf("\x1b[32;1mFS: %+v\x1b[0m", ev) log.Printf("\x1b[32;1mFS: %+v\x1b[0m", ev)
} }
} }
}
} }

View File

@@ -13,7 +13,7 @@ type Inotify struct {
paused bool paused bool
} }
func NewInotify(ping chan struct{}) (*Inotify, error) { func NewInotify(ping chan struct{}, print bool) (*Inotify, error) {
fd, errno := unix.InotifyInit1(unix.IN_CLOEXEC) fd, errno := unix.InotifyInit1(unix.IN_CLOEXEC)
if fd == -1 { if fd == -1 {
return nil, fmt.Errorf("Can't init inotify: %d", errno) return nil, fmt.Errorf("Can't init inotify: %d", errno)
@@ -25,15 +25,11 @@ func NewInotify(ping chan struct{}) (*Inotify, error) {
ping: ping, ping: ping,
paused: false, paused: false,
} }
go watch(i) go watch(i, print)
return i, nil return i, nil
} }
func (i *Inotify) Start() {
go watch(i)
}
func (i *Inotify) Watch(dir string) error { func (i *Inotify) Watch(dir string) error {
w, err := newWatcher(i.fd, dir, i.ping) w, err := newWatcher(i.fd, dir, i.ping)
if err != nil { if err != nil {
@@ -79,10 +75,10 @@ type bufRead struct {
buf []byte buf []byte
} }
func watch(i *Inotify) { func watch(i *Inotify, print bool) {
buf := make([]byte, 5*unix.SizeofInotifyEvent) buf := make([]byte, 5*unix.SizeofInotifyEvent)
buffers := make(chan bufRead) buffers := make(chan bufRead)
go eventLogger(i, buffers) go eventLogger(i, buffers, print)
for { for {
n, _ := unix.Read(i.fd, buf) n, _ := unix.Read(i.fd, buf)
if !i.paused { if !i.paused {

View File

@@ -15,7 +15,7 @@ func NewProcList() *ProcList {
return &pl return &pl
} }
func (pl ProcList) Refresh() error { func (pl ProcList) Refresh(print bool) error {
pids, err := getPIDs() pids, err := getPIDs()
if err != nil { if err != nil {
return err return err
@@ -33,7 +33,9 @@ func (pl ProcList) Refresh() error {
if err != nil { if err != nil {
uid = "???" uid = "???"
} }
if print {
log.Printf("\x1b[31;1mCMD: UID=%-4s PID=%-6d | %s\x1b[0m\n", uid, pid, cmd) log.Printf("\x1b[31;1mCMD: UID=%-4s PID=%-6d | %s\x1b[0m\n", uid, pid, cmd)
}
pl[pid] = cmd pl[pid] = cmd
} }
} }

View File

@@ -1,7 +1,9 @@
package pspy package pspy
import ( import (
"fmt"
"log" "log"
"os"
"time" "time"
"github.com/dominicbreuker/pspy/internal/inotify" "github.com/dominicbreuker/pspy/internal/inotify"
@@ -23,7 +25,7 @@ func Watch(rdirs, dirs []string, logPS, logFS bool) {
log.Printf("Inotify watcher limit: %d (/proc/sys/fs/inotify/max_user_watches)\n", maxWatchers) log.Printf("Inotify watcher limit: %d (/proc/sys/fs/inotify/max_user_watches)\n", maxWatchers)
ping := make(chan struct{}) ping := make(chan struct{})
in, err := inotify.NewInotify(ping) in, err := inotify.NewInotify(ping, logFS)
if err != nil { if err != nil {
log.Fatalf("Can't init inotify: %v", err) log.Fatalf("Can't init inotify: %v", err)
} }
@@ -45,9 +47,9 @@ func Watch(rdirs, dirs []string, logPS, logFS bool) {
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
refresh(in, procList) refresh(in, procList, logPS)
case <-ping: case <-ping:
refresh(in, procList) refresh(in, procList, logPS)
} }
} }
} }
@@ -66,17 +68,17 @@ loop:
break loop break loop
} }
if err := in.Watch(dir); err != nil { if err := in.Watch(dir); err != nil {
log.Printf("Can't create watcher: %v", err) fmt.Fprintf(os.Stderr, "Can't create watcher: %v", err)
} }
case err := <-errCh: case err := <-errCh:
log.Printf("Error walking filesystem: %v", err) fmt.Fprintf(os.Stderr, "Error walking filesystem: %v", err)
} }
} }
} }
func refresh(in *inotify.Inotify, pl *process.ProcList) { func refresh(in *inotify.Inotify, pl *process.ProcList, print bool) {
in.Pause() in.Pause()
if err := pl.Refresh(); err != nil { if err := pl.Refresh(print); err != nil {
log.Printf("ERROR refreshing process list: %v", err) log.Printf("ERROR refreshing process list: %v", err)
} }
time.Sleep(5 * time.Millisecond) time.Sleep(5 * time.Millisecond)