statping/notifiers/slack.go

164 lines
4.4 KiB
Go
Raw Normal View History

2018-07-10 12:05:20 +00:00
package notifiers
import (
"bytes"
"fmt"
2018-07-14 02:37:39 +00:00
"github.com/hunterlong/statup/types"
2018-07-10 12:05:20 +00:00
"github.com/hunterlong/statup/utils"
"net/http"
2018-07-14 02:37:39 +00:00
"sync"
2018-07-13 09:43:15 +00:00
"text/template"
2018-07-14 02:37:39 +00:00
"time"
2018-07-10 12:05:20 +00:00
)
const (
2018-07-14 02:37:39 +00:00
SLACK_ID = 2
SLACK_METHOD = "slack"
2018-07-16 07:02:33 +00:00
FAILING_TEMPLATE = `{ "attachments": [ { "fallback": "Service {{.Service.Name}} - is currently failing", "text": "<{{.Service.Domain}}|{{.Service.Name}}> - Your Statup service '{{.Service.Name}}' has just received a Failure notification with a HTTP Status code of {{.Service.LastStatusCode}}.", "fields": [ { "title": "Expected", "value": "{{.Service.Expected}}", "short": true }, { "title": "Status Code", "value": "{{.Service.LastStatusCode}}", "short": true } ], "color": "#FF0000", "thumb_url": "https://statup.io", "footer": "Statup", "footer_icon": "https://img.cjx.io/statuplogo32.png" } ] }`
SUCCESS_TEMPLATE = `{ "attachments": [ { "fallback": "Service {{.Service.Name}} - is now back online", "text": "<{{.Service.Domain}}|{{.Service.Name}}> - Your Statup service '{{.Service.Name}}' has just received a Failure notification.", "fields": [ { "title": "Issue", "value": "Awesome Project", "short": true }, { "title": "Status Code", "value": "{{.Service.LastStatusCode}}", "short": true } ], "color": "#00FF00", "thumb_url": "https://statup.io", "footer": "Statup", "footer_icon": "https://img.cjx.io/statuplogo32.png" } ] }`
2018-07-14 02:37:39 +00:00
TEST_TEMPLATE = `{"text":"{{.}}"}`
2018-07-10 12:05:20 +00:00
)
var (
slacker *Slack
2018-07-10 12:05:20 +00:00
slackMessages []string
2018-07-14 02:37:39 +00:00
messageLock *sync.Mutex
2018-07-10 12:05:20 +00:00
)
type Slack struct {
*Notification
}
2018-07-14 02:37:39 +00:00
type slackMessage struct {
Service *types.Service
Time int64
}
2018-07-10 12:05:20 +00:00
// DEFINE YOUR NOTIFICATION HERE.
func init() {
slacker = &Slack{&Notification{
2018-07-10 12:05:20 +00:00
Id: SLACK_ID,
Method: SLACK_METHOD,
Host: "https://webhooksurl.slack.com/***",
Form: []NotificationForm{{
2018-07-17 09:18:20 +00:00
Id: 2,
2018-07-10 12:05:20 +00:00
Type: "text",
Title: "Incoming Webhook Url",
Placeholder: "Insert your Slack webhook URL here.",
DbField: "Host",
}}},
2018-07-10 12:05:20 +00:00
}
add(slacker)
2018-07-14 02:37:39 +00:00
messageLock = new(sync.Mutex)
2018-07-10 12:05:20 +00:00
}
// Select Obj
func (u *Slack) Select() *Notification {
return u.Notification
}
2018-07-10 12:05:20 +00:00
// WHEN NOTIFIER LOADS
func (u *Slack) Init() error {
err := u.Install()
if err == nil {
notifier, _ := SelectNotification(u.Id)
forms := u.Form
u.Notification = notifier
u.Form = forms
if u.Enabled {
go u.Run()
}
}
2018-07-10 12:05:20 +00:00
return err
}
2018-07-12 06:53:18 +00:00
func (u *Slack) Test() error {
2018-07-14 02:37:39 +00:00
msg := fmt.Sprintf("You're Statup Slack Notifier is working correctly!")
SendSlack(TEST_TEMPLATE, msg)
2018-07-12 06:53:18 +00:00
return nil
}
2018-07-10 12:05:20 +00:00
// AFTER NOTIFIER LOADS, IF ENABLED, START A QUEUE PROCESS
func (u *Slack) Run() error {
2018-07-14 02:37:39 +00:00
messageLock.Lock()
slackMessages = uniqueStrings(slackMessages)
2018-07-10 12:05:20 +00:00
for _, msg := range slackMessages {
2018-07-17 09:18:20 +00:00
if u.CanSend() {
utils.Log(1, fmt.Sprintf("Sending JSON to Slack Webhook"))
client := http.Client{Timeout: 15 * time.Second}
_, err := client.Post(u.Host, "application/json", bytes.NewBuffer([]byte(msg)))
if err != nil {
utils.Log(3, fmt.Sprintf("Issue sending Slack notification: %v", err))
}
u.Log(msg)
2018-07-10 12:05:20 +00:00
}
}
2018-07-16 07:02:33 +00:00
slackMessages = []string{}
2018-07-14 02:37:39 +00:00
messageLock.Unlock()
2018-07-10 12:05:20 +00:00
time.Sleep(60 * time.Second)
if u.Enabled {
u.Run()
}
return nil
}
// CUSTOM FUNCTION FO SENDING SLACK MESSAGES
2018-07-14 02:37:39 +00:00
func SendSlack(temp string, data interface{}) error {
messageLock.Lock()
2018-07-13 09:43:15 +00:00
buf := new(bytes.Buffer)
slackTemp, _ := template.New("slack").Parse(temp)
slackTemp.Execute(buf, data)
slackMessages = append(slackMessages, buf.String())
2018-07-14 02:37:39 +00:00
messageLock.Unlock()
2018-07-10 12:05:20 +00:00
return nil
}
// ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS
2018-07-14 02:37:39 +00:00
func (u *Slack) OnFailure(s *types.Service) error {
if u.Enabled {
2018-07-14 02:37:39 +00:00
message := slackMessage{
Service: s,
Time: time.Now().Unix(),
}
SendSlack(FAILING_TEMPLATE, message)
}
2018-07-10 12:05:20 +00:00
return nil
}
// ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS
2018-07-14 02:37:39 +00:00
func (u *Slack) OnSuccess(s *types.Service) error {
if u.Enabled {
2018-07-16 07:02:33 +00:00
//message := slackMessage{
// Service: s,
// Time: time.Now().Unix(),
//}
//SendSlack(SUCCESS_TEMPLATE, message)
}
2018-07-10 12:05:20 +00:00
return nil
}
// ON SAVE OR UPDATE OF THE NOTIFIER FORM
func (u *Slack) OnSave() error {
2018-07-10 12:05:20 +00:00
utils.Log(1, fmt.Sprintf("Notification %v is receiving updated information.", u.Method))
// Do updating stuff here
return nil
}
// ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS
func (u *Slack) Install() error {
2018-07-17 09:18:20 +00:00
inDb, err := slacker.Notification.IsInDatabase()
if !inDb {
newNotifer, err := InsertDatabase(u.Notification)
if err != nil {
utils.Log(3, err)
return err
}
utils.Log(1, fmt.Sprintf("new notifier #%v installed: %v", newNotifer, u.Method))
}
return err
}