2023-03-19 23:10:18 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-03-20 23:25:57 +01:00
|
|
|
"bufio"
|
2023-03-24 17:36:41 +01:00
|
|
|
"fmt"
|
2023-03-19 23:10:18 +01:00
|
|
|
"log"
|
|
|
|
"os/exec"
|
2023-03-24 17:36:41 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
2023-03-19 23:10:18 +01:00
|
|
|
)
|
|
|
|
|
2023-03-24 17:36:41 +01:00
|
|
|
// Executes a command and channel-send its stdout
|
2023-03-24 00:27:51 +01:00
|
|
|
func cmdStdout(commandline []string) chan string {
|
|
|
|
lines := make(chan string)
|
2023-03-19 23:10:18 +01:00
|
|
|
|
2023-03-24 00:27:51 +01:00
|
|
|
go func() {
|
|
|
|
cmd := exec.Command(commandline[0], commandline[1:]...)
|
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("couldn't open stdout on command:", err)
|
|
|
|
}
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
log.Fatal("couldn't start command:", err)
|
|
|
|
}
|
|
|
|
defer stdout.Close()
|
2023-03-20 23:25:57 +01:00
|
|
|
|
2023-03-24 00:27:51 +01:00
|
|
|
scanner := bufio.NewScanner(stdout)
|
|
|
|
for scanner.Scan() {
|
|
|
|
lines <- scanner.Text()
|
|
|
|
}
|
|
|
|
close(lines)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return lines
|
2023-03-19 23:10:18 +01:00
|
|
|
}
|
|
|
|
|
2023-03-24 17:36:41 +01:00
|
|
|
// Whether one of the filter's regexes is matched on a line
|
|
|
|
func (f *Filter) match(line string) string {
|
2023-03-24 00:27:51 +01:00
|
|
|
for _, regex := range f.compiledRegex {
|
2023-03-24 17:36:41 +01:00
|
|
|
|
|
|
|
if matches := regex.FindStringSubmatch(line); matches != nil {
|
|
|
|
|
|
|
|
match := matches[regex.SubexpIndex(f.patternName)]
|
|
|
|
|
|
|
|
log.Printf("INFO %s.%s: match `%v`\n", f.stream.name, f.name, match)
|
|
|
|
return match
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
2023-03-20 23:25:57 +01:00
|
|
|
}
|
2023-03-24 17:36:41 +01:00
|
|
|
return ""
|
2023-03-20 23:25:57 +01:00
|
|
|
}
|
|
|
|
|
2023-03-24 17:36:41 +01:00
|
|
|
func (f *Filter) execActions(match string) {
|
|
|
|
pattern := fmt.Sprintf("<%s>", f.patternName)
|
2023-03-24 00:27:51 +01:00
|
|
|
for _, a := range f.Actions {
|
2023-03-24 17:36:41 +01:00
|
|
|
go a.exec(match, pattern)
|
2023-03-19 23:10:18 +01:00
|
|
|
}
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
2023-03-20 23:25:57 +01:00
|
|
|
|
2023-03-24 17:36:41 +01:00
|
|
|
func (a *Action) exec(match, pattern string) {
|
2023-03-24 00:49:59 +01:00
|
|
|
if a.afterDuration != 0 {
|
|
|
|
time.Sleep(a.afterDuration)
|
|
|
|
}
|
2023-03-20 23:25:57 +01:00
|
|
|
|
2023-03-24 17:36:41 +01:00
|
|
|
computedCommand := make([]string, 0, len(a.Cmd))
|
|
|
|
for _, item := range a.Cmd {
|
|
|
|
computedCommand = append(computedCommand, strings.ReplaceAll(item, pattern, match))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("INFO %s.%s.%s: run %s\n", a.filter.stream.name, a.filter.name, a.name, computedCommand)
|
|
|
|
|
|
|
|
cmd := exec.Command(computedCommand[0], computedCommand[1:]...)
|
2023-03-24 00:27:51 +01:00
|
|
|
if ret := cmd.Run(); ret != nil {
|
2023-03-24 17:36:41 +01:00
|
|
|
log.Printf("ERR %s.%s.%s: run %s, code %s\n", a.filter.stream.name, a.filter.name, a.name, computedCommand, ret)
|
2023-03-19 23:10:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 00:27:51 +01:00
|
|
|
func (s *Stream) handle() {
|
2023-03-24 17:36:41 +01:00
|
|
|
log.Printf("INFO %s: start %s\n", s.name, s.Cmd)
|
2023-03-24 00:27:51 +01:00
|
|
|
|
|
|
|
lines := cmdStdout(s.Cmd)
|
|
|
|
|
|
|
|
for line := range lines {
|
|
|
|
for _, filter := range s.Filters {
|
2023-03-24 17:36:41 +01:00
|
|
|
if match := filter.match(line); match != "" {
|
|
|
|
filter.execActions(match)
|
2023-03-19 23:10:18 +01:00
|
|
|
}
|
|
|
|
}
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
2023-03-19 23:10:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2023-03-23 21:14:53 +01:00
|
|
|
conf := parseConf("./reaction.yml")
|
2023-03-24 00:27:51 +01:00
|
|
|
|
|
|
|
for _, stream := range conf.Streams {
|
|
|
|
go stream.handle()
|
|
|
|
}
|
|
|
|
// Infinite wait
|
|
|
|
<-make(chan bool)
|
2023-03-19 23:10:18 +01:00
|
|
|
}
|