From 9588b96f5cf78c9971a4fe7f53c6c9436ee017f1 Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Thu, 8 Jul 2021 22:04:37 +0200 Subject: [PATCH] feat: Initial commit --- README.md | 7 ++++++ go.mod | 5 ++++ go.sum | 3 +++ main.go | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..9c2f840 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# dmesg-notify + +For running as a regular user, use: +``` +sudo sysctl -w kernel.dmesg_restrict=0 +``` +(note: this allows all users to access dmesg) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..affcd70 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.ddd.rip/ptrcnull/dmesg-notify + +go 1.15 + +require github.com/euank/go-kmsg-parser v2.0.0+incompatible diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b17e31c --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +github.com/euank/go-kmsg-parser v1.0.0 h1:rtNgGgSPLxuBSrjPtDNf6oFvT90i/VraFHuX8YBH+SU= +github.com/euank/go-kmsg-parser v2.0.0+incompatible h1:cHD53+PLQuuQyLZeriD1V/esuG4MuU0Pjs5y6iknohY= +github.com/euank/go-kmsg-parser v2.0.0+incompatible/go.mod h1:MhmAMZ8V4CYH4ybgdRwPr2TU5ThnS43puaKEMpja1uw= diff --git a/main.go b/main.go new file mode 100644 index 0000000..32f51cb --- /dev/null +++ b/main.go @@ -0,0 +1,73 @@ +package main + +import ( + "fmt" + "log" + "os/exec" + "regexp" + "strings" + "time" + + "github.com/euank/go-kmsg-parser/kmsgparser" +) + +func notify(msg string) { + cmd := exec.Command("notify-send", "-t", "5000", "dmesg", msg) + err := cmd.Run() + if err != nil { + log.Println(err) + } +} + +func main() { + parser, err := kmsgparser.NewParser() + if err != nil { + panic(err) + } + + working := false + go func(){ + time.Sleep(1 * time.Second) + working = true + }() + + usbPattern := regexp.MustCompile("usb (\\d+-\\d+): (.*)") + + usbManufacturer := map[string]string{} + usbProduct := map[string]string{} + + for m := range parser.Parse() { + msg := "" + + // log.Println("owo", m.Message) + if sm := usbPattern.FindStringSubmatch(m.Message); len(sm) > 0 { + addr := sm[1] + message := sm[2] + + if strings.HasPrefix(message, "Manufacturer: ") { + usbManufacturer[addr] = message[14:] + } + + if strings.HasPrefix(message, "Product: ") { + usbProduct[addr] = message[9:] + msg = fmt.Sprintf( + "New USB device: %s %s", + usbManufacturer[addr], + usbProduct[addr], + ) + } + + if strings.HasPrefix(message, "USB disconnect") { + msg = fmt.Sprintf( + "USB disconnect: %s %s", + usbManufacturer[addr], + usbProduct[addr], + ) + } + } + + if working && msg != "" { + notify(msg) + } + } +}