-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
188 lines (162 loc) · 5.14 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/asaskevich/govalidator"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
"github.com/sensu-community/sensu-plugin-sdk/templates"
corev2 "github.com/sensu/sensu-go/api/core/v2"
)
// VOEvent is the JSON type for a VictorOps message
type VOEvent struct {
MessageType string `json:"message_type"`
StateMessage string `json:"state_message,omitempty"`
EntityID string `json:"entity_id,omitempty"`
HostName string `json:"host_name,omitempty"`
MonitoringTool string `json:"monitoring_tool,omitempty"`
// This is feature parity with Ruby sensu-plugins-victorops
// which includes the check and client in its payload.
Check *corev2.Check `json:"check,omitempty"`
Entity *corev2.Entity `json:"entity,omitempty"`
}
// VOResopnse is the JSON type for a VictorOps response message
type VOResponse struct {
Result string `json:"result"`
EntityID string `json:"entity_id"`
}
// HandlerConfig is needed for Sensu Go Handlers
type HandlerConfig struct {
sensu.PluginConfig
RoutingKey string
APIURL string
MessageTemplate string
EntityIDTemplate string
}
var (
config = HandlerConfig{
PluginConfig: sensu.PluginConfig{
Name: "sensu-victorops-handler",
Short: "The Sensu Go VictorOps handler for sending notifications",
Keyspace: "sensu.io/plugins/victorops/config",
},
}
// VictorOpsConfigOptions contains the Sensu Plugin Config Options
VictorOpsConfigOptions = []*sensu.PluginConfigOption{
{
Path: "routingkey",
Env: "SENSU_VICTOROPS_ROUTINGKEY",
Argument: "routingkey",
Shorthand: "r",
Default: "",
Secret: true,
Usage: "The VictorOps Routing Key",
Value: &config.RoutingKey,
},
{
Path: "api-url",
Env: "SENSU_VICTOROPS_APIURL",
Argument: "api-url",
Shorthand: "a",
Default: "https://alert.victorops.com/integrations/generic/20131114/alert",
Usage: "The URL for the VictorOps API",
Value: &config.APIURL,
},
{
Path: "message-template",
Env: "",
Argument: "message-template",
Shorthand: "m",
Default: "{{.Entity.Name}:{{.Check.Name}}:{{.Check.Output}}",
Usage: "The template for the message sent to VictorOps",
Value: &config.MessageTemplate,
},
{
Path: "entity-id-template",
Env: "",
Argument: "entity-id-template",
Shorthand: "e",
Default: "{{.Entity.Name}/{{.Check.Name}}",
Usage: "The template for the Entity ID sent to VictorOps",
Value: &config.EntityIDTemplate,
},
}
)
func main() {
goHandler := sensu.NewGoHandler(&config.PluginConfig, VictorOpsConfigOptions, CheckArgs, SendVictorOps)
goHandler.Execute()
}
// CheckArgs checks that necessary arguments are set
func CheckArgs(_ *corev2.Event) error {
if len(config.RoutingKey) == 0 {
return errors.New("missing VictorOps Routing Key")
}
if len(config.APIURL) == 0 {
return errors.New("missing VictorOps API URL specification")
}
if !govalidator.IsURL(config.APIURL) {
return errors.New("invlaid VictorOps API URL specification")
}
config.APIURL = strings.TrimSuffix(config.APIURL, "/")
return nil
}
// SendVictorOps builds the event message and sends it to VO
func SendVictorOps(event *corev2.Event) error {
var msgType string
switch eventStatus := event.Check.Status; eventStatus {
case 0:
msgType = "RECOVERY"
case 1:
msgType = "WARNING"
default:
msgType = "CRITICAL"
}
msgEntityID, err := templates.EvalTemplate("EntityID", config.EntityIDTemplate, event)
if err != nil {
return fmt.Errorf("Failed to create Entity ID from template: %v", err)
}
msgStateMessage, err := templates.EvalTemplate("Message", config.MessageTemplate, event)
if err != nil {
return fmt.Errorf("Failed to create message from template: %v", err)
}
message := VOEvent{
MessageType: msgType,
StateMessage: msgStateMessage,
EntityID: msgEntityID,
HostName: event.Entity.Name,
MonitoringTool: "sensu",
Check: event.Check,
Entity: event.Entity,
}
msgBytes, err := json.Marshal(message)
if err != nil {
return fmt.Errorf("Failed to marshal VictorOps message: %s", err)
}
url := fmt.Sprintf("%s/%s", config.APIURL, config.RoutingKey)
resp, err := http.Post(url, "application/json", bytes.NewBuffer(msgBytes))
if err != nil {
return fmt.Errorf("Post to %s failed: %s", url, err)
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return fmt.Errorf("POST to %s failed with %v", url, resp.Status)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("Failed to read response body from %s: %v", config.APIURL, err)
}
voResponse := VOResponse{}
err = json.Unmarshal(body, &voResponse)
if err != nil {
return fmt.Errorf("Failed to unmarshal response from VictorOps: %v", err)
}
// FUTURE: send to AH
// VictorOps only sends back a "success" or "failure" message along with the Entity ID
fmt.Printf("Submission to VictorOps for Entity ID %s result: %s\n", voResponse.EntityID, voResponse.Result)
// Should we actually return error if Result above is "failure"?
return nil
}