forked from sensu/sensu-pagerduty-handler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (71 loc) · 1.83 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"github.com/PagerDuty/go-pagerduty"
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-plugins-go-library/sensu"
)
type HandlerConfig struct {
sensu.PluginConfig
authToken string
}
var (
config = HandlerConfig{
PluginConfig: sensu.PluginConfig{
Name: "sensu-pagerduty-handler",
Short: "The Sensu Go PagerDuty handler for incident management",
},
}
pagerDutyConfigOptions = []*sensu.PluginConfigOption{
{
Path: "token",
Env: "PAGERDUTY_TOKEN",
Argument: "token",
Shorthand: "t",
Usage: "The PagerDuty V2 API authentication token, use default from PAGERDUTY_TOKEN env var",
Value: &config.authToken,
Default: "",
},
}
)
func main() {
goHandler := sensu.NewGoHandler(&config.PluginConfig, pagerDutyConfigOptions, checkArgs, manageIncident)
goHandler.Execute()
}
func checkArgs(event *corev2.Event) error {
if !event.HasCheck() {
return fmt.Errorf("event does not contain check")
}
return nil
}
func manageIncident(event *corev2.Event) error {
severity := "warning"
if event.Check.Status < 3 {
severities := []string{"info", "warning", "critical"}
severity = severities[event.Check.Status]
}
summary := fmt.Sprintf("%s/%s : %s", event.Entity.Name, event.Check.Name, event.Check.Output)
pdPayload := pagerduty.V2Payload{
Source: event.Entity.Name,
Component: event.Check.Name,
Severity: severity,
Summary: summary,
Details: event,
}
action := "trigger"
if event.Check.Status == 0 {
action = "resolve"
}
dedupKey := fmt.Sprintf("%s-%s", event.Entity.Name, event.Check.Name)
pdEvent := pagerduty.V2Event{
RoutingKey: config.authToken,
Action: action,
Payload: &pdPayload,
DedupKey: dedupKey,
}
_, err := pagerduty.ManageEvent(pdEvent)
if err != nil {
return err
}
return nil
}