From 43510e18e2221457fd1e33ea1ac44b7a0e701bae Mon Sep 17 00:00:00 2001 From: Mac Chiang Date: Wed, 18 Dec 2019 11:54:43 +0800 Subject: [PATCH 1/9] Add slack --- cli/Makefile | 3 ++ cli/go.mod | 3 ++ cli/main.go | 4 +-- config.go | 1 + example.config.json | 3 +- example.config.yml | 7 ++-- incident.go | 32 +++++++++++++++++- slack.go | 80 +++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 126 insertions(+), 7 deletions(-) create mode 100644 cli/Makefile create mode 100644 cli/go.mod create mode 100644 slack.go diff --git a/cli/Makefile b/cli/Makefile new file mode 100644 index 0000000..caff838 --- /dev/null +++ b/cli/Makefile @@ -0,0 +1,3 @@ +build: + docker build --pull -t ${PREFIX}/$$(gcloud config get-value project 2> /dev/null)/status-monitor:$(TAG) . + diff --git a/cli/go.mod b/cli/go.mod new file mode 100644 index 0000000..98d9f66 --- /dev/null +++ b/cli/go.mod @@ -0,0 +1,3 @@ +module main + +go 1.13 diff --git a/cli/main.go b/cli/main.go index 2ac47ce..11bfc34 100644 --- a/cli/main.go +++ b/cli/main.go @@ -12,7 +12,7 @@ import ( "sync" "github.com/Sirupsen/logrus" - cachet "github.com/castawaylabs/cachet-monitor" + cachet "github.com/macchiang/cachet-monitor" docopt "github.com/docopt/docopt-go" "github.com/mitchellh/mapstructure" "gopkg.in/yaml.v2" @@ -38,7 +38,7 @@ Options: -h --help Show this screen. --version Show version --immediate Tick immediately (by default waits for first defined interval) - + Environment varaibles: CACHET_API override API url from configuration CACHET_TOKEN override API token from configuration diff --git a/config.go b/config.go index af2a05c..c573a10 100644 --- a/config.go +++ b/config.go @@ -12,6 +12,7 @@ import ( type CachetMonitor struct { SystemName string `json:"system_name" yaml:"system_name"` DateFormat string `json:"date_format" yaml:"date_format"` + SlackWebhook string `json:"slack_webhook" yaml:"slack_webhook"` API CachetAPI `json:"api"` RawMonitors []map[string]interface{} `json:"monitors" yaml:"monitors"` diff --git a/example.config.json b/example.config.json index f74c46e..6ec4bbf 100644 --- a/example.config.json +++ b/example.config.json @@ -5,6 +5,7 @@ "insecure": false }, "date_format": "02/01/2006 15:04:05 MST", + "slack_webhook": "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX", "monitors": [ { "name": "google", @@ -56,4 +57,4 @@ ] } ] -} \ No newline at end of file +} diff --git a/example.config.yml b/example.config.yml index 8cd05f9..e299631 100644 --- a/example.config.yml +++ b/example.config.yml @@ -6,6 +6,7 @@ api: insecure: false # https://golang.org/src/time/format.go#L57 date_format: 02/01/2006 15:04:05 MST +slack_webhook: https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX monitors: # http monitor example - name: google @@ -15,7 +16,7 @@ monitors: strict: true # HTTP method method: POST - + # set to update component (either component_id or metric_id are required) component_id: 1 # set to post lag to cachet metric (graph) @@ -28,7 +29,7 @@ monitors: message: "{{ .Monitor.Name }} check **failed** (server time: {{ .now }})\n\n{{ .FailReason }}" fixed: subject: "I HAVE BEEN FIXED" - + # seconds between checks interval: 1 # seconds for timeout @@ -62,4 +63,4 @@ monitors: - regex: [1-9] alt[1-9].aspmx.l.google.com. - exact: 10 aspmx2.googlemail.com. - exact: 1 aspmx.l.google.com. - - exact: 10 aspmx3.googlemail.com. \ No newline at end of file + - exact: 10 aspmx3.googlemail.com. diff --git a/incident.go b/incident.go index 597e84f..41e6ce2 100644 --- a/incident.go +++ b/incident.go @@ -67,7 +67,10 @@ func (incident *Incident) Send(cfg *CachetMonitor) error { if resp.StatusCode != 200 { return fmt.Errorf("Could not create/update incident!") } - + // send slack message + if cfg.SlackWebhook !="" { + sendSlack(cfg) + } return nil } @@ -110,3 +113,30 @@ func (incident *Incident) SetWatching() { func (incident *Incident) SetFixed() { incident.Status = 4 } + +// Send slack message +func (incident *Incident) sendSlack(cfg *CachetMonitor) { + color:="#bf1932" //red + if incident.ComponentStatus == 1 { + + color="#36a64f" //green + } + slack := Slack{ + WebhookUrl: cfg.SlackWebhook + Attachments: []Attachments{ + Attachments{ + Fallback: incident.Name, + Color: color, + Title: incident.Name, + TitleLink: "https://status.easyship.com", + Text: incident.Message, + Footer: "Cachet Monitor", + FooterIcon: "https://i.imgur.com/spck1w6.png", + Ts: time.Now().Unix(), + }, + }} + err := slack.SendSlackNotification() + if err != nil { + fmt.Errorf(err) + } +} diff --git a/slack.go b/slack.go new file mode 100644 index 0000000..2faef21 --- /dev/null +++ b/slack.go @@ -0,0 +1,80 @@ +package cachet + +import ( + "bytes" + "encoding/json" + "errors" + "log" + "net/http" + "time" +) + +type Slack struct { + WebhookUrl string + Attachments []Attachments `json:"attachments"` +} +type Fields struct { + Title string `json:"title"` + Value string `json:"value"` + Short bool `json:"short"` +} +type Attachments struct { + Fallback string `json:"fallback"` + Color string `json:"color"` + Pretext string `json:"pretext"` + Title string `json:"title"` + TitleLink string `json:"title_link"` + Text string `json:"text"` + Fields []Fields `json:"fields"` + ThumbURL string `json:"thumb_url"` + Footer string `json:"footer"` + FooterIcon string `json:"footer_icon"` + Ts int64 `json:"ts"` +} + +func test() { + slack := Slack{ + Attachments: []Attachments{ + Attachments{ + Fallback: "Required plain-text summary of the attachment.", + Color: "#36a64f", + Title: "Slack API Documentation", + TitleLink: "https://status.easyship.com", + Text: "Optional text that appears within the attachment", + Footer: "Cachet Monitor", + FooterIcon: "https://i.imgur.com/spck1w6.png", + Ts: time.Now().Unix(), + }, + }} + slack.WebhookUrl = "https://hooks.slack.com/services/0000000/00000000/xxxxxxxxxxxxxxxxxxx" + err := slack.SendSlackNotification() + if err != nil { + log.Fatal(err) + } +} + +// SendSlackNotification will post to an 'Incoming Webook' url setup in Slack Apps. It accepts +// some text and the slack channel is saved within Slack. +func (slack *Slack) SendSlackNotification() error { + + slackBody, _ := json.Marshal(slack) + req, err := http.NewRequest(http.MethodPost, slack.WebhookUrl, bytes.NewBuffer(slackBody)) + if err != nil { + return err + } + + req.Header.Add("Content-Type", "application/json") + + client := &http.Client{Timeout: 10 * time.Second} + resp, err := client.Do(req) + if err != nil { + return err + } + + buf := new(bytes.Buffer) + buf.ReadFrom(resp.Body) + if buf.String() != "ok" { + return errors.New("Non-ok response returned from Slack") + } + return nil +} From f9f5c278ec1b05178eea10c1bde03da6ba3932bd Mon Sep 17 00:00:00 2001 From: Mac Chiang Date: Wed, 18 Dec 2019 12:35:44 +0800 Subject: [PATCH 2/9] add test --- cli/Dockerfile | 12 ++++++++++++ cli/Makefile | 2 +- cli/go.mod | 9 +++++++++ cli/go.sum | 20 ++++++++++++++++++++ cli/main.go | 2 +- monitor.go | 2 +- 6 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 cli/Dockerfile create mode 100644 cli/go.sum diff --git a/cli/Dockerfile b/cli/Dockerfile new file mode 100644 index 0000000..7fa15ca --- /dev/null +++ b/cli/Dockerfile @@ -0,0 +1,12 @@ +# build stage +FROM golang:alpine AS build-env +RUN set -eux; \ + apk add --no-cache --virtual .build-deps \ + git gcc libc-dev; +ENV GO111MODULE on +WORKDIR /go/main +ADD go.mod go.sum ./ +RUN go mod download +# WORKDIR /go/src/status-monitor +ADD main.go ./ +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -a -installsuffix cgo -o main main.go diff --git a/cli/Makefile b/cli/Makefile index caff838..8d9ae53 100644 --- a/cli/Makefile +++ b/cli/Makefile @@ -1,3 +1,3 @@ build: - docker build --pull -t ${PREFIX}/$$(gcloud config get-value project 2> /dev/null)/status-monitor:$(TAG) . + docker build . diff --git a/cli/go.mod b/cli/go.mod index 98d9f66..759b6a6 100644 --- a/cli/go.mod +++ b/cli/go.mod @@ -1,3 +1,12 @@ module main go 1.13 + +require ( + github.com/castawaylabs/cachet-monitor v1.1.0 // indirect + github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 // indirect + github.com/macchiang/cachet-monitor v1.1.0 // indirect + github.com/mitchellh/mapstructure v1.1.2 // indirect + github.com/sirupsen/logrus v1.4.2 // indirect + gopkg.in/yaml.v2 v2.2.7 // indirect +) diff --git a/cli/go.sum b/cli/go.sum new file mode 100644 index 0000000..32482f1 --- /dev/null +++ b/cli/go.sum @@ -0,0 +1,20 @@ +github.com/castawaylabs/cachet-monitor v1.1.0 h1:T8NshhwHjEIbdlWkeRBuYdnU2ktpBnZpwgh6nxrp62w= +github.com/castawaylabs/cachet-monitor v1.1.0/go.mod h1:FBK1+4+fLd80wd/+U4Zy8YshPlpgWEvup79AoapOZ2E= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ= +github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/macchiang/cachet-monitor v1.1.0 h1:um5ymKBF11Dau8yKCAg82HS6F/og0tODiIOLOdwnnzc= +github.com/macchiang/cachet-monitor v1.1.0/go.mod h1:cNiyqtsa4pEzPn0v2tlf6oejVg4mfUts2qNaxFnCDeE= +github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo= +gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/cli/main.go b/cli/main.go index 11bfc34..cac33dd 100644 --- a/cli/main.go +++ b/cli/main.go @@ -11,7 +11,7 @@ import ( "strings" "sync" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" cachet "github.com/macchiang/cachet-monitor" docopt "github.com/docopt/docopt-go" "github.com/mitchellh/mapstructure" diff --git a/monitor.go b/monitor.go index f7cbba6..d90e090 100644 --- a/monitor.go +++ b/monitor.go @@ -4,7 +4,7 @@ import ( "sync" "time" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) const DefaultInterval = time.Second * 60 From 3c343fb0a2c4cb49038ddb72c12e02f7dff1187d Mon Sep 17 00:00:00 2001 From: Alexander Trost Date: Wed, 8 Jan 2020 17:45:12 +0100 Subject: [PATCH 3/9] incident: fixed json struct tag Signed-off-by: Alexander Trost --- incident.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/incident.go b/incident.go index 597e84f..dd7a6b1 100644 --- a/incident.go +++ b/incident.go @@ -14,7 +14,7 @@ type Incident struct { Name string `json:"name"` Message string `json:"message"` Status int `json:"status"` - Visible int `json"visible"` + Visible int `json:"visible"` Notify bool `json:"notify"` ComponentID int `json:"component_id"` @@ -65,7 +65,7 @@ func (incident *Incident) Send(cfg *CachetMonitor) error { incident.ID = data.ID if resp.StatusCode != 200 { - return fmt.Errorf("Could not create/update incident!") + return fmt.Errorf("Could not create/update incident") } return nil From 33bb722e06fe22b9853377a3fc62e6aec542807b Mon Sep 17 00:00:00 2001 From: "A. Feldmann" Date: Wed, 14 Mar 2018 09:48:49 +0100 Subject: [PATCH 4/9] go fmt --- cli/main.go | 5 +++++ config.go | 1 + incident.go | 26 ++++++++++++++++++++++++++ monitor.go | 10 ++++++++++ readme.md | 1 + 5 files changed, 43 insertions(+) diff --git a/cli/main.go b/cli/main.go index 2ac47ce..2fb525b 100644 --- a/cli/main.go +++ b/cli/main.go @@ -38,6 +38,7 @@ Options: -h --help Show this screen. --version Show version --immediate Tick immediately (by default waits for first defined interval) + --restarted Get open incidents before start monitoring (if monitor died or restarted) Environment varaibles: CACHET_API override API url from configuration @@ -58,6 +59,10 @@ func main() { cfg.Immediate = immediate.(bool) } + if restarted, ok := arguments["--restarted"]; ok { + cfg.Restarted = restarted.(bool) + } + if name := arguments["--name"]; name != nil { cfg.SystemName = name.(string) } diff --git a/config.go b/config.go index af2a05c..31e7012 100644 --- a/config.go +++ b/config.go @@ -17,6 +17,7 @@ type CachetMonitor struct { Monitors []MonitorInterface `json:"-" yaml:"-"` Immediate bool `json:"-" yaml:"-"` + Restarted bool `json:"-" yaml:"-"` } // Validate configuration diff --git a/incident.go b/incident.go index dd7a6b1..e2bf655 100644 --- a/incident.go +++ b/incident.go @@ -21,6 +21,32 @@ type Incident struct { ComponentStatus int `json:"component_status"` } +//Get the last still open incident +func (mon *AbstractMonitor) Get(cfg *CachetMonitor) (*Incident, error) { + requestType := "GET" + requestURL := fmt.Sprintf("/incidents?component_id=%d", mon.ComponentID) + _, body, err := cfg.API.NewRequest(requestType, requestURL, nil) + if err != nil { + return nil, err + } + data := make([]Incident, 0) + if err := json.Unmarshal(body.Data, &data); err != nil { + return nil, fmt.Errorf("Cannot parse incident body: %v, %v", err, string(body.Data)) + } + //filter out resolved incidents + openIncidents := make([]Incident, 0) + for _, i := range data { + if i.Status < 4 { + openIncidents = append(openIncidents, i) + } + } + if len(openIncidents) == 0 { + return nil, nil + } + return &openIncidents[0], nil + +} + // Send - Create or Update incident func (incident *Incident) Send(cfg *CachetMonitor) error { switch incident.Status { diff --git a/monitor.go b/monitor.go index f7cbba6..9e09657 100644 --- a/monitor.go +++ b/monitor.go @@ -118,6 +118,16 @@ func (mon *AbstractMonitor) ClockStart(cfg *CachetMonitor, iface MonitorInterfac mon.tick(iface) } + if cfg.Restarted { + initialIncident, err := mon.Get(cfg) + if err != nil { + logrus.Warn("could not fetch initial incident: %v", err) + } + if initialIncident != nil { + mon.incident = initialIncident + } + } + ticker := time.NewTicker(mon.Interval * time.Second) for { select { diff --git a/readme.md b/readme.md index e40e248..7041068 100644 --- a/readme.md +++ b/readme.md @@ -111,6 +111,7 @@ Options: -h --help Show this screen. --version Show version --immediate Tick immediately (by default waits for first defined interval) + --restarted Get open incidents before start monitoring (if monitor died or restarted) Environment varaibles: CACHET_API override API url from configuration From 3f82108f9843e045cc09ab5068a8b33627d26e4c Mon Sep 17 00:00:00 2001 From: Alexander Trost Date: Wed, 8 Jan 2020 17:49:31 +0100 Subject: [PATCH 5/9] added some built binary names to the gitignore Signed-off-by: Alexander Trost --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d969c09..c4d4626 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ /config.yml /config.json -examples/ \ No newline at end of file +examples/ +/cachet-monitor +/cli From c186e4b2920dbf3bd6dd3c124109604f662d9b6f Mon Sep 17 00:00:00 2001 From: Alexander Trost Date: Wed, 8 Jan 2020 23:53:30 +0100 Subject: [PATCH 6/9] fixed sirupsen logrus pkg switched to go mods Signed-off-by: Alexander Trost --- api.go | 2 +- cli/main.go | 2 +- config.go | 2 +- dns.go | 2 +- go.mod | 14 ++++++++++++++ go.sum | 45 +++++++++++++++++++++++++++++++++++++++++++++ incident.go | 2 +- monitor.go | 2 +- 8 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 go.mod create mode 100644 go.sum diff --git a/api.go b/api.go index 397c0e9..465e277 100644 --- a/api.go +++ b/api.go @@ -9,7 +9,7 @@ import ( "strconv" "time" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) type CachetAPI struct { diff --git a/cli/main.go b/cli/main.go index 2fb525b..5a29b72 100644 --- a/cli/main.go +++ b/cli/main.go @@ -11,7 +11,7 @@ import ( "strings" "sync" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" cachet "github.com/castawaylabs/cachet-monitor" docopt "github.com/docopt/docopt-go" "github.com/mitchellh/mapstructure" diff --git a/config.go b/config.go index 31e7012..f4ab481 100644 --- a/config.go +++ b/config.go @@ -6,7 +6,7 @@ import ( "strings" "time" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) type CachetMonitor struct { diff --git a/dns.go b/dns.go index 6fe9372..8d9d51f 100644 --- a/dns.go +++ b/dns.go @@ -5,7 +5,7 @@ import ( "regexp" "strings" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" "github.com/miekg/dns" ) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e6d5f5f --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module github.com/castawaylabs/cachet-monitor + +go 1.13 + +require ( + github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 + github.com/miekg/dns v1.1.27 + github.com/mitchellh/mapstructure v1.1.2 + github.com/sirupsen/logrus v1.4.2 + golang.org/x/crypto v0.0.0-20200108215511-5d647ca15757 // indirect + golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect + golang.org/x/sys v0.0.0-20200107162124-548cf772de50 // indirect + gopkg.in/yaml.v2 v2.2.7 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a246d44 --- /dev/null +++ b/go.sum @@ -0,0 +1,45 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ= +github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= +github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/miekg/dns v1.1.27 h1:aEH/kqUzUxGJ/UHcEKdJY+ugH6WEzsEBBSPa8zuy1aM= +github.com/miekg/dns v1.1.27/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= +github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= +github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.0.0-20200108215511-5d647ca15757 h1:pJ9H8lzdBh301qPX4VpwJ8TRpLt1IhNK1PxVOICyP54= +golang.org/x/crypto v0.0.0-20200108215511-5d647ca15757/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7MvYXnkV9/iQNo1lX6g= +golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8= +golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe h1:6fAMxZRR6sl1Uq8U61gxU+kPTs2tR8uOySCbBP7BN/M= +golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200107162124-548cf772de50 h1:YvQ10rzcqWXLlJZ3XCUoO25savxmscf4+SC+ZqiCHhA= +golang.org/x/sys v0.0.0-20200107162124-548cf772de50/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo= +gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/incident.go b/incident.go index e2bf655..45336d3 100644 --- a/incident.go +++ b/incident.go @@ -5,7 +5,7 @@ import ( "fmt" "strconv" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) // Incident Cachet data model diff --git a/monitor.go b/monitor.go index 9e09657..a0dd45d 100644 --- a/monitor.go +++ b/monitor.go @@ -4,7 +4,7 @@ import ( "sync" "time" - "github.com/Sirupsen/logrus" + "github.com/sirupsen/logrus" ) const DefaultInterval = time.Second * 60 From 79676357f382d1e1748f587e90b42341bb839b72 Mon Sep 17 00:00:00 2001 From: Christoph Eck Date: Mon, 27 Jan 2020 18:36:05 +0100 Subject: [PATCH 7/9] slack: webhookurl and sendSlack - GetComponentStatus fix - get main url from api definition for slack post - fix run send slack --- config.go | 6 ++++++ incident.go | 19 ++++++++++--------- slack.go | 6 +++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/config.go b/config.go index 7e8e731..8336566 100644 --- a/config.go +++ b/config.go @@ -89,3 +89,9 @@ func getTemplateData(monitor *AbstractMonitor) map[string]interface{} { "now": time.Now().Format(monitor.config.DateFormat), } } + +func MainUrl(cfg *CachetMonitor) string { + var url = cfg.API.URL + var index = strings.Index(url,"/api/") + return url[0:index] +} \ No newline at end of file diff --git a/incident.go b/incident.go index 21e6eeb..bbadae4 100644 --- a/incident.go +++ b/incident.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "strconv" + "time" "github.com/sirupsen/logrus" ) @@ -94,8 +95,8 @@ func (incident *Incident) Send(cfg *CachetMonitor) error { return fmt.Errorf("Could not create/update incident") } // send slack message - if cfg.SlackWebhook !="" { - sendSlack(cfg) + if cfg.SlackWebhook != "" { + incident.sendSlack(cfg) } return nil } @@ -111,7 +112,7 @@ func (incident *Incident) GetComponentStatus(cfg *CachetMonitor) (int, error) { } var data struct { - Status int `json:"status,string"` + Status int `json:"status"` } if err := json.Unmarshal(body.Data, &data); err != nil { return 0, fmt.Errorf("Cannot parse component body: %v. Err = %v", string(body.Data), err) @@ -142,19 +143,19 @@ func (incident *Incident) SetFixed() { // Send slack message func (incident *Incident) sendSlack(cfg *CachetMonitor) { - color:="#bf1932" //red + color := "#bf1932" //red if incident.ComponentStatus == 1 { - - color="#36a64f" //green + color = "#36a64f" //green } + titleLink := MainUrl(cfg) + "/dashboard/incidents/" + strconv.Itoa(incident.ID) slack := Slack{ - WebhookUrl: cfg.SlackWebhook + WebhookURL: cfg.SlackWebhook, Attachments: []Attachments{ Attachments{ Fallback: incident.Name, Color: color, Title: incident.Name, - TitleLink: "https://status.easyship.com", + TitleLink: titleLink, Text: incident.Message, Footer: "Cachet Monitor", FooterIcon: "https://i.imgur.com/spck1w6.png", @@ -163,6 +164,6 @@ func (incident *Incident) sendSlack(cfg *CachetMonitor) { }} err := slack.SendSlackNotification() if err != nil { - fmt.Errorf(err) + fmt.Errorf("Cannot send slack message. Err = %v", err) } } diff --git a/slack.go b/slack.go index 2faef21..99bd589 100644 --- a/slack.go +++ b/slack.go @@ -10,7 +10,7 @@ import ( ) type Slack struct { - WebhookUrl string + WebhookURL string Attachments []Attachments `json:"attachments"` } type Fields struct { @@ -46,7 +46,7 @@ func test() { Ts: time.Now().Unix(), }, }} - slack.WebhookUrl = "https://hooks.slack.com/services/0000000/00000000/xxxxxxxxxxxxxxxxxxx" + slack.WebhookURL = "https://hooks.slack.com/services/0000000/00000000/xxxxxxxxxxxxxxxxxxx" err := slack.SendSlackNotification() if err != nil { log.Fatal(err) @@ -58,7 +58,7 @@ func test() { func (slack *Slack) SendSlackNotification() error { slackBody, _ := json.Marshal(slack) - req, err := http.NewRequest(http.MethodPost, slack.WebhookUrl, bytes.NewBuffer(slackBody)) + req, err := http.NewRequest(http.MethodPost, slack.WebhookURL, bytes.NewBuffer(slackBody)) if err != nil { return err } From de4d2451d5dd1f1201914de4448ad4b663ac5f2a Mon Sep 17 00:00:00 2001 From: Christoph Eck Date: Mon, 27 Jan 2020 18:36:54 +0100 Subject: [PATCH 8/9] chore(missing): add start argument restarted --- cli/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/main.go b/cli/main.go index 164e748..ce121ed 100644 --- a/cli/main.go +++ b/cli/main.go @@ -21,7 +21,7 @@ import ( const usage = `cachet-monitor Usage: - cachet-monitor (-c PATH | --config PATH) [--log=LOGPATH] [--name=NAME] [--immediate] + cachet-monitor (-c PATH | --config PATH) [--log=LOGPATH] [--name=NAME] [--immediate] [--restarted] cachet-monitor -h | --help | --version Arguments: From 29b2bf457afc39d1ff1e538ebf81c46aa795d627 Mon Sep 17 00:00:00 2001 From: Christoph Eck Date: Mon, 27 Jan 2020 18:37:17 +0100 Subject: [PATCH 9/9] fix: update libraries --- cli/go.mod | 10 +++++----- cli/go.sum | 4 ++-- cli/main.go | 4 ++-- go.mod | 9 +++++---- go.sum | 8 ++++++++ 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/cli/go.mod b/cli/go.mod index 759b6a6..28cd04f 100644 --- a/cli/go.mod +++ b/cli/go.mod @@ -4,9 +4,9 @@ go 1.13 require ( github.com/castawaylabs/cachet-monitor v1.1.0 // indirect - github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 // indirect - github.com/macchiang/cachet-monitor v1.1.0 // indirect - github.com/mitchellh/mapstructure v1.1.2 // indirect - github.com/sirupsen/logrus v1.4.2 // indirect - gopkg.in/yaml.v2 v2.2.7 // indirect + github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 + github.com/milkinteractive/cachet-monitor v1.1.0 + github.com/mitchellh/mapstructure v1.1.2 + github.com/sirupsen/logrus v1.4.2 + gopkg.in/yaml.v2 v2.2.7 ) diff --git a/cli/go.sum b/cli/go.sum index 32482f1..1c3e0b9 100644 --- a/cli/go.sum +++ b/cli/go.sum @@ -4,8 +4,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ= github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/macchiang/cachet-monitor v1.1.0 h1:um5ymKBF11Dau8yKCAg82HS6F/og0tODiIOLOdwnnzc= -github.com/macchiang/cachet-monitor v1.1.0/go.mod h1:cNiyqtsa4pEzPn0v2tlf6oejVg4mfUts2qNaxFnCDeE= +github.com/milkinteractive/cachet-monitor v1.1.0 h1:IyX/gYVDfSEwO4kbNMs9W4hLt5FXIV/ig/4c1nFVlfU= +github.com/milkinteractive/cachet-monitor v1.1.0/go.mod h1:Y+g0f0C8fAj2ub5RhfQgh/oU85pYx6QbL/56WMFkmVo= github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/cli/main.go b/cli/main.go index ce121ed..5f9ada0 100644 --- a/cli/main.go +++ b/cli/main.go @@ -11,10 +11,10 @@ import ( "strings" "sync" - "github.com/sirupsen/logrus" - cachet "github.com/macchiang/cachet-monitor" docopt "github.com/docopt/docopt-go" + cachet "github.com/milkinteractive/cachet-monitor" "github.com/mitchellh/mapstructure" + "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" ) diff --git a/go.mod b/go.mod index e6d5f5f..5e98dc1 100644 --- a/go.mod +++ b/go.mod @@ -1,14 +1,15 @@ -module github.com/castawaylabs/cachet-monitor +module github.com/milkinteractive/cachet-monitor go 1.13 require ( github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 + github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect github.com/miekg/dns v1.1.27 github.com/mitchellh/mapstructure v1.1.2 github.com/sirupsen/logrus v1.4.2 - golang.org/x/crypto v0.0.0-20200108215511-5d647ca15757 // indirect - golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect - golang.org/x/sys v0.0.0-20200107162124-548cf772de50 // indirect + golang.org/x/crypto v0.0.0-20200117160349-530e935923ad // indirect + golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect + golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 // indirect gopkg.in/yaml.v2 v2.2.7 ) diff --git a/go.sum b/go.sum index a246d44..75b91a4 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s= +github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/miekg/dns v1.1.27 h1:aEH/kqUzUxGJ/UHcEKdJY+ugH6WEzsEBBSPa8zuy1aM= github.com/miekg/dns v1.1.27/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE= @@ -20,6 +22,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200108215511-5d647ca15757 h1:pJ9H8lzdBh301qPX4VpwJ8TRpLt1IhNK1PxVOICyP54= golang.org/x/crypto v0.0.0-20200108215511-5d647ca15757/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200117160349-530e935923ad h1:Jh8cai0fqIK+f6nG0UgPW5wFk8wmiMhM3AyciDBdtQg= +golang.org/x/crypto v0.0.0-20200117160349-530e935923ad/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -27,6 +31,8 @@ golang.org/x/net v0.0.0-20190923162816-aa69164e4478 h1:l5EDrHhldLYb3ZRHDUhXF7Om7 golang.org/x/net v0.0.0-20190923162816-aa69164e4478/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 h1:efeOvDhwQ29Dj3SdAV/MJf8oukgn+8D8WgaCaRMchF8= golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa h1:F+8P+gmewFQYRk6JoLQLwjBCTu3mcIURZfNkVweuRKA= +golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -36,6 +42,8 @@ golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe h1:6fAMxZRR6sl1Uq8U61gxU+kPT golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200107162124-548cf772de50 h1:YvQ10rzcqWXLlJZ3XCUoO25savxmscf4+SC+ZqiCHhA= golang.org/x/sys v0.0.0-20200107162124-548cf772de50/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 h1:1/DFK4b7JH8DmkqhUk48onnSfrPzImPoVxuomtbT2nk= +golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/tools v0.0.0-20191216052735-49a3e744a425/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=