statping/notifiers/slack.go

139 lines
4.4 KiB
Go
Raw Normal View History

2018-08-16 06:22:20 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-07-10 12:05:20 +00:00
package notifiers
import (
"bytes"
"fmt"
2018-09-12 04:14:22 +00:00
"github.com/hunterlong/statup/core/notifier"
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 (
slackMessages []string
2018-07-14 02:37:39 +00:00
messageLock *sync.Mutex
2018-07-10 12:05:20 +00:00
)
type Slack struct {
2018-09-12 04:14:22 +00:00
*notifier.Notification
}
2018-07-14 02:37:39 +00:00
type slackMessage struct {
Service *types.Service
Time int64
}
2018-09-12 04:14:22 +00:00
var slacker = &Slack{&notifier.Notification{
Method: SLACK_METHOD,
Host: "https://webhooksurl.slack.com/***",
Form: []notifier.NotificationForm{{
Type: "text",
Title: "Incoming Webhook Url",
Placeholder: "Insert your Slack webhook URL here.",
DbField: "Host",
}}},
}
2018-07-10 12:05:20 +00:00
// DEFINE YOUR NOTIFICATION HERE.
func init() {
2018-09-12 04:14:22 +00:00
err := notifier.AddNotifier(slacker)
2018-07-14 02:37:39 +00:00
messageLock = new(sync.Mutex)
if err != nil {
2018-09-12 04:14:22 +00:00
panic(err)
}
2018-07-10 12:05:20 +00:00
}
2018-07-12 06:53:18 +00:00
func (u *Slack) Test() error {
utils.Log(1, "Slack notifier loaded")
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()
2018-09-12 04:14:22 +00:00
slackMessages = notifier.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-09-12 04:14:22 +00:00
slacker.Log(buf.String())
2018-07-10 12:05:20 +00:00
return nil
}
// ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS
func (u *Slack) OnFailure(s *types.Service, f *types.Failure) {
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
}
// ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS
func (u *Slack) OnSuccess(s *types.Service) {
2018-07-10 12:05:20 +00:00
}
// 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
u.Test()
2018-07-10 12:05:20 +00:00
return nil
}