2023-03-25 18:27:01 +01:00
|
|
|
package app
|
2023-03-23 21:14:53 +01:00
|
|
|
|
|
|
|
import (
|
2023-04-26 17:18:55 +02:00
|
|
|
"encoding/gob"
|
2023-04-27 10:42:19 +02:00
|
|
|
"errors"
|
2023-03-24 00:27:51 +01:00
|
|
|
"fmt"
|
2023-04-26 17:18:55 +02:00
|
|
|
"io"
|
2023-03-23 21:14:53 +01:00
|
|
|
"log"
|
|
|
|
"os"
|
2023-03-24 00:27:51 +01:00
|
|
|
"regexp"
|
2023-03-24 17:36:41 +01:00
|
|
|
"strings"
|
2023-03-24 00:49:59 +01:00
|
|
|
"time"
|
2023-03-23 21:14:53 +01:00
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Conf struct {
|
2023-08-21 23:33:56 +02:00
|
|
|
Patterns map[string]*Pattern `yaml:"patterns"`
|
|
|
|
Streams map[string]*Stream `yaml:"streams"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Pattern struct {
|
|
|
|
Regex string `yaml:"regex"`
|
|
|
|
Ignore []string `yaml:"ignore"`
|
|
|
|
|
|
|
|
name string `yaml:"-"`
|
|
|
|
nameWithBraces string `yaml:"-"`
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
|
|
|
|
2023-05-01 18:21:31 +02:00
|
|
|
// Stream, Filter & Action structures must never be copied.
|
|
|
|
// They're always referenced through pointers
|
|
|
|
|
2023-03-24 00:27:51 +01:00
|
|
|
type Stream struct {
|
2023-04-09 21:24:03 +02:00
|
|
|
name string `yaml:"-"`
|
2023-03-24 17:36:41 +01:00
|
|
|
|
2023-04-09 21:24:03 +02:00
|
|
|
Cmd []string `yaml:"cmd"`
|
|
|
|
Filters map[string]*Filter `yaml:"filters"`
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Filter struct {
|
2023-04-09 21:24:03 +02:00
|
|
|
stream *Stream `yaml:"-"`
|
|
|
|
name string `yaml:"-"`
|
2023-03-24 17:36:41 +01:00
|
|
|
|
2023-08-21 23:33:56 +02:00
|
|
|
Regex []string `yaml:"regex"`
|
|
|
|
compiledRegex []regexp.Regexp `yaml:"-"`
|
|
|
|
pattern *Pattern `yaml:"-"`
|
2023-03-24 17:36:41 +01:00
|
|
|
|
2023-04-09 21:24:03 +02:00
|
|
|
Retry int `yaml:"retry"`
|
|
|
|
RetryPeriod string `yaml:"retry-period"`
|
|
|
|
retryDuration time.Duration `yaml:"-"`
|
2023-03-24 17:36:41 +01:00
|
|
|
|
2023-04-26 17:18:55 +02:00
|
|
|
Actions map[string]*Action `yaml:"actions"`
|
|
|
|
longuestActionDuration *time.Duration
|
2023-03-25 18:04:44 +01:00
|
|
|
|
2023-04-09 21:24:03 +02:00
|
|
|
matches map[string][]time.Time `yaml:"-"`
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Action struct {
|
2023-04-09 21:24:03 +02:00
|
|
|
filter *Filter `yaml:"-"`
|
|
|
|
name string `yaml:"-"`
|
2023-03-24 17:36:41 +01:00
|
|
|
|
2023-04-09 21:24:03 +02:00
|
|
|
Cmd []string `yaml:"cmd"`
|
2023-03-24 17:36:41 +01:00
|
|
|
|
2023-04-09 21:24:03 +02:00
|
|
|
After string `yaml:"after"`
|
|
|
|
afterDuration time.Duration `yaml:"-"`
|
2023-07-12 17:45:16 +02:00
|
|
|
|
|
|
|
OnExit bool `yaml:"onexit"`
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
|
|
|
|
2023-04-26 17:18:55 +02:00
|
|
|
type LogEntry struct {
|
2023-04-27 10:42:19 +02:00
|
|
|
T time.Time
|
|
|
|
Pattern string
|
|
|
|
Stream, Filter string
|
|
|
|
Exec bool
|
2023-04-26 17:18:55 +02:00
|
|
|
}
|
|
|
|
|
2023-03-24 00:27:51 +01:00
|
|
|
func (c *Conf) setup() {
|
2023-08-21 23:33:56 +02:00
|
|
|
|
|
|
|
for patternName := range c.Patterns {
|
|
|
|
pattern := c.Patterns[patternName]
|
|
|
|
pattern.name = patternName
|
|
|
|
pattern.nameWithBraces = fmt.Sprintf("<%s>", pattern.name)
|
|
|
|
|
|
|
|
if pattern.Regex == "" {
|
|
|
|
log.Fatalf("FATAL Bad configuration: pattern's regex %v is empty!", patternName)
|
|
|
|
}
|
|
|
|
|
|
|
|
compiled, err := regexp.Compile(fmt.Sprintf("^%v$", pattern.Regex))
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("FATAL Bad configuration: pattern %v doesn't compile!", patternName)
|
|
|
|
}
|
|
|
|
c.Patterns[patternName].Regex = fmt.Sprintf("(?P<%s>%s)", patternName, pattern.Regex)
|
|
|
|
for _, ignore := range pattern.Ignore {
|
|
|
|
if !compiled.MatchString(ignore) {
|
|
|
|
log.Fatalf("FATAL Bad configuration: pattern ignore '%v' doesn't match pattern %v! It should be fixed or removed.", ignore, pattern.nameWithBraces)
|
|
|
|
}
|
|
|
|
}
|
2023-03-24 17:36:41 +01:00
|
|
|
}
|
2023-08-21 23:33:56 +02:00
|
|
|
|
2023-04-12 09:53:44 +02:00
|
|
|
if len(c.Streams) == 0 {
|
2023-04-27 12:33:56 +02:00
|
|
|
log.Fatalln("FATAL Bad configuration: no streams configured!")
|
2023-04-12 09:53:44 +02:00
|
|
|
}
|
2023-03-24 17:36:41 +01:00
|
|
|
for streamName := range c.Streams {
|
|
|
|
|
|
|
|
stream := c.Streams[streamName]
|
|
|
|
stream.name = streamName
|
|
|
|
|
2023-04-12 09:53:44 +02:00
|
|
|
if len(stream.Filters) == 0 {
|
2023-05-05 16:30:35 +02:00
|
|
|
log.Fatalln("FATAL Bad configuration: no filters configured in", stream.name)
|
2023-04-12 09:53:44 +02:00
|
|
|
}
|
2023-03-24 17:36:41 +01:00
|
|
|
for filterName := range stream.Filters {
|
|
|
|
|
|
|
|
filter := stream.Filters[filterName]
|
|
|
|
filter.stream = stream
|
|
|
|
filter.name = filterName
|
2023-03-25 19:12:11 +01:00
|
|
|
filter.matches = make(map[string][]time.Time)
|
2023-03-24 00:49:59 +01:00
|
|
|
|
|
|
|
// Parse Duration
|
2023-05-05 16:30:35 +02:00
|
|
|
if filter.RetryPeriod == "" {
|
|
|
|
if filter.Retry > 1 {
|
|
|
|
log.Fatalln("FATAL Bad configuration: retry but no retry-duration in", stream.name, ".", filter.name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
retryDuration, err := time.ParseDuration(filter.RetryPeriod)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("FATAL Bad configuration: Failed to parse retry time in", stream.name, ".", filter.name, ":", err)
|
|
|
|
}
|
|
|
|
filter.retryDuration = retryDuration
|
2023-03-24 00:49:59 +01:00
|
|
|
}
|
|
|
|
|
2023-04-12 09:53:44 +02:00
|
|
|
if len(filter.Regex) == 0 {
|
2023-05-05 16:30:35 +02:00
|
|
|
log.Fatalln("FATAL Bad configuration: no regexes configured in", stream.name, ".", filter.name)
|
2023-04-12 09:53:44 +02:00
|
|
|
}
|
2023-03-24 00:27:51 +01:00
|
|
|
// Compute Regexes
|
2023-03-24 17:36:41 +01:00
|
|
|
// Look for Patterns inside Regexes
|
2023-03-24 00:27:51 +01:00
|
|
|
for _, regex := range filter.Regex {
|
2023-03-24 17:36:41 +01:00
|
|
|
for patternName, pattern := range c.Patterns {
|
2023-08-21 23:33:56 +02:00
|
|
|
if strings.Contains(regex, pattern.nameWithBraces) {
|
2023-03-24 17:36:41 +01:00
|
|
|
|
2023-08-21 23:33:56 +02:00
|
|
|
if filter.pattern == nil {
|
|
|
|
filter.pattern = pattern
|
|
|
|
} else if filter.pattern == pattern {
|
2023-03-24 17:36:41 +01:00
|
|
|
// no op
|
2023-08-21 23:33:56 +02:00
|
|
|
} else {
|
2023-03-24 17:36:41 +01:00
|
|
|
log.Fatalf(
|
2023-04-27 12:33:56 +02:00
|
|
|
"Bad configuration: Can't mix different patterns (%s, %s) in same filter (%s.%s)\n",
|
2023-08-21 23:33:56 +02:00
|
|
|
filter.pattern.name, patternName, streamName, filterName,
|
2023-03-24 17:36:41 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-21 23:33:56 +02:00
|
|
|
// FIXME should go in the `if filter.pattern == nil`?
|
|
|
|
regex = strings.Replace(regex, pattern.nameWithBraces, pattern.Regex, 1)
|
2023-03-24 17:36:41 +01:00
|
|
|
}
|
|
|
|
}
|
2023-08-21 23:33:56 +02:00
|
|
|
// TODO regexp.Compile and show proper message if it doesn't instead of panicing
|
2023-03-24 00:27:51 +01:00
|
|
|
filter.compiledRegex = append(filter.compiledRegex, *regexp.MustCompile(regex))
|
|
|
|
}
|
2023-03-24 00:49:59 +01:00
|
|
|
|
2023-04-12 09:53:44 +02:00
|
|
|
if len(filter.Actions) == 0 {
|
2023-05-05 16:30:35 +02:00
|
|
|
log.Fatalln("FATAL Bad configuration: no actions configured in", stream.name, ".", filter.name)
|
2023-04-12 09:53:44 +02:00
|
|
|
}
|
2023-03-24 17:36:41 +01:00
|
|
|
for actionName := range filter.Actions {
|
2023-03-24 00:49:59 +01:00
|
|
|
|
2023-03-24 17:36:41 +01:00
|
|
|
action := filter.Actions[actionName]
|
|
|
|
action.filter = filter
|
2023-03-24 00:27:51 +01:00
|
|
|
action.name = actionName
|
2023-03-24 00:49:59 +01:00
|
|
|
|
|
|
|
// Parse Duration
|
|
|
|
if action.After != "" {
|
|
|
|
afterDuration, err := time.ParseDuration(action.After)
|
|
|
|
if err != nil {
|
2023-05-05 16:30:35 +02:00
|
|
|
log.Fatalln("FATAL Bad configuration: Failed to parse after time in ", stream.name, ".", filter.name, ".", action.name, ":", err)
|
2023-03-24 00:49:59 +01:00
|
|
|
}
|
|
|
|
action.afterDuration = afterDuration
|
2023-07-12 17:45:16 +02:00
|
|
|
} else if action.OnExit {
|
|
|
|
log.Fatalln("FATAL Bad configuration: Cannot have `onexit:true` without an `after` directive in", stream.name, ".", filter.name, ".", action.name)
|
2023-03-24 00:49:59 +01:00
|
|
|
}
|
2023-04-27 10:42:19 +02:00
|
|
|
if filter.longuestActionDuration == nil || filter.longuestActionDuration.Milliseconds() < action.afterDuration.Milliseconds() {
|
|
|
|
filter.longuestActionDuration = &action.afterDuration
|
|
|
|
}
|
2023-03-23 21:14:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-26 17:18:55 +02:00
|
|
|
var DBname = "./reaction.db"
|
|
|
|
var DBnewName = "./reaction.new.db"
|
|
|
|
|
2023-04-27 10:42:19 +02:00
|
|
|
func (c *Conf) updateFromDB() *gob.Encoder {
|
2023-04-26 17:18:55 +02:00
|
|
|
file, err := os.Open(DBname)
|
|
|
|
if err != nil {
|
2023-04-27 10:42:19 +02:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
2023-04-27 12:33:56 +02:00
|
|
|
log.Printf("WARN No DB found at %s. It's ok if this is the first time reaction is running.\n", DBname)
|
2023-04-27 10:42:19 +02:00
|
|
|
|
|
|
|
file, err := os.Create(DBname)
|
|
|
|
if err != nil {
|
2023-04-27 12:33:56 +02:00
|
|
|
log.Fatalln("FATAL Failed to create DB:", err)
|
2023-04-27 10:42:19 +02:00
|
|
|
}
|
|
|
|
return gob.NewEncoder(file)
|
2023-04-26 17:18:55 +02:00
|
|
|
}
|
2023-04-27 12:33:56 +02:00
|
|
|
log.Fatalln("FATAL Failed to open DB:", err)
|
2023-04-26 17:18:55 +02:00
|
|
|
}
|
2023-04-27 10:42:19 +02:00
|
|
|
dec := gob.NewDecoder(file)
|
2023-04-26 17:18:55 +02:00
|
|
|
|
|
|
|
newfile, err := os.Create(DBnewName)
|
|
|
|
if err != nil {
|
2023-04-27 12:33:56 +02:00
|
|
|
log.Fatalln("FATAL Failed to create new DB:", err)
|
2023-04-26 17:18:55 +02:00
|
|
|
}
|
2023-04-27 10:42:19 +02:00
|
|
|
enc := gob.NewEncoder(newfile)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err := file.Close()
|
|
|
|
if err != nil {
|
2023-04-27 12:33:56 +02:00
|
|
|
log.Fatalln("FATAL Failed to close old DB:", err)
|
2023-04-27 10:42:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// It should be ok to rename an open file
|
|
|
|
err = os.Rename(DBnewName, DBname)
|
|
|
|
if err != nil {
|
2023-04-27 12:33:56 +02:00
|
|
|
log.Fatalln("FATAL Failed to replace old DB with new one:", err)
|
2023-04-27 10:42:19 +02:00
|
|
|
}
|
|
|
|
}()
|
2023-04-26 17:18:55 +02:00
|
|
|
|
|
|
|
// This extra code is made to warn only one time for each non-existant filter
|
|
|
|
type SF struct{ s, f string }
|
|
|
|
discardedEntries := make(map[SF]bool)
|
|
|
|
malformedEntries := 0
|
|
|
|
defer func() {
|
|
|
|
for sf, t := range discardedEntries {
|
|
|
|
if t {
|
2023-04-27 12:33:56 +02:00
|
|
|
log.Printf("WARN info discarded from the DB: stream/filter not found: %s.%s\n", sf.s, sf.f)
|
2023-04-26 17:18:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if malformedEntries > 0 {
|
2023-04-27 12:33:56 +02:00
|
|
|
log.Printf("WARN %v malformed entries discarded from the DB\n", malformedEntries)
|
2023-04-26 17:18:55 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
encodeOrFatal := func(entry LogEntry) {
|
|
|
|
err = enc.Encode(entry)
|
|
|
|
if err != nil {
|
2023-04-27 12:33:56 +02:00
|
|
|
log.Fatalln("FATAL Failed to write to new DB:", err)
|
2023-04-26 17:18:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
for {
|
|
|
|
var entry LogEntry
|
|
|
|
var filter *Filter
|
|
|
|
|
|
|
|
// decode entry
|
|
|
|
err = dec.Decode(&entry)
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
2023-04-27 10:42:19 +02:00
|
|
|
return enc
|
2023-04-26 17:18:55 +02:00
|
|
|
}
|
|
|
|
malformedEntries++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve related filter
|
2023-04-27 10:42:19 +02:00
|
|
|
if stream := c.Streams[entry.Stream]; stream != nil {
|
|
|
|
filter = stream.Filters[entry.Filter]
|
2023-04-26 17:18:55 +02:00
|
|
|
}
|
|
|
|
if filter == nil {
|
2023-04-27 10:42:19 +02:00
|
|
|
discardedEntries[SF{entry.Stream, entry.Filter}] = true
|
2023-04-26 17:18:55 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// store matches
|
2023-04-27 10:42:19 +02:00
|
|
|
if !entry.Exec && entry.T.Add(filter.retryDuration).Unix() > now.Unix() {
|
|
|
|
filter.matches[entry.Pattern] = append(filter.matches[entry.Pattern], entry.T)
|
2023-04-26 17:18:55 +02:00
|
|
|
|
|
|
|
encodeOrFatal(entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
// replay executions
|
2023-04-27 10:42:19 +02:00
|
|
|
if entry.Exec && entry.T.Add(*filter.longuestActionDuration).Unix() > now.Unix() {
|
|
|
|
delete(filter.matches, entry.Pattern)
|
|
|
|
filter.execActions(entry.Pattern, now.Sub(entry.T))
|
2023-04-26 17:18:55 +02:00
|
|
|
|
|
|
|
encodeOrFatal(entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 20:03:22 +02:00
|
|
|
func parseConf(filename string) *Conf {
|
2023-03-23 21:14:53 +01:00
|
|
|
|
|
|
|
data, err := os.ReadFile(filename)
|
|
|
|
if err != nil {
|
2023-04-27 12:33:56 +02:00
|
|
|
log.Fatalln("FATAL Failed to read configuration file:", err)
|
2023-03-23 21:14:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var conf Conf
|
|
|
|
err = yaml.Unmarshal(data, &conf)
|
|
|
|
if err != nil {
|
2023-04-27 12:33:56 +02:00
|
|
|
log.Fatalln("FATAL Failed to parse configuration file:", err)
|
2023-03-23 21:14:53 +01:00
|
|
|
}
|
|
|
|
|
2023-03-24 00:27:51 +01:00
|
|
|
conf.setup()
|
2023-05-01 18:21:31 +02:00
|
|
|
return &conf
|
2023-03-23 21:14:53 +01:00
|
|
|
}
|