diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 47ae277b262f186502b2e99515ed66bf8592d598..f7a15a44d454dd19ec8a392f483adfeac02151de 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -10,6 +10,7 @@ image: golang:latest before_script: - ln -s /builds /go/src/git.ethitter.com - cd /go/src/git.ethitter.com/debian/eth-log-alerting + - go version stages: - test diff --git a/eth-log-alerting.go b/eth-log-alerting.go index 89e05adb3ca148ed5695f276021633b3ebcb6469..dac58a50339629f02d3dc3b16b0058d52338dd70 100644 --- a/eth-log-alerting.go +++ b/eth-log-alerting.go @@ -12,8 +12,8 @@ import ( "regexp" "syscall" - "github.com/42wim/matterbridge/matterhook" "github.com/asaskevich/govalidator" + "github.com/ashwanthkumar/slack-go-webhook" "github.com/hpcloud/tail" ) @@ -33,15 +33,6 @@ type logConfig struct { SearchRegex string `json:"search"` } -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 ( configPath string logConfigs []logConfig @@ -122,44 +113,48 @@ func tailLog(logCfg logConfig) { return } - mh := matterhook.New(logCfg.WebhookURL, matterhook.Config{DisableServer: true}) - - parseLinesAndSend(t, mh, logCfg) + parseLinesAndSend(t, logCfg) t.Stop() t.Cleanup() } -func parseLinesAndSend(t *tail.Tail, mh *matterhook.Client, logCfg logConfig) { +func parseLinesAndSend(t *tail.Tail, logCfg logConfig) { for line := range t.Lines { if line.Err != nil { continue } if len(logCfg.SearchRegex) == 0 { - go sendLine(line, mh, logCfg) + go sendLine(line, logCfg) } else if matched, _ := regexp.MatchString(logCfg.SearchRegex, line.Text); matched { - go sendLine(line, mh, logCfg) + go sendLine(line, logCfg) } } } -func sendLine(line *tail.Line, mh *matterhook.Client, logCfg logConfig) { - atts := attachments{ - attachment{ - Fallback: fmt.Sprintf("New entry in %s", logCfg.LogPath), - Pretext: fmt.Sprintf("In `%s` at `%s`:", logCfg.LogPath, line.Time), - Text: fmt.Sprintf(" %s", line.Text), - Color: logCfg.Color, - }, - } +func sendLine(line *tail.Line, logCfg logConfig) { + text := fmt.Sprintf(" %s", line.Text) + + att := slack.Attachment{} + att.AddField(slack.Field{Title: "Fallback", Value: fmt.Sprintf("New entry in %s", logCfg.LogPath)}) + att.AddField(slack.Field{Title: "Pretext", Value: fmt.Sprintf("In `%s` at `%s`:", logCfg.LogPath, line.Time)}) + att.AddField(slack.Field{Title: "Text", Value: text}) + att.AddField(slack.Field{Title: "Color", Value: logCfg.Color}) - mh.Send(matterhook.OMessage{ + payload := slack.Payload{ + Text: text, + Username: logCfg.Username, Channel: logCfg.Channel, - UserName: logCfg.Username, - Attachments: atts, - IconURL: logCfg.IconURL, - }) + IconEmoji: logCfg.IconURL, + Attachments: []slack.Attachment{att}, + } + + err := slack.Send(logCfg.WebhookURL, "", payload) + if len(err) > 0 { + fmt.Printf("error: %s\n", err) + } + } func setUpLogger() {