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

Successful Mattermost integration

parent 24034aef
Branches
Tags
No related merge requests found
...@@ -10,10 +10,20 @@ import ( ...@@ -10,10 +10,20 @@ import (
"regexp" "regexp"
"syscall" "syscall"
"github.com/42wim/matterbridge/matterhook"
"github.com/asaskevich/govalidator" "github.com/asaskevich/govalidator"
"github.com/hpcloud/tail" "github.com/hpcloud/tail"
) )
type attachment struct {
Fallback string `json:"fallback,omitempty"`
Pretext string `json:"pretext,omitempty"`
Text string `json:"text"`
Color string `json:"color,omitempty"`
}
type attachments []attachment
var ( var (
logPath string logPath string
webhookURL string webhookURL string
...@@ -57,7 +67,7 @@ func main() { ...@@ -57,7 +67,7 @@ func main() {
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 tailLog() go tailAndSend()
caughtSig := <-sig caughtSig := <-sig
tailer.Stop() tailer.Stop()
...@@ -65,8 +75,17 @@ func main() { ...@@ -65,8 +75,17 @@ func main() {
logger.Printf("Stopping, got signal %s", caughtSig) logger.Printf("Stopping, got signal %s", caughtSig)
} }
func tailLog() { func tailAndSend() {
t, err := tail.TailFile(logPath, tail.Config{Follow: true, MustExist: true, ReOpen: true}) mhConfig := matterhook.Config{DisableServer: true}
mh := matterhook.New(webhookURL, mhConfig)
t, err := tail.TailFile(logPath, tail.Config{
Follow: true,
Location: &tail.SeekInfo{Offset: 0, Whence: 2},
MustExist: true,
ReOpen: true,
Logger: logger,
})
tailer = t tailer = t
if err != nil { if err != nil {
...@@ -80,18 +99,32 @@ func tailLog() { ...@@ -80,18 +99,32 @@ func tailLog() {
} }
if len(searchRegex) == 0 { if len(searchRegex) == 0 {
go sendLine(line) go sendLine(mh, line)
} else { } else {
matched, _ := regexp.MatchString(searchRegex, line.Text) matched, _ := regexp.MatchString(searchRegex, line.Text)
if matched { if matched {
go sendLine(line) go sendLine(mh, line)
} }
} }
} }
} }
func sendLine(line *tail.Line) { func sendLine(mh *matterhook.Client, line *tail.Line) {
logger.Println(line.Text) atts := attachments{
attachment{
Fallback: fmt.Sprintf("New entry in %s", logPath),
Pretext: fmt.Sprintf("In `%s` at `%s`:", logPath, line.Time),
Text: fmt.Sprintf(" %s", line.Text),
Color: color,
},
}
mh.Send(matterhook.OMessage{
Channel: channel,
UserName: username,
Attachments: atts,
IconURL: iconURL,
})
} }
func setUpLogger() { func setUpLogger() {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment