diff options
Diffstat (limited to 'database/sqlite')
-rw-r--r-- | database/sqlite/sqlite_aclk_alert.c | 13 | ||||
-rw-r--r-- | database/sqlite/sqlite_db_migration.c | 17 | ||||
-rw-r--r-- | database/sqlite/sqlite_functions.c | 36 | ||||
-rw-r--r-- | database/sqlite/sqlite_health.c | 275 | ||||
-rw-r--r-- | database/sqlite/sqlite_health.h | 3 |
5 files changed, 287 insertions, 57 deletions
diff --git a/database/sqlite/sqlite_aclk_alert.c b/database/sqlite/sqlite_aclk_alert.c index d57ae043..f10a866f 100644 --- a/database/sqlite/sqlite_aclk_alert.c +++ b/database/sqlite/sqlite_aclk_alert.c @@ -7,7 +7,7 @@ #include "../../aclk/aclk_alarm_api.h" #endif -#define SQL_UPDATE_FILTERED_ALERT "UPDATE aclk_alert_%s SET filtered_alert_unique_id = %u where filtered_alert_unique_id = %u" +#define SQL_UPDATE_FILTERED_ALERT "UPDATE aclk_alert_%s SET filtered_alert_unique_id = %u, date_created = unixepoch() where filtered_alert_unique_id = %u" void update_filtered(ALARM_ENTRY *ae, uint32_t unique_id, char *uuid_str) { char sql[ACLK_SYNC_QUERY_SIZE]; snprintfz(sql, ACLK_SYNC_QUERY_SIZE-1, SQL_UPDATE_FILTERED_ALERT, uuid_str, ae->unique_id, unique_id); @@ -268,7 +268,7 @@ void aclk_push_alert_event(struct aclk_sync_host_config *wc) buffer_sprintf(sql, "select aa.sequence_id, hld.unique_id, hld.alarm_id, hl.config_hash_id, hld.updated_by_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, ha.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, hl.chart_context, hld.transition_id, hld.alarm_event_id " \ + " hld.old_status, hld.delay, hld.new_value, hld.old_value, hld.last_repeat, hl.chart_context, hld.transition_id, hld.alarm_event_id, hl.chart_name " \ " from health_log hl, aclk_alert_%s aa, alert_hash ha, health_log_detail hld " \ " where hld.unique_id = aa.alert_unique_id and hl.config_hash_id = ha.hash_id and aa.date_submitted is null " \ " and hl.host_id = @host_id and hl.health_log_id = hld.health_log_id " \ @@ -371,14 +371,12 @@ void aclk_push_alert_event(struct aclk_sync_host_config *wc) alarm_log.value = (NETDATA_DOUBLE) sqlite3_column_double(res, 23); alarm_log.old_value = (NETDATA_DOUBLE) sqlite3_column_double(res, 24); - alarm_log.updated = (sqlite3_column_int64(res, 8) & HEALTH_ENTRY_FLAG_UPDATED) ? 1 : 0; alarm_log.rendered_info = sqlite3_text_strdupz_empty(res, 18); - alarm_log.chart_context = sqlite3_text_strdupz_empty(res, 26); alarm_log.transition_id = sqlite3_uuid_unparse_strdupz(res, 27); - alarm_log.event_id = (time_t) sqlite3_column_int64(res, 28); + alarm_log.chart_name = sqlite3_text_strdupz_empty(res, 29); aclk_send_alarm_log_entry(&alarm_log); @@ -788,7 +786,7 @@ void health_alarm_entry2proto_nolock(struct alarm_log_entry *alarm_log, ALARM_EN char transition_id[UUID_STR_LEN]; uuid_unparse_lower(ae->transition_id, transition_id); - alarm_log->chart = strdupz(ae_chart_name(ae)); + alarm_log->chart = strdupz(ae_chart_id(ae)); alarm_log->name = strdupz(ae_name(ae)); alarm_log->family = strdupz(ae_family(ae)); @@ -827,6 +825,7 @@ void health_alarm_entry2proto_nolock(struct alarm_log_entry *alarm_log, ALARM_EN alarm_log->updated = (ae->flags & HEALTH_ENTRY_FLAG_UPDATED) ? 1 : 0; alarm_log->rendered_info = strdupz(ae_info(ae)); alarm_log->chart_context = strdupz(ae_chart_context(ae)); + alarm_log->chart_name = strdupz(ae_chart_name(ae)); alarm_log->transition_id = strdupz((char *)transition_id); alarm_log->event_id = (uint64_t) ae->alarm_event_id; @@ -980,7 +979,7 @@ void aclk_push_alert_snapshot_event(char *node_id __maybe_unused) #endif } -#define SQL_DELETE_ALERT_ENTRIES "DELETE FROM aclk_alert_%s WHERE filtered_alert_unique_id + %d < UNIXEPOCH();" +#define SQL_DELETE_ALERT_ENTRIES "DELETE FROM aclk_alert_%s WHERE date_created + %d < UNIXEPOCH();" void sql_aclk_alert_clean_dead_entries(RRDHOST *host) { char uuid_str[UUID_STR_LEN]; diff --git a/database/sqlite/sqlite_db_migration.c b/database/sqlite/sqlite_db_migration.c index 1a6233fc..548b7de0 100644 --- a/database/sqlite/sqlite_db_migration.c +++ b/database/sqlite/sqlite_db_migration.c @@ -83,6 +83,11 @@ const char *database_migrate_v9_v10[] = { NULL }; +const char *database_migrate_v10_v11[] = { + "ALTER TABLE health_log ADD chart_name TEXT;", + NULL +}; + static int do_migration_v1_v2(sqlite3 *database, const char *name) { UNUSED(name); @@ -293,7 +298,6 @@ static int do_migration_v8_v9(sqlite3 *database, const char *name) static int do_migration_v9_v10(sqlite3 *database, const char *name) { - UNUSED(name); netdata_log_info("Running \"%s\" database migration", name); if (table_exists_in_database("alert_hash") && !column_exists_in_table("alert_hash", "chart_labels")) @@ -301,6 +305,16 @@ static int do_migration_v9_v10(sqlite3 *database, const char *name) return 0; } +static int do_migration_v10_v11(sqlite3 *database, const char *name) +{ + netdata_log_info("Running \"%s\" database migration", name); + + if (table_exists_in_database("health_log") && !column_exists_in_table("health_log", "chart_name")) + return init_database_batch(database, DB_CHECK_NONE, 0, &database_migrate_v10_v11[0]); + + return 0; +} + static int do_migration_noop(sqlite3 *database, const char *name) { UNUSED(database); @@ -354,6 +368,7 @@ DATABASE_FUNC_MIGRATION_LIST migration_action[] = { {.name = "v7 to v8", .func = do_migration_v7_v8}, {.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}, // the terminator of this array {.name = NULL, .func = NULL} }; diff --git a/database/sqlite/sqlite_functions.c b/database/sqlite/sqlite_functions.c index 4200c159..d976a3c6 100644 --- a/database/sqlite/sqlite_functions.c +++ b/database/sqlite/sqlite_functions.c @@ -3,7 +3,7 @@ #include "sqlite_functions.h" #include "sqlite_db_migration.h" -#define DB_METADATA_VERSION 10 +#define DB_METADATA_VERSION 11 const char *database_config[] = { "CREATE TABLE IF NOT EXISTS host(host_id BLOB PRIMARY KEY, hostname TEXT NOT NULL, " @@ -34,8 +34,6 @@ const char *database_config[] = { "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);", - "CREATE INDEX IF NOT EXISTS alert_hash_index ON alert_hash (hash_id);", - "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));", @@ -47,7 +45,7 @@ const char *database_config[] = { "CREATE TABLE IF NOT EXISTS health_log (health_log_id INTEGER PRIMARY KEY, host_id blob, alarm_id int, " "config_hash_id blob, name text, chart text, family text, recipient text, units text, exec text, " - "chart_context text, last_transition_id blob, UNIQUE (host_id, alarm_id)) ;", + "chart_context text, last_transition_id blob, chart_name text, UNIQUE (host_id, alarm_id)) ;", "CREATE INDEX IF NOT EXISTS health_log_ind_1 ON health_log (host_id);", @@ -75,6 +73,7 @@ const char *database_cleanup[] = { "DROP INDEX IF EXISTS ind_d1;", "DROP INDEX IF EXISTS ind_c1;", "DROP INDEX IF EXISTS ind_c2;", + "DROP INDEX IF EXISTS alert_hash_index;", NULL }; @@ -593,15 +592,20 @@ static inline void set_host_node_id(RRDHOST *host, uuid_t *node_id) if (unlikely(!node_id)) { freez(host->node_id); - host->node_id = NULL; + __atomic_store_n(&host->node_id, NULL, __ATOMIC_RELAXED); return; } struct aclk_sync_host_config *wc = host->aclk_sync_host_config; - if (unlikely(!host->node_id)) - host->node_id = mallocz(sizeof(*host->node_id)); - uuid_copy(*(host->node_id), *node_id); + if (unlikely(!host->node_id)) { + uuid_t *t = mallocz(sizeof(*host->node_id)); + uuid_copy(*t, *node_id); + __atomic_store_n(&host->node_id, t, __ATOMIC_RELAXED); + } + else { + uuid_copy(*(host->node_id), *node_id); + } if (unlikely(!wc)) sql_create_aclk_table(host, &host->host_uuid, node_id); @@ -617,6 +621,14 @@ int update_node_id(uuid_t *host_id, uuid_t *node_id) RRDHOST *host = NULL; int rc = 2; + char host_guid[GUID_LEN + 1]; + uuid_unparse_lower(*host_id, host_guid); + rrd_wrlock(); + host = rrdhost_find_by_guid(host_guid); + if (likely(host)) + set_host_node_id(host, node_id); + rrd_unlock(); + if (unlikely(!db_meta)) { if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE) error_report("Database has not been initialized"); @@ -646,14 +658,6 @@ int update_node_id(uuid_t *host_id, uuid_t *node_id) error_report("Failed to store node instance information, rc = %d", rc); rc = sqlite3_changes(db_meta); - char host_guid[GUID_LEN + 1]; - uuid_unparse_lower(*host_id, host_guid); - rrd_wrlock(); - host = rrdhost_find_by_guid(host_guid); - if (likely(host)) - set_host_node_id(host, node_id); - rrd_unlock(); - failed: if (unlikely(sqlite3_finalize(res) != SQLITE_OK)) error_report("Failed to finalize the prepared statement when storing node instance information"); diff --git a/database/sqlite/sqlite_health.c b/database/sqlite/sqlite_health.c index 3ecd783d..9c103f09 100644 --- a/database/sqlite/sqlite_health.c +++ b/database/sqlite/sqlite_health.c @@ -83,9 +83,10 @@ failed: Inserts an entry in the table */ #define SQL_INSERT_HEALTH_LOG "INSERT INTO health_log (host_id, alarm_id, " \ - "config_hash_id, name, chart, family, exec, recipient, units, chart_context, last_transition_id) " \ - "VALUES (?,?,?,?,?,?,?,?,?,?,?) " \ - "ON CONFLICT (host_id, alarm_id) DO UPDATE SET last_transition_id = excluded.last_transition_id RETURNING health_log_id; " + "config_hash_id, name, chart, family, exec, recipient, units, chart_context, last_transition_id, chart_name) " \ + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?) " \ + "ON CONFLICT (host_id, alarm_id) DO UPDATE SET last_transition_id = excluded.last_transition_id, " \ + "chart_name = excluded.chart_name RETURNING health_log_id; " #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, " \ @@ -174,6 +175,12 @@ void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae) { goto failed; } + rc = sqlite3_bind_string_or_null(res, ae->chart_name, 12); + if (unlikely(rc != SQLITE_OK)) { + error_report("Failed to bind chart_name parameter for SQL_INSERT_HEALTH_LOG"); + goto failed; + } + rc = sqlite3_step_monitored(res); if (likely(rc == SQLITE_ROW)) health_log_id = (size_t) sqlite3_column_int64(res, 0); @@ -685,7 +692,10 @@ failed: error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event."); } -#define SQL_SELECT_MAX_UNIQUE_ID "SELECT MAX(hld.unique_id) from health_log_detail hld, health_log hl where hl.host_id = @host_id; and hl.health_log_id = hld.health_log_id" +#define SQL_SELECT_MAX_UNIQUE_ID \ + "SELECT MAX(hld.unique_id) FROM health_log_detail hld, health_log hl " \ + "WHERE hl.host_id = @host_id AND hl.health_log_id = hld.health_log_id" + uint32_t sql_get_max_unique_id (RRDHOST *host) { int rc; @@ -766,7 +776,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 " \ + "hld.last_repeat, ah.class, ah.component, ah.type, hl.chart_context, hld.transition_id, hld.global_id, hl.chart_name " \ "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;" void sql_health_alarm_log_load(RRDHOST *host) { @@ -945,6 +955,11 @@ void sql_health_alarm_log_load(RRDHOST *host) { if (sqlite3_column_type(res, 32) != SQLITE_NULL) ae->global_id = sqlite3_column_int64(res, 32); + if (sqlite3_column_type(res, 33) != SQLITE_NULL) + ae->chart_name = string_strdupz((char *) sqlite3_column_text(res, 33)); + else + ae->chart_name = NULL; + char value_string[100 + 1]; string_freez(ae->old_value_string); string_freez(ae->new_value_string); @@ -1203,7 +1218,7 @@ bind_fail: if cloud is disabled or openssl is not available (which will prevent cloud connectivity) skip hash calculations */ -#if !defined DISABLE_CLOUD && defined ENABLE_HTTPS +#if defined ENABLE_HTTPS #define DIGEST_ALERT_CONFIG_VAL(v) ((v) ? EVP_DigestUpdate(evpctx, (string2str(v)), string_strlen((v))) : EVP_DigestUpdate(evpctx, "", 1)) #endif int alert_hash_and_store_config( @@ -1211,7 +1226,7 @@ int alert_hash_and_store_config( struct alert_config *cfg, int store_hash) { -#if !defined DISABLE_CLOUD && defined ENABLE_HTTPS +#if defined ENABLE_HTTPS EVP_MD_CTX *evpctx; unsigned char hash_value[EVP_MAX_MD_SIZE]; unsigned int hash_len; @@ -1717,6 +1732,135 @@ uint32_t sql_get_alarm_id(RRDHOST *host, STRING *chart, STRING *name, uint32_t * return alarm_id; } +#define SQL_UPDATE_ALARM_ID_WITH_CONFIG_HASH "update health_log set config_hash_id = @config_hash_id where host_id = @host_id and alarm_id = @alarm_id and health_log_id = @health_log_id" +void sql_update_alarm_with_config_hash(RRDHOST *host, uint32_t alarm_id, uint64_t health_log_id, uuid_t *config_hash_id) +{ + int rc = 0; + sqlite3_stmt *res = NULL; + + rc = sqlite3_prepare_v2(db_meta, SQL_UPDATE_ALARM_ID_WITH_CONFIG_HASH, -1, &res, 0); + if (rc != SQLITE_OK) { + error_report("Failed to prepare statement when trying to update an alarm id with a config hash."); + return; + } + + rc = sqlite3_bind_blob(res, 1, config_hash_id, sizeof(*config_hash_id), SQLITE_STATIC); + if (unlikely(rc != SQLITE_OK)) { + error_report("Failed to bind config_hash_id parameter for SQL_UPDATE_ALARM_ID_WITH_CONFIG_HASH."); + sqlite3_finalize(res); + return; + } + + rc = sqlite3_bind_blob(res, 2, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC); + if (unlikely(rc != SQLITE_OK)) { + error_report("Failed to bind host_id parameter for SQL_UPDATE_ALARM_ID_WITH_CONFIG_HASH."); + sqlite3_finalize(res); + return; + } + + rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) alarm_id); + if (unlikely(rc != SQLITE_OK)) { + error_report("Failed to bind alarm_id parameter for SQL_GET_ALARM_ID."); + sqlite3_finalize(res); + return; + } + + rc = sqlite3_bind_int64(res, 4, (sqlite3_int64) health_log_id); + if (unlikely(rc != SQLITE_OK)) { + error_report("Failed to bind alarm_id parameter for SQL_GET_ALARM_ID."); + sqlite3_finalize(res); + return; + } + + rc = execute_insert(res); + if (unlikely(rc != SQLITE_DONE)) { + error_report("Failed to execute SQL_UPDATE_ALARM_ID_WITH_CONFIG_HASH, rc = %d", rc); + rc = sqlite3_finalize(res); + if (unlikely(rc != SQLITE_OK)) + error_report("Failed to reset statement to update health log detail table with config hash ids, rc = %d", rc); + return; + } +} + +#define SQL_GET_ALARM_ID_CHECK_ZERO_HASH "select alarm_id, health_log_id from health_log where host_id = @host_id and chart = @chart and name = @name and (config_hash_id is null or config_hash_id = zeroblob(16))" +uint32_t sql_get_alarm_id_check_zero_hash(RRDHOST *host, STRING *chart, STRING *name, uint32_t *next_event_id, uuid_t *config_hash_id) +{ + int rc = 0; + sqlite3_stmt *res = NULL; + uint32_t alarm_id = 0; + uint64_t health_log_id = 0; + + rc = sqlite3_prepare_v2(db_meta, SQL_GET_ALARM_ID_CHECK_ZERO_HASH, -1, &res, 0); + if (rc != SQLITE_OK) { + error_report("Failed to prepare statement when trying to get an alarm id with zero hash"); + return alarm_id; + } + + rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC); + if (unlikely(rc != SQLITE_OK)) { + error_report("Failed to bind host_id parameter for SQL_GET_ALARM_ID_CHECK_ZERO_HASH."); + sqlite3_finalize(res); + return alarm_id; + } + + rc = sqlite3_bind_string_or_null(res, chart, 2); + if (unlikely(rc != SQLITE_OK)) { + error_report("Failed to bind char parameter for SQL_GET_ALARM_ID_CHECK_ZERO_HASH."); + sqlite3_finalize(res); + return alarm_id; + } + + rc = sqlite3_bind_string_or_null(res, name, 3); + if (unlikely(rc != SQLITE_OK)) { + error_report("Failed to bind name parameter for SQL_GET_ALARM_ID_CHECK_ZERO_HASH."); + sqlite3_finalize(res); + return alarm_id; + } + + while (sqlite3_step_monitored(res) == SQLITE_ROW) { + alarm_id = (uint32_t) sqlite3_column_int64(res, 0); + health_log_id = (uint64_t) sqlite3_column_int64(res, 1); + } + + rc = sqlite3_finalize(res); + if (unlikely(rc != SQLITE_OK)) + error_report("Failed to finalize the statement while getting an alarm id."); + + if (alarm_id) { + sql_update_alarm_with_config_hash(host, alarm_id, health_log_id, config_hash_id); + + rc = sqlite3_prepare_v2(db_meta, SQL_GET_EVENT_ID, -1, &res, 0); + if (rc != SQLITE_OK) { + error_report("Failed to prepare statement when trying to get an event id"); + return alarm_id; + } + + rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) health_log_id); + if (unlikely(rc != SQLITE_OK)) { + error_report("Failed to bind host_id parameter for SQL_GET_EVENT_ID."); + sqlite3_finalize(res); + return alarm_id; + } + + rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) alarm_id); + if (unlikely(rc != SQLITE_OK)) { + error_report("Failed to bind char parameter for SQL_GET_EVENT_ID."); + sqlite3_finalize(res); + return alarm_id; + } + + while (sqlite3_step_monitored(res) == SQLITE_ROW) { + *next_event_id = (uint32_t) sqlite3_column_int64(res, 0); + } + + rc = sqlite3_finalize(res); + if (unlikely(rc != SQLITE_OK)) + error_report("Failed to finalize the statement while getting an alarm id."); + } + + return alarm_id; +} + #define SQL_GET_ALARM_ID_FROM_TRANSITION_ID "SELECT hld.alarm_id, hl.host_id, hl.chart_context FROM " \ "health_log_detail hld, health_log hl WHERE hld.transition_id = @transition_id " \ "and hld.health_log_id = hl.health_log_id" @@ -1767,7 +1911,7 @@ fail: #define SQL_POPULATE_TEMP_ALERT_TRANSITION_TABLE "INSERT INTO v_%p (host_id) VALUES (@host_id)" #define SQL_SEARCH_ALERT_TRANSITION_SELECT "SELECT " \ - "h.host_id, h.alarm_id, h.config_hash_id, h.name, h.chart, h.family, h.recipient, h.units, h.exec, " \ + "h.host_id, h.alarm_id, h.config_hash_id, h.name, h.chart, h.chart_name, h.family, h.recipient, h.units, h.exec, " \ "h.chart_context, d.when_key, d.duration, d.non_clear_duration, d.flags, d.delay_up_to_timestamp, " \ "d.info, d.exec_code, d.new_status, d.old_status, d.delay, d.new_value, d.old_value, d.last_repeat, " \ "d.transition_id, d.global_id, ah.class, ah.type, ah.component, d.exec_run_timestamp" @@ -1913,31 +2057,31 @@ run_query:; atd.config_hash_id = (uuid_t *)sqlite3_column_blob(res, 2); atd.alert_name = (const char *) sqlite3_column_text(res, 3); atd.chart = (const char *) sqlite3_column_text(res, 4); - atd.chart_name = (const char *) sqlite3_column_text(res, 4); // FIXME don't copy the id, find the name - atd.family = (const char *) sqlite3_column_text(res, 5); - atd.recipient = (const char *) sqlite3_column_text(res, 6); - atd.units = (const char *) sqlite3_column_text(res, 7); - atd.exec = (const char *) sqlite3_column_text(res, 8); - atd.chart_context = (const char *) sqlite3_column_text(res, 9); - atd.when_key = sqlite3_column_int64(res, 10); - atd.duration = sqlite3_column_int64(res, 11); - atd.non_clear_duration = sqlite3_column_int64(res, 12); - atd.flags = sqlite3_column_int64(res, 13); - atd.delay_up_to_timestamp = sqlite3_column_int64(res, 14); - atd.info = (const char *) sqlite3_column_text(res, 15); - atd.exec_code = sqlite3_column_int(res, 16); - atd.new_status = sqlite3_column_int(res, 17); - atd.old_status = sqlite3_column_int(res, 18); - atd.delay = (int) sqlite3_column_int(res, 19); - atd.new_value = (NETDATA_DOUBLE) sqlite3_column_double(res, 20); - atd.old_value = (NETDATA_DOUBLE) sqlite3_column_double(res, 21); - atd.last_repeat = sqlite3_column_int64(res, 22); - atd.transition_id = (uuid_t *) sqlite3_column_blob(res, 23); - atd.global_id = sqlite3_column_int64(res, 24); - atd.classification = (const char *) sqlite3_column_text(res, 25); - atd.type = (const char *) sqlite3_column_text(res, 26); - atd.component = (const char *) sqlite3_column_text(res, 27); - atd.exec_run_timestamp = sqlite3_column_int64(res, 28); + atd.chart_name = (const char *) sqlite3_column_text(res, 5); + atd.family = (const char *) sqlite3_column_text(res, 6); + atd.recipient = (const char *) sqlite3_column_text(res, 7); + atd.units = (const char *) sqlite3_column_text(res, 8); + atd.exec = (const char *) sqlite3_column_text(res, 9); + atd.chart_context = (const char *) sqlite3_column_text(res, 10); + atd.when_key = sqlite3_column_int64(res, 11); + atd.duration = sqlite3_column_int64(res, 12); + atd.non_clear_duration = sqlite3_column_int64(res, 13); + atd.flags = sqlite3_column_int64(res, 14); + atd.delay_up_to_timestamp = sqlite3_column_int64(res, 15); + atd.info = (const char *) sqlite3_column_text(res, 16); + atd.exec_code = sqlite3_column_int(res, 17); + atd.new_status = sqlite3_column_int(res, 18); + atd.old_status = sqlite3_column_int(res, 19); + atd.delay = (int) sqlite3_column_int(res, 20); + atd.new_value = (NETDATA_DOUBLE) sqlite3_column_double(res, 21); + atd.old_value = (NETDATA_DOUBLE) sqlite3_column_double(res, 22); + atd.last_repeat = sqlite3_column_int64(res, 23); + atd.transition_id = (uuid_t *) sqlite3_column_blob(res, 24); + atd.global_id = sqlite3_column_int64(res, 25); + atd.classification = (const char *) sqlite3_column_text(res, 26); + atd.type = (const char *) sqlite3_column_text(res, 27); + atd.component = (const char *) sqlite3_column_text(res, 28); + atd.exec_run_timestamp = sqlite3_column_int64(res, 29); cb(&atd, data); } @@ -2087,3 +2231,68 @@ fail_only_drop: return added; } +#define SQL_FETCH_CHART_NAME "SELECT chart_name FROM health_log where host_id = @host_id LIMIT 1;" +bool is_chart_name_populated(uuid_t *host_uuid) +{ + sqlite3_stmt *res = NULL; + int rc; + + bool status = true; + + rc = sqlite3_prepare_v2(db_meta, SQL_FETCH_CHART_NAME, -1, &res, 0); + if (unlikely(rc != SQLITE_OK)) { + error_report("Failed to prepare statement to check health_log chart_name"); + return true; + } + + rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC); + if (unlikely(rc != SQLITE_OK)) { + error_report("Failed to bind host_id for health_log chart_name check"); + goto fail; + } + + rc = sqlite3_step_monitored(res); + if (likely(rc == SQLITE_ROW)) + status = sqlite3_column_type(res, 0) != SQLITE_NULL; +fail: + + rc = sqlite3_finalize(res); + if (unlikely(rc != SQLITE_OK)) + error_report("Failed to finalize the prepared statement for health_log chart_name check"); + + return status; +} + +#define SQL_POPULATE_CHART_NAME " UPDATE health_log SET chart_name = upd.chart_name FROM " \ + "(SELECT c.type || '.' || IFNULL(c.name, c.id) AS chart_name, hl.host_id, hl.health_log_id FROM " \ + "chart c, health_log hl WHERE (c.type || '.' || c.id) = hl.chart AND c.host_id = hl.host_id " \ + "AND hl.host_id = @host_id) AS upd WHERE health_log.host_id = upd.host_id " \ + "AND health_log.health_log_id = upd.health_log_id" + +void chart_name_populate(uuid_t *host_uuid) +{ + sqlite3_stmt *res = NULL; + int rc; + + rc = sqlite3_prepare_v2(db_meta, SQL_POPULATE_CHART_NAME, -1, &res, 0); + if (unlikely(rc != SQLITE_OK)) { + error_report("Failed to prepare statement to update health_log chart_name"); + return; + } + + rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC); + if (unlikely(rc != SQLITE_OK)) { + error_report("Failed to bind host_id for health_log chart_name update"); + goto fail; + } + + rc = execute_insert(res); + if (unlikely(rc != SQLITE_DONE)) + error_report("Failed to update chart name in health_log, rc = %d", rc); + +fail: + + rc = sqlite3_finalize(res); + if (unlikely(rc != SQLITE_OK)) + error_report("Failed to finalize the prepared statement for health_log chart_name update"); +} diff --git a/database/sqlite/sqlite_health.h b/database/sqlite/sqlite_health.h index 55e523d2..3aebb94b 100644 --- a/database/sqlite/sqlite_health.h +++ b/database/sqlite/sqlite_health.h @@ -19,6 +19,7 @@ int sql_health_get_last_executed_event(RRDHOST *host, ALARM_ENTRY *ae, RRDCALC_S void sql_health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after, char *chart); int health_migrate_old_health_log_table(char *table); uint32_t sql_get_alarm_id(RRDHOST *host, STRING *chart, STRING *name, uint32_t *next_event_id, uuid_t *config_hash_id); +uint32_t sql_get_alarm_id_check_zero_hash(RRDHOST *host, STRING *chart, STRING *name, uint32_t *next_event_id, uuid_t *config_hash_id); void sql_alert_transitions( DICTIONARY *nodes, time_t after, @@ -37,4 +38,6 @@ int sql_get_alert_configuration( bool debug __maybe_unused); bool sql_find_alert_transition(const char *transition, void (*cb)(const char *machine_guid, const char *context, time_t alert_id, void *data), void *data); +bool is_chart_name_populated(uuid_t *host_uuid); +void chart_name_populate(uuid_t *host_uuid); #endif //NETDATA_SQLITE_HEALTH_H |