alertmanager/config/config.go

99 lines
3.2 KiB
Go
Raw Normal View History

2013-07-26 01:04:53 +00:00
// 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 (
2013-07-26 11:02:51 +00:00
"fmt"
"time"
2013-07-26 01:04:53 +00:00
"code.google.com/p/goprotobuf/proto"
2013-08-05 09:49:56 +00:00
pb "github.com/prometheus/alertmanager/config/generated"
2013-07-26 11:02:51 +00:00
2013-08-05 09:49:56 +00:00
"github.com/prometheus/alertmanager/manager"
2013-07-26 01:04:53 +00:00
)
// 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))
}
}
if _, ok := ncNames[nc.GetName()]; ok {
return fmt.Errorf("Notification config name not unique: %s", nc.GetName())
}
ncNames[nc.GetName()] = true
}
2013-07-26 11:02:51 +00:00
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))
}
}
2013-07-30 12:49:16 +00:00
if _, ok := ncNames[a.GetNotificationConfigName()]; !ok {
return fmt.Errorf("No such notification config: %s", a.GetNotificationConfigName())
}
2013-07-26 11:02:51 +00:00
}
2013-07-26 01:04:53 +00:00
return nil
}
// Rules returns all the AggregationRules in a Config object.
func (c Config) AggregationRules() manager.AggregationRules {
2013-07-26 11:02:51 +00:00
rules := make(manager.AggregationRules, 0, len(c.AggregationRule))
2013-07-26 01:04:53 +00:00
for _, r := range c.AggregationRule {
2013-07-26 11:02:51 +00:00
filters := make(manager.Filters, 0, len(r.Filter))
for _, filter := range r.Filter {
filters = append(filters, manager.NewFilter(filter.GetNameRe(), filter.GetValueRe()))
}
2013-07-26 01:04:53 +00:00
rules = append(rules, &manager.AggregationRule{
2013-07-30 12:49:16 +00:00
Filters: filters,
RepeatRate: time.Duration(r.GetRepeatRateSeconds()) * time.Second,
NotificationConfigName: r.GetNotificationConfigName(),
2013-07-26 11:02:51 +00:00
})
2013-07-26 01:04:53 +00:00
}
return rules
}