2021-04-05 17:05:39 -05:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-04-27 15:15:00 -05:00
|
|
|
"encoding/json"
|
2021-04-13 16:38:09 -05:00
|
|
|
"fmt"
|
2021-04-05 17:05:39 -05:00
|
|
|
"net/http"
|
2021-04-13 16:38:09 -05:00
|
|
|
"time"
|
|
|
|
|
2021-04-21 11:30:03 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/eval"
|
2021-04-13 16:38:09 -05:00
|
|
|
apiv1 "github.com/prometheus/client_golang/api/prometheus/v1"
|
|
|
|
|
|
|
|
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
2021-04-05 17:05:39 -05:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-04-19 13:26:04 -05:00
|
|
|
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
2021-04-05 17:05:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/state"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PrometheusSrv struct {
|
2021-04-23 14:32:25 -05:00
|
|
|
log log.Logger
|
|
|
|
manager *state.Manager
|
|
|
|
store store.RuleStore
|
2021-04-05 17:05:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (srv PrometheusSrv) RouteGetAlertStatuses(c *models.ReqContext) response.Response {
|
|
|
|
alertResponse := apimodels.AlertResponse{
|
|
|
|
DiscoveryBase: apimodels.DiscoveryBase{
|
|
|
|
Status: "success",
|
|
|
|
},
|
|
|
|
Data: apimodels.AlertDiscovery{
|
|
|
|
Alerts: []*apimodels.Alert{},
|
|
|
|
},
|
|
|
|
}
|
2021-05-04 11:57:50 -05:00
|
|
|
for _, alertState := range srv.manager.GetAll(c.OrgId) {
|
2021-04-05 17:05:39 -05:00
|
|
|
startsAt := alertState.StartsAt
|
2021-05-18 08:12:39 -05:00
|
|
|
valString := ""
|
|
|
|
if len(alertState.Results) > 0 && alertState.State == eval.Alerting {
|
|
|
|
valString = alertState.Results[0].EvaluationString
|
|
|
|
}
|
2021-04-05 17:05:39 -05:00
|
|
|
alertResponse.Data.Alerts = append(alertResponse.Data.Alerts, &apimodels.Alert{
|
|
|
|
Labels: map[string]string(alertState.Labels),
|
|
|
|
Annotations: map[string]string{}, //TODO: Once annotations are added to the evaluation result, set them here
|
|
|
|
State: alertState.State.String(),
|
|
|
|
ActiveAt: &startsAt,
|
2021-05-18 08:12:39 -05:00
|
|
|
Value: valString,
|
2021-04-05 17:05:39 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return response.JSON(http.StatusOK, alertResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (srv PrometheusSrv) RouteGetRuleStatuses(c *models.ReqContext) response.Response {
|
2021-04-13 16:38:09 -05:00
|
|
|
ruleResponse := apimodels.RuleResponse{
|
|
|
|
DiscoveryBase: apimodels.DiscoveryBase{
|
|
|
|
Status: "success",
|
|
|
|
},
|
2021-04-23 13:47:52 -05:00
|
|
|
Data: apimodels.RuleDiscovery{
|
|
|
|
RuleGroups: []*apimodels.RuleGroup{},
|
|
|
|
},
|
2021-04-13 16:38:09 -05:00
|
|
|
}
|
|
|
|
|
2021-09-14 09:08:04 -05:00
|
|
|
namespaceMap, err := srv.store.GetNamespaces(c.Req.Context(), c.OrgId, c.SignedInUser)
|
2021-07-22 01:53:14 -05:00
|
|
|
if err != nil {
|
|
|
|
return ErrResp(http.StatusInternalServerError, err, "failed to get namespaces visible to the user")
|
|
|
|
}
|
|
|
|
|
|
|
|
namespaceUIDs := make([]string, len(namespaceMap))
|
|
|
|
for k := range namespaceMap {
|
|
|
|
namespaceUIDs = append(namespaceUIDs, k)
|
|
|
|
}
|
|
|
|
|
2021-04-13 16:38:09 -05:00
|
|
|
ruleGroupQuery := ngmodels.ListOrgRuleGroupsQuery{
|
2021-07-22 01:53:14 -05:00
|
|
|
OrgID: c.SignedInUser.OrgId,
|
|
|
|
NamespaceUIDs: namespaceUIDs,
|
2021-04-13 16:38:09 -05:00
|
|
|
}
|
|
|
|
if err := srv.store.GetOrgRuleGroups(&ruleGroupQuery); err != nil {
|
|
|
|
ruleResponse.DiscoveryBase.Status = "error"
|
|
|
|
ruleResponse.DiscoveryBase.Error = fmt.Sprintf("failure getting rule groups: %s", err.Error())
|
|
|
|
ruleResponse.DiscoveryBase.ErrorType = apiv1.ErrServer
|
|
|
|
return response.JSON(http.StatusInternalServerError, ruleResponse)
|
|
|
|
}
|
|
|
|
|
2021-04-14 12:49:26 -05:00
|
|
|
for _, r := range ruleGroupQuery.Result {
|
|
|
|
if len(r) < 3 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
groupId, namespaceUID, namespace := r[0], r[1], r[2]
|
|
|
|
alertRuleQuery := ngmodels.ListRuleGroupAlertRulesQuery{OrgID: c.SignedInUser.OrgId, NamespaceUID: namespaceUID, RuleGroup: groupId}
|
2021-04-13 16:38:09 -05:00
|
|
|
if err := srv.store.GetRuleGroupAlertRules(&alertRuleQuery); err != nil {
|
|
|
|
ruleResponse.DiscoveryBase.Status = "error"
|
|
|
|
ruleResponse.DiscoveryBase.Error = fmt.Sprintf("failure getting rules for group %s: %s", groupId, err.Error())
|
|
|
|
ruleResponse.DiscoveryBase.ErrorType = apiv1.ErrServer
|
|
|
|
return response.JSON(http.StatusInternalServerError, ruleResponse)
|
|
|
|
}
|
|
|
|
|
|
|
|
newGroup := &apimodels.RuleGroup{
|
2021-04-14 12:49:26 -05:00
|
|
|
Name: groupId,
|
|
|
|
// This doesn't make sense in our architecture
|
2021-04-15 15:23:16 -05:00
|
|
|
// so we use this field for passing to the frontend the namespace
|
2021-04-14 12:49:26 -05:00
|
|
|
File: namespace,
|
2021-04-13 16:38:09 -05:00
|
|
|
LastEvaluation: time.Time{},
|
|
|
|
EvaluationTime: 0, // TODO: see if we are able to pass this along with evaluation results
|
|
|
|
}
|
|
|
|
|
2021-04-21 11:30:03 -05:00
|
|
|
for _, rule := range alertRuleQuery.Result {
|
2021-04-27 15:15:00 -05:00
|
|
|
var queryStr string
|
|
|
|
encodedQuery, err := json.Marshal(rule.Data)
|
|
|
|
if err != nil {
|
|
|
|
queryStr = err.Error()
|
|
|
|
} else {
|
|
|
|
queryStr = string(encodedQuery)
|
|
|
|
}
|
2021-04-13 16:38:09 -05:00
|
|
|
alertingRule := apimodels.AlertingRule{
|
|
|
|
State: "inactive",
|
|
|
|
Name: rule.Title,
|
2021-05-04 12:08:12 -05:00
|
|
|
Query: queryStr,
|
2021-04-15 07:54:37 -05:00
|
|
|
Duration: rule.For.Seconds(),
|
2021-04-13 16:38:09 -05:00
|
|
|
Annotations: rule.Annotations,
|
|
|
|
}
|
|
|
|
|
|
|
|
newRule := apimodels.Rule{
|
|
|
|
Name: rule.Title,
|
2021-04-21 11:30:03 -05:00
|
|
|
Labels: rule.Labels,
|
2021-04-23 13:47:52 -05:00
|
|
|
Health: "ok",
|
2021-04-13 16:38:09 -05:00
|
|
|
Type: apiv1.RuleTypeAlerting,
|
2021-04-21 11:30:03 -05:00
|
|
|
LastEvaluation: time.Time{},
|
2021-04-13 16:38:09 -05:00
|
|
|
}
|
2021-04-21 11:30:03 -05:00
|
|
|
|
2021-05-04 11:57:50 -05:00
|
|
|
for _, alertState := range srv.manager.GetStatesForRuleUID(c.OrgId, rule.UID) {
|
2021-04-21 11:30:03 -05:00
|
|
|
activeAt := alertState.StartsAt
|
2021-05-18 08:12:39 -05:00
|
|
|
valString := ""
|
|
|
|
if len(alertState.Results) > 0 && alertState.State == eval.Alerting {
|
|
|
|
valString = alertState.Results[0].EvaluationString
|
|
|
|
}
|
2021-04-13 16:38:09 -05:00
|
|
|
alert := &apimodels.Alert{
|
2021-04-21 11:30:03 -05:00
|
|
|
Labels: map[string]string(alertState.Labels),
|
|
|
|
Annotations: alertState.Annotations,
|
|
|
|
State: alertState.State.String(),
|
2021-04-13 16:38:09 -05:00
|
|
|
ActiveAt: &activeAt,
|
2021-05-18 08:12:39 -05:00
|
|
|
Value: valString, // TODO: set this once it is added to the evaluation results
|
2021-04-13 16:38:09 -05:00
|
|
|
}
|
2021-04-21 11:30:03 -05:00
|
|
|
|
|
|
|
if alertState.LastEvaluationTime.After(newRule.LastEvaluation) {
|
|
|
|
newRule.LastEvaluation = alertState.LastEvaluationTime
|
|
|
|
newGroup.LastEvaluation = alertState.LastEvaluationTime
|
2021-04-13 16:38:09 -05:00
|
|
|
}
|
2021-04-21 11:30:03 -05:00
|
|
|
|
|
|
|
newRule.EvaluationTime = alertState.EvaluationDuration.Seconds()
|
|
|
|
|
|
|
|
switch alertState.State {
|
|
|
|
case eval.Normal:
|
|
|
|
case eval.Pending:
|
2021-04-13 16:38:09 -05:00
|
|
|
if alertingRule.State == "inactive" {
|
|
|
|
alertingRule.State = "pending"
|
|
|
|
}
|
2021-04-21 11:30:03 -05:00
|
|
|
case eval.Alerting:
|
2021-04-13 16:38:09 -05:00
|
|
|
alertingRule.State = "firing"
|
2021-04-21 11:30:03 -05:00
|
|
|
case eval.Error:
|
2021-04-23 13:47:52 -05:00
|
|
|
newRule.Health = "error"
|
2021-04-21 11:30:03 -05:00
|
|
|
case eval.NoData:
|
2021-04-23 13:47:52 -05:00
|
|
|
newRule.Health = "nodata"
|
2021-04-13 16:38:09 -05:00
|
|
|
}
|
2021-05-04 12:08:12 -05:00
|
|
|
|
|
|
|
if alertState.Error != nil {
|
|
|
|
newRule.LastError = alertState.Error.Error()
|
|
|
|
newRule.Health = "error"
|
|
|
|
}
|
2021-04-13 16:38:09 -05:00
|
|
|
alertingRule.Alerts = append(alertingRule.Alerts, alert)
|
|
|
|
}
|
2021-04-21 11:30:03 -05:00
|
|
|
|
2021-04-13 16:38:09 -05:00
|
|
|
alertingRule.Rule = newRule
|
|
|
|
newGroup.Rules = append(newGroup.Rules, alertingRule)
|
|
|
|
newGroup.Interval = float64(rule.IntervalSeconds)
|
|
|
|
}
|
|
|
|
ruleResponse.Data.RuleGroups = append(ruleResponse.Data.RuleGroups, newGroup)
|
|
|
|
}
|
|
|
|
return response.JSON(http.StatusOK, ruleResponse)
|
|
|
|
}
|