-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.go
142 lines (120 loc) · 3.11 KB
/
watch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"os"
"runtime"
"strings"
"time"
"github.com/hpcloud/tail"
log "github.com/sirupsen/logrus"
)
// Watch defines one or more things to look for in a given file
// along with window properties and trigger threshold
// TODO: Support defining an action here
type Watch struct {
ID string `toml:"id"`
File string `toml:"file"`
Matches []string `toml:"matches"`
Threshold int `toml:"threshold"`
WindowLength int `toml:"window_length"`
Comparison Comparison `toml:"comparison"`
Action string `toml:"action"`
Cooldown int `toml:"cooldown"`
lastTrigger time.Time
}
// Comparison type for use in constants below
type Comparison string
const (
// GreaterThan means comparison by >
GreaterThan Comparison = "GreaterThan"
// LessThan compares by <
LessThan Comparison = "LessThan"
)
// Monitor sets up a tail on a file and continuously
// checks against the defined conditions if an action
// should be triggered
func (w *Watch) Monitor() {
// Default to a window length of 10
if w.WindowLength < 1 {
w.WindowLength = 10
}
// Open a new window
win := &Window{Length: w.WindowLength}
mark := time.Now()
// Get some tail
t := w.setupTail()
// Tell all about it
w.announceNew()
// This is basically the main loop
// We get a line and possibly react to it
for l := range t.Lines {
if w.stringSearch(l.Text) {
win.Append(time.Since(mark).Seconds())
mark = time.Now()
// Don't trigger unless window is full and conditions are met
if win.Full() && w.conditionsMet(win.WindowMean) && time.Since(w.lastTrigger).Seconds() > float64(w.Cooldown) {
w.triggerAction(win)
}
}
}
}
func (w *Watch) setupTail() *tail.Tail {
t, err := tail.TailFile(w.File, tail.Config{
Follow: true,
ReOpen: true,
MustExist: false,
Logger: tail.DiscardingLogger,
Location: &tail.SeekInfo{
Offset: 0,
Whence: os.SEEK_END,
},
})
// Use Polling on Windows
if runtime.GOOS == "windows" {
t.Config.Poll = true
}
if err != nil {
log.Fatalf("Error tailing '%s': %s", w.File, err)
}
return t
}
func (w *Watch) announceNew() {
log.WithFields(log.Fields{
"ID": w.ID,
"File": w.File,
"Matches": w.Matches,
"Threshold": w.Threshold,
"Comparison": w.Comparison,
"WindowLength": w.WindowLength,
"Cooldown": w.Cooldown,
"Action": w.Action,
}).Info("New watch")
}
func (w *Watch) conditionsMet(val float64) bool {
switch w.Comparison {
case GreaterThan:
return val > float64(w.Threshold)
case LessThan:
return val < float64(w.Threshold)
}
// Default to LessThan
return val < float64(w.Threshold)
}
func (w *Watch) stringSearch(haystack string) bool {
for _, needle := range w.Matches {
if strings.Contains(haystack, needle) {
return true
}
}
return false
}
func (w *Watch) triggerAction(win *Window) {
log.WithFields(log.Fields{
"ID": w.ID,
"File": w.File,
"WindowMean": win.WindowMean,
"WindowLength": w.WindowLength,
"Threshold": w.Threshold,
}).Warn("Achtung, baby!")
w.lastTrigger = time.Now()
RunAction(w.Action)
}