|
|
@@ -1,7 +1,6 @@
|
|
|
package alerting
|
|
|
|
|
|
import (
|
|
|
- "encoding/json"
|
|
|
"fmt"
|
|
|
"github.com/franela/goreq"
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
@@ -14,13 +13,12 @@ import (
|
|
|
|
|
|
type GraphiteExecutor struct{}
|
|
|
|
|
|
-type Series struct {
|
|
|
- Datapoints []DataPoint
|
|
|
+type GraphiteSerie struct {
|
|
|
+ Datapoints [][2]float64
|
|
|
Target string
|
|
|
}
|
|
|
|
|
|
-type Response []Series
|
|
|
-type DataPoint []json.Number
|
|
|
+type GraphiteResponse []GraphiteSerie
|
|
|
|
|
|
func (this *GraphiteExecutor) Execute(rule m.AlertRule, responseQueue chan *AlertResult) {
|
|
|
response, err := this.getSeries(rule)
|
|
|
@@ -32,38 +30,7 @@ func (this *GraphiteExecutor) Execute(rule m.AlertRule, responseQueue chan *Aler
|
|
|
responseQueue <- this.executeRules(response, rule)
|
|
|
}
|
|
|
|
|
|
-func (this *GraphiteExecutor) executeRules(series []Series, rule m.AlertRule) *AlertResult {
|
|
|
- for _, v := range series {
|
|
|
- var avg float64
|
|
|
- var sum float64
|
|
|
- for _, dp := range v.Datapoints {
|
|
|
- i, _ := dp[0].Float64()
|
|
|
- sum += i
|
|
|
- }
|
|
|
-
|
|
|
- avg = sum / float64(len(v.Datapoints))
|
|
|
-
|
|
|
- if float64(rule.CritLevel) < avg {
|
|
|
- return &AlertResult{State: m.AlertStateCritical, Id: rule.Id, ActualValue: avg}
|
|
|
- }
|
|
|
-
|
|
|
- if float64(rule.WarnLevel) < avg {
|
|
|
- return &AlertResult{State: m.AlertStateWarn, Id: rule.Id, ActualValue: avg}
|
|
|
- }
|
|
|
-
|
|
|
- if float64(rule.CritLevel) < sum {
|
|
|
- return &AlertResult{State: m.AlertStateCritical, Id: rule.Id, ActualValue: sum}
|
|
|
- }
|
|
|
-
|
|
|
- if float64(rule.WarnLevel) < sum {
|
|
|
- return &AlertResult{State: m.AlertStateWarn, Id: rule.Id, ActualValue: sum}
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return &AlertResult{State: m.AlertStateOk, Id: rule.Id}
|
|
|
-}
|
|
|
-
|
|
|
-func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (Response, error) {
|
|
|
+func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (GraphiteResponse, error) {
|
|
|
query := &m.GetDataSourceByIdQuery{Id: rule.DatasourceId, OrgId: rule.OrgId}
|
|
|
if err := bus.Dispatch(query); err != nil {
|
|
|
return nil, err
|
|
|
@@ -71,22 +38,19 @@ func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (Response, error) {
|
|
|
|
|
|
v := url.Values{
|
|
|
"format": []string{"json"},
|
|
|
- "target": []string{getTargetFromQuery(rule)},
|
|
|
+ "target": []string{getTargetFromRule(rule)},
|
|
|
+ "until": []string{"now"},
|
|
|
+ "from": []string{"-" + rule.QueryRange},
|
|
|
}
|
|
|
|
|
|
- v.Add("from", "-"+rule.QueryRange)
|
|
|
- v.Add("until", "now")
|
|
|
-
|
|
|
- req := goreq.Request{
|
|
|
+ res, err := goreq.Request{
|
|
|
Method: "POST",
|
|
|
Uri: query.Result.Url + "/render",
|
|
|
Body: v.Encode(),
|
|
|
Timeout: 500 * time.Millisecond,
|
|
|
- }
|
|
|
-
|
|
|
- res, err := req.Do()
|
|
|
+ }.Do()
|
|
|
|
|
|
- response := Response{}
|
|
|
+ response := GraphiteResponse{}
|
|
|
res.Body.FromJsonTo(&response)
|
|
|
|
|
|
if err != nil {
|
|
|
@@ -100,7 +64,7 @@ func (this *GraphiteExecutor) getSeries(rule m.AlertRule) (Response, error) {
|
|
|
return response, nil
|
|
|
}
|
|
|
|
|
|
-func getTargetFromQuery(rule m.AlertRule) string {
|
|
|
+func getTargetFromRule(rule m.AlertRule) string {
|
|
|
json, _ := simplejson.NewJson([]byte(rule.Query))
|
|
|
|
|
|
return json.Get("target").MustString()
|