mirror of
https://github.com/DominicBreuker/pspy.git
synced 2025-12-21 03:34:50 +00:00
pass through arguments from command line
This commit is contained in:
16
Makefile
16
Makefile
@@ -6,3 +6,19 @@ build-dev:
|
||||
|
||||
dev:
|
||||
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
|
||||
|
||||
20
cmd/root.go
20
cmd/root.go
@@ -2,6 +2,7 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
@@ -39,18 +40,29 @@ var rootCmd = &cobra.Command{
|
||||
|
||||
var logPS, logFS bool
|
||||
var rDirs, dirs []string
|
||||
var defaultRDirs = []string{
|
||||
"/usr",
|
||||
"/tmp",
|
||||
"/etc",
|
||||
"/home",
|
||||
"/var",
|
||||
"/opt",
|
||||
}
|
||||
var defaultDirs = []string{}
|
||||
|
||||
func init() {
|
||||
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().StringArrayVarP(&rDirs, "recursive_dirs", "r", []string{"/tmp"}, "watch these dirs recursively")
|
||||
rootCmd.PersistentFlags().StringArrayVarP(&dirs, "dirs", "d", []string{}, "watch these dirs")
|
||||
rootCmd.PersistentFlags().BoolVarP(&logFS, "fsevents", "f", false, "print file system events to stdout")
|
||||
rootCmd.PersistentFlags().StringArrayVarP(&rDirs, "recursive_dirs", "r", defaultRDirs, "watch these dirs recursively")
|
||||
rootCmd.PersistentFlags().StringArrayVarP(&dirs, "dirs", "d", defaultDirs, "watch these dirs")
|
||||
|
||||
log.SetOutput(os.Stdout)
|
||||
}
|
||||
|
||||
func root(cmd *cobra.Command, args []string) {
|
||||
fmt.Printf("Watching recursively : %+v (%d)\n", rDirs, len(rDirs))
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strconv"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
@@ -55,7 +56,13 @@ func newEvent(name string, mask uint32) Event {
|
||||
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 {
|
||||
n := bf.n
|
||||
buf := bf.buf
|
||||
@@ -82,7 +89,9 @@ func eventLogger(i *Inotify, buffers chan bufRead) {
|
||||
}
|
||||
|
||||
ev := newEvent(name, sys.Mask)
|
||||
log.Printf("\x1b[32;1mFS: %+v\x1b[0m", ev)
|
||||
if printEnabled {
|
||||
log.Printf("\x1b[32;1mFS: %+v\x1b[0m", ev)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ type Inotify struct {
|
||||
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)
|
||||
if fd == -1 {
|
||||
return nil, fmt.Errorf("Can't init inotify: %d", errno)
|
||||
@@ -25,15 +25,11 @@ func NewInotify(ping chan struct{}) (*Inotify, error) {
|
||||
ping: ping,
|
||||
paused: false,
|
||||
}
|
||||
go watch(i)
|
||||
go watch(i, print)
|
||||
|
||||
return i, nil
|
||||
}
|
||||
|
||||
func (i *Inotify) Start() {
|
||||
go watch(i)
|
||||
}
|
||||
|
||||
func (i *Inotify) Watch(dir string) error {
|
||||
w, err := newWatcher(i.fd, dir, i.ping)
|
||||
if err != nil {
|
||||
@@ -79,10 +75,10 @@ type bufRead struct {
|
||||
buf []byte
|
||||
}
|
||||
|
||||
func watch(i *Inotify) {
|
||||
func watch(i *Inotify, print bool) {
|
||||
buf := make([]byte, 5*unix.SizeofInotifyEvent)
|
||||
buffers := make(chan bufRead)
|
||||
go eventLogger(i, buffers)
|
||||
go eventLogger(i, buffers, print)
|
||||
for {
|
||||
n, _ := unix.Read(i.fd, buf)
|
||||
if !i.paused {
|
||||
|
||||
@@ -15,7 +15,7 @@ func NewProcList() *ProcList {
|
||||
return &pl
|
||||
}
|
||||
|
||||
func (pl ProcList) Refresh() error {
|
||||
func (pl ProcList) Refresh(print bool) error {
|
||||
pids, err := getPIDs()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -33,7 +33,9 @@ func (pl ProcList) Refresh() error {
|
||||
if err != nil {
|
||||
uid = "???"
|
||||
}
|
||||
log.Printf("\x1b[31;1mCMD: UID=%-4s PID=%-6d | %s\x1b[0m\n", uid, pid, cmd)
|
||||
if print {
|
||||
log.Printf("\x1b[31;1mCMD: UID=%-4s PID=%-6d | %s\x1b[0m\n", uid, pid, cmd)
|
||||
}
|
||||
pl[pid] = cmd
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package pspy
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"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)
|
||||
|
||||
ping := make(chan struct{})
|
||||
in, err := inotify.NewInotify(ping)
|
||||
in, err := inotify.NewInotify(ping, logFS)
|
||||
if err != nil {
|
||||
log.Fatalf("Can't init inotify: %v", err)
|
||||
}
|
||||
@@ -45,9 +47,9 @@ func Watch(rdirs, dirs []string, logPS, logFS bool) {
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
refresh(in, procList)
|
||||
refresh(in, procList, logPS)
|
||||
case <-ping:
|
||||
refresh(in, procList)
|
||||
refresh(in, procList, logPS)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,17 +68,17 @@ loop:
|
||||
break loop
|
||||
}
|
||||
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:
|
||||
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()
|
||||
if err := pl.Refresh(); err != nil {
|
||||
if err := pl.Refresh(print); err != nil {
|
||||
log.Printf("ERROR refreshing process list: %v", err)
|
||||
}
|
||||
time.Sleep(5 * time.Millisecond)
|
||||
|
||||
Reference in New Issue
Block a user