Skip to content

Commit 283c566

Browse files
authored
Nagios integration uses V1 events (#28)
* Nagios integration uses V1 events.
1 parent 75f16bf commit 283c566

2 files changed

Lines changed: 38 additions & 48 deletions

File tree

cmd/integrations/nagios/nagios_enqueue.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@ import (
2525
)
2626

2727
type nagiosEnqueueInput struct {
28-
routingKey string
28+
serviceKey string
2929
notificationType string
3030
sourceType string
31-
dedupKey string
31+
incidentKey string
3232
customFields map[string]string
3333
}
3434

35-
const defaultNagiosIntegrationSeverity = "error"
36-
3735
var allowedNotificationTypes = []string{"PROBLEM", "ACKNOWLEDGEMENT", "RECOVERY"}
3836
var allowedSourceTypes = []string{"host", "service"}
3937

@@ -54,7 +52,7 @@ var nagiosToPagerDutyEventType = map[string]string{
5452
func NewNagiosEnqueueCmd(config *cmdutil.Config) *cobra.Command {
5553
var cmdInput nagiosEnqueueInput
5654

57-
requiredFlags := []string{"routing-key", "notification-type", "source-type"}
55+
requiredFlags := []string{"service-key", "notification-type", "source-type"}
5856

5957
cmd := &cobra.Command{
6058
Use: "enqueue",
@@ -81,10 +79,10 @@ func NewNagiosEnqueueCmd(config *cmdutil.Config) *cobra.Command {
8179
},
8280
}
8381

84-
cmd.Flags().StringVarP(&cmdInput.routingKey, "routing-key", "k", "", "Service Events API Key (required)")
82+
cmd.Flags().StringVarP(&cmdInput.serviceKey, "service-key", "k", "", "Service Events API Key (required)")
8583
cmd.Flags().StringVarP(&cmdInput.notificationType, "notification-type", "t", "", "The Nagios notification type (required)")
8684
cmd.Flags().StringVarP(&cmdInput.sourceType, "source-type", "n", "", "The Nagios source type (host or service, required)")
87-
cmd.Flags().StringVarP(&cmdInput.dedupKey, "dedup-key", "y", "", "Deduplication key for correlating triggers and resolves")
85+
cmd.Flags().StringVarP(&cmdInput.incidentKey, "incident-key", "y", "", "Incident key for correlating triggers and resolves")
8886
cmd.Flags().StringToStringVarP(&cmdInput.customFields, "field", "f", map[string]string{}, "Add given KEY=VALUE pair to the event details")
8987

9088
for _, flag := range requiredFlags {
@@ -94,19 +92,15 @@ func NewNagiosEnqueueCmd(config *cmdutil.Config) *cobra.Command {
9492
return cmd
9593
}
9694

97-
func buildSendEvent(cmdInputs nagiosEnqueueInput) (eventsapi.EventV2, map[string]string) {
98-
sendEvent := eventsapi.EventV2{
99-
RoutingKey: cmdInputs.routingKey,
100-
EventAction: nagiosToPagerDutyEventType[cmdInputs.notificationType],
101-
DedupKey: cmdInputs.dedupKey,
102-
Payload: eventsapi.PayloadV2{
103-
Summary: buildEventDescription(cmdInputs),
104-
Source: cmdInputs.customFields["HOSTNAME"],
105-
Severity: defaultNagiosIntegrationSeverity,
106-
},
95+
func buildSendEvent(cmdInputs nagiosEnqueueInput) (eventsapi.EventV1, map[string]string) {
96+
sendEvent := eventsapi.EventV1{
97+
ServiceKey: cmdInputs.serviceKey,
98+
EventType: nagiosToPagerDutyEventType[cmdInputs.notificationType],
99+
IncidentKey: cmdInputs.incidentKey,
100+
Description: buildEventDescription(cmdInputs),
107101
}
108-
if sendEvent.DedupKey == "" {
109-
sendEvent.DedupKey = buildDedupKey(cmdInputs)
102+
if sendEvent.IncidentKey == "" {
103+
sendEvent.IncidentKey = buildIncidentKey(cmdInputs)
110104
}
111105

112106
customDetails := cmdInputs.customFields
@@ -123,7 +117,7 @@ func buildEventDescription(cmdInputs nagiosEnqueueInput) string {
123117
return strings.Join(descriptionFields, "; ")
124118
}
125119

126-
func buildDedupKey(cmdInputs nagiosEnqueueInput) string {
120+
func buildIncidentKey(cmdInputs nagiosEnqueueInput) string {
127121
if cmdInputs.sourceType == "host" {
128122
return fmt.Sprintf("event_source=host;host_name=%v", cmdInputs.customFields["HOSTNAME"])
129123
}

cmd/integrations/nagios/nagios_enqueue_test.go

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func buildCmdArgs(inputs nagiosEnqueueInput) []string {
3434
flag string
3535
val string
3636
}{
37-
{"-k", inputs.routingKey}, {"-t", inputs.notificationType}, {"-n", inputs.sourceType}, {"-y", inputs.dedupKey},
37+
{"-k", inputs.serviceKey}, {"-t", inputs.notificationType}, {"-n", inputs.sourceType}, {"-y", inputs.incidentKey},
3838
}
3939
for _, f := range flags {
4040
if f.val != "" {
@@ -56,12 +56,12 @@ func TestNagiosEnqueue_errors(t *testing.T) {
5656
{
5757
name: "missingRequiredFlags",
5858
inputs: nagiosEnqueueInput{},
59-
expectedError: errors.New("required flag(s) \"notification-type\", \"routing-key\", \"source-type\" not set"),
59+
expectedError: errors.New("required flag(s) \"notification-type\", \"service-key\", \"source-type\" not set"),
6060
},
6161
{
6262
name: "invalidNotficationType",
6363
inputs: nagiosEnqueueInput{
64-
routingKey: "abc",
64+
serviceKey: "abc",
6565
notificationType: "trigger",
6666
sourceType: "host",
6767
},
@@ -70,7 +70,7 @@ func TestNagiosEnqueue_errors(t *testing.T) {
7070
{
7171
name: "invalidSourceType",
7272
inputs: nagiosEnqueueInput{
73-
routingKey: "abc",
73+
serviceKey: "abc",
7474
notificationType: "PROBLEM",
7575
sourceType: "invalidSourceType",
7676
},
@@ -79,7 +79,7 @@ func TestNagiosEnqueue_errors(t *testing.T) {
7979
{
8080
name: "hostnameNotSetServiceCustomDetails",
8181
inputs: nagiosEnqueueInput{
82-
routingKey: "abc",
82+
serviceKey: "abc",
8383
notificationType: "RECOVERY",
8484
sourceType: "service",
8585
},
@@ -88,7 +88,7 @@ func TestNagiosEnqueue_errors(t *testing.T) {
8888
{
8989
name: "serviceDescNotSetServiceCustomDetails",
9090
inputs: nagiosEnqueueInput{
91-
routingKey: "abc",
91+
serviceKey: "abc",
9292
notificationType: "RECOVERY",
9393
sourceType: "service",
9494
customFields: map[string]string{
@@ -100,7 +100,7 @@ func TestNagiosEnqueue_errors(t *testing.T) {
100100
{
101101
name: "serviceStateNotSetServiceCustomDetails",
102102
inputs: nagiosEnqueueInput{
103-
routingKey: "abc",
103+
serviceKey: "abc",
104104
notificationType: "RECOVERY",
105105
sourceType: "service",
106106
customFields: map[string]string{
@@ -113,7 +113,7 @@ func TestNagiosEnqueue_errors(t *testing.T) {
113113
{
114114
name: "hostnameNotSetHostCustomDetails",
115115
inputs: nagiosEnqueueInput{
116-
routingKey: "abc",
116+
serviceKey: "abc",
117117
notificationType: "RECOVERY",
118118
sourceType: "host",
119119
},
@@ -122,7 +122,7 @@ func TestNagiosEnqueue_errors(t *testing.T) {
122122
{
123123
name: "hoststateNotSetHostCustomDetails",
124124
inputs: nagiosEnqueueInput{
125-
routingKey: "abc",
125+
serviceKey: "abc",
126126
notificationType: "RECOVERY",
127127
sourceType: "host",
128128
customFields: map[string]string{
@@ -158,7 +158,7 @@ func TestNagiosEnqueue_validInputs(t *testing.T) {
158158
{
159159
name: "validSourceHostInput",
160160
cmdInputs: nagiosEnqueueInput{
161-
routingKey: "xyz",
161+
serviceKey: "xyz",
162162
notificationType: "PROBLEM",
163163
sourceType: "host",
164164
customFields: map[string]string{
@@ -170,7 +170,7 @@ func TestNagiosEnqueue_validInputs(t *testing.T) {
170170
{
171171
name: "validSourceServiceInput",
172172
cmdInputs: nagiosEnqueueInput{
173-
routingKey: "xyz",
173+
serviceKey: "xyz",
174174
notificationType: "PROBLEM",
175175
sourceType: "service",
176176
customFields: map[string]string{
@@ -181,12 +181,12 @@ func TestNagiosEnqueue_validInputs(t *testing.T) {
181181
},
182182
},
183183
{
184-
name: "userProvidedDedupKey",
184+
name: "userProvidedIncidentKey",
185185
cmdInputs: nagiosEnqueueInput{
186-
routingKey: "xyz",
186+
serviceKey: "xyz",
187187
notificationType: "PROBLEM",
188188
sourceType: "service",
189-
dedupKey: "somededupkey",
189+
incidentKey: "someincidentkey",
190190
customFields: map[string]string{
191191
"HOSTNAME": "computer.network",
192192
"SERVICESTATE": "down",
@@ -214,9 +214,9 @@ func TestNagiosEnqueue_validInputs(t *testing.T) {
214214
cmd := NewNagiosEnqueueCmd(realConfig)
215215
cmd.SetArgs(buildCmdArgs(tt.cmdInputs))
216216

217-
dedupKey := tt.cmdInputs.dedupKey
218-
if dedupKey == "" {
219-
dedupKey = buildDedupKey(tt.cmdInputs)
217+
incidentKey := tt.cmdInputs.incidentKey
218+
if incidentKey == "" {
219+
incidentKey = buildIncidentKey(tt.cmdInputs)
220220
}
221221

222222
customDetails := map[string]string{
@@ -227,20 +227,16 @@ func TestNagiosEnqueue_validInputs(t *testing.T) {
227227
}
228228

229229
expectedRequestBody := map[string]interface{}{
230-
"routing_key": tt.cmdInputs.routingKey,
231-
"event_action": nagiosToPagerDutyEventType[tt.cmdInputs.notificationType],
232-
"dedup_key": dedupKey,
233-
"payload": map[string]interface{}{
234-
"summary": buildEventDescription(tt.cmdInputs),
235-
"source": tt.cmdInputs.customFields["HOSTNAME"],
236-
"severity": defaultNagiosIntegrationSeverity,
237-
"custom_details": customDetails,
238-
},
230+
"service_key": tt.cmdInputs.serviceKey,
231+
"event_type": nagiosToPagerDutyEventType[tt.cmdInputs.notificationType],
232+
"incident_key": incidentKey,
233+
"description": buildEventDescription(tt.cmdInputs),
234+
"details": customDetails,
239235
}
240236

241237
gock.New(cmdutil.GetDefaults().Address).
242238
Post("/send").JSON(expectedRequestBody).
243-
Reply(200).JSON(map[string]interface{}{"key": tt.cmdInputs.routingKey})
239+
Reply(200).JSON(map[string]interface{}{"key": tt.cmdInputs.serviceKey})
244240

245241
gock.InterceptClient(defaultHTTPClient)
246242

@@ -253,7 +249,7 @@ func TestNagiosEnqueue_validInputs(t *testing.T) {
253249
t.Errorf("error running command `enqueue`: %v", err)
254250
}
255251

256-
assert.Contains(t, out, fmt.Sprintf(`{"key":"%v"}`, tt.cmdInputs.routingKey))
252+
assert.Contains(t, out, fmt.Sprintf(`{"key":"%v"}`, tt.cmdInputs.serviceKey))
257253
})
258254
}
259255
}

0 commit comments

Comments
 (0)