mirror of
https://github.com/DominicBreuker/pspy.git
synced 2025-12-21 11:44:51 +00:00
integrate inotify syscalls
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
FROM golang:1.9-stretch
|
FROM golang:1.9-stretch
|
||||||
|
|
||||||
|
RUN useradd -ms /bin/bash myuser
|
||||||
|
|
||||||
|
USER myuser
|
||||||
|
|
||||||
WORKDIR /go/src/github.com/dominicbreuker
|
WORKDIR /go/src/github.com/dominicbreuker
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
package dev
|
package dev
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"log"
|
"log"
|
||||||
"strconv"
|
"time"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/dominicbreuker/pspy/internal/inotify"
|
||||||
|
"github.com/dominicbreuker/pspy/internal/process"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Process struct {
|
type Process struct {
|
||||||
@@ -30,78 +29,47 @@ func Monitor() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func watch() {
|
func watch() {
|
||||||
watcher, err := fsnotify.NewWatcher()
|
ping := make(chan struct{})
|
||||||
|
in, err := inotify.NewInotify(ping)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Can't create file system watcher: %v", err)
|
log.Fatalf("Can't init inotify: %v", err)
|
||||||
}
|
}
|
||||||
defer watcher.Close()
|
|
||||||
|
|
||||||
done := make(chan bool)
|
dirs := []string{
|
||||||
|
"/proc",
|
||||||
|
"/var/log",
|
||||||
|
"/home",
|
||||||
|
"/tmp",
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, dir := range dirs {
|
||||||
|
if err := in.Watch(dir); err != nil {
|
||||||
|
log.Fatalf("Can't create watcher: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Inotify set up: %s\n", in)
|
||||||
|
|
||||||
|
procList := process.NewProcList()
|
||||||
|
|
||||||
|
ticker := time.NewTicker(50 * time.Millisecond).C
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case event := <-watcher.Events:
|
case <-ticker:
|
||||||
log.Println("event:", event)
|
refresh(in, procList)
|
||||||
if event.Op&fsnotify.Write == fsnotify.Write {
|
case <-ping:
|
||||||
log.Println("modified file:", event.Name)
|
log.Printf("PING")
|
||||||
}
|
refresh(in, procList)
|
||||||
case err := <-watcher.Errors:
|
|
||||||
log.Println("error:", err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
|
||||||
err = watcher.Add("/tmp")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
<-done
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func refresh(procList map[int]string) error {
|
func refresh(in *inotify.Inotify, pl *process.ProcList) {
|
||||||
proc, err := ioutil.ReadDir("/proc")
|
in.Pause()
|
||||||
if err != nil {
|
if err := pl.Refresh(); err != nil {
|
||||||
return fmt.Errorf("opening proc dir: %v", err)
|
log.Printf("ERROR refreshing process list: %v", err)
|
||||||
}
|
}
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
pids := make([]int, 0)
|
in.UnPause()
|
||||||
|
|
||||||
for _, f := range proc {
|
|
||||||
if f.IsDir() {
|
|
||||||
name := f.Name()
|
|
||||||
pid, err := strconv.Atoi(name)
|
|
||||||
if err != nil {
|
|
||||||
continue // not a pid
|
|
||||||
}
|
|
||||||
pids = append(pids, pid)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, pid := range pids {
|
|
||||||
_, ok := procList[pid]
|
|
||||||
if !ok {
|
|
||||||
cmd, err := getCmd(pid)
|
|
||||||
if err != nil {
|
|
||||||
cmd = "UNKNOWN" // process probably terminated
|
|
||||||
}
|
|
||||||
log.Printf("New process: %5d: %s\n", pid, cmd)
|
|
||||||
procList[pid] = cmd
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getCmd(pid int) (string, error) {
|
|
||||||
cmdPath := fmt.Sprintf("/proc/%d/cmdline", pid)
|
|
||||||
cmd, err := ioutil.ReadFile(cmdPath)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
for i := 0; i < len(cmd); i++ {
|
|
||||||
if cmd[i] == 0 {
|
|
||||||
cmd[i] = 32
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return string(cmd), nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
65
internal/inotify/inotify.go
Normal file
65
internal/inotify/inotify.go
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package inotify
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Inotify struct {
|
||||||
|
fd int
|
||||||
|
watchers []*watcher
|
||||||
|
ping chan struct{}
|
||||||
|
paused bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewInotify(ping chan struct{}) (*Inotify, error) {
|
||||||
|
fd, errno := unix.InotifyInit1(unix.IN_CLOEXEC)
|
||||||
|
if fd == -1 {
|
||||||
|
return nil, fmt.Errorf("Can't init inotify: %d", errno)
|
||||||
|
}
|
||||||
|
|
||||||
|
i := &Inotify{
|
||||||
|
fd: fd,
|
||||||
|
ping: ping,
|
||||||
|
paused: false,
|
||||||
|
}
|
||||||
|
go watch(i)
|
||||||
|
|
||||||
|
return i, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Inotify) Watch(dir string) error {
|
||||||
|
w, err := newWatcher(i.fd, dir, i.ping)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("creating watcher: %v", err)
|
||||||
|
}
|
||||||
|
i.watchers = append(i.watchers, w)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Inotify) Pause() {
|
||||||
|
i.paused = true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Inotify) UnPause() {
|
||||||
|
i.paused = false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *Inotify) String() string {
|
||||||
|
dirs := make([]string, 0)
|
||||||
|
for _, w := range i.watchers {
|
||||||
|
dirs = append(dirs, w.dir)
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("Watching: %v", dirs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func watch(i *Inotify) {
|
||||||
|
buf := make([]byte, 1024)
|
||||||
|
for {
|
||||||
|
_, _ = unix.Read(i.fd, buf)
|
||||||
|
if !i.paused {
|
||||||
|
i.ping <- struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
25
internal/inotify/watcher.go
Normal file
25
internal/inotify/watcher.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package inotify
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
)
|
||||||
|
|
||||||
|
const events = unix.IN_ALL_EVENTS
|
||||||
|
|
||||||
|
type watcher struct {
|
||||||
|
wd int
|
||||||
|
dir string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newWatcher(fd int, dir string, ping chan struct{}) (*watcher, error) {
|
||||||
|
wd, errno := unix.InotifyAddWatch(fd, dir, events)
|
||||||
|
if wd == -1 {
|
||||||
|
return nil, fmt.Errorf("adding watcher on %s: %d", dir, errno)
|
||||||
|
}
|
||||||
|
return &watcher{
|
||||||
|
wd: wd,
|
||||||
|
dir: dir,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
68
internal/process/process.go
Normal file
68
internal/process/process.go
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package process
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ProcList map[int]string
|
||||||
|
|
||||||
|
// type Proc struct {
|
||||||
|
// Cmd string
|
||||||
|
// User string
|
||||||
|
// }
|
||||||
|
|
||||||
|
func NewProcList() *ProcList {
|
||||||
|
pl := make(ProcList)
|
||||||
|
return &pl
|
||||||
|
}
|
||||||
|
|
||||||
|
func (pl ProcList) Refresh() error {
|
||||||
|
proc, err := ioutil.ReadDir("/proc")
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("opening proc dir: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pids := make([]int, 0)
|
||||||
|
|
||||||
|
for _, f := range proc {
|
||||||
|
if f.IsDir() {
|
||||||
|
name := f.Name()
|
||||||
|
pid, err := strconv.Atoi(name)
|
||||||
|
if err != nil {
|
||||||
|
continue // not a pid
|
||||||
|
}
|
||||||
|
pids = append(pids, pid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for i := len(pids) - 1; i >= 0; i-- {
|
||||||
|
pid := pids[i]
|
||||||
|
_, ok := pl[pid]
|
||||||
|
if !ok {
|
||||||
|
cmd, err := getCmd(pid)
|
||||||
|
if err != nil {
|
||||||
|
cmd = "UNKNOWN" // process probably terminated
|
||||||
|
}
|
||||||
|
log.Printf("New process: %5d: %s\n", pid, cmd)
|
||||||
|
pl[pid] = cmd
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getCmd(pid int) (string, error) {
|
||||||
|
cmdPath := fmt.Sprintf("/proc/%d/cmdline", pid)
|
||||||
|
cmd, err := ioutil.ReadFile(cmdPath)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
for i := 0; i < len(cmd); i++ {
|
||||||
|
if cmd[i] == 0 {
|
||||||
|
cmd[i] = 32
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return string(cmd), nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user