Skip to content
Snippets Groups Projects
Commit 964fc742 authored by Erick Hitter's avatar Erick Hitter
Browse files

Better organization

parent 5198d906
No related branches found
No related tags found
No related merge requests found
...@@ -33,11 +33,11 @@ var ( ...@@ -33,11 +33,11 @@ var (
iconURL string iconURL string
searchRegex string searchRegex string
mh *matterhook.Client
logger *log.Logger logger *log.Logger
debugDest string debugDest string
debug bool debug bool
tailer *tail.Tail
) )
func init() { func init() {
...@@ -59,26 +59,17 @@ func init() { ...@@ -59,26 +59,17 @@ func init() {
if !govalidator.IsURL(webhookURL) || len(channel) < 2 { if !govalidator.IsURL(webhookURL) || len(channel) < 2 {
usage() usage()
} }
mh = matterhook.New(webhookURL, matterhook.Config{DisableServer: true})
} }
func main() { func main() {
logger.Printf("Monitoring %s", logPath) logger.Printf("Monitoring %s", logPath)
logger.Printf("Forwarding to channel \"%s\" as user \"%s\" at %s", channel, username, webhookURL) logger.Printf("Forwarding to channel \"%s\" as user \"%s\" at %s", channel, username, webhookURL)
sig := make(chan os.Signal, 1) sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
go tailAndSend()
caughtSig := <-sig
tailer.Stop()
tailer.Cleanup()
logger.Printf("Stopping, got signal %s", caughtSig)
}
func tailAndSend() {
mhConfig := matterhook.Config{DisableServer: true}
mh := matterhook.New(webhookURL, mhConfig)
t, err := tail.TailFile(logPath, tail.Config{ t, err := tail.TailFile(logPath, tail.Config{
Follow: true, Follow: true,
Location: &tail.SeekInfo{Offset: 0, Whence: 2}, Location: &tail.SeekInfo{Offset: 0, Whence: 2},
...@@ -86,30 +77,37 @@ func tailAndSend() { ...@@ -86,30 +77,37 @@ func tailAndSend() {
ReOpen: true, ReOpen: true,
Logger: logger, Logger: logger,
}) })
tailer = t
if err != nil { if err != nil {
logger.Println(err) logger.Println(err)
usage() t.Cleanup()
close(sig)
os.Exit(3)
} }
go parseLinesAndSend(t)
caughtSig := <-sig
t.Stop()
t.Cleanup()
logger.Printf("Stopping, got signal %s", caughtSig)
}
func parseLinesAndSend(t *tail.Tail) {
for line := range t.Lines { for line := range t.Lines {
if line.Err != nil { if line.Err != nil {
continue continue
} }
if len(searchRegex) == 0 { if len(searchRegex) == 0 {
go sendLine(mh, line) go sendLine(line)
} else { } else if matched, _ := regexp.MatchString(searchRegex, line.Text); matched {
matched, _ := regexp.MatchString(searchRegex, line.Text) go sendLine(line)
if matched {
go sendLine(mh, line)
}
} }
} }
} }
func sendLine(mh *matterhook.Client, line *tail.Line) { func sendLine(line *tail.Line) {
atts := attachments{ atts := attachments{
attachment{ attachment{
Fallback: fmt.Sprintf("New entry in %s", logPath), Fallback: fmt.Sprintf("New entry in %s", logPath),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment