feat: Initial commit

This commit is contained in:
ptrcnull 2021-07-08 22:04:37 +02:00
commit 9588b96f5c
4 changed files with 88 additions and 0 deletions

7
README.md Normal file
View file

@ -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)

5
go.mod Normal file
View file

@ -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

3
go.sum Normal file
View file

@ -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=

73
main.go Normal file
View file

@ -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)
}
}
}