-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
236 lines (221 loc) · 5.49 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"os/exec"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
log "github.com/Sirupsen/logrus"
"github.com/mattn/go-shellwords"
"github.com/urfave/cli"
)
const version = "0.1"
var (
username string
url string
color string
attach bool
prefix string
syntax string
)
func main() {
app := cli.NewApp()
app.Name = "mmjournalmon"
app.Usage = "Monitor logs and send updates to mattermost"
app.Version = version
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug, d",
Usage: "Enable debugging.",
},
cli.StringFlag{
Name: "prefix, p",
Usage: "Prefix for messages.",
Value: ":warning:",
},
cli.StringFlag{
Name: "syntax",
Usage: "Syntax for logs.",
},
cli.StringFlag{
Name: "username, u",
Usage: "Username for messaging mattermost",
},
cli.StringFlag{
Name: "url",
Usage: "URL for mattermost webhook",
},
cli.StringFlag{
Name: "color",
Usage: "Color for mattermost webhook",
Value: "#FF0000",
},
cli.StringFlag{
Name: "param, jp",
Usage: "journalctl parameters",
},
cli.StringSliceFlag{
Name: "include, i",
Usage: "Regex pattern of MESSAGES to include.",
},
cli.StringSliceFlag{
Name: "exclude, x",
Usage: "Regex pattern of MESSAGES to exclude.",
},
cli.BoolFlag{
Name: "no-attach",
Usage: "Post logs as text instead of an attachment.",
},
}
app.Action = Run
err := app.Run(os.Args)
if err != nil {
panic(err)
}
}
func Run(ctx *cli.Context) error {
if ctx.Bool("debug") {
log.SetLevel(log.DebugLevel)
log.Info("Debug logging enabled")
}
username = ctx.String("username")
url = ctx.String("url")
color = ctx.String("color")
prefix = ctx.String("prefix")
attach = !ctx.Bool("no-attach")
go mon(ctx)
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
s := <-sig
log.Infof("Signal (%s) received, stopping\n", s)
return nil
}
var (
excludes []*regexp.Regexp
includes []*regexp.Regexp
matches []string
jsplit *regexp.Regexp
)
func mon(ctx *cli.Context) {
var err error
jsplit, err = regexp.Compile("(?m:^[^ ])")
if err != nil {
log.WithError(err).Fatal("Error compiling jsplit regex")
}
for _, ex := range ctx.StringSlice("exclude") {
rx, err := regexp.Compile(ex)
if err != nil {
log.WithError(err).WithField("exclude", ctx.String("exclude")).Fatal("Error compiling exclude regex")
}
excludes = append(excludes, rx)
}
for _, in := range ctx.StringSlice("include") {
ix, err := regexp.Compile(in)
if err != nil {
log.WithError(err).WithField("include", ctx.String("include")).Fatal("Error compiling include regex")
}
includes = append(includes, ix)
}
log.Debug("Starting watch on journal")
go monJournal(ctx.String("param"))
}
func monJournal(param string) {
args := []string{"--follow"}
if len(param) > 0 {
shellArgs, err := shellwords.Parse(param)
if err != nil {
log.WithField("param", param).WithError(err).Fatal("Error parsing parameters")
}
args = append(args, shellArgs...)
}
cmd := exec.Command("journalctl", args...)
stdout, err := cmd.StdoutPipe()
cmd.Stderr = os.Stderr
if err != nil {
log.WithError(err).Fatal("Failed to setup stdout pipe for journalctl")
}
err = cmd.Start()
if err != nil {
log.WithError(err).Fatal("Failed to run journalctl")
}
scanner := bufio.NewScanner(stdout)
scanner.Split(splitJournal)
log.WithField("scanner", scanner).Debug("Scanner setup")
log.Debug("Starting journal watch")
for scanner.Scan() {
msg := scanner.Text()
//log.WithField("msg", msg).Debug("Recieved message")
go notify(msg, "journal")
}
}
func splitJournal(data []byte, atEOF bool) (advance int, token []byte, err error) {
ai := jsplit.FindAllIndex(data, 2)
if ai == nil {
return 0, nil, nil
}
log.WithField("len(ai)", len(ai)).Debug("AI matched")
for _, i := range ai {
if i[0] == 0 {
continue
}
return i[0], data[0:i[0]], nil
}
return len(data), data, nil
}
func notify(lb string, file string) {
for _, in := range includes {
if !in.MatchString(lb) {
return
}
}
for _, ex := range excludes {
if ex.MatchString(lb) {
return
}
}
if strings.TrimSpace(lb) == "" {
return
}
log.WithField("lb", lb).Debug("Notify triggered")
p := make(map[string]interface{})
p["username"] = username
if attach {
a := make(map[string]interface{})
a["fallback"] = fmt.Sprintf("New log entry in %v. \n%v\n[...]%v\n", file, lb[0], lb[len(lb)-1])
a["color"] = color
a["pretext"] = fmt.Sprintf("%v New log entry in %v", prefix, file)
// TODO: When MM PLT-3340 is fixed, wrap txt in code blocks
txt := " " + lb
a["text"] = txt
p["attachments"] = []map[string]interface{}{a}
} else {
txt := lb
txt = fmt.Sprintf("%v New log entry in %v\n```%v\n%v\n```", prefix, file, syntax, txt)
p["text"] = txt
}
pj, err := json.Marshal(p)
if err != nil {
log.WithError(err).WithField("payload", p).Error("Failed to marshall json")
return
}
log.WithField("payload-json", string(pj)).Debug("Json prepared")
r, err := http.Post(url, "application/json", bytes.NewBuffer(pj))
if err != nil {
if strings.Contains(err.Error(), "REFUSED_STREAM") {
log.WithError(err).WithField("url", url).WithField("json", string(pj)).Debug("Failed to post json to url. Retrying.")
time.Sleep(1 * time.Millisecond)
go notify(lb, file)
return
}
log.WithError(err).WithField("url", url).WithField("json", string(pj)).Error("Failed to post json to url.")
return
}
log.WithField("Response", r).Debug("Response from web hook")
}