From 7cdb9703396258a82aaaf3bfff3ea4d6a7496ded Mon Sep 17 00:00:00 2001 From: Pavel Zavora Date: Fri, 26 Feb 2021 18:43:31 +0100 Subject: [PATCH] feat(kapacitor): add serviceNow alert properties to server --- kapacitor.go | 51 ++++++++++++++++++++++++---------------- kapacitor/alerts.go | 12 +++++++++- kapacitor/alerts_test.go | 24 +++++++++++++++++++ 3 files changed, 66 insertions(+), 21 deletions(-) diff --git a/kapacitor.go b/kapacitor.go index 666915a70..f1a613463 100644 --- a/kapacitor.go +++ b/kapacitor.go @@ -4,26 +4,27 @@ import "encoding/json" // AlertNodes defines all possible kapacitor interactions with an alert. type AlertNodes struct { - IsStateChangesOnly bool `json:"stateChangesOnly"` // IsStateChangesOnly will only send alerts on state changes. - UseFlapping bool `json:"useFlapping"` // UseFlapping enables flapping detection. Flapping occurs when a service or host changes state too frequently, resulting in a storm of problem and recovery notification - Posts []*Post `json:"post"` // HTTPPost will post the JSON alert data to the specified URLs. - TCPs []*TCP `json:"tcp"` // TCP will send the JSON alert data to the specified endpoint via TCP. - Email []*Email `json:"email"` // Email will send alert data to the specified emails. - Exec []*Exec `json:"exec"` // Exec will run shell commandss when an alert triggers - Log []*Log `json:"log"` // Log will log JSON alert data to files in JSON lines format. - VictorOps []*VictorOps `json:"victorOps"` // VictorOps will send alert to all VictorOps - PagerDuty []*PagerDuty `json:"pagerDuty"` // PagerDuty will send alert to all PagerDuty - PagerDuty2 []*PagerDuty `json:"pagerDuty2"` // PagerDuty2 will send alert to PagerDuty v2 - Pushover []*Pushover `json:"pushover"` // Pushover will send alert to all Pushover - Sensu []*Sensu `json:"sensu"` // Sensu will send alert to all Sensu - Slack []*Slack `json:"slack"` // Slack will send alert to Slack - Telegram []*Telegram `json:"telegram"` // Telegram will send alert to all Telegram - HipChat []*HipChat `json:"hipChat"` // HipChat will send alert to all HipChat - Alerta []*Alerta `json:"alerta"` // Alerta will send alert to all Alerta - OpsGenie []*OpsGenie `json:"opsGenie"` // OpsGenie will send alert to all OpsGenie - OpsGenie2 []*OpsGenie `json:"opsGenie2"` // OpsGenie2 will send alert to all OpsGenie v2 - Talk []*Talk `json:"talk"` // Talk will send alert to all Talk - Kafka []*Kafka `json:"kafka"` // Kafka will send alert to all Kafka + IsStateChangesOnly bool `json:"stateChangesOnly"` // IsStateChangesOnly will only send alerts on state changes. + UseFlapping bool `json:"useFlapping"` // UseFlapping enables flapping detection. Flapping occurs when a service or host changes state too frequently, resulting in a storm of problem and recovery notification + Posts []*Post `json:"post"` // HTTPPost will post the JSON alert data to the specified URLs. + TCPs []*TCP `json:"tcp"` // TCP will send the JSON alert data to the specified endpoint via TCP. + Email []*Email `json:"email"` // Email will send alert data to the specified emails. + Exec []*Exec `json:"exec"` // Exec will run shell commandss when an alert triggers + Log []*Log `json:"log"` // Log will log JSON alert data to files in JSON lines format. + VictorOps []*VictorOps `json:"victorOps"` // VictorOps will send alert to all VictorOps + PagerDuty []*PagerDuty `json:"pagerDuty"` // PagerDuty will send alert to all PagerDuty + PagerDuty2 []*PagerDuty `json:"pagerDuty2"` // PagerDuty2 will send alert to PagerDuty v2 + Pushover []*Pushover `json:"pushover"` // Pushover will send alert to all Pushover + Sensu []*Sensu `json:"sensu"` // Sensu will send alert to all Sensu + Slack []*Slack `json:"slack"` // Slack will send alert to Slack + Telegram []*Telegram `json:"telegram"` // Telegram will send alert to all Telegram + HipChat []*HipChat `json:"hipChat"` // HipChat will send alert to all HipChat + Alerta []*Alerta `json:"alerta"` // Alerta will send alert to all Alerta + OpsGenie []*OpsGenie `json:"opsGenie"` // OpsGenie will send alert to all OpsGenie + OpsGenie2 []*OpsGenie `json:"opsGenie2"` // OpsGenie2 will send alert to all OpsGenie v2 + Talk []*Talk `json:"talk"` // Talk will send alert to all Talk + Kafka []*Kafka `json:"kafka"` // Kafka will send alert to all Kafka + ServiceNow []*ServiceNow `json:"serviceNow"` // ServiceNow alert options } // Post will POST alerts to a destination URL @@ -144,6 +145,16 @@ type Kafka struct { Template string `json:"template"` } +// ServiceNow properties +type ServiceNow struct { + Source string `json:"source"` + Node string `json:"node"` + Type string `json:"type"` + Resource string `json:"resource"` + MetricName string `json:"metric_name"` + MessageKey string `json:"message_key"` +} + // MarshalJSON converts AlertNodes to JSON func (n *AlertNodes) MarshalJSON() ([]byte, error) { type Alias AlertNodes diff --git a/kapacitor/alerts.go b/kapacitor/alerts.go index ddc1f3454..57a4d1b4f 100644 --- a/kapacitor/alerts.go +++ b/kapacitor/alerts.go @@ -19,7 +19,17 @@ func AlertServices(rule chronograf.AlertRule) (string, error) { } if err := ValidateAlert(node); err != nil { - return "", err + // workaround for not-yet released fix https://github.com/influxdata/kapacitor/pull/2488 + // it can be deleted once kapacitor 1.5.9+ is released + // can be simply: return "", err + if !strings.Contains(err.Error(), `property "servicenow"`) { + return "", err + } + // no method or property "servicenow" on *pipeline.AlertNode + node = strings.Replace(node, ".servicenow()", ".serviceNow()", 1) + if err2 := ValidateAlert(node); err2 != nil { + return "", err + } } return node, nil } diff --git a/kapacitor/alerts_test.go b/kapacitor/alerts_test.go index 9634e2170..3e9e66097 100644 --- a/kapacitor/alerts_test.go +++ b/kapacitor/alerts_test.go @@ -139,6 +139,30 @@ func TestAlertServices(t *testing.T) { want: `alert() .post('http://myaddress') .header('key', 'value') +`, + }, + { + name: "Test serviceNow", + rule: chronograf.AlertRule{ + AlertNodes: chronograf.AlertNodes{ + ServiceNow: []*chronograf.ServiceNow{{ + Source: "a", + Type: "b", + Node: "c", + Resource: "d", + MetricName: "e", + MessageKey: "f", + }}, + }, + }, + want: `alert() + .serviceNow() + .source('a') + .node('c') + .type('b') + .resource('d') + .metricName('e') + .messageKey('f') `, }, }