2021-03-18 13:12:28 -05:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-23 03:31:46 -05:00
|
|
|
"crypto/md5"
|
2021-03-18 13:12:28 -05:00
|
|
|
"fmt"
|
2022-06-27 17:20:21 -05:00
|
|
|
"time"
|
2021-03-18 13:12:28 -05:00
|
|
|
|
2021-09-21 10:01:23 -05:00
|
|
|
"xorm.io/builder"
|
2022-06-27 17:20:21 -05:00
|
|
|
"xorm.io/core"
|
2021-09-21 10:01:23 -05:00
|
|
|
|
2021-03-18 13:12:28 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrNoAlertmanagerConfiguration is an error for when no alertmanager configuration is found.
|
2021-03-24 09:20:44 -05:00
|
|
|
ErrNoAlertmanagerConfiguration = fmt.Errorf("could not find an Alertmanager configuration")
|
2022-03-23 03:31:46 -05:00
|
|
|
// ErrVersionLockedObjectNotFound is returned when an object is not
|
|
|
|
// found using the current hash.
|
|
|
|
ErrVersionLockedObjectNotFound = fmt.Errorf("could not find object using provided id and hash")
|
2022-07-20 09:54:18 -05:00
|
|
|
// ConfigRecordsLimit defines the limit of how many alertmanager configuration versions
|
|
|
|
// should be stored in the database for each organization including the current one.
|
|
|
|
// Has to be > 0
|
2022-08-01 16:48:34 -05:00
|
|
|
ConfigRecordsLimit int = 100
|
2021-03-18 13:12:28 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetLatestAlertmanagerConfiguration returns the lastest version of the alertmanager configuration.
|
|
|
|
// It returns ErrNoAlertmanagerConfiguration if no configuration is found.
|
2022-02-09 03:22:09 -06:00
|
|
|
func (st *DBstore) GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) error {
|
|
|
|
return st.SQLStore.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
2021-05-14 15:13:44 -05:00
|
|
|
c := &models.AlertConfiguration{}
|
|
|
|
// The ID is already an auto incremental column, using the ID as an order should guarantee the latest.
|
2021-08-12 08:04:09 -05:00
|
|
|
ok, err := sess.Desc("id").Where("org_id = ?", query.OrgID).Limit(1).Get(c)
|
2021-03-18 13:12:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-14 15:13:44 -05:00
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return ErrNoAlertmanagerConfiguration
|
|
|
|
}
|
|
|
|
|
2021-03-31 15:00:56 -05:00
|
|
|
query.Result = c
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2021-03-18 13:12:28 -05:00
|
|
|
|
2021-09-21 10:01:23 -05:00
|
|
|
// GetAllLatestAlertmanagerConfiguration returns the latest configuration of every organization
|
|
|
|
func (st *DBstore) GetAllLatestAlertmanagerConfiguration(ctx context.Context) ([]*models.AlertConfiguration, error) {
|
|
|
|
var result []*models.AlertConfiguration
|
|
|
|
err := st.SQLStore.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
|
|
|
condition := builder.In("id", builder.Select("MAX(id)").From("alert_configuration").GroupBy("org_id"))
|
|
|
|
if err := sess.Table("alert_configuration").Where(condition).Find(&result); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2021-03-31 15:00:56 -05:00
|
|
|
// SaveAlertmanagerConfiguration creates an alertmanager configuration.
|
2022-02-09 03:22:09 -06:00
|
|
|
func (st DBstore) SaveAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error {
|
|
|
|
return st.SaveAlertmanagerConfigurationWithCallback(ctx, cmd, func() error { return nil })
|
2021-05-14 13:49:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type SaveCallback func() error
|
|
|
|
|
|
|
|
// SaveAlertmanagerConfigurationWithCallback creates an alertmanager configuration version and then executes a callback.
|
2021-09-21 10:01:23 -05:00
|
|
|
// If the callback results in error it rolls back the transaction.
|
2022-02-09 03:22:09 -06:00
|
|
|
func (st DBstore) SaveAlertmanagerConfigurationWithCallback(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd, callback SaveCallback) error {
|
|
|
|
return st.SQLStore.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
2021-03-31 15:00:56 -05:00
|
|
|
config := models.AlertConfiguration{
|
|
|
|
AlertmanagerConfiguration: cmd.AlertmanagerConfiguration,
|
2022-03-23 03:31:46 -05:00
|
|
|
ConfigurationHash: fmt.Sprintf("%x", md5.Sum([]byte(cmd.AlertmanagerConfiguration))),
|
2021-03-31 15:00:56 -05:00
|
|
|
ConfigurationVersion: cmd.ConfigurationVersion,
|
2021-05-14 13:49:54 -05:00
|
|
|
Default: cmd.Default,
|
2021-08-12 08:04:09 -05:00
|
|
|
OrgID: cmd.OrgID,
|
2021-03-31 15:00:56 -05:00
|
|
|
}
|
|
|
|
if _, err := sess.Insert(config); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-20 09:54:18 -05:00
|
|
|
if _, err := st.deleteOldConfigurations(ctx, cmd.OrgID, ConfigRecordsLimit); err != nil {
|
|
|
|
st.Logger.Warn("failed to delete old am configs", "org", cmd.OrgID, "err", err)
|
|
|
|
}
|
2021-05-14 13:49:54 -05:00
|
|
|
if err := callback(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-31 15:00:56 -05:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2022-03-23 03:31:46 -05:00
|
|
|
|
2022-04-05 16:48:51 -05:00
|
|
|
func (st *DBstore) UpdateAlertmanagerConfiguration(ctx context.Context, cmd *models.SaveAlertmanagerConfigurationCmd) error {
|
|
|
|
return st.SQLStore.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
2022-03-23 03:31:46 -05:00
|
|
|
config := models.AlertConfiguration{
|
|
|
|
AlertmanagerConfiguration: cmd.AlertmanagerConfiguration,
|
|
|
|
ConfigurationHash: fmt.Sprintf("%x", md5.Sum([]byte(cmd.AlertmanagerConfiguration))),
|
|
|
|
ConfigurationVersion: cmd.ConfigurationVersion,
|
|
|
|
Default: cmd.Default,
|
|
|
|
OrgID: cmd.OrgID,
|
2022-06-27 17:20:21 -05:00
|
|
|
CreatedAt: time.Now().Unix(),
|
|
|
|
}
|
2022-10-14 14:33:06 -05:00
|
|
|
res, err := sess.Exec(fmt.Sprintf(getInsertQuery(st.SQLStore.GetDialect().DriverName()), st.SQLStore.GetDialect().Quote("default")),
|
2022-06-27 17:20:21 -05:00
|
|
|
config.AlertmanagerConfiguration,
|
|
|
|
config.ConfigurationHash,
|
|
|
|
config.ConfigurationVersion,
|
|
|
|
config.OrgID,
|
|
|
|
config.CreatedAt,
|
2022-10-14 14:33:06 -05:00
|
|
|
st.SQLStore.GetDialect().BooleanStr(config.Default),
|
2022-06-27 17:20:21 -05:00
|
|
|
cmd.OrgID,
|
|
|
|
cmd.OrgID,
|
|
|
|
cmd.FetchedConfigurationHash,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rows, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-03-23 03:31:46 -05:00
|
|
|
}
|
|
|
|
if rows == 0 {
|
|
|
|
return ErrVersionLockedObjectNotFound
|
|
|
|
}
|
2022-07-20 09:54:18 -05:00
|
|
|
if _, err := st.deleteOldConfigurations(ctx, cmd.OrgID, ConfigRecordsLimit); err != nil {
|
|
|
|
st.Logger.Warn("failed to delete old am configs", "org", cmd.OrgID, "err", err)
|
|
|
|
}
|
2022-03-23 03:31:46 -05:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
2022-06-27 17:20:21 -05:00
|
|
|
|
|
|
|
// getInsertQuery is used to determinate the insert query for the alertmanager config
|
|
|
|
// based on the provided sql driver. This is necesarry as such an advanced query
|
|
|
|
// is not supported by our ORM and we need to generate it manually for each SQL dialect.
|
|
|
|
// We introduced this as part of a bug fix as the old approach wasn't working.
|
|
|
|
// Rel: https://github.com/grafana/grafana/issues/51356
|
|
|
|
func getInsertQuery(driver string) string {
|
|
|
|
switch driver {
|
|
|
|
case core.MYSQL:
|
|
|
|
return `
|
|
|
|
INSERT INTO alert_configuration
|
|
|
|
(alertmanager_configuration, configuration_hash, configuration_version, org_id, created_at, %s)
|
|
|
|
SELECT T.* FROM (SELECT ? AS alertmanager_configuration,? AS configuration_hash,? AS configuration_version,? AS org_id,? AS created_at,? AS 'default') AS T
|
|
|
|
WHERE
|
|
|
|
EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM alert_configuration
|
|
|
|
WHERE
|
|
|
|
org_id = ?
|
|
|
|
AND
|
|
|
|
id = (SELECT MAX(id) FROM alert_configuration WHERE org_id = ?)
|
|
|
|
AND
|
|
|
|
configuration_hash = ?
|
|
|
|
)`
|
|
|
|
case core.POSTGRES:
|
|
|
|
return `
|
|
|
|
INSERT INTO alert_configuration
|
|
|
|
(alertmanager_configuration, configuration_hash, configuration_version, org_id, created_at, %s)
|
|
|
|
SELECT T.* FROM (VALUES($1,$2,$3,$4::bigint,$5::integer,$6::boolean)) AS T
|
|
|
|
WHERE
|
|
|
|
EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM alert_configuration
|
|
|
|
WHERE
|
|
|
|
org_id = $7
|
|
|
|
AND
|
|
|
|
id = (SELECT MAX(id) FROM alert_configuration WHERE org_id = $8::bigint)
|
|
|
|
AND
|
|
|
|
configuration_hash = $9
|
|
|
|
)`
|
|
|
|
case core.SQLITE:
|
|
|
|
return `
|
|
|
|
INSERT INTO alert_configuration
|
|
|
|
(alertmanager_configuration, configuration_hash, configuration_version, org_id, created_at, %s)
|
|
|
|
SELECT T.* FROM (VALUES(?,?,?,?,?,?)) AS T
|
|
|
|
WHERE
|
|
|
|
EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM alert_configuration
|
|
|
|
WHERE
|
|
|
|
org_id = ?
|
|
|
|
AND
|
|
|
|
id = (SELECT MAX(id) FROM alert_configuration WHERE org_id = ?)
|
|
|
|
AND
|
|
|
|
configuration_hash = ?
|
|
|
|
)`
|
|
|
|
default:
|
|
|
|
// SQLite version
|
|
|
|
return `
|
|
|
|
INSERT INTO alert_configuration
|
|
|
|
(alertmanager_configuration, configuration_hash, configuration_version, org_id, created_at, %s)
|
|
|
|
SELECT T.* FROM (VALUES(?,?,?,?,?,?)) AS T
|
|
|
|
WHERE
|
|
|
|
EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM alert_configuration
|
|
|
|
WHERE
|
|
|
|
org_id = ?
|
|
|
|
AND
|
|
|
|
id = (SELECT MAX(id) FROM alert_configuration WHERE org_id = ?)
|
|
|
|
AND
|
|
|
|
configuration_hash = ?
|
|
|
|
)`
|
|
|
|
}
|
|
|
|
}
|
2022-07-20 09:54:18 -05:00
|
|
|
|
2022-08-01 16:48:34 -05:00
|
|
|
func (st *DBstore) deleteOldConfigurations(ctx context.Context, orgID int64, limit int) (int64, error) {
|
2022-07-20 09:54:18 -05:00
|
|
|
if limit < 1 {
|
|
|
|
return 0, fmt.Errorf("failed to delete old configurations: limit is set to '%d' but needs to be > 0", limit)
|
|
|
|
}
|
2022-08-01 16:48:34 -05:00
|
|
|
|
|
|
|
if limit < 1 {
|
|
|
|
limit = ConfigRecordsLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
var affectedRows int64
|
2022-07-20 09:54:18 -05:00
|
|
|
err := st.SQLStore.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
2022-08-01 16:48:34 -05:00
|
|
|
highest := &models.AlertConfiguration{}
|
|
|
|
ok, err := sess.Desc("id").Where("org_id = ?", orgID).OrderBy("id").Limit(1, limit-1).Get(highest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
// No configurations exist. Nothing to clean up.
|
|
|
|
affectedRows = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
threshold := highest.ID - 1
|
|
|
|
if threshold < 1 {
|
|
|
|
// Fewer than `limit` records even exist. Nothing to clean up.
|
|
|
|
affectedRows = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-20 09:54:18 -05:00
|
|
|
res, err := sess.Exec(`
|
|
|
|
DELETE FROM
|
|
|
|
alert_configuration
|
|
|
|
WHERE
|
|
|
|
org_id = ?
|
|
|
|
AND
|
2022-08-01 16:48:34 -05:00
|
|
|
id < ?
|
|
|
|
`, orgID, threshold)
|
2022-07-20 09:54:18 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rows, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-01 16:48:34 -05:00
|
|
|
affectedRows = rows
|
|
|
|
if affectedRows > 0 {
|
|
|
|
st.Logger.Info("deleted old alert_configuration(s)", "org", orgID, "limit", limit, "delete_count", affectedRows)
|
2022-07-20 09:54:18 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-08-01 16:48:34 -05:00
|
|
|
return affectedRows, err
|
2022-07-20 09:54:18 -05:00
|
|
|
}
|