1
0
Fork 0
mirror of https://github.com/Luzifer/duplicity-backup.git synced 2024-11-08 15:10:06 +00:00
duplicity-backup/notification.go

141 lines
3 KiB
Go
Raw Normal View History

2016-05-23 08:41:20 +00:00
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/Luzifer/go_helpers/str"
2016-05-23 08:41:20 +00:00
)
func (c *configFile) Notify(command string, success bool, err error) error {
if !str.StringInSlice(command, notifyCommands) {
return nil
}
2016-05-23 08:41:20 +00:00
errs := []error{}
for _, n := range []func(bool, error) error{
c.notifyMonDash,
c.notifySlack,
} {
if e := n(success, err); e != nil {
errs = append(errs, e)
}
}
if len(errs) == 0 {
return nil
}
2016-05-23 08:41:20 +00:00
estr := ""
for _, e := range errs {
if e == nil {
continue
2016-05-23 08:41:20 +00:00
}
estr = fmt.Sprintf("%s\n- %s", estr, e)
2016-05-23 08:41:20 +00:00
}
return fmt.Errorf("%d notifiers failed:%s", len(errs), estr)
2016-05-23 08:41:20 +00:00
}
type mondashResult struct {
Title string `json:"title"`
Description string `json:"description"`
Status string `json:"status"`
Freshness int64 `json:"freshness"`
IgnoreMAD bool `json:"ignore_mad"`
HideMAD bool `json:"hide_mad"`
HideValue bool `json:"hide_value"`
2016-05-23 08:41:20 +00:00
}
func (c *configFile) notifyMonDash(success bool, err error) error {
if c.Notifications.MonDash.BoardURL == "" {
return nil
}
monitoringResult := mondashResult{
Title: fmt.Sprintf("duplicity-backup on %s", c.Hostname),
Freshness: c.Notifications.MonDash.Freshness,
IgnoreMAD: true,
HideMAD: true,
HideValue: true,
2016-05-23 08:41:20 +00:00
}
if success {
monitoringResult.Status = "OK"
monitoringResult.Description = "Backup succeeded"
} else {
monitoringResult.Status = "Critical"
monitoringResult.Description = fmt.Sprintf("Backup failed: %s", err)
}
buf := bytes.NewBuffer([]byte{})
if err = json.NewEncoder(buf).Encode(monitoringResult); err != nil {
2016-05-23 08:41:20 +00:00
return err
}
url := fmt.Sprintf("%s/duplicity-%s",
c.Notifications.MonDash.BoardURL,
c.Hostname,
)
req, _ := http.NewRequest(http.MethodPut, url, buf) // #nosec G104
2016-05-23 08:41:20 +00:00
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", c.Notifications.MonDash.Token)
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("Received unexpected status code: %d", res.StatusCode)
}
return nil
}
type slackResult struct {
Username string `json:"username,omitempty"`
Channel string `json:"channel,omitempty"`
Icon string `json:"icon_emoji,omitempty"`
Text string `json:"text"`
}
func (c *configFile) notifySlack(success bool, err error) error {
if c.Notifications.Slack.HookURL == "" {
return nil
}
text := "Backup succeeded"
if !success {
text = fmt.Sprintf("Backup failed: %s", err)
}
sr := slackResult{
Username: c.Notifications.Slack.Username,
Channel: c.Notifications.Slack.Channel,
Icon: c.Notifications.Slack.Emoji,
Text: text,
}
buf := bytes.NewBuffer([]byte{})
if err = json.NewEncoder(buf).Encode(sr); err != nil {
2016-05-23 08:41:20 +00:00
return err
}
res, err := http.Post(c.Notifications.Slack.HookURL, "application/json", buf)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("Received unexpected status code: %d", res.StatusCode)
}
return nil
}