mirror of
https://github.com/prometheus/alertmanager
synced 2025-02-16 18:47:10 +00:00
This adds notifications to Amazon SNS. It is using Amazon's go sdk. The notification config should look like amazon_sns_config { topic_arn: "arn:aws:sns:us-east-1:an-arn-id:topic" send_resolved: true } The alert summary and status will be used as the SNS subject. The alert description and status will be used as the SNS message. SNS notifictions require Amazon AWS credentials to be setup as described by the Amazon go sdk.
158 lines
5.0 KiB
Go
158 lines
5.0 KiB
Go
// Copyright 2013 Prometheus Team
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
pb "github.com/prometheus/alertmanager/config/generated"
|
|
|
|
"github.com/prometheus/alertmanager/manager"
|
|
)
|
|
|
|
const minimumRepeatRate = 1 * time.Minute
|
|
|
|
// Config encapsulates the configuration of an Alert Manager instance. It wraps
|
|
// the raw configuration protocol buffer to be able to add custom methods to
|
|
// it.
|
|
type Config struct {
|
|
// The protobuf containing the actual configuration values.
|
|
pb.AlertManagerConfig
|
|
}
|
|
|
|
// String returns an ASCII serialization of the loaded configuration protobuf.
|
|
func (c Config) String() string {
|
|
return proto.MarshalTextString(&c.AlertManagerConfig)
|
|
}
|
|
|
|
// Validate checks an entire parsed Config for the validity of its fields.
|
|
func (c Config) Validate() error {
|
|
ncNames := map[string]bool{}
|
|
for _, nc := range c.NotificationConfig {
|
|
if nc.Name == nil {
|
|
return fmt.Errorf("Missing name in notification config: %s", proto.MarshalTextString(nc))
|
|
}
|
|
for _, pdc := range nc.PagerdutyConfig {
|
|
if pdc.ServiceKey == nil {
|
|
return fmt.Errorf("Missing service key in PagerDuty notification config: %s", proto.MarshalTextString(pdc))
|
|
}
|
|
}
|
|
for _, ec := range nc.EmailConfig {
|
|
if ec.Email == nil {
|
|
return fmt.Errorf("Missing email address in email notification config: %s", proto.MarshalTextString(ec))
|
|
}
|
|
}
|
|
for _, ec := range nc.PushoverConfig {
|
|
if ec.Token == nil {
|
|
return fmt.Errorf("Missing token in Pushover notification config: %s", proto.MarshalTextString(ec))
|
|
}
|
|
if ec.UserKey == nil {
|
|
return fmt.Errorf("Missing user key in Pushover notification config: %s", proto.MarshalTextString(ec))
|
|
}
|
|
}
|
|
for _, hcc := range nc.HipchatConfig {
|
|
if hcc.AuthToken == nil {
|
|
return fmt.Errorf("Missing token in HipChat config: %s", proto.MarshalTextString(hcc))
|
|
}
|
|
if hcc.RoomId == nil {
|
|
return fmt.Errorf("Missing room in HipChat config: %s", proto.MarshalTextString(hcc))
|
|
}
|
|
}
|
|
for _, sc := range nc.SlackConfig {
|
|
if sc.WebhookUrl == nil {
|
|
return fmt.Errorf("Missing webhook URL in Slack config: %s", proto.MarshalTextString(sc))
|
|
}
|
|
}
|
|
for _, fc := range nc.FlowdockConfig {
|
|
if fc.ApiToken == nil {
|
|
return fmt.Errorf("Missing API token in Flowdock config: %s", proto.MarshalTextString(fc))
|
|
}
|
|
if fc.FromAddress == nil {
|
|
return fmt.Errorf("Missing from_address Flowdock config: %s", proto.MarshalTextString(fc))
|
|
}
|
|
}
|
|
for _, snsConfig := range nc.AmazonSnsConfig {
|
|
if snsConfig.TopicArn == nil {
|
|
return fmt.Errorf("Missing Topic ARN in Amazon SNS config: %s", proto.MarshalTextString(snsConfig))
|
|
}
|
|
}
|
|
|
|
if _, ok := ncNames[nc.GetName()]; ok {
|
|
return fmt.Errorf("Notification config name not unique: %s", nc.GetName())
|
|
}
|
|
|
|
ncNames[nc.GetName()] = true
|
|
}
|
|
|
|
for _, a := range c.AggregationRule {
|
|
for _, f := range a.Filter {
|
|
if f.NameRe == nil {
|
|
return fmt.Errorf("Missing name pattern (name_re) in filter definition: %s", proto.MarshalTextString(f))
|
|
}
|
|
if f.ValueRe == nil {
|
|
return fmt.Errorf("Missing value pattern (value_re) in filter definition: %s", proto.MarshalTextString(f))
|
|
}
|
|
}
|
|
|
|
if _, ok := ncNames[a.GetNotificationConfigName()]; !ok {
|
|
return fmt.Errorf("No such notification config: %s", a.GetNotificationConfigName())
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func filtersFromPb(filters []*pb.Filter) manager.Filters {
|
|
fs := make(manager.Filters, 0, len(filters))
|
|
for _, f := range filters {
|
|
fs = append(fs, manager.NewFilter(f.GetNameRe(), f.GetValueRe()))
|
|
}
|
|
return fs
|
|
}
|
|
|
|
// AggregationRules returns all the AggregationRules in a Config object.
|
|
func (c Config) AggregationRules() manager.AggregationRules {
|
|
rules := make(manager.AggregationRules, 0, len(c.AggregationRule))
|
|
for _, r := range c.AggregationRule {
|
|
rate := time.Duration(r.GetRepeatRateSeconds()) * time.Second
|
|
if rate < minimumRepeatRate {
|
|
rate = minimumRepeatRate
|
|
}
|
|
rules = append(rules, &manager.AggregationRule{
|
|
Filters: filtersFromPb(r.Filter),
|
|
RepeatRate: rate,
|
|
NotificationConfigName: r.GetNotificationConfigName(),
|
|
})
|
|
}
|
|
return rules
|
|
}
|
|
|
|
// InhibitRules returns all the InhibitRules in a Config object.
|
|
func (c Config) InhibitRules() manager.InhibitRules {
|
|
rules := make(manager.InhibitRules, 0, len(c.InhibitRule))
|
|
for _, r := range c.InhibitRule {
|
|
sFilters := filtersFromPb(r.SourceFilter)
|
|
tFilters := filtersFromPb(r.TargetFilter)
|
|
rules = append(rules, &manager.InhibitRule{
|
|
SourceFilters: sFilters,
|
|
TargetFilters: tFilters,
|
|
MatchOn: r.MatchOn,
|
|
})
|
|
}
|
|
return rules
|
|
}
|