Skip to content

Commit

Permalink
Nagios integration uses V1 events (#28)
Browse files Browse the repository at this point in the history
* Nagios integration uses V1 events.
  • Loading branch information
rafusel authored Jul 13, 2021
1 parent 75f16bf commit 283c566
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 48 deletions.
34 changes: 14 additions & 20 deletions cmd/integrations/nagios/nagios_enqueue.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ import (
)

type nagiosEnqueueInput struct {
routingKey string
serviceKey string
notificationType string
sourceType string
dedupKey string
incidentKey string
customFields map[string]string
}

const defaultNagiosIntegrationSeverity = "error"

var allowedNotificationTypes = []string{"PROBLEM", "ACKNOWLEDGEMENT", "RECOVERY"}
var allowedSourceTypes = []string{"host", "service"}

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

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

cmd := &cobra.Command{
Use: "enqueue",
Expand All @@ -81,10 +79,10 @@ func NewNagiosEnqueueCmd(config *cmdutil.Config) *cobra.Command {
},
}

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

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

func buildSendEvent(cmdInputs nagiosEnqueueInput) (eventsapi.EventV2, map[string]string) {
sendEvent := eventsapi.EventV2{
RoutingKey: cmdInputs.routingKey,
EventAction: nagiosToPagerDutyEventType[cmdInputs.notificationType],
DedupKey: cmdInputs.dedupKey,
Payload: eventsapi.PayloadV2{
Summary: buildEventDescription(cmdInputs),
Source: cmdInputs.customFields["HOSTNAME"],
Severity: defaultNagiosIntegrationSeverity,
},
func buildSendEvent(cmdInputs nagiosEnqueueInput) (eventsapi.EventV1, map[string]string) {
sendEvent := eventsapi.EventV1{
ServiceKey: cmdInputs.serviceKey,
EventType: nagiosToPagerDutyEventType[cmdInputs.notificationType],
IncidentKey: cmdInputs.incidentKey,
Description: buildEventDescription(cmdInputs),
}
if sendEvent.DedupKey == "" {
sendEvent.DedupKey = buildDedupKey(cmdInputs)
if sendEvent.IncidentKey == "" {
sendEvent.IncidentKey = buildIncidentKey(cmdInputs)
}

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

func buildDedupKey(cmdInputs nagiosEnqueueInput) string {
func buildIncidentKey(cmdInputs nagiosEnqueueInput) string {
if cmdInputs.sourceType == "host" {
return fmt.Sprintf("event_source=host;host_name=%v", cmdInputs.customFields["HOSTNAME"])
}
Expand Down
52 changes: 24 additions & 28 deletions cmd/integrations/nagios/nagios_enqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func buildCmdArgs(inputs nagiosEnqueueInput) []string {
flag string
val string
}{
{"-k", inputs.routingKey}, {"-t", inputs.notificationType}, {"-n", inputs.sourceType}, {"-y", inputs.dedupKey},
{"-k", inputs.serviceKey}, {"-t", inputs.notificationType}, {"-n", inputs.sourceType}, {"-y", inputs.incidentKey},
}
for _, f := range flags {
if f.val != "" {
Expand All @@ -56,12 +56,12 @@ func TestNagiosEnqueue_errors(t *testing.T) {
{
name: "missingRequiredFlags",
inputs: nagiosEnqueueInput{},
expectedError: errors.New("required flag(s) \"notification-type\", \"routing-key\", \"source-type\" not set"),
expectedError: errors.New("required flag(s) \"notification-type\", \"service-key\", \"source-type\" not set"),
},
{
name: "invalidNotficationType",
inputs: nagiosEnqueueInput{
routingKey: "abc",
serviceKey: "abc",
notificationType: "trigger",
sourceType: "host",
},
Expand All @@ -70,7 +70,7 @@ func TestNagiosEnqueue_errors(t *testing.T) {
{
name: "invalidSourceType",
inputs: nagiosEnqueueInput{
routingKey: "abc",
serviceKey: "abc",
notificationType: "PROBLEM",
sourceType: "invalidSourceType",
},
Expand All @@ -79,7 +79,7 @@ func TestNagiosEnqueue_errors(t *testing.T) {
{
name: "hostnameNotSetServiceCustomDetails",
inputs: nagiosEnqueueInput{
routingKey: "abc",
serviceKey: "abc",
notificationType: "RECOVERY",
sourceType: "service",
},
Expand All @@ -88,7 +88,7 @@ func TestNagiosEnqueue_errors(t *testing.T) {
{
name: "serviceDescNotSetServiceCustomDetails",
inputs: nagiosEnqueueInput{
routingKey: "abc",
serviceKey: "abc",
notificationType: "RECOVERY",
sourceType: "service",
customFields: map[string]string{
Expand All @@ -100,7 +100,7 @@ func TestNagiosEnqueue_errors(t *testing.T) {
{
name: "serviceStateNotSetServiceCustomDetails",
inputs: nagiosEnqueueInput{
routingKey: "abc",
serviceKey: "abc",
notificationType: "RECOVERY",
sourceType: "service",
customFields: map[string]string{
Expand All @@ -113,7 +113,7 @@ func TestNagiosEnqueue_errors(t *testing.T) {
{
name: "hostnameNotSetHostCustomDetails",
inputs: nagiosEnqueueInput{
routingKey: "abc",
serviceKey: "abc",
notificationType: "RECOVERY",
sourceType: "host",
},
Expand All @@ -122,7 +122,7 @@ func TestNagiosEnqueue_errors(t *testing.T) {
{
name: "hoststateNotSetHostCustomDetails",
inputs: nagiosEnqueueInput{
routingKey: "abc",
serviceKey: "abc",
notificationType: "RECOVERY",
sourceType: "host",
customFields: map[string]string{
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestNagiosEnqueue_validInputs(t *testing.T) {
{
name: "validSourceHostInput",
cmdInputs: nagiosEnqueueInput{
routingKey: "xyz",
serviceKey: "xyz",
notificationType: "PROBLEM",
sourceType: "host",
customFields: map[string]string{
Expand All @@ -170,7 +170,7 @@ func TestNagiosEnqueue_validInputs(t *testing.T) {
{
name: "validSourceServiceInput",
cmdInputs: nagiosEnqueueInput{
routingKey: "xyz",
serviceKey: "xyz",
notificationType: "PROBLEM",
sourceType: "service",
customFields: map[string]string{
Expand All @@ -181,12 +181,12 @@ func TestNagiosEnqueue_validInputs(t *testing.T) {
},
},
{
name: "userProvidedDedupKey",
name: "userProvidedIncidentKey",
cmdInputs: nagiosEnqueueInput{
routingKey: "xyz",
serviceKey: "xyz",
notificationType: "PROBLEM",
sourceType: "service",
dedupKey: "somededupkey",
incidentKey: "someincidentkey",
customFields: map[string]string{
"HOSTNAME": "computer.network",
"SERVICESTATE": "down",
Expand Down Expand Up @@ -214,9 +214,9 @@ func TestNagiosEnqueue_validInputs(t *testing.T) {
cmd := NewNagiosEnqueueCmd(realConfig)
cmd.SetArgs(buildCmdArgs(tt.cmdInputs))

dedupKey := tt.cmdInputs.dedupKey
if dedupKey == "" {
dedupKey = buildDedupKey(tt.cmdInputs)
incidentKey := tt.cmdInputs.incidentKey
if incidentKey == "" {
incidentKey = buildIncidentKey(tt.cmdInputs)
}

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

expectedRequestBody := map[string]interface{}{
"routing_key": tt.cmdInputs.routingKey,
"event_action": nagiosToPagerDutyEventType[tt.cmdInputs.notificationType],
"dedup_key": dedupKey,
"payload": map[string]interface{}{
"summary": buildEventDescription(tt.cmdInputs),
"source": tt.cmdInputs.customFields["HOSTNAME"],
"severity": defaultNagiosIntegrationSeverity,
"custom_details": customDetails,
},
"service_key": tt.cmdInputs.serviceKey,
"event_type": nagiosToPagerDutyEventType[tt.cmdInputs.notificationType],
"incident_key": incidentKey,
"description": buildEventDescription(tt.cmdInputs),
"details": customDetails,
}

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

gock.InterceptClient(defaultHTTPClient)

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

assert.Contains(t, out, fmt.Sprintf(`{"key":"%v"}`, tt.cmdInputs.routingKey))
assert.Contains(t, out, fmt.Sprintf(`{"key":"%v"}`, tt.cmdInputs.serviceKey))
})
}
}

0 comments on commit 283c566

Please sign in to comment.