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

Merge branch 'fix/tests' into 'master'

Update how webhooks are sent

Closes #6

See merge request !2
parents f0d16e26 bbb28a38
No related branches found
No related tags found
1 merge request!2Update how webhooks are sent
Pipeline #86 passed with stages
in 1 minute and 17 seconds
# This file is a template, and might need editing before it works on your project. # This file is a template, and might need editing before it works on your project.
image: golang:latest 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 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 # 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 # 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. # Thus, making a symbolic link corrects this.
before_script: before_script:
- ln -s /builds /go/src/git.ethitter.com - mkdir -p $GOPATH/src/$(dirname $REPO_NAME)
- cd /go/src/git.ethitter.com/debian/eth-log-alerting - 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: stages:
- test - test
- build - build
format: format:
stage: test stage: test
script: script:
# Add here all the dependencies, or use glide/govendor to get - go tool vet -composites=false -shadow=true *.go
# them automatically. - go test -race $(go list ./... | grep -v /vendor/)
# - 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/)
compile: compile:
stage: build stage: build
script: script:
# Add here all the dependencies, or use glide/govendor/... - go build -race -ldflags "-extldflags '-static'" -o eth-log-alerting
# to get them automatically. artifacts:
- go get github.com/42wim/matterbridge/matterhook paths:
- go get github.com/asaskevich/govalidator - eth-log-alerting
- 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
Log Alerting Log Alerting [![pipeline status](https://git.ethitter.com/debian/eth-log-alerting/badges/master/pipeline.svg)](https://git.ethitter.com/debian/eth-log-alerting/commits/master)
============ ============
Pipe logs to Mattermost (or Slack) webhooks Pipe logs to Mattermost (or Slack) webhooks
...@@ -11,7 +11,7 @@ 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. `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. `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/asaskevich/govalidator`
1. `go get github.com/hpcloud/tail` 1. `go get github.com/hpcloud/tail`
1. `go build eth-log-alerting.go` 1. `go build eth-log-alerting.go`
......
...@@ -12,8 +12,8 @@ import ( ...@@ -12,8 +12,8 @@ import (
"regexp" "regexp"
"syscall" "syscall"
"github.com/42wim/matterbridge/matterhook"
"github.com/asaskevich/govalidator" "github.com/asaskevich/govalidator"
"github.com/ashwanthkumar/slack-go-webhook"
"github.com/hpcloud/tail" "github.com/hpcloud/tail"
) )
...@@ -33,15 +33,6 @@ type logConfig struct { ...@@ -33,15 +33,6 @@ type logConfig struct {
SearchRegex string `json:"search"` 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 ( var (
configPath string configPath string
logConfigs []logConfig logConfigs []logConfig
...@@ -122,44 +113,49 @@ func tailLog(logCfg logConfig) { ...@@ -122,44 +113,49 @@ func tailLog(logCfg logConfig) {
return return
} }
mh := matterhook.New(logCfg.WebhookURL, matterhook.Config{DisableServer: true}) parseLinesAndSend(t, logCfg)
parseLinesAndSend(t, mh, logCfg)
t.Stop() t.Stop()
t.Cleanup() t.Cleanup()
} }
func parseLinesAndSend(t *tail.Tail, mh *matterhook.Client, logCfg logConfig) { func parseLinesAndSend(t *tail.Tail, logCfg logConfig) {
for line := range t.Lines { for line := range t.Lines {
if line.Err != nil { if line.Err != nil {
continue continue
} }
if len(logCfg.SearchRegex) == 0 { if len(logCfg.SearchRegex) == 0 {
go sendLine(line, mh, logCfg) go sendLine(line, logCfg)
} else if matched, _ := regexp.MatchString(logCfg.SearchRegex, line.Text); matched { } 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) { func sendLine(line *tail.Line, logCfg logConfig) {
atts := attachments{ fallback := fmt.Sprintf("New entry in %s", logCfg.LogPath)
attachment{ pretext := fmt.Sprintf("In `%s` at `%s`:", logCfg.LogPath, line.Time)
Fallback: fmt.Sprintf("New entry in %s", logCfg.LogPath), text := fmt.Sprintf("```\n%s\n```", line.Text)
Pretext: fmt.Sprintf("In `%s` at `%s`:", logCfg.LogPath, line.Time), att := slack.Attachment{
Text: fmt.Sprintf(" %s", line.Text), Color: &logCfg.Color,
Color: logCfg.Color, Fallback: &fallback,
}, PreText: &pretext,
Text: &text,
} }
mh.Send(matterhook.OMessage{ payload := slack.Payload{
Username: logCfg.Username,
Channel: logCfg.Channel, Channel: logCfg.Channel,
UserName: logCfg.Username, IconUrl: logCfg.IconURL,
Attachments: atts, Attachments: []slack.Attachment{att},
IconURL: logCfg.IconURL, }
})
err := slack.Send(logCfg.WebhookURL, "", payload)
if len(err) > 0 {
fmt.Printf("error: %s\n", err)
}
} }
func setUpLogger() { func setUpLogger() {
......
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