Add entity_display_name for VictorOps, use better state_message (#769)

* Add entity_display_name for VictorOps, use better state_message

* Reuse existing long-form templates for state_message

* Rebuild binaries

* Limit state_message to 20K
This commit is contained in:
Siavash Safi 2017-07-03 11:44:53 +02:00 committed by Fabian Reinartz
parent 41c5aaf3f4
commit d5f0d1773f
4 changed files with 44 additions and 26 deletions

View File

@ -98,9 +98,10 @@ var (
NotifierConfig: NotifierConfig{ NotifierConfig: NotifierConfig{
VSendResolved: true, VSendResolved: true,
}, },
MessageType: `CRITICAL`, MessageType: `CRITICAL`,
StateMessage: `{{ template "victorops.default.state_message" . }}`, StateMessage: `{{ template "victorops.default.state_message" . }}`,
MonitoringTool: `{{ template "victorops.default.monitoring_tool" . }}`, EntityDisplayName: `{{ template "victorops.default.entity_display_name" . }}`,
MonitoringTool: `{{ template "victorops.default.monitoring_tool" . }}`,
} }
// DefaultPushoverConfig defines default values for Pushover configurations. // DefaultPushoverConfig defines default values for Pushover configurations.
@ -321,12 +322,13 @@ func (c *OpsGenieConfig) UnmarshalYAML(unmarshal func(interface{}) error) error
type VictorOpsConfig struct { type VictorOpsConfig struct {
NotifierConfig `yaml:",inline" json:",inline"` NotifierConfig `yaml:",inline" json:",inline"`
APIKey Secret `yaml:"api_key,omitempty" json:"api_key,omitempty"` APIKey Secret `yaml:"api_key" json:"api_key"`
APIURL string `yaml:"api_url,omitempty" json:"api_url,omitempty"` APIURL string `yaml:"api_url" json:"api_url"`
RoutingKey string `yaml:"routing_key,omitempty" json:"routing_key,omitempty"` RoutingKey string `yaml:"routing_key" json:"routing_key"`
MessageType string `yaml:"message_type,omitempty" json:"message_type,omitempty"` MessageType string `yaml:"message_type" json:"message_type"`
StateMessage string `yaml:"state_message,omitempty" json:"state_message,omitempty"` StateMessage string `yaml:"state_message" json:"state_message"`
MonitoringTool string `yaml:"monitoring_tool,omitempty" json:"monitoring_tool,omitempty"` EntityDisplayName string `yaml:"entity_display_name" json:"entity_display_name"`
MonitoringTool string `yaml:"monitoring_tool" json:"monitoring_tool"`
XXX map[string]interface{} `yaml:",inline" json:"-"` XXX map[string]interface{} `yaml:",inline" json:"-"`
} }

View File

@ -777,10 +777,11 @@ const (
) )
type victorOpsMessage struct { type victorOpsMessage struct {
MessageType string `json:"message_type"` MessageType string `json:"message_type"`
EntityID string `json:"entity_id"` EntityID string `json:"entity_id"`
StateMessage string `json:"state_message"` EntityDisplayName string `json:"entity_display_name"`
MonitoringTool string `json:"monitoring_tool"` StateMessage string `json:"state_message"`
MonitoringTool string `json:"monitoring_tool"`
} }
type victorOpsErrorResponse struct { type victorOpsErrorResponse struct {
@ -803,11 +804,12 @@ func (n *VictorOps) Notify(ctx context.Context, as ...*types.Alert) (bool, error
var err error var err error
var ( var (
alerts = types.Alerts(as...) alerts = types.Alerts(as...)
data = n.tmpl.Data(receiverName(ctx), groupLabels(ctx), as...) data = n.tmpl.Data(receiverName(ctx), groupLabels(ctx), as...)
tmpl = tmplText(n.tmpl, data, &err) tmpl = tmplText(n.tmpl, data, &err)
apiURL = fmt.Sprintf("%s%s/%s", n.conf.APIURL, n.conf.APIKey, n.conf.RoutingKey) apiURL = fmt.Sprintf("%s%s/%s", n.conf.APIURL, n.conf.APIKey, n.conf.RoutingKey)
messageType = n.conf.MessageType messageType = n.conf.MessageType
stateMessage = tmpl(n.conf.StateMessage)
) )
if alerts.Status() == model.AlertFiring && !victorOpsAllowedEvents[messageType] { if alerts.Status() == model.AlertFiring && !victorOpsAllowedEvents[messageType] {
@ -818,11 +820,16 @@ func (n *VictorOps) Notify(ctx context.Context, as ...*types.Alert) (bool, error
messageType = victorOpsEventResolve messageType = victorOpsEventResolve
} }
if len(stateMessage) > 20480 {
stateMessage = stateMessage[0:20475] + "\n..."
}
msg := &victorOpsMessage{ msg := &victorOpsMessage{
MessageType: messageType, MessageType: messageType,
EntityID: hashKey(key), EntityID: hashKey(key),
StateMessage: tmpl(n.conf.StateMessage), EntityDisplayName: tmpl(n.conf.EntityDisplayName),
MonitoringTool: tmpl(n.conf.MonitoringTool), StateMessage: stateMessage,
MonitoringTool: tmpl(n.conf.MonitoringTool),
} }
if err != nil { if err != nil {

View File

@ -46,10 +46,19 @@ Alerts Resolved:
{{ define "opsgenie.default.source" }}{{ template "__alertmanagerURL" . }}{{ end }} {{ define "opsgenie.default.source" }}{{ template "__alertmanagerURL" . }}{{ end }}
{{ define "victorops.default.state_message" }}{{ template "__subject" . }} | {{ template "__alertmanagerURL" . }}{{ end }} {{ define "victorops.default.state_message" }}{{ .CommonAnnotations.SortedPairs.Values | join " " }}
{{ if gt (len .Alerts.Firing) 0 -}}
Alerts Firing:
{{ template "__text_alert_list" .Alerts.Firing }}
{{- end }}
{{ if gt (len .Alerts.Resolved) 0 -}}
Alerts Resolved:
{{ template "__text_alert_list" .Alerts.Resolved }}
{{- end }}
{{- end }}
{{ define "victorops.default.entity_display_name" }}{{ template "__subject" . }}{{ end }}
{{ define "victorops.default.monitoring_tool" }}{{ template "__alertmanager" . }}{{ end }} {{ define "victorops.default.monitoring_tool" }}{{ template "__alertmanager" . }}{{ end }}
{{ define "email.default.subject" }}{{ template "__subject" . }}{{ end }} {{ define "email.default.subject" }}{{ template "__subject" . }}{{ end }}
{{ define "email.default.html" }} {{ define "email.default.html" }}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

File diff suppressed because one or more lines are too long