slack: webhookurl and sendSlack
- GetComponentStatus fix - get main url from api definition for slack post - fix run send slack
This commit is contained in:
@@ -89,3 +89,9 @@ func getTemplateData(monitor *AbstractMonitor) map[string]interface{} {
|
|||||||
"now": time.Now().Format(monitor.config.DateFormat),
|
"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]
|
||||||
|
}
|
||||||
13
incident.go
13
incident.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@@ -95,7 +96,7 @@ func (incident *Incident) Send(cfg *CachetMonitor) error {
|
|||||||
}
|
}
|
||||||
// send slack message
|
// send slack message
|
||||||
if cfg.SlackWebhook != "" {
|
if cfg.SlackWebhook != "" {
|
||||||
sendSlack(cfg)
|
incident.sendSlack(cfg)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -111,7 +112,7 @@ func (incident *Incident) GetComponentStatus(cfg *CachetMonitor) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var data struct {
|
var data struct {
|
||||||
Status int `json:"status,string"`
|
Status int `json:"status"`
|
||||||
}
|
}
|
||||||
if err := json.Unmarshal(body.Data, &data); err != nil {
|
if err := json.Unmarshal(body.Data, &data); err != nil {
|
||||||
return 0, fmt.Errorf("Cannot parse component body: %v. Err = %v", string(body.Data), err)
|
return 0, fmt.Errorf("Cannot parse component body: %v. Err = %v", string(body.Data), err)
|
||||||
@@ -144,17 +145,17 @@ func (incident *Incident) SetFixed() {
|
|||||||
func (incident *Incident) sendSlack(cfg *CachetMonitor) {
|
func (incident *Incident) sendSlack(cfg *CachetMonitor) {
|
||||||
color := "#bf1932" //red
|
color := "#bf1932" //red
|
||||||
if incident.ComponentStatus == 1 {
|
if incident.ComponentStatus == 1 {
|
||||||
|
|
||||||
color = "#36a64f" //green
|
color = "#36a64f" //green
|
||||||
}
|
}
|
||||||
|
titleLink := MainUrl(cfg) + "/dashboard/incidents/" + strconv.Itoa(incident.ID)
|
||||||
slack := Slack{
|
slack := Slack{
|
||||||
WebhookUrl: cfg.SlackWebhook
|
WebhookURL: cfg.SlackWebhook,
|
||||||
Attachments: []Attachments{
|
Attachments: []Attachments{
|
||||||
Attachments{
|
Attachments{
|
||||||
Fallback: incident.Name,
|
Fallback: incident.Name,
|
||||||
Color: color,
|
Color: color,
|
||||||
Title: incident.Name,
|
Title: incident.Name,
|
||||||
TitleLink: "https://status.easyship.com",
|
TitleLink: titleLink,
|
||||||
Text: incident.Message,
|
Text: incident.Message,
|
||||||
Footer: "Cachet Monitor",
|
Footer: "Cachet Monitor",
|
||||||
FooterIcon: "https://i.imgur.com/spck1w6.png",
|
FooterIcon: "https://i.imgur.com/spck1w6.png",
|
||||||
@@ -163,6 +164,6 @@ func (incident *Incident) sendSlack(cfg *CachetMonitor) {
|
|||||||
}}
|
}}
|
||||||
err := slack.SendSlackNotification()
|
err := slack.SendSlackNotification()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Errorf(err)
|
fmt.Errorf("Cannot send slack message. Err = %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
6
slack.go
6
slack.go
@@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Slack struct {
|
type Slack struct {
|
||||||
WebhookUrl string
|
WebhookURL string
|
||||||
Attachments []Attachments `json:"attachments"`
|
Attachments []Attachments `json:"attachments"`
|
||||||
}
|
}
|
||||||
type Fields struct {
|
type Fields struct {
|
||||||
@@ -46,7 +46,7 @@ func test() {
|
|||||||
Ts: time.Now().Unix(),
|
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()
|
err := slack.SendSlackNotification()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@@ -58,7 +58,7 @@ func test() {
|
|||||||
func (slack *Slack) SendSlackNotification() error {
|
func (slack *Slack) SendSlackNotification() error {
|
||||||
|
|
||||||
slackBody, _ := json.Marshal(slack)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user