mirror of https://github.com/statping/statping
Created Notifiers (markdown)
parent
adf5261d14
commit
99855bcc3c
|
@ -0,0 +1,164 @@
|
||||||
|
Statup includes multiple Notifiers to alert you when your services are offline. You can also create your own notifier and send a Push Request to this repo! Creating a custom notifier is pretty easy as long as you follow the requirements. A notifier will automatically be installed into the users Statup database.
|
||||||
|
|
||||||
|
## Notifier Requirements
|
||||||
|
- Must have a unique `ID` for your notifier
|
||||||
|
- Must have a unique `METHOD` name
|
||||||
|
- Struct must have `*Notification` pointer in it.
|
||||||
|
- Must create and add your notifier variable in `init()`
|
||||||
|
|
||||||
|
## Notification Interface
|
||||||
|
Statup has the `Notifier` interface which you'll need to include in your notifier. Below is the current interface.
|
||||||
|
```go
|
||||||
|
type Notifier interface {
|
||||||
|
Init() error // runs when notifier starts
|
||||||
|
Install() error // installs the notifier into the Statup database
|
||||||
|
Run() error // main notification go routine (loops and should run actions)
|
||||||
|
OnFailure() error // anytime a service failure happens, this is ran
|
||||||
|
OnSuccess() error // anytime a service has a successful response this is ran
|
||||||
|
Select() *Notification // required method to select the Notification from database
|
||||||
|
Test() error // a method to send a test for your notifier
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Slack Notifier
|
||||||
|
Below is a full example of the Slack notifier which will give you a good example of how to create your own.
|
||||||
|
```go
|
||||||
|
package notifiers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"github.com/hunterlong/statup/utils"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
SLACK_ID = 2
|
||||||
|
SLACK_METHOD = "slack"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
slacker *Slack
|
||||||
|
slackMessages []string
|
||||||
|
)
|
||||||
|
|
||||||
|
type Slack struct {
|
||||||
|
*Notification
|
||||||
|
}
|
||||||
|
|
||||||
|
// DEFINE YOUR NOTIFICATION HERE.
|
||||||
|
func init() {
|
||||||
|
slacker = &Slack{&Notification{
|
||||||
|
Id: SLACK_ID,
|
||||||
|
Method: SLACK_METHOD,
|
||||||
|
Host: "https://webhooksurl.slack.com/***",
|
||||||
|
Form: []NotificationForm{{
|
||||||
|
id: 2,
|
||||||
|
Type: "text",
|
||||||
|
Title: "Incoming Webhook Url",
|
||||||
|
Placeholder: "Insert your Slack webhook URL here.",
|
||||||
|
DbField: "Host",
|
||||||
|
}}},
|
||||||
|
}
|
||||||
|
add(slacker)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Select Obj
|
||||||
|
func (u *Slack) Select() *Notification {
|
||||||
|
return u.Notification
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *Slack) Test() error {
|
||||||
|
SendSlack("Slack notifications on your Statup server is working!")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AFTER NOTIFIER LOADS, IF ENABLED, START A QUEUE PROCESS
|
||||||
|
func (u *Slack) Run() error {
|
||||||
|
for _, msg := range slackMessages {
|
||||||
|
utils.Log(1, fmt.Sprintf("Sending JSON to Slack Webhook: %v", msg))
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
slackMessages = uniqueMessages(slackMessages, msg)
|
||||||
|
}
|
||||||
|
time.Sleep(60 * time.Second)
|
||||||
|
if u.Enabled {
|
||||||
|
u.Run()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// CUSTOM FUNCTION FO SENDING SLACK MESSAGES
|
||||||
|
func SendSlack(msg string) error {
|
||||||
|
//if slackUrl == "" {
|
||||||
|
// return errors.New("Slack Webhook URL has not been set in settings")
|
||||||
|
//}
|
||||||
|
fullMessage := fmt.Sprintf("{\"text\":\"%v\"}", msg)
|
||||||
|
slackMessages = append(slackMessages, fullMessage)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ON SERVICE FAILURE, DO YOUR OWN FUNCTIONS
|
||||||
|
func (u *Slack) OnFailure() error {
|
||||||
|
if u.Enabled {
|
||||||
|
utils.Log(1, fmt.Sprintf("Notification %v is receiving a failure notification.", u.Method))
|
||||||
|
// Do failing stuff here!
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ON SERVICE SUCCESS, DO YOUR OWN FUNCTIONS
|
||||||
|
func (u *Slack) OnSuccess() error {
|
||||||
|
if u.Enabled {
|
||||||
|
utils.Log(1, fmt.Sprintf("Notification %v is receiving a successful notification.", u.Method))
|
||||||
|
// Do checking or any successful things here
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ON SAVE OR UPDATE OF THE NOTIFIER FORM
|
||||||
|
func (u *Slack) OnSave() error {
|
||||||
|
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 {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
Loading…
Reference in New Issue