Repository navigation
Expand file tree
/
Copy pathnotification.go
More file actions
165 lines (133 loc) · 3.94 KB
/
Copy pathnotification.go
File metadata and controls
165 lines (133 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/Luzifer/go_helpers/v2/str"
"github.com/pkg/errors"
)
const notifyRequestTimeout = 2 * time.Second
func (c *configFile) Notify(command string, success bool, err error) error {
if !str.StringInSlice(command, notifyCommands) {
return nil
}
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
}
estr := ""
for _, e := range errs {
if e == nil {
continue
}
estr = fmt.Sprintf("%s\n- %s", estr, e)
}
return errors.Errorf("%d notifiers failed:%s", len(errs), estr)
}
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"`
}
//revive:disable-next-line:flag-parameter // not a flag parameter
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,
}
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 {
return errors.Wrap(err, "encoding request payload")
}
url := fmt.Sprintf("%s/duplicity-%s",
c.Notifications.MonDash.BoardURL,
c.Hostname,
)
ctx, cancel := context.WithTimeout(context.Background(), notifyRequestTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, buf)
if err != nil {
return errors.Wrap(err, "creating request")
}
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 errors.Wrap(err, "executing request")
}
defer res.Body.Close() //nolint:errcheck // Will be cleaned by process exit shortly after
if res.StatusCode != http.StatusOK {
return errors.Errorf("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"`
}
//revive:disable-next-line:flag-parameter // not a flag parameter
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 {
return errors.Wrap(err, "encoding payload")
}
ctx, cancel := context.WithTimeout(context.Background(), notifyRequestTimeout)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.Notifications.Slack.HookURL, buf)
if err != nil {
return errors.Wrap(err, "creating request")
}
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "executing request")
}
defer res.Body.Close() //nolint:errcheck // Will be cleaned by process exit shortly after
if res.StatusCode != http.StatusOK {
return errors.Errorf("unexpected status code: %d", res.StatusCode)
}
return nil
}