watch home dirs of logged in users
parent
27c0060195
commit
bb69e9c613
|
@ -1,33 +1,73 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/fsnotify/fsnotify"
|
"github.com/fsnotify/fsnotify"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// TODO flesh out event handling with an eye towards eventual sampling
|
||||||
|
|
||||||
func initHomeWatcher() (*fsnotify.Watcher, error) {
|
func initHomeWatcher() (*fsnotify.Watcher, error) {
|
||||||
|
fmt.Fprintf(os.Stderr, "setting up home watcher...\n")
|
||||||
watcher, err := fsnotify.NewWatcher()
|
watcher, err := fsnotify.NewWatcher()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO filepath.Walk over /home, adding to watcher
|
out, err := exec.Command("sh", "-c", "stats | jq .active_users[] | tr -d '\"'").Output()
|
||||||
paths := []string{"/home/vilmibm", "/home/wren"}
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to call and process stats: %w", err)
|
||||||
for _, path := range paths {
|
|
||||||
err = watcher.Add(path)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "failed to watch path %s: %w", path, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(bytes.NewReader(out))
|
||||||
|
for scanner.Scan() {
|
||||||
|
username := strings.TrimSpace(scanner.Text())
|
||||||
|
home := filepath.Join("/home", username)
|
||||||
|
addHome(watcher, home)
|
||||||
|
}
|
||||||
|
|
||||||
return watcher, nil
|
return watcher, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func watchHome(watcher *fsnotify.Watcher) {
|
func addHome(watcher *fsnotify.Watcher, homePath string) error {
|
||||||
|
fileCount := 0
|
||||||
|
filepath.Walk(homePath, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if info.IsDir() && strings.HasPrefix(info.Name(), ".") {
|
||||||
|
return filepath.SkipDir
|
||||||
|
}
|
||||||
|
fileCount++
|
||||||
|
if info.Mode()&os.ModeSymlink != 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err = watcher.Add(path)
|
||||||
|
if err != nil && err.Error() != "permission denied" && err.Error() != "no such file or directory" {
|
||||||
|
fmt.Printf("%#v\n", err)
|
||||||
|
fmt.Println("Died at ", fileCount)
|
||||||
|
fmt.Printf("%#v\n", info)
|
||||||
|
panic(err.Error())
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(os.Stderr, "watching %s\n", path)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func watchHomes(watcher *fsnotify.Watcher) {
|
||||||
|
fmt.Fprintf(os.Stderr, "starting poll\n")
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case event, ok := <-watcher.Events:
|
case event, ok := <-watcher.Events:
|
||||||
|
@ -48,9 +88,11 @@ func watchHome(watcher *fsnotify.Watcher) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func cli(args []string) int {
|
func cli(args []string) int {
|
||||||
|
fmt.Fprintf(os.Stderr, "starting\n")
|
||||||
watcher, err := initHomeWatcher()
|
watcher, err := initHomeWatcher()
|
||||||
|
fmt.Fprintf(os.Stderr, "watcher initialized\n")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to create watcher: %w", err)
|
fmt.Fprintf(os.Stderr, "failed to create watcher: %w\n", err)
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
defer watcher.Close()
|
defer watcher.Close()
|
||||||
|
@ -58,7 +100,7 @@ func cli(args []string) int {
|
||||||
// TODO have an event bus to write to
|
// TODO have an event bus to write to
|
||||||
// TODO what is this done for do i need this done
|
// TODO what is this done for do i need this done
|
||||||
done := make(chan bool)
|
done := make(chan bool)
|
||||||
go watchHome(watcher)
|
go watchHomes(watcher)
|
||||||
<-done
|
<-done
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
Loading…
Reference in New Issue