summaryrefslogtreecommitdiffstats
path: root/database/sqlite
diff options
context:
space:
mode:
authorEmmanuel Vasilakis <mrzammler@mm.st>2023-09-19 15:35:44 +0300
committerGitHub <noreply@github.com>2023-09-19 15:35:44 +0300
commit9f0fbff5b8d014235b44d4460291d56128855f1d (patch)
treee791cdf9841e0121e50798183fff8c7519f05cbc /database/sqlite
parentf07a55346a888fe4928522b6ae5381cde14667b6 (diff)
Add a summary field to alerts (#15886)
* add a summary field to alerts * add summary field to db * rebase * better migration * rebase * change email notification * revert to silent * use macro * add the summary field to some alerts * add more summary fields * change migration function * add to postgres alerts * add summary to vernemq * more summary fields * more summary fields * fixes * add doc
Diffstat (limited to 'database/sqlite')
-rw-r--r--database/sqlite/sqlite_db_migration.c24
-rw-r--r--database/sqlite/sqlite_functions.c6
-rw-r--r--database/sqlite/sqlite_health.c32
3 files changed, 49 insertions, 13 deletions
diff --git a/database/sqlite/sqlite_db_migration.c b/database/sqlite/sqlite_db_migration.c
index 459b772494..d1cf3a9256 100644
--- a/database/sqlite/sqlite_db_migration.c
+++ b/database/sqlite/sqlite_db_migration.c
@@ -88,6 +88,12 @@ const char *database_migrate_v10_v11[] = {
NULL
};
+const char *database_migrate_v11_v12[] = {
+ "ALTER TABLE health_log_detail ADD summary TEXT;",
+ "ALTER TABLE alert_hash ADD summary TEXT;",
+ NULL
+};
+
static int do_migration_v1_v2(sqlite3 *database, const char *name)
{
UNUSED(name);
@@ -315,6 +321,23 @@ static int do_migration_v10_v11(sqlite3 *database, const char *name)
return 0;
}
+#define MIGR_11_12_UPD_HEALTH_LOG_DETAIL "UPDATE health_log_detail SET summary = (select name from health_log where health_log_id = health_log_detail.health_log_id);"
+static int do_migration_v11_v12(sqlite3 *database, const char *name)
+{
+ int rc = 0;
+
+ netdata_log_info("Running \"%s\" database migration", name);
+
+ if (table_exists_in_database("health_log_detail") && !column_exists_in_table("health_log_detail", "summary") &&
+ table_exists_in_database("alert_hash") && !column_exists_in_table("alert_hash", "summary"))
+ rc = init_database_batch(database, &database_migrate_v11_v12[0]);
+
+ if (!rc)
+ sqlite3_exec_monitored(database, MIGR_11_12_UPD_HEALTH_LOG_DETAIL, 0, 0, NULL);
+
+ return rc;
+}
+
static int do_migration_noop(sqlite3 *database, const char *name)
{
UNUSED(database);
@@ -369,6 +392,7 @@ DATABASE_FUNC_MIGRATION_LIST migration_action[] = {
{.name = "v8 to v9", .func = do_migration_v8_v9},
{.name = "v9 to v10", .func = do_migration_v9_v10},
{.name = "v10 to v11", .func = do_migration_v10_v11},
+ {.name = "v11 to v12", .func = do_migration_v11_v12},
// the terminator of this array
{.name = NULL, .func = NULL}
};
diff --git a/database/sqlite/sqlite_functions.c b/database/sqlite/sqlite_functions.c
index 1b37ca9525..03f876aec0 100644
--- a/database/sqlite/sqlite_functions.c
+++ b/database/sqlite/sqlite_functions.c
@@ -4,7 +4,7 @@
#include "sqlite3recover.h"
#include "sqlite_db_migration.h"
-#define DB_METADATA_VERSION 11
+#define DB_METADATA_VERSION 12
const char *database_config[] = {
"CREATE TABLE IF NOT EXISTS host(host_id BLOB PRIMARY KEY, hostname TEXT NOT NULL, "
@@ -33,7 +33,7 @@ const char *database_config[] = {
"every text, units text, calc text, families text, plugin text, module text, charts text, green text, "
"red text, warn text, crit text, exec text, to_key text, info text, delay text, options text, "
"repeat text, host_labels text, p_db_lookup_dimensions text, p_db_lookup_method text, p_db_lookup_options int, "
- "p_db_lookup_after int, p_db_lookup_before int, p_update_every int, source text, chart_labels text);",
+ "p_db_lookup_after int, p_db_lookup_before int, p_update_every int, source text, chart_labels text, summary text);",
"CREATE TABLE IF NOT EXISTS host_info(host_id blob, system_key text NOT NULL, system_value text NOT NULL, "
"date_created INT, PRIMARY KEY(host_id, system_key));",
@@ -54,7 +54,7 @@ const char *database_config[] = {
"updated_by_id int, updates_id int, when_key int, duration int, non_clear_duration int, "
"flags int, exec_run_timestamp int, delay_up_to_timestamp int, "
"info text, exec_code int, new_status real, old_status real, delay int, "
- "new_value double, old_value double, last_repeat int, transition_id blob, global_id int);",
+ "new_value double, old_value double, last_repeat int, transition_id blob, global_id int, summary text);",
"CREATE INDEX IF NOT EXISTS health_log_d_ind_2 ON health_log_detail (global_id);",
"CREATE INDEX IF NOT EXISTS health_log_d_ind_3 ON health_log_detail (transition_id);",
diff --git a/database/sqlite/sqlite_health.c b/database/sqlite/sqlite_health.c
index 104168144f..c842103835 100644
--- a/database/sqlite/sqlite_health.c
+++ b/database/sqlite/sqlite_health.c
@@ -105,9 +105,8 @@ failed:
#define SQL_INSERT_HEALTH_LOG_DETAIL \
"INSERT INTO health_log_detail (health_log_id, unique_id, alarm_id, alarm_event_id, " \
"updated_by_id, updates_id, when_key, duration, non_clear_duration, flags, exec_run_timestamp, delay_up_to_timestamp, " \
- "info, exec_code, new_status, old_status, delay, new_value, old_value, last_repeat, transition_id, global_id) " \
- "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@global_id); "
-
+ "info, exec_code, new_status, old_status, delay, new_value, old_value, last_repeat, transition_id, global_id, summary) " \
+ "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@global_id,?); "
static void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae) {
sqlite3_stmt *res = NULL;
int rc;
@@ -347,6 +346,12 @@ static void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae) {
goto failed;
}
+ rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->summary, 23);
+ if (unlikely(rc != SQLITE_OK)) {
+ error_report("Failed to bind summary parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
+ goto failed;
+ }
+
rc = execute_insert(res);
if (unlikely(rc != SQLITE_DONE)) {
error_report("HEALTH [%s]: Failed to execute SQL_INSERT_HEALTH_LOG_DETAIL, rc = %d", rrdhost_hostname(host), rc);
@@ -500,9 +505,9 @@ done:
#define SQL_INJECT_REMOVED \
"insert into health_log_detail (health_log_id, unique_id, alarm_id, alarm_event_id, updated_by_id, updates_id, when_key, " \
"duration, non_clear_duration, flags, exec_run_timestamp, delay_up_to_timestamp, info, exec_code, new_status, old_status, " \
- "delay, new_value, old_value, last_repeat, transition_id, global_id) " \
+ "delay, new_value, old_value, last_repeat, transition_id, global_id, summary) " \
"select health_log_id, ?1, ?2, ?3, 0, ?4, unixepoch(), 0, 0, flags, exec_run_timestamp, unixepoch(), info, exec_code, -2, " \
- "new_status, delay, NULL, new_value, 0, ?5, now_usec(0) from health_log_detail where unique_id = ?6 and transition_id = ?7;"
+ "new_status, delay, NULL, new_value, 0, ?5, now_usec(0), summary from health_log_detail where unique_id = ?6 and transition_id = ?7;"
#define SQL_INJECT_REMOVED_UPDATE_DETAIL "update health_log_detail set flags = flags | ?1, updated_by_id = ?2 where unique_id = ?3 and transition_id = ?4;"
@@ -742,7 +747,7 @@ void sql_check_removed_alerts_state(RRDHOST *host)
"hld.updates_id, hld.when_key, hld.duration, hld.non_clear_duration, hld.flags, hld.exec_run_timestamp, " \
"hld.delay_up_to_timestamp, hl.name, hl.chart, hl.family, hl.exec, hl.recipient, ah.source, hl.units, " \
"hld.info, hld.exec_code, hld.new_status, hld.old_status, hld.delay, hld.new_value, hld.old_value, " \
- "hld.last_repeat, ah.class, ah.component, ah.type, hl.chart_context, hld.transition_id, hld.global_id, hl.chart_name " \
+ "hld.last_repeat, ah.class, ah.component, ah.type, hl.chart_context, hld.transition_id, hld.global_id, hl.chart_name, hld.summary " \
"FROM health_log hl, alert_hash ah, health_log_detail hld " \
"WHERE hl.config_hash_id = ah.hash_id and hl.host_id = @host_id and hl.last_transition_id = hld.transition_id;"
@@ -890,6 +895,7 @@ void sql_health_alarm_log_load(RRDHOST *host)
ae->global_id = sqlite3_column_int64(res, 32);
ae->chart_name = SQLITE3_COLUMN_STRINGDUP_OR_NULL(res, 33);
+ ae->summary = SQLITE3_COLUMN_STRINGDUP_OR_NULL(res, 34);
char value_string[100 + 1];
string_freez(ae->old_value_string);
@@ -940,8 +946,8 @@ void sql_health_alarm_log_load(RRDHOST *host)
"on_key, class, component, type, os, hosts, lookup, every, units, calc, families, plugin, module, " \
"charts, green, red, warn, crit, exec, to_key, info, delay, options, repeat, host_labels, " \
"p_db_lookup_dimensions, p_db_lookup_method, p_db_lookup_options, p_db_lookup_after, " \
- "p_db_lookup_before, p_update_every, source, chart_labels) values (?1,unixepoch(),?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12," \
- "?13,?14,?15,?16,?17,?18,?19,?20,?21,?22,?23,?24,?25,?26,?27,?28,?29,?30,?31,?32,?33,?34,?35,?36);"
+ "p_db_lookup_before, p_update_every, source, chart_labels, summary) values (?1,unixepoch(),?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12," \
+ "?13,?14,?15,?16,?17,?18,?19,?20,?21,?22,?23,?24,?25,?26,?27,?28,?29,?30,?31,?32,?33,?34,?35,?36,?37);"
int sql_store_alert_config_hash(uuid_t *hash_id, struct alert_config *cfg)
{
@@ -1129,6 +1135,10 @@ int sql_store_alert_config_hash(uuid_t *hash_id, struct alert_config *cfg)
if (unlikely(rc != SQLITE_OK))
goto bind_fail;
+ rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->summary, ++param);
+ if (unlikely(rc != SQLITE_OK))
+ goto bind_fail;
+
rc = execute_insert(res);
if (unlikely(rc != SQLITE_DONE))
error_report("Failed to store alert config, rc = %d", rc);
@@ -1195,6 +1205,7 @@ int alert_hash_and_store_config(
DIGEST_ALERT_CONFIG_VAL(cfg->repeat);
DIGEST_ALERT_CONFIG_VAL(cfg->host_labels);
DIGEST_ALERT_CONFIG_VAL(cfg->chart_labels);
+ DIGEST_ALERT_CONFIG_VAL(cfg->summary);
EVP_DigestFinal_ex(evpctx, hash_value, &hash_len);
EVP_MD_CTX_destroy(evpctx);
@@ -1275,8 +1286,8 @@ done:
"hld.when_key, hld.duration, hld.non_clear_duration, hld.flags, hld.exec_run_timestamp, " \
"hld.delay_up_to_timestamp, hl.name, hl.chart, hl.family, hl.exec, hl.recipient, ah.source, " \
"hl.units, hld.info, hld.exec_code, hld.new_status, hld.old_status, hld.delay, hld.new_value, hld.old_value, " \
- "hld.last_repeat, ah.class, ah.component, ah.type, hl.chart_context, hld.transition_id FROM health_log hl, " \
- "alert_hash ah, health_log_detail hld WHERE hl.config_hash_id = ah.hash_id and " \
+ "hld.last_repeat, ah.class, ah.component, ah.type, hl.chart_context, hld.transition_id, hld.summary " \
+ "FROM health_log hl, alert_hash ah, health_log_detail hld WHERE hl.config_hash_id = ah.hash_id and " \
"hl.health_log_id = hld.health_log_id and hl.host_id = @host_id "
void sql_health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after, char *chart) {
@@ -1424,6 +1435,7 @@ void sql_health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after, char *
(long unsigned int)sqlite3_column_int64(res, 26),
(sqlite3_column_int64(res, 9) & HEALTH_ENTRY_FLAG_SILENCED)?"true":"false");
+ health_string2json(wb, "\t\t", "summary", (char *) sqlite3_column_text(res, 32), ",\n");
health_string2json(wb, "\t\t", "info", (char *) sqlite3_column_text(res, 19), ",\n");
if(unlikely(sqlite3_column_int64(res, 9) & HEALTH_ENTRY_FLAG_NO_CLEAR_NOTIFICATION)) {