This commit is contained in:
Ranjib Dey 2016-08-21 01:37:56 -07:00
parent 2a5c92710b
commit 2b67d3b886
2 changed files with 27 additions and 15 deletions

View File

@ -82,17 +82,20 @@ func (p *PD) Write(metrics []telegraf.Metric) error {
return nil
}
func (p *PD) processForEvent(metric telegraf.Metric) error {
func (p *PD) isTripped(metric telegraf.Metric) (bool, error) {
value, ok := metric.Fields()[p.Field]
if !ok {
return fmt.Errorf("Filed '%s' absent", p.Field)
return false, fmt.Errorf("Filed '%s' absent", p.Field)
}
expr := fmt.Sprintf("%v %s", value, p.Expression)
trigger, err := evalBoolExpr(expr)
return evalBoolExpr(expr)
}
func (p *PD) processForEvent(metric telegraf.Metric) error {
trigger, err := p.isTripped(metric)
if err != nil {
return err
}
event := Event{
ServiceKey: p.ServiceKey,
Client: "telegraf",

View File

@ -5,22 +5,31 @@ import (
"testing"
)
func TestMetricMatch(t *testing.T) {
metric := testutil.TestMetric(1.0, "foo")
func TestPDAlert(t *testing.T) {
metric := testutil.TestMetric(2.0, "foo")
var err error
var tripped bool
p := PD{
Metric: "foo",
Field: "value",
Expression: "> 0",
Expression: "> 5",
}
if !p.Match(metric) {
t.Error("Metric did not match for greater than expression")
if !p.isMatch(metric) {
t.Error("Metric should match when name is same")
}
p.Expression = "== 1"
if !p.Match(metric) {
t.Error("Metric did not match for equality expression")
tripped, err = p.isTripped(metric)
if err != nil {
t.Error(err)
}
p.Expression = "< 0"
if p.Match(metric) {
t.Error("Metric did not match for less than expression")
if tripped {
t.Error("Metric should not trigger alert when its expression evaluates to false")
}
p.Expression = "> 1"
tripped, err = p.isTripped(metric)
if err != nil {
t.Error(err)
}
if !tripped {
t.Error("Metric should trigger alert when expression evaluates to true")
}
}