2016-06-11 03:13:33 -05:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2016-11-03 01:25:00 -05:00
|
|
|
"fmt"
|
|
|
|
|
2016-06-11 03:13:33 -05:00
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
|
|
|
"github.com/grafana/grafana/pkg/log"
|
|
|
|
m "github.com/grafana/grafana/pkg/models"
|
|
|
|
)
|
|
|
|
|
2016-06-11 03:54:24 -05:00
|
|
|
type DashAlertExtractor struct {
|
2016-06-11 03:13:33 -05:00
|
|
|
Dash *m.Dashboard
|
|
|
|
OrgId int64
|
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
2016-06-11 03:54:24 -05:00
|
|
|
func NewDashAlertExtractor(dash *m.Dashboard, orgId int64) *DashAlertExtractor {
|
|
|
|
return &DashAlertExtractor{
|
2016-06-11 03:13:33 -05:00
|
|
|
Dash: dash,
|
|
|
|
OrgId: orgId,
|
|
|
|
log: log.New("alerting.extractor"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 04:44:06 -05:00
|
|
|
func (e *DashAlertExtractor) lookupDatasourceId(dsName string) (*m.DataSource, error) {
|
2016-06-11 03:13:33 -05:00
|
|
|
if dsName == "" {
|
|
|
|
query := &m.GetDataSourcesQuery{OrgId: e.OrgId}
|
|
|
|
if err := bus.Dispatch(query); err != nil {
|
2016-06-20 04:44:06 -05:00
|
|
|
return nil, err
|
2016-06-11 03:13:33 -05:00
|
|
|
} else {
|
|
|
|
for _, ds := range query.Result {
|
|
|
|
if ds.IsDefault {
|
2016-06-20 04:44:06 -05:00
|
|
|
return ds, nil
|
2016-06-11 03:13:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
query := &m.GetDataSourceByNameQuery{Name: dsName, OrgId: e.OrgId}
|
|
|
|
if err := bus.Dispatch(query); err != nil {
|
2016-06-20 04:44:06 -05:00
|
|
|
return nil, err
|
2016-06-11 03:13:33 -05:00
|
|
|
} else {
|
2016-06-20 04:44:06 -05:00
|
|
|
return query.Result, nil
|
2016-06-11 03:13:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 04:44:06 -05:00
|
|
|
return nil, errors.New("Could not find datasource id for " + dsName)
|
2016-06-11 03:13:33 -05:00
|
|
|
}
|
|
|
|
|
2016-07-19 14:00:41 -05:00
|
|
|
func findPanelQueryByRefId(panel *simplejson.Json, refId string) *simplejson.Json {
|
|
|
|
for _, targetsObj := range panel.Get("targets").MustArray() {
|
|
|
|
target := simplejson.NewFromAny(targetsObj)
|
|
|
|
|
|
|
|
if target.Get("refId").MustString() == refId {
|
|
|
|
return target
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-01-13 08:46:23 -06:00
|
|
|
func copyJson(in *simplejson.Json) (*simplejson.Json, error) {
|
|
|
|
rawJson, err := in.MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return simplejson.NewJson(rawJson)
|
|
|
|
}
|
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
func (e *DashAlertExtractor) GetAlertFromPanels(jsonWithPanels *simplejson.Json) ([]*m.Alert, error) {
|
|
|
|
alerts := make([]*m.Alert, 0)
|
2016-06-11 03:13:33 -05:00
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
for _, panelObj := range jsonWithPanels.Get("panels").MustArray() {
|
|
|
|
panel := simplejson.NewFromAny(panelObj)
|
2018-03-13 16:23:37 -05:00
|
|
|
|
|
|
|
collapsedJson, collapsed := panel.CheckGet("collapsed")
|
|
|
|
// check if the panel is collapsed
|
|
|
|
if collapsed && collapsedJson.MustBool() {
|
|
|
|
|
|
|
|
// extract alerts from sub panels for collapsed panels
|
|
|
|
als, err := e.GetAlertFromPanels(panel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
alerts = append(alerts, als...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
jsonAlert, hasAlert := panel.CheckGet("alert")
|
2016-06-11 03:13:33 -05:00
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
if !hasAlert {
|
|
|
|
continue
|
|
|
|
}
|
2016-06-11 03:13:33 -05:00
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
panelId, err := panel.Get("id").Int64()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("panel id is required. err %v", err)
|
|
|
|
}
|
2016-06-11 03:13:33 -05:00
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
// backward compatibility check, can be removed later
|
|
|
|
enabled, hasEnabled := jsonAlert.CheckGet("enabled")
|
|
|
|
if hasEnabled && enabled.MustBool() == false {
|
|
|
|
continue
|
|
|
|
}
|
2016-06-11 03:13:33 -05:00
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
frequency, err := getTimeDurationStringToSeconds(jsonAlert.Get("frequency").MustString())
|
|
|
|
if err != nil {
|
|
|
|
return nil, ValidationError{Reason: "Could not parse frequency"}
|
|
|
|
}
|
|
|
|
|
|
|
|
alert := &m.Alert{
|
|
|
|
DashboardId: e.Dash.Id,
|
|
|
|
OrgId: e.OrgId,
|
|
|
|
PanelId: panelId,
|
|
|
|
Id: jsonAlert.Get("id").MustInt64(),
|
|
|
|
Name: jsonAlert.Get("name").MustString(),
|
|
|
|
Handler: jsonAlert.Get("handler").MustInt64(),
|
|
|
|
Message: jsonAlert.Get("message").MustString(),
|
|
|
|
Frequency: frequency,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, condition := range jsonAlert.Get("conditions").MustArray() {
|
|
|
|
jsonCondition := simplejson.NewFromAny(condition)
|
|
|
|
|
|
|
|
jsonQuery := jsonCondition.Get("query")
|
|
|
|
queryRefId := jsonQuery.Get("params").MustArray()[0].(string)
|
|
|
|
panelQuery := findPanelQueryByRefId(panel, queryRefId)
|
2017-09-18 03:31:45 -05:00
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
if panelQuery == nil {
|
|
|
|
reason := fmt.Sprintf("Alert on PanelId: %v refers to query(%s) that cannot be found", alert.PanelId, queryRefId)
|
|
|
|
return nil, ValidationError{Reason: reason}
|
2016-06-15 04:39:25 -05:00
|
|
|
}
|
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
dsName := ""
|
|
|
|
if panelQuery.Get("datasource").MustString() != "" {
|
|
|
|
dsName = panelQuery.Get("datasource").MustString()
|
|
|
|
} else if panel.Get("datasource").MustString() != "" {
|
|
|
|
dsName = panel.Get("datasource").MustString()
|
2016-10-11 10:36:30 -05:00
|
|
|
}
|
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
if datasource, err := e.lookupDatasourceId(dsName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
jsonQuery.SetPath([]string{"datasourceId"}, datasource.Id)
|
2016-06-11 03:13:33 -05:00
|
|
|
}
|
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
if interval, err := panel.Get("interval").String(); err == nil {
|
|
|
|
panelQuery.Set("interval", interval)
|
|
|
|
}
|
2016-07-19 14:00:41 -05:00
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
jsonQuery.Set("model", panelQuery.Interface())
|
|
|
|
}
|
2016-07-19 14:00:41 -05:00
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
alert.Settings = jsonAlert
|
2016-07-19 14:00:41 -05:00
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
// validate
|
|
|
|
_, err = NewRuleFromDBAlert(alert)
|
2018-03-07 09:20:05 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if alert.ValidToSave() {
|
2017-12-19 04:19:52 -06:00
|
|
|
alerts = append(alerts, alert)
|
|
|
|
} else {
|
2018-03-07 09:20:05 -06:00
|
|
|
e.log.Debug("Invalid Alert Data. Dashboard, Org or Panel ID is not correct", "alertName", alert.Name, "panelId", alert.PanelId)
|
|
|
|
return nil, m.ErrDashboardContainsInvalidAlertData
|
2017-12-19 04:19:52 -06:00
|
|
|
}
|
|
|
|
}
|
2016-07-19 14:00:41 -05:00
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
return alerts, nil
|
|
|
|
}
|
2016-07-19 14:00:41 -05:00
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
func (e *DashAlertExtractor) GetAlerts() ([]*m.Alert, error) {
|
|
|
|
e.log.Debug("GetAlerts")
|
2016-10-13 02:43:05 -05:00
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
dashboardJson, err := copyJson(e.Dash.Data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-06-11 03:13:33 -05:00
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
alerts := make([]*m.Alert, 0)
|
2016-06-11 03:13:33 -05:00
|
|
|
|
2017-12-19 04:19:52 -06:00
|
|
|
// We extract alerts from rows to be backwards compatible
|
|
|
|
// with the old dashboard json model.
|
|
|
|
rows := dashboardJson.Get("rows").MustArray()
|
|
|
|
if len(rows) > 0 {
|
|
|
|
for _, rowObj := range rows {
|
|
|
|
row := simplejson.NewFromAny(rowObj)
|
|
|
|
a, err := e.GetAlertFromPanels(row)
|
|
|
|
if err != nil {
|
2016-07-21 06:09:12 -05:00
|
|
|
return nil, err
|
2016-06-11 03:13:33 -05:00
|
|
|
}
|
2017-12-19 04:19:52 -06:00
|
|
|
|
|
|
|
alerts = append(alerts, a...)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
a, err := e.GetAlertFromPanels(dashboardJson)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-06-11 03:13:33 -05:00
|
|
|
}
|
2017-12-19 04:19:52 -06:00
|
|
|
|
|
|
|
alerts = append(alerts, a...)
|
2016-06-11 03:13:33 -05:00
|
|
|
}
|
|
|
|
|
2016-06-11 04:54:46 -05:00
|
|
|
e.log.Debug("Extracted alerts from dashboard", "alertCount", len(alerts))
|
2016-06-11 03:54:24 -05:00
|
|
|
return alerts, nil
|
2016-06-11 03:13:33 -05:00
|
|
|
}
|