diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 47ae277b262f186502b2e99515ed66bf8592d598..a428e66eb5b8e480e188c67c54986de7622b7755 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,44 +1,40 @@ # This file is a template, and might need editing before it works on your project. image: golang:latest +variables: + REPO_NAME: git.ethitter.com/debian/eth-log-alerting + # The problem is that to be able to use go get, one needs to put # the repository in the $GOPATH. So for example if your gitlab domain -# is mydomainperso.com, and that your repository is repos/projectname, and +# is gitlab.com, and that your repository is namespace/project, and # the default GOPATH being /go, then you'd need to have your -# repository in /go/src/mydomainperso.com/repos/projectname +# repository in /go/src/gitlab.com/namespace/project # Thus, making a symbolic link corrects this. before_script: - - ln -s /builds /go/src/git.ethitter.com - - cd /go/src/git.ethitter.com/debian/eth-log-alerting + - mkdir -p $GOPATH/src/$(dirname $REPO_NAME) + - ln -svf $CI_PROJECT_DIR $GOPATH/src/$REPO_NAME + - cd $GOPATH/src/$REPO_NAME + + - go version + + - go get github.com/asaskevich/govalidator + - go get github.com/ashwanthkumar/slack-go-webhook + - go get github.com/hpcloud/tail stages: - - test - - build + - test + - build format: - stage: test - script: - # Add here all the dependencies, or use glide/govendor to get - # them automatically. - # - curl https://glide.sh/get | sh - - go get github.com/42wim/matterbridge/matterhook - - go get github.com/asaskevich/govalidator - - go get github.com/hpcloud/tail - - go get github.com/alecthomas/kingpin - - go tool vet -composites=false -shadow=true *.go - - go test -race $(go list ./... | grep -v /vendor/) + stage: test + script: + - go tool vet -composites=false -shadow=true *.go + - go test -race $(go list ./... | grep -v /vendor/) compile: - stage: build - script: - # Add here all the dependencies, or use glide/govendor/... - # to get them automatically. - - go get github.com/42wim/matterbridge/matterhook - - go get github.com/asaskevich/govalidator - - go get github.com/hpcloud/tail - - go get github.com/alecthomas/kingpin - # Better put this in a Makefile - - go build -race -ldflags "-extldflags '-static'" -o eth-log-alerting - artifacts: - paths: - - eth-log-alerting + stage: build + script: + - go build -race -ldflags "-extldflags '-static'" -o eth-log-alerting + artifacts: + paths: + - eth-log-alerting diff --git a/README.md b/README.md index 9f18b99c8fd1296c234f1a98b50711a85c50d394..332927776f120cfcab9da903943b0222b754f142 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Log Alerting +Log Alerting [](https://git.ethitter.com/debian/eth-log-alerting/commits/master) ============ Pipe logs to Mattermost (or Slack) webhooks @@ -11,7 +11,7 @@ Pipe logs to Mattermost (or Slack) webhooks 1. `git clone https://git.ethitter.com/debian/eth-log-alerting.git /usr/local/bin/eth-log-alerting` 1. `cd /usr/local/bin/eth-log-alerting` -1. `go get github.com/42wim/matterbridge/matterhook` +1. `go get github.com/ashwanthkumar/slack-go-webhook` 1. `go get github.com/asaskevich/govalidator` 1. `go get github.com/hpcloud/tail` 1. `go build eth-log-alerting.go` diff --git a/eth-log-alerting.go b/eth-log-alerting.go index 89e05adb3ca148ed5695f276021633b3ebcb6469..0b1ed746710acd680417fcaabd455c1411eee099 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,49 @@ 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) { + fallback := fmt.Sprintf("New entry in %s", logCfg.LogPath) + pretext := fmt.Sprintf("In `%s` at `%s`:", logCfg.LogPath, line.Time) + text := fmt.Sprintf("```\n%s\n```", line.Text) + att := slack.Attachment{ + Color: &logCfg.Color, + Fallback: &fallback, + PreText: &pretext, + Text: &text, } - mh.Send(matterhook.OMessage{ + payload := slack.Payload{ + Username: logCfg.Username, Channel: logCfg.Channel, - UserName: logCfg.Username, - Attachments: atts, - IconURL: logCfg.IconURL, - }) + IconUrl: logCfg.IconURL, + Attachments: []slack.Attachment{att}, + } + + err := slack.Send(logCfg.WebhookURL, "", payload) + if len(err) > 0 { + fmt.Printf("error: %s\n", err) + } + } func setUpLogger() {