-
Notifications
You must be signed in to change notification settings - Fork 0
/
opsgenie_notify.go
65 lines (55 loc) · 1.46 KB
/
opsgenie_notify.go
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
package notify
import (
"errors"
"fmt"
"github.com/opsgenie/opsgenie-go-sdk/alertsv2"
ogcli "github.com/opsgenie/opsgenie-go-sdk/client"
"github.com/patdaman/endpoint-monitor/model"
)
type OpsgenieNotify struct {
APIKey string `json:"apikey"`
Alias string `json:"alias"`
}
func (opsgenieNotify OpsgenieNotify) GetClientName() string {
return "OpsGenie Alert"
}
func (opsgenieNotify OpsgenieNotify) Initialize() error {
return nil
}
func (opsgenieNotify OpsgenieNotify) SendNotification(notification model.Notification) error {
cli := new(ogcli.OpsGenieClient)
cli.SetAPIKey(opsgenieNotify.APIKey)
alertCli, _ := cli.AlertV2()
request := alertsv2.CreateAlertRequest{
Message: notification.Message,
Alias: opsgenieNotify.Alias,
Description: notification.Description,
Tags: notification.Tags,
Details: notification.Details,
Entity: notification.Environment,
Priority: Priority(notification.Priority),
Note: notification.Note,
}
response, err := alertCli.Create(request)
if err != nil {
// return err.Error()
return err
}
if len(response.RequestID) == 0 {
return errors.New(fmt.Sprintf("OpsGenie did not return a new alert ID."))
}
return nil
}
func Priority(priority string) alertsv2.Priority {
switch priority {
case "Critical", "P1":
return alertsv2.P1
case "High", "P2":
return alertsv2.P2
case "Low", "P4":
return alertsv2.P4
case "Information", "P5":
return alertsv2.P5
}
return alertsv2.P3
}