Add slack
This commit is contained in:
3
cli/Makefile
Normal file
3
cli/Makefile
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
build:
|
||||||
|
docker build --pull -t ${PREFIX}/$$(gcloud config get-value project 2> /dev/null)/status-monitor:$(TAG) .
|
||||||
|
|
||||||
3
cli/go.mod
Normal file
3
cli/go.mod
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
module main
|
||||||
|
|
||||||
|
go 1.13
|
||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
cachet "github.com/castawaylabs/cachet-monitor"
|
cachet "github.com/macchiang/cachet-monitor"
|
||||||
docopt "github.com/docopt/docopt-go"
|
docopt "github.com/docopt/docopt-go"
|
||||||
"github.com/mitchellh/mapstructure"
|
"github.com/mitchellh/mapstructure"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
type CachetMonitor struct {
|
type CachetMonitor struct {
|
||||||
SystemName string `json:"system_name" yaml:"system_name"`
|
SystemName string `json:"system_name" yaml:"system_name"`
|
||||||
DateFormat string `json:"date_format" yaml:"date_format"`
|
DateFormat string `json:"date_format" yaml:"date_format"`
|
||||||
|
SlackWebhook string `json:"slack_webhook" yaml:"slack_webhook"`
|
||||||
API CachetAPI `json:"api"`
|
API CachetAPI `json:"api"`
|
||||||
RawMonitors []map[string]interface{} `json:"monitors" yaml:"monitors"`
|
RawMonitors []map[string]interface{} `json:"monitors" yaml:"monitors"`
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
"insecure": false
|
"insecure": false
|
||||||
},
|
},
|
||||||
"date_format": "02/01/2006 15:04:05 MST",
|
"date_format": "02/01/2006 15:04:05 MST",
|
||||||
|
"slack_webhook": "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX",
|
||||||
"monitors": [
|
"monitors": [
|
||||||
{
|
{
|
||||||
"name": "google",
|
"name": "google",
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ api:
|
|||||||
insecure: false
|
insecure: false
|
||||||
# https://golang.org/src/time/format.go#L57
|
# https://golang.org/src/time/format.go#L57
|
||||||
date_format: 02/01/2006 15:04:05 MST
|
date_format: 02/01/2006 15:04:05 MST
|
||||||
|
slack_webhook: https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
|
||||||
monitors:
|
monitors:
|
||||||
# http monitor example
|
# http monitor example
|
||||||
- name: google
|
- name: google
|
||||||
|
|||||||
32
incident.go
32
incident.go
@@ -67,7 +67,10 @@ func (incident *Incident) Send(cfg *CachetMonitor) error {
|
|||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
return fmt.Errorf("Could not create/update incident!")
|
return fmt.Errorf("Could not create/update incident!")
|
||||||
}
|
}
|
||||||
|
// send slack message
|
||||||
|
if cfg.SlackWebhook !="" {
|
||||||
|
sendSlack(cfg)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,3 +113,30 @@ func (incident *Incident) SetWatching() {
|
|||||||
func (incident *Incident) SetFixed() {
|
func (incident *Incident) SetFixed() {
|
||||||
incident.Status = 4
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
80
slack.go
Normal file
80
slack.go
Normal file
@@ -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
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user