2023-03-25 18:27:01 +01:00
|
|
|
package app
|
2023-03-19 23:10:18 +01:00
|
|
|
|
|
|
|
import (
|
2023-03-20 23:25:57 +01:00
|
|
|
"bufio"
|
2023-04-27 10:42:19 +02:00
|
|
|
"syscall"
|
2023-03-25 19:12:11 +01:00
|
|
|
|
2023-03-25 18:04:44 +01:00
|
|
|
// "fmt"
|
2023-03-19 23:10:18 +01:00
|
|
|
"log"
|
2023-03-24 17:52:00 +01:00
|
|
|
"os"
|
2023-03-19 23:10:18 +01:00
|
|
|
"os/exec"
|
2023-04-27 10:42:19 +02:00
|
|
|
"os/signal"
|
2023-03-24 17:36:41 +01:00
|
|
|
"strings"
|
2023-03-25 18:04:44 +01:00
|
|
|
"sync"
|
2023-03-24 17:36:41 +01:00
|
|
|
"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-25 18:04:44 +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 {
|
2023-05-04 01:01:22 +02:00
|
|
|
log.Fatalln("FATAL couldn't open stdout on command:", err)
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
|
|
|
if err := cmd.Start(); err != nil {
|
2023-05-04 01:01:22 +02:00
|
|
|
log.Fatalln("FATAL couldn't start command:", err)
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
|
|
|
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() {
|
2023-03-25 18:04:44 +01:00
|
|
|
line := scanner.Text()
|
|
|
|
lines <- &line
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
|
|
|
close(lines)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return lines
|
2023-03-19 23:10:18 +01:00
|
|
|
}
|
|
|
|
|
2023-08-21 23:33:56 +02:00
|
|
|
func (p *Pattern) notAnIgnore(match *string) bool {
|
|
|
|
for _, ignore := range p.Ignore {
|
|
|
|
if ignore == *match {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-03-24 17:36:41 +01:00
|
|
|
// Whether one of the filter's regexes is matched on a line
|
2023-03-25 18:04:44 +01:00
|
|
|
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
|
|
|
|
2023-03-25 18:04:44 +01:00
|
|
|
if matches := regex.FindStringSubmatch(*line); matches != nil {
|
2023-03-24 17:36:41 +01:00
|
|
|
|
2023-08-21 23:33:56 +02:00
|
|
|
match := matches[regex.SubexpIndex(f.pattern.name)]
|
2023-03-24 17:36:41 +01:00
|
|
|
|
2023-08-21 23:33:56 +02:00
|
|
|
if f.pattern.notAnIgnore(&match) {
|
|
|
|
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-04-26 17:18:55 +02:00
|
|
|
func (f *Filter) execActions(match string, advance time.Duration) {
|
2023-03-24 00:27:51 +01:00
|
|
|
for _, a := range f.Actions {
|
2023-03-25 18:04:44 +01:00
|
|
|
wgActions.Add(1)
|
2023-04-26 17:18:55 +02:00
|
|
|
go a.exec(match, advance)
|
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-04-26 17:18:55 +02:00
|
|
|
func (a *Action) exec(match string, advance time.Duration) {
|
2023-03-25 18:04:44 +01:00
|
|
|
defer wgActions.Done()
|
2023-04-27 11:58:57 +02:00
|
|
|
|
2023-05-03 20:03:22 +02:00
|
|
|
// Wait for either end of sleep time, or actionStore requesting stop
|
2023-04-26 17:18:55 +02:00
|
|
|
if a.afterDuration != 0 && a.afterDuration > advance {
|
2023-05-03 20:03:22 +02:00
|
|
|
stopAction := actionStore.Register(a, match)
|
2023-04-27 11:58:57 +02:00
|
|
|
select {
|
2023-09-09 19:46:04 +02:00
|
|
|
case <-time.After(a.afterDuration - advance):
|
2023-09-06 02:00:33 +02:00
|
|
|
// Let's not wait for the lock
|
|
|
|
go actionStore.Unregister(a, match, stopAction)
|
|
|
|
case doExec := <-stopAction:
|
|
|
|
// no need to unregister here
|
|
|
|
if !doExec {
|
|
|
|
return
|
|
|
|
}
|
2023-04-27 11:58:57 +02:00
|
|
|
}
|
2023-03-24 00:49:59 +01:00
|
|
|
}
|
2023-03-20 23:25:57 +01:00
|
|
|
|
2023-09-06 02:00:33 +02:00
|
|
|
computedCommand := make([]string, 0, len(a.Cmd))
|
|
|
|
for _, item := range a.Cmd {
|
|
|
|
computedCommand = append(computedCommand, strings.ReplaceAll(item, a.filter.pattern.nameWithBraces, match))
|
|
|
|
}
|
2023-03-24 17:36:41 +01:00
|
|
|
|
2023-09-06 02:00:33 +02:00
|
|
|
log.Printf("INFO %s.%s.%s: run %s\n", a.filter.stream.name, a.filter.name, a.name, computedCommand)
|
2023-03-24 17:36:41 +01:00
|
|
|
|
2023-09-06 02:00:33 +02:00
|
|
|
cmd := exec.Command(computedCommand[0], computedCommand[1:]...)
|
2023-03-24 18:06:57 +01:00
|
|
|
|
2023-09-06 02:00:33 +02:00
|
|
|
if ret := cmd.Run(); ret != nil {
|
|
|
|
log.Printf("ERROR %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-09-09 23:38:53 +02:00
|
|
|
func MatchesManager() {
|
|
|
|
matches := make(MatchesMap)
|
|
|
|
var pf PF
|
|
|
|
var pft PFT
|
|
|
|
end := false
|
|
|
|
|
|
|
|
for !end {
|
|
|
|
select {
|
|
|
|
case pf = <-cleanMatchesC:
|
|
|
|
delete(matches[pf.f], pf.p)
|
|
|
|
case pft, ok := <-startupMatchesC:
|
|
|
|
if !ok {
|
|
|
|
end = true
|
|
|
|
} else {
|
|
|
|
_ = matchesManagerHandleMatch(matches, pft)
|
|
|
|
}
|
2023-03-25 19:12:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case pf = <-cleanMatchesC:
|
|
|
|
delete(matches[pf.f], pf.p)
|
|
|
|
case pft = <-matchesC:
|
2023-04-26 17:18:55 +02:00
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
entry := LogEntry{pft.t, pft.p, pft.f.stream.name, pft.f.name, false}
|
2023-03-25 19:12:11 +01:00
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
entry.Exec = matchesManagerHandleMatch(matches, pft)
|
2023-03-25 19:12:11 +01:00
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
logsC <- entry
|
2023-09-09 20:36:41 +02:00
|
|
|
}
|
2023-09-09 23:38:53 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-26 17:18:55 +02:00
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
func matchesManagerHandleMatch(matches MatchesMap, pft PFT) bool {
|
|
|
|
var filter *Filter
|
|
|
|
var match string
|
|
|
|
var now time.Time
|
|
|
|
filter = pft.f
|
|
|
|
match = pft.p
|
|
|
|
now = pft.t
|
|
|
|
|
|
|
|
if filter.Retry > 1 {
|
|
|
|
// make sure map exists
|
|
|
|
if matches[filter] == nil {
|
|
|
|
matches[filter] = make(map[string][]time.Time)
|
|
|
|
}
|
|
|
|
// clean old matches
|
|
|
|
newMatches := make([]time.Time, 0, len(matches[filter][match]))
|
|
|
|
for _, old := range matches[filter][match] {
|
|
|
|
if old.Add(filter.retryDuration).After(now) {
|
|
|
|
newMatches = append(newMatches, old)
|
|
|
|
}
|
2023-03-25 18:04:44 +01:00
|
|
|
}
|
2023-09-09 23:38:53 +02:00
|
|
|
// add new match
|
|
|
|
newMatches = append(newMatches, now)
|
|
|
|
matches[filter][match] = newMatches
|
|
|
|
}
|
2023-03-25 18:04:44 +01:00
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
if filter.Retry <= 1 || len(matches[filter][match]) >= filter.Retry {
|
|
|
|
delete(matches[filter], match)
|
|
|
|
filter.execActions(match, time.Duration(0))
|
|
|
|
return true
|
2023-09-09 20:36:41 +02:00
|
|
|
}
|
2023-09-09 23:38:53 +02:00
|
|
|
return false
|
2023-03-25 18:04:44 +01:00
|
|
|
}
|
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
func StreamManager(s *Stream, endedSignal chan *Stream) {
|
2023-04-27 12:33:56 +02:00
|
|
|
log.Printf("INFO %s: start %s\n", s.name, s.Cmd)
|
2023-03-24 00:27:51 +01:00
|
|
|
|
|
|
|
lines := cmdStdout(s.Cmd)
|
2023-04-27 11:58:57 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case line, ok := <-lines:
|
|
|
|
if !ok {
|
|
|
|
endedSignal <- s
|
|
|
|
return
|
|
|
|
}
|
2023-09-09 20:36:41 +02:00
|
|
|
for _, filter := range s.Filters {
|
2023-09-09 23:38:53 +02:00
|
|
|
if match := filter.match(line); match != "" {
|
|
|
|
matchesC <- PFT{match, filter, time.Now()}
|
|
|
|
}
|
2023-04-27 11:58:57 +02:00
|
|
|
}
|
|
|
|
case _, ok := <-stopStreams:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-25 18:04:44 +01:00
|
|
|
|
2023-03-19 23:10:18 +01:00
|
|
|
}
|
|
|
|
|
2023-04-27 11:58:57 +02:00
|
|
|
var stopStreams chan bool
|
2023-05-03 20:03:22 +02:00
|
|
|
var actionStore ActionStore
|
2023-03-25 18:04:44 +01:00
|
|
|
var wgActions sync.WaitGroup
|
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
// MatchesManager → DatabaseManager
|
|
|
|
var logsC chan LogEntry
|
|
|
|
// SocketManager → DatabaseManager
|
|
|
|
var flushesC chan LogEntry
|
|
|
|
|
|
|
|
// DatabaseManager → MatchesManager
|
|
|
|
var startupMatchesC chan PFT
|
|
|
|
// StreamManager → MatchesManager
|
|
|
|
var matchesC chan PFT
|
|
|
|
// StreamManager, DatabaseManager → MatchesManager
|
|
|
|
var cleanMatchesC chan PF
|
|
|
|
// MatchesManager → ExecsManager
|
|
|
|
var execsC chan PA
|
2023-04-26 17:18:55 +02:00
|
|
|
|
2023-09-03 12:13:18 +02:00
|
|
|
func Daemon(confFilename string) {
|
2023-05-03 20:03:22 +02:00
|
|
|
actionStore.store = make(ActionMap)
|
|
|
|
|
2023-09-03 12:13:18 +02:00
|
|
|
conf := parseConf(confFilename)
|
2023-09-06 02:00:33 +02:00
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
logsC = make(chan LogEntry)
|
|
|
|
flushesC = make(chan LogEntry)
|
|
|
|
matchesC = make(chan PFT)
|
|
|
|
startupMatchesC = make(chan PFT)
|
|
|
|
cleanMatchesC = make(chan PF)
|
|
|
|
execsC = make(chan PA)
|
|
|
|
|
2023-09-09 20:42:47 +02:00
|
|
|
go DatabaseManager(conf)
|
2023-09-09 23:38:53 +02:00
|
|
|
go MatchesManager()
|
2023-05-01 18:21:31 +02:00
|
|
|
|
|
|
|
// Ready to start
|
|
|
|
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
2023-04-26 17:18:55 +02:00
|
|
|
|
2023-04-27 11:58:57 +02:00
|
|
|
stopStreams = make(chan bool)
|
|
|
|
|
2023-03-25 18:04:44 +01:00
|
|
|
endSignals := make(chan *Stream)
|
2023-09-09 19:32:23 +02:00
|
|
|
nbStreamsInExecution := len(conf.Streams)
|
2023-03-25 18:04:44 +01:00
|
|
|
|
2023-03-24 00:27:51 +01:00
|
|
|
for _, stream := range conf.Streams {
|
2023-09-09 23:38:53 +02:00
|
|
|
go StreamManager(stream, endSignals)
|
2023-03-25 18:04:44 +01:00
|
|
|
}
|
|
|
|
|
2023-09-09 23:38:53 +02:00
|
|
|
go SocketManager()
|
2023-05-03 20:03:22 +02:00
|
|
|
|
2023-04-27 10:42:19 +02:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case finishedStream := <-endSignals:
|
2023-04-27 12:33:56 +02:00
|
|
|
log.Printf("ERROR %s stream finished", finishedStream.name)
|
2023-09-09 19:32:23 +02:00
|
|
|
nbStreamsInExecution--
|
|
|
|
if nbStreamsInExecution == 0 {
|
2023-04-27 10:42:19 +02:00
|
|
|
quit()
|
|
|
|
}
|
|
|
|
case <-sigs:
|
2023-05-04 01:01:22 +02:00
|
|
|
log.Printf("INFO Received SIGINT/SIGTERM, exiting")
|
2023-04-27 10:42:19 +02:00
|
|
|
quit()
|
|
|
|
}
|
2023-03-24 00:27:51 +01:00
|
|
|
}
|
2023-04-27 10:42:19 +02:00
|
|
|
}
|
2023-03-25 18:04:44 +01:00
|
|
|
|
2023-04-27 10:42:19 +02:00
|
|
|
func quit() {
|
2023-09-09 23:38:53 +02:00
|
|
|
log.Println("INFO Quitting...")
|
2023-04-27 11:58:57 +02:00
|
|
|
// stop all streams
|
|
|
|
close(stopStreams)
|
|
|
|
// stop all actions
|
2023-05-03 20:03:22 +02:00
|
|
|
actionStore.Quit()
|
2023-04-27 11:58:57 +02:00
|
|
|
// wait for them to complete
|
2023-03-25 18:04:44 +01:00
|
|
|
wgActions.Wait()
|
2023-05-03 20:03:22 +02:00
|
|
|
// delete pipe
|
2023-09-03 12:13:18 +02:00
|
|
|
err := os.Remove(*SocketPath)
|
2023-05-05 15:33:00 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Failed to remove socket:", err)
|
|
|
|
}
|
2023-04-27 11:58:57 +02:00
|
|
|
|
2023-03-25 18:04:44 +01:00
|
|
|
os.Exit(3)
|
2023-03-19 23:10:18 +01:00
|
|
|
}
|