2015-09-25 12:38:07 +00:00
|
|
|
// Copyright 2015 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 provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNotFound = fmt.Errorf("item not found")
|
|
|
|
)
|
|
|
|
|
2015-09-27 17:50:24 +00:00
|
|
|
type MemData struct {
|
|
|
|
mtx sync.RWMutex
|
|
|
|
alerts map[model.Fingerprint]*types.Alert
|
2015-09-27 18:16:42 +00:00
|
|
|
notifies map[string]map[model.Fingerprint]*types.Notify
|
2015-09-27 17:50:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemData() *MemData {
|
|
|
|
return &MemData{
|
|
|
|
alerts: map[model.Fingerprint]*types.Alert{},
|
2015-09-27 18:16:42 +00:00
|
|
|
notifies: map[string]map[model.Fingerprint]*types.Notify{},
|
2015-09-27 17:50:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-26 09:12:47 +00:00
|
|
|
type memAlertIterator struct {
|
|
|
|
ch <-chan *types.Alert
|
|
|
|
close func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ai memAlertIterator) Next() <-chan *types.Alert {
|
|
|
|
return ai.ch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ai memAlertIterator) Err() error { return nil }
|
|
|
|
func (ai memAlertIterator) Close() { ai.close() }
|
|
|
|
|
2015-09-25 12:38:07 +00:00
|
|
|
// MemAlerts implements an Alerts provider based on in-memory data.
|
|
|
|
type MemAlerts struct {
|
2015-09-27 17:50:24 +00:00
|
|
|
data *MemData
|
|
|
|
|
2015-09-25 12:38:07 +00:00
|
|
|
mtx sync.RWMutex
|
|
|
|
listeners []chan *types.Alert
|
|
|
|
}
|
|
|
|
|
2015-09-27 17:50:24 +00:00
|
|
|
func NewMemAlerts(data *MemData) *MemAlerts {
|
2015-09-25 12:38:07 +00:00
|
|
|
return &MemAlerts{
|
2015-09-27 17:50:24 +00:00
|
|
|
data: data,
|
2015-09-25 12:38:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-26 09:12:47 +00:00
|
|
|
func (a *MemAlerts) IterActive() AlertIterator {
|
2015-09-25 12:38:07 +00:00
|
|
|
a.mtx.Lock()
|
|
|
|
defer a.mtx.Unlock()
|
2015-09-27 17:50:24 +00:00
|
|
|
a.data.mtx.Lock()
|
|
|
|
defer a.data.mtx.Unlock()
|
2015-09-25 12:38:07 +00:00
|
|
|
|
2015-09-26 09:12:47 +00:00
|
|
|
var alerts []*types.Alert
|
2015-09-27 17:50:24 +00:00
|
|
|
for _, a := range a.data.alerts {
|
2015-09-26 09:12:47 +00:00
|
|
|
if !a.Resolved() {
|
|
|
|
alerts = append(alerts, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-25 12:38:07 +00:00
|
|
|
ch := make(chan *types.Alert)
|
|
|
|
|
2015-09-26 09:12:47 +00:00
|
|
|
go func() {
|
|
|
|
for _, a := range alerts {
|
|
|
|
ch <- a
|
|
|
|
}
|
|
|
|
}()
|
2015-09-25 12:38:07 +00:00
|
|
|
|
2015-09-26 09:12:47 +00:00
|
|
|
i := len(a.listeners)
|
2015-09-25 12:38:07 +00:00
|
|
|
a.listeners = append(a.listeners, ch)
|
|
|
|
|
2015-09-26 09:12:47 +00:00
|
|
|
return memAlertIterator{
|
|
|
|
ch: ch,
|
|
|
|
close: func() {
|
|
|
|
a.mtx.Lock()
|
|
|
|
a.listeners = append(a.listeners[:i], a.listeners[i+1:]...)
|
|
|
|
close(ch)
|
|
|
|
a.mtx.Unlock()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *MemAlerts) All() ([]*types.Alert, error) {
|
2015-09-27 17:50:24 +00:00
|
|
|
a.data.mtx.RLock()
|
|
|
|
defer a.data.mtx.RUnlock()
|
2015-09-27 10:28:03 +00:00
|
|
|
|
2015-09-26 09:12:47 +00:00
|
|
|
var alerts []*types.Alert
|
2015-09-27 17:50:24 +00:00
|
|
|
for _, a := range a.data.alerts {
|
2015-09-26 09:12:47 +00:00
|
|
|
alerts = append(alerts, a)
|
|
|
|
}
|
|
|
|
return alerts, nil
|
2015-09-25 12:38:07 +00:00
|
|
|
}
|
|
|
|
|
2015-09-26 09:12:47 +00:00
|
|
|
func (a *MemAlerts) Put(alerts ...*types.Alert) error {
|
2015-09-27 10:28:03 +00:00
|
|
|
a.mtx.Lock()
|
|
|
|
defer a.mtx.Unlock()
|
2015-09-27 17:50:24 +00:00
|
|
|
a.data.mtx.Lock()
|
|
|
|
defer a.data.mtx.Unlock()
|
2015-09-25 12:38:07 +00:00
|
|
|
|
2015-09-26 09:12:47 +00:00
|
|
|
for _, alert := range alerts {
|
2015-09-27 17:50:24 +00:00
|
|
|
a.data.alerts[alert.Fingerprint()] = alert
|
2015-09-25 12:38:07 +00:00
|
|
|
|
2015-09-26 09:12:47 +00:00
|
|
|
for _, ch := range a.listeners {
|
|
|
|
ch <- alert
|
|
|
|
}
|
2015-09-25 12:38:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *MemAlerts) Get(fp model.Fingerprint) (*types.Alert, error) {
|
2015-09-27 17:50:24 +00:00
|
|
|
a.data.mtx.RLock()
|
|
|
|
defer a.data.mtx.RUnlock()
|
2015-09-25 12:38:07 +00:00
|
|
|
|
2015-09-27 17:50:24 +00:00
|
|
|
if a, ok := a.data.alerts[fp]; ok {
|
2015-09-25 12:38:07 +00:00
|
|
|
return a, nil
|
|
|
|
}
|
|
|
|
return nil, ErrNotFound
|
|
|
|
}
|
2015-09-27 11:18:13 +00:00
|
|
|
|
2015-09-27 18:16:42 +00:00
|
|
|
type MemNotifies struct {
|
|
|
|
data *MemData
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemNotifies(data *MemData) *MemNotifies {
|
|
|
|
return &MemNotifies{data: data}
|
|
|
|
}
|
2015-09-27 17:50:24 +00:00
|
|
|
|
2015-09-27 18:16:42 +00:00
|
|
|
func (n *MemNotifies) Set(dest string, ns ...*types.Notify) error {
|
|
|
|
n.data.mtx.Lock()
|
|
|
|
defer n.data.mtx.Unlock()
|
2015-09-27 17:50:24 +00:00
|
|
|
|
2015-09-27 18:16:42 +00:00
|
|
|
for _, notify := range ns {
|
|
|
|
am, ok := n.data.notifies[dest]
|
|
|
|
if !ok {
|
|
|
|
am = map[model.Fingerprint]*types.Notify{}
|
|
|
|
n.data.notifies[dest] = am
|
|
|
|
}
|
|
|
|
am[notify.Alert] = notify
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *MemNotifies) Get(dest string, fps ...model.Fingerprint) ([]*types.Notify, error) {
|
|
|
|
n.data.mtx.RLock()
|
|
|
|
defer n.data.mtx.RUnlock()
|
|
|
|
|
|
|
|
ns, ok := n.data.notifies[dest]
|
|
|
|
if !ok {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var res []*types.Notify
|
|
|
|
for _, fp := range fps {
|
|
|
|
res = append(res, ns[fp])
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
2015-09-27 17:50:24 +00:00
|
|
|
|
2015-09-27 11:18:13 +00:00
|
|
|
type MemSilences struct {
|
|
|
|
mtx sync.RWMutex
|
|
|
|
silences map[model.Fingerprint]*types.Silence
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemSilences() *MemSilences {
|
|
|
|
return &MemSilences{
|
|
|
|
silences: map[model.Fingerprint]*types.Silence{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MemSilences) Mutes(lset model.LabelSet) bool {
|
|
|
|
s.mtx.RLock()
|
|
|
|
defer s.mtx.RUnlock()
|
|
|
|
|
|
|
|
for _, sil := range s.silences {
|
|
|
|
if sil.Mutes(lset) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MemSilences) All() ([]*types.Silence, error) {
|
2015-09-27 12:07:04 +00:00
|
|
|
s.mtx.RLock()
|
|
|
|
defer s.mtx.RUnlock()
|
|
|
|
|
|
|
|
var sils []*types.Silence
|
|
|
|
for _, sil := range s.silences {
|
|
|
|
sils = append(sils, sil)
|
|
|
|
}
|
|
|
|
return sils, nil
|
2015-09-27 11:18:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MemSilences) Set(sil *types.Silence) error {
|
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
|
|
|
|
2015-09-27 12:07:04 +00:00
|
|
|
if sil.ID == 0 {
|
|
|
|
sil.ID = model.Fingerprint(len(s.silences) + 1)
|
|
|
|
}
|
|
|
|
|
2015-09-27 11:18:13 +00:00
|
|
|
s.silences[sil.ID] = sil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MemSilences) Del(id model.Fingerprint) error {
|
|
|
|
s.mtx.Lock()
|
|
|
|
defer s.mtx.Unlock()
|
|
|
|
|
|
|
|
delete(s.silences, id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MemSilences) Get(id model.Fingerprint) (*types.Silence, error) {
|
|
|
|
s.mtx.RLock()
|
|
|
|
defer s.mtx.RUnlock()
|
|
|
|
|
|
|
|
sil, ok := s.silences[id]
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrNotFound
|
|
|
|
}
|
|
|
|
return sil, nil
|
|
|
|
}
|