@cryptotaxi247 / netdata-1 / commits / 35ae71754

Misc code cleanup (#15665)

* Cleanup code * Add SQLITE3_COLUMN_STRDUPZ_OR_NULL for readability * Bind unique id properly * Cleanup with is_claimed parameter to decide which cleanup to use Unify cleanup function sql_health_alarm_log_cleanup Add SQLITE3_BIND_STRING_OR_NULL and SQLITE3_COLUMN_STRINGDUP_OR_NULL sql_health_alarm_log_count returns number of rows instead of updating host->health.health_log_entries_written Reformat queries for clarity * Try to fix codacy issue * Try to fix codacy issue -- issue small warning * Change label from fail to done * Drop index on unique_id and health_log_id and create one on both * Update database/sqlite/sqlite_aclk_alert.c Co-authored-by: Emmanuel Vasilakis <mrzammler@gmail.com> * Fix double bind --------- Co-authored-by: Emmanuel Vasilakis <mrzammler@gmail.com>

Stelios Fragkakis committed Aug 22, 2023 at 20:00 UTC 35ae7175420304a6ef151bca223f74ee234868b2
8 files changed +475 -580
database/rrd.h
+2 -11
@@ -716,13 +716,6 @@ STORAGE_ENGINE* storage_engine_find(const char* name);
716
717 #define rrddim_foreach_read(rd, st) \
718 dfe_start_read((st)->rrddim_root_index, rd)
719 -
720 -#define rrddim_foreach_write(rd, st) \
721 - dfe_start_write((st)->rrddim_root_index, rd)
722 -
723 -#define rrddim_foreach_reentrant(rd, st) \
724 - dfe_start_reentrant((st)->rrddim_root_index, rd)
725 -
719 #define rrddim_foreach_done(rd) \
720 dfe_done(rd)
721
@@ -1094,8 +1087,6 @@ struct alarm_entry {
1087 #define ae_chart_context(ae) string2str((ae)->chart_context)
1088 #define ae_family(ae) string2str((ae)->family)
1089 #define ae_classification(ae) string2str((ae)->classification)
1097 -#define ae_component(ae) string2str((ae)->component)
1098 -#define ae_type(ae) string2str((ae)->type)
1090 #define ae_exec(ae) string2str((ae)->exec)
1091 #define ae_recipient(ae) string2str((ae)->recipient)
1092 #define ae_source(ae) string2str((ae)->source)
@@ -1115,13 +1106,13 @@ typedef struct alarm_log {
1106 } ALARM_LOG;
1107
1108 typedef struct health {
1118 - unsigned int health_enabled; // 1 when this host has health enabled
1109 time_t health_delay_up_to; // a timestamp to delay alarms processing up to
1110 STRING *health_default_exec; // the full path of the alarms notifications program
1111 STRING *health_default_recipient; // the default recipient for all alarms
1122 - size_t health_log_entries_written; // the number of alarm events written to the alarms event log
1112 + int health_log_entries_written; // the number of alarm events written to the alarms event log
1113 uint32_t health_default_warn_repeat_every; // the default value for the interval between repeating warning notifications
1114 uint32_t health_default_crit_repeat_every; // the default value for the interval between repeating critical notifications
1115 + unsigned int health_enabled; // 1 when this host has health enabled
1116 } HEALTH;
1117
1118 // ----------------------------------------------------------------------------
database/sqlite/sqlite_aclk_alert.c
+157 -141
@@ -7,43 +7,58 @@
7 #include "../../aclk/aclk_alarm_api.h"
8 #endif
9
10 -#define SQL_UPDATE_FILTERED_ALERT "UPDATE aclk_alert_%s SET filtered_alert_unique_id = %u, date_created = unixepoch() where filtered_alert_unique_id = %u"
11 -void update_filtered(ALARM_ENTRY *ae, uint32_t unique_id, char *uuid_str) {
10 +#define SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param) \
11 + ({ \
12 + int _param = (param); \
13 + sqlite3_column_bytes((res), (_param)) ? strdupz((char *)sqlite3_column_text((res), (_param))) : NULL; \
14 + })
15 +
16 +
17 +#define SQL_UPDATE_FILTERED_ALERT \
18 + "UPDATE aclk_alert_%s SET filtered_alert_unique_id = %u, date_created = unixepoch() where filtered_alert_unique_id = %u"
19 +
20 +static void update_filtered(ALARM_ENTRY *ae, uint32_t unique_id, char *uuid_str)
21 +{
22 char sql[ACLK_SYNC_QUERY_SIZE];
23 snprintfz(sql, ACLK_SYNC_QUERY_SIZE-1, SQL_UPDATE_FILTERED_ALERT, uuid_str, ae->unique_id, unique_id);
24 sqlite3_exec_monitored(db_meta, sql, 0, 0, NULL);
25 ae->flags |= HEALTH_ENTRY_FLAG_ACLK_QUEUED;
26 }
27
18 -#define SQL_SELECT_VARIABLE_ALERT_BY_UNIQUE_ID "SELECT hld.unique_id FROM health_log hl, alert_hash ah, health_log_detail hld WHERE hld.unique_id = %u " \
19 - "AND hl.config_hash_id = ah.hash_id AND hld.health_log_id = hl.health_log_id AND host_id = @host_id " \
20 - "AND ah.warn IS NULL AND ah.crit IS NULL;"
21 -static inline bool is_event_from_alert_variable_config(uint32_t unique_id, uuid_t *host_id) {
28 +#define SQL_SELECT_VARIABLE_ALERT_BY_UNIQUE_ID \
29 + "SELECT hld.unique_id FROM health_log hl, alert_hash ah, health_log_detail hld " \
30 + "WHERE hld.unique_id = @unique_id AND hl.config_hash_id = ah.hash_id AND hld.health_log_id = hl.health_log_id " \
31 + "AND hl.host_id = @host_id AND ah.warn IS NULL AND ah.crit IS NULL"
32 +
33 +static inline bool is_event_from_alert_variable_config(uint32_t unique_id, uuid_t *host_id)
34 +{
35 sqlite3_stmt *res = NULL;
36 int rc = 0;
37 bool ret = false;
38
26 - char sql[ACLK_SYNC_QUERY_SIZE];
27 - snprintfz(sql,ACLK_SYNC_QUERY_SIZE-1, SQL_SELECT_VARIABLE_ALERT_BY_UNIQUE_ID, unique_id);
28 -
29 - rc = sqlite3_prepare_v2(db_meta, sql, -1, &res, 0);
39 + rc = sqlite3_prepare_v2(db_meta, SQL_SELECT_VARIABLE_ALERT_BY_UNIQUE_ID, -1, &res, 0);
40 if (rc != SQLITE_OK) {
41 error_report("Failed to prepare statement when trying to check for alert variables.");
42 return false;
43 }
44
35 - rc = sqlite3_bind_blob(res, 1, host_id, sizeof(*host_id), SQLITE_STATIC);
45 + rc = sqlite3_bind_int(res, 1, (int) unique_id);
46 + if (unlikely(rc != SQLITE_OK)) {
47 + error_report("Failed to bind unique_id for checking alert variable.");
48 + goto fail;
49 + }
50 +
51 + rc = sqlite3_bind_blob(res, 2, host_id, sizeof(*host_id), SQLITE_STATIC);
52 if (unlikely(rc != SQLITE_OK)) {
53 error_report("Failed to bind host_id for checking alert variable.");
38 - sqlite3_finalize(res);
39 - return false;
54 + goto fail;
55 }
56
57 rc = sqlite3_step_monitored(res);
43 - if (likely(rc == SQLITE_ROW)) {
58 + if (likely(rc == SQLITE_ROW))
59 ret = true;
45 - }
60
61 +fail:
62 rc = sqlite3_finalize(res);
63 if (unlikely(rc != SQLITE_OK))
64 error_report("Failed to finalize statement when trying to check for alert variables, rc = %d", rc);
@@ -54,20 +69,22 @@ static inline bool is_event_from_alert_variable_config(uint32_t unique_id, uuid_
69 #define MAX_REMOVED_PERIOD 604800 //a week
70
71 //decide if some events should be sent or not
57 -#define SQL_SELECT_ALERT_BY_ID "SELECT hld.new_status, hl.config_hash_id, hld.unique_id FROM health_log hl, aclk_alert_%s aa, health_log_detail hld " \
58 - "WHERE hld.unique_id = aa.filtered_alert_unique_id " \
59 - "AND hld.alarm_id = %u AND hl.host_id = @host_id AND hl.health_log_id = hld.health_log_id " \
60 - "ORDER BY hld.alarm_event_id DESC LIMIT 1;"
61 -int should_send_to_cloud(RRDHOST *host, ALARM_ENTRY *ae)
72 +#define SQL_SELECT_ALERT_BY_ID \
73 + "SELECT hld.new_status, hl.config_hash_id, hld.unique_id FROM health_log hl, aclk_alert_%s aa, health_log_detail hld " \
74 + "WHERE hl.host_id = @host_id AND hld.unique_id = aa.filtered_alert_unique_id " \
75 + "AND hld.alarm_id = @alarm_id AND hl.health_log_id = hld.health_log_id " \
76 + "ORDER BY hld.alarm_event_id DESC LIMIT 1;"
77 +
78 +static bool should_send_to_cloud(RRDHOST *host, ALARM_ENTRY *ae)
79 {
80 sqlite3_stmt *res = NULL;
81 char uuid_str[UUID_STR_LEN];
82 uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
66 - int send = 1;
83
68 - if (ae->new_status == RRDCALC_STATUS_REMOVED || ae->new_status == RRDCALC_STATUS_UNINITIALIZED) {
84 + bool send = false;
85 +
86 + if (ae->new_status == RRDCALC_STATUS_REMOVED || ae->new_status == RRDCALC_STATUS_UNINITIALIZED)
87 return 0;
70 - }
88
89 if (unlikely(uuid_is_null(ae->config_hash_id)))
90 return 0;
@@ -79,92 +96,80 @@ int should_send_to_cloud(RRDHOST *host, ALARM_ENTRY *ae)
96
97 //get the previous sent event of this alarm_id
98 //base the search on the last filtered event
82 - snprintfz(sql,ACLK_SYNC_QUERY_SIZE-1, SQL_SELECT_ALERT_BY_ID, uuid_str, ae->alarm_id);
99 + snprintfz(sql, ACLK_SYNC_QUERY_SIZE - 1, SQL_SELECT_ALERT_BY_ID, uuid_str);
100
101 int rc = sqlite3_prepare_v2(db_meta, sql, -1, &res, 0);
102 if (rc != SQLITE_OK) {
86 - error_report("Failed to prepare statement when trying to filter alert events.");
87 - send = 1;
88 - return send;
103 + error_report("Failed to prepare statement when trying should_send_to_cloud.");
104 + return true;
105 }
106
107 rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
108 if (unlikely(rc != SQLITE_OK)) {
93 - error_report("Failed to bind host_id for checking alert variable.");
94 - sqlite3_finalize(res);
95 - return false;
96 - }
97 -
98 - rc = sqlite3_step_monitored(res);
99 - if (likely(rc == SQLITE_ROW)) {
100 - status = (RRDCALC_STATUS) sqlite3_column_int(res, 0);
101 - if (sqlite3_column_type(res, 1) != SQLITE_NULL)
102 - uuid_copy(config_hash_id, *((uuid_t *) sqlite3_column_blob(res, 1)));
103 - unique_id = (uint32_t) sqlite3_column_int64(res, 2);
104 - } else {
105 - send = 1;
109 + error_report("Failed to bind host_id for checking should_send_to_cloud");
110 goto done;
111 }
112
109 - if (ae->new_status != (RRDCALC_STATUS)status) {
110 - send = 1;
113 + rc = sqlite3_bind_int(res, 2, (int) ae->alarm_id);
114 + if (unlikely(rc != SQLITE_OK)) {
115 + error_report("Failed to bind alarm_id for checking should_send_to_cloud");
116 goto done;
117 }
118
114 - if (uuid_memcmp(&ae->config_hash_id, &config_hash_id)) {
115 - send = 1;
116 - goto done;
117 - }
119 + rc = sqlite3_step_monitored(res);
120 +
121 + if (likely(rc == SQLITE_ROW)) {
122 + status = (RRDCALC_STATUS)sqlite3_column_int(res, 0);
123 +
124 + if (sqlite3_column_type(res, 1) != SQLITE_NULL)
125 + uuid_copy(config_hash_id, *((uuid_t *)sqlite3_column_blob(res, 1)));
126
119 - //same status, same config
120 - send = 0;
121 - update_filtered(ae, unique_id, uuid_str);
127 + unique_id = (uint32_t)sqlite3_column_int64(res, 2);
128 +
129 + if (ae->new_status != (RRDCALC_STATUS)status || uuid_memcmp(&ae->config_hash_id, &config_hash_id))
130 + send = true;
131 + else
132 + update_filtered(ae, unique_id, uuid_str);
133 + } else
134 + send = true;
135
136 done:
137 rc = sqlite3_finalize(res);
138 if (unlikely(rc != SQLITE_OK))
126 - error_report("Failed to finalize statement when trying to filter alert events, rc = %d", rc);
139 + error_report("Failed to finalize statement when trying should_send_to_cloud, rc = %d", rc);
140
141 return send;
142 }
143
131 -#define SQL_QUEUE_ALERT_TO_CLOUD "INSERT INTO aclk_alert_%s (alert_unique_id, date_created, filtered_alert_unique_id) " \
132 - "VALUES (@alert_unique_id, unixepoch(), @alert_unique_id) ON CONFLICT (alert_unique_id) do nothing;"
133 -int sql_queue_alarm_to_aclk(RRDHOST *host, ALARM_ENTRY *ae, int skip_filter)
134 -{
135 - if(!service_running(SERVICE_ACLK))
136 - return 0;
137 -
138 - if (!claimed())
139 - return 0;
144 +#define SQL_QUEUE_ALERT_TO_CLOUD \
145 + "INSERT INTO aclk_alert_%s (alert_unique_id, date_created, filtered_alert_unique_id) " \
146 + "VALUES (@alert_unique_id, UNIXEPOCH(), @alert_unique_id) ON CONFLICT (alert_unique_id) DO NOTHING;"
147
141 - if (ae->flags & HEALTH_ENTRY_FLAG_ACLK_QUEUED) {
142 - return 0;
143 - }
148 +void sql_queue_alarm_to_aclk(RRDHOST *host, ALARM_ENTRY *ae, bool skip_filter)
149 +{
150 + sqlite3_stmt *res_alert = NULL;
151 + char sql[ACLK_SYNC_QUERY_SIZE];
152 + char uuid_str[UUID_STR_LEN];
153
145 - CHECK_SQLITE_CONNECTION(db_meta);
154 + if (!service_running(SERVICE_ACLK))
155 + return;
156
147 - if (!skip_filter) {
148 - if (!should_send_to_cloud(host, ae)) {
149 - return 0;
150 - }
151 - }
157 + if (!claimed() || ae->flags & HEALTH_ENTRY_FLAG_ACLK_QUEUED)
158 + return;
159
153 - char uuid_str[UUID_STR_LEN];
154 - uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
160 + if (false == skip_filter && !should_send_to_cloud(host, ae))
161 + return;
162
163 if (is_event_from_alert_variable_config(ae->unique_id, &host->host_uuid))
157 - return 0;
158 -
159 - sqlite3_stmt *res_alert = NULL;
160 - char sql[ACLK_SYNC_QUERY_SIZE];
164 + return;
165
166 + uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
167 snprintfz(sql, ACLK_SYNC_QUERY_SIZE - 1, SQL_QUEUE_ALERT_TO_CLOUD, uuid_str);
168
169 int rc = sqlite3_prepare_v2(db_meta, sql, -1, &res_alert, 0);
170 if (unlikely(rc != SQLITE_OK)) {
171 error_report("Failed to prepare statement to store alert event");
167 - return 1;
172 + return;
173 }
174
175 rc = sqlite3_bind_int(res_alert, 1, (int) ae->unique_id);
@@ -172,19 +177,15 @@ int sql_queue_alarm_to_aclk(RRDHOST *host, ALARM_ENTRY *ae, int skip_filter)
177 goto bind_fail;
178
179 rc = execute_insert(res_alert);
175 - if (unlikely(rc != SQLITE_DONE)) {
180 + if (unlikely(rc == SQLITE_DONE)) {
181 + ae->flags |= HEALTH_ENTRY_FLAG_ACLK_QUEUED;
182 + rrdhost_flag_set(host, RRDHOST_FLAG_ACLK_STREAM_ALERTS);
183 + } else
184 error_report("Failed to store alert event %u, rc = %d", ae->unique_id, rc);
177 - goto bind_fail;
178 - }
179 -
180 - ae->flags |= HEALTH_ENTRY_FLAG_ACLK_QUEUED;
181 - rrdhost_flag_set(host, RRDHOST_FLAG_ACLK_STREAM_ALERTS);
185
186 bind_fail:
187 if (unlikely(sqlite3_finalize(res_alert) != SQLITE_OK))
188 error_report("Failed to reset statement in store alert event, rc = %d", rc);
186 -
187 - return 0;
189 }
190
191 int rrdcalc_status_to_proto_enum(RRDCALC_STATUS status)
@@ -246,7 +247,10 @@ void aclk_push_alert_event(struct aclk_sync_host_config *wc)
247 int rc;
248
249 if (unlikely(!wc->alert_updates)) {
249 - netdata_log_access("ACLK STA [%s (%s)]: Ignoring alert push event, updates have been turned off for this node.", wc->node_id, wc->host ? rrdhost_hostname(wc->host) : "N/A");
250 + netdata_log_access(
251 + "ACLK STA [%s (%s)]: Ignoring alert push event, updates have been turned off for this node.",
252 + wc->node_id,
253 + wc->host ? rrdhost_hostname(wc->host) : "N/A");
254 return;
255 }
256
@@ -265,23 +269,30 @@ void aclk_push_alert_event(struct aclk_sync_host_config *wc)
269
270 sqlite3_stmt *res = NULL;
271
268 - buffer_sprintf(sql, "select aa.sequence_id, hld.unique_id, hld.alarm_id, hl.config_hash_id, hld.updated_by_id, hld.when_key, " \
269 - " hld.duration, hld.non_clear_duration, hld.flags, hld.exec_run_timestamp, hld.delay_up_to_timestamp, hl.name, " \
270 - " hl.chart, hl.family, hl.exec, hl.recipient, ha.source, hl.units, hld.info, hld.exec_code, hld.new_status, " \
271 - " 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 " \
272 - " from health_log hl, aclk_alert_%s aa, alert_hash ha, health_log_detail hld " \
273 - " where hld.unique_id = aa.alert_unique_id and hl.config_hash_id = ha.hash_id and aa.date_submitted is null " \
274 - " and hl.host_id = @host_id and hl.health_log_id = hld.health_log_id " \
275 - " order by aa.sequence_id asc limit %d;", wc->uuid_str, limit);
272 + buffer_sprintf(
273 + sql,
274 + "select aa.sequence_id, hld.unique_id, hld.alarm_id, hl.config_hash_id, hld.updated_by_id, hld.when_key, "
275 + " hld.duration, hld.non_clear_duration, hld.flags, hld.exec_run_timestamp, hld.delay_up_to_timestamp, hl.name, "
276 + " hl.chart, hl.family, hl.exec, hl.recipient, ha.source, hl.units, hld.info, hld.exec_code, hld.new_status, "
277 + " hld.old_status, hld.delay, hld.new_value, hld.old_value, hld.last_repeat, hl.chart_context, hld.transition_id, "
278 + "hld.alarm_event_id, hl.chart_name "
279 + " from health_log hl, aclk_alert_%s aa, alert_hash ha, health_log_detail hld "
280 + " where hld.unique_id = aa.alert_unique_id and hl.config_hash_id = ha.hash_id and aa.date_submitted is null "
281 + " and hl.host_id = @host_id and hl.health_log_id = hld.health_log_id "
282 + " order by aa.sequence_id asc limit %d;",
283 + wc->uuid_str,
284 + limit);
285
286 rc = sqlite3_prepare_v2(db_meta, buffer_tostring(sql), -1, &res, 0);
287 if (rc != SQLITE_OK) {
288
289 BUFFER *sql_fix = buffer_create(1024, &netdata_buffers_statistics.buffers_sqlite);
290 buffer_sprintf(sql_fix, TABLE_ACLK_ALERT, wc->uuid_str);
291 +
292 rc = db_execute(db_meta, buffer_tostring(sql_fix));
293 if (unlikely(rc))
294 error_report("Failed to create ACLK alert table for host %s", rrdhost_hostname(wc->host));
295 +
296 else {
297 buffer_flush(sql_fix);
298 buffer_sprintf(sql_fix, INDEX_ACLK_ALERT, wc->uuid_str, wc->uuid_str);
@@ -431,10 +442,10 @@ void aclk_push_alert_events_for_all_hosts(void)
442 RRDHOST *host;
443
444 dfe_start_reentrant(rrdhost_root_index, host) {
434 - if (rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED) || !rrdhost_flag_check(host, RRDHOST_FLAG_ACLK_STREAM_ALERTS))
445 + if (rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED) ||
446 + !rrdhost_flag_check(host, RRDHOST_FLAG_ACLK_STREAM_ALERTS))
447 continue;
448
437 - internal_error(true, "ACLK SYNC: Scanning host %s", rrdhost_hostname(host));
449 rrdhost_flag_clear(host, RRDHOST_FLAG_ACLK_STREAM_ALERTS);
450
451 struct aclk_sync_host_config *wc = host->aclk_sync_host_config;
@@ -462,10 +473,13 @@ void sql_queue_existing_alerts_to_aclk(RRDHOST *host)
473 }
474
475 buffer_flush(sql);
465 - buffer_sprintf(sql, "insert into aclk_alert_%s (alert_unique_id, date_created, filtered_alert_unique_id) " \
466 - "select hld.unique_id alert_unique_id, unixepoch(), hld.unique_id alert_unique_id from health_log_detail hld, health_log hl " \
467 - "where hld.new_status <> 0 and hld.new_status <> -2 and hl.health_log_id = hld.health_log_id and hl.config_hash_id is not null " \
468 - "and hld.updated_by_id = 0 and hl.host_id = @host_id order by hld.unique_id asc on conflict (alert_unique_id) do nothing;", uuid_str);
476 + buffer_sprintf(
477 + sql,
478 + "insert into aclk_alert_%s (alert_unique_id, date_created, filtered_alert_unique_id) "
479 + "select hld.unique_id alert_unique_id, unixepoch(), hld.unique_id alert_unique_id from health_log_detail hld, health_log hl "
480 + "where hld.new_status <> 0 and hld.new_status <> -2 and hl.health_log_id = hld.health_log_id and hl.config_hash_id is not null "
481 + "and hld.updated_by_id = 0 and hl.host_id = @host_id order by hld.unique_id asc on conflict (alert_unique_id) do nothing;",
482 + uuid_str);
483
484 rc = sqlite3_prepare_v2(db_meta, buffer_tostring(sql), -1, &res, 0);
485 if (rc != SQLITE_OK) {
@@ -485,9 +499,8 @@ void sql_queue_existing_alerts_to_aclk(RRDHOST *host)
499 }
500
501 rc = execute_insert(res);
488 - if (unlikely(rc != SQLITE_DONE)) {
502 + if (unlikely(rc != SQLITE_DONE))
503 error_report("Failed to queue existing alerts, rc = %d", rc);
490 - }
504
505 rc = sqlite3_finalize(res);
506 if (unlikely(rc != SQLITE_OK))
@@ -509,15 +522,21 @@ void aclk_send_alarm_configuration(char *config_hash)
522 if (unlikely(!wc))
523 return;
524
512 - netdata_log_access("ACLK REQ [%s (%s)]: Request to send alert config %s.", wc->node_id, wc->host ? rrdhost_hostname(wc->host) : "N/A", config_hash);
525 + netdata_log_access(
526 + "ACLK REQ [%s (%s)]: Request to send alert config %s.",
527 + wc->node_id,
528 + wc->host ? rrdhost_hostname(wc->host) : "N/A",
529 + config_hash);
530
531 aclk_push_alert_config(wc->node_id, config_hash);
532 }
533
517 -#define SQL_SELECT_ALERT_CONFIG "SELECT alarm, template, on_key, class, type, component, os, hosts, plugin," \
534 +#define SQL_SELECT_ALERT_CONFIG \
535 + "SELECT alarm, template, on_key, class, type, component, os, hosts, plugin," \
536 "module, charts, families, lookup, every, units, green, red, calc, warn, crit, to_key, exec, delay, repeat, info," \
519 - "options, host_labels, p_db_lookup_dimensions, p_db_lookup_method, p_db_lookup_options, p_db_lookup_after," \
537 + "options, host_labels, p_db_lookup_dimensions, p_db_lookup_method, p_db_lookup_options, p_db_lookup_after," \
538 "p_db_lookup_before, p_update_every, chart_labels FROM alert_hash WHERE hash_id = @hash_id;"
539 +
540 int aclk_push_alert_config_event(char *node_id __maybe_unused, char *config_hash __maybe_unused)
541 {
542 int rc = 0;
@@ -564,37 +583,34 @@ int aclk_push_alert_config_event(char *node_id __maybe_unused, char *config_hash
583
584 if (sqlite3_step_monitored(res) == SQLITE_ROW) {
585
567 - alarm_config.alarm = sqlite3_column_bytes(res, 0) > 0 ? strdupz((char *)sqlite3_column_text(res, 0)) : NULL;
568 - alarm_config.tmpl = sqlite3_column_bytes(res, 1) > 0 ? strdupz((char *)sqlite3_column_text(res, 1)) : NULL;
569 - alarm_config.on_chart = sqlite3_column_bytes(res, 2) > 0 ? strdupz((char *)sqlite3_column_text(res, 2)) : NULL;
570 - alarm_config.classification = sqlite3_column_bytes(res, 3) > 0 ? strdupz((char *)sqlite3_column_text(res, 3)) : NULL;
571 - alarm_config.type = sqlite3_column_bytes(res, 4) > 0 ? strdupz((char *)sqlite3_column_text(res, 4)) : NULL;
572 - alarm_config.component = sqlite3_column_bytes(res, 5) > 0 ? strdupz((char *)sqlite3_column_text(res, 5)) : NULL;
573 -
574 - alarm_config.os = sqlite3_column_bytes(res, 6) > 0 ? strdupz((char *)sqlite3_column_text(res, 6)) : NULL;
575 - alarm_config.hosts = sqlite3_column_bytes(res, 7) > 0 ? strdupz((char *)sqlite3_column_text(res, 7)) : NULL;
576 - alarm_config.plugin = sqlite3_column_bytes(res, 8) > 0 ? strdupz((char *)sqlite3_column_text(res, 8)) : NULL;
577 - alarm_config.module = sqlite3_column_bytes(res, 9) > 0 ? strdupz((char *)sqlite3_column_text(res, 9)) : NULL;
578 - alarm_config.charts = sqlite3_column_bytes(res, 10) > 0 ? strdupz((char *)sqlite3_column_text(res, 10)) : NULL;
579 - alarm_config.families = sqlite3_column_bytes(res, 11) > 0 ? strdupz((char *)sqlite3_column_text(res, 11)) : NULL;
580 - alarm_config.lookup = sqlite3_column_bytes(res, 12) > 0 ? strdupz((char *)sqlite3_column_text(res, 12)) : NULL;
581 - alarm_config.every = sqlite3_column_bytes(res, 13) > 0 ? strdupz((char *)sqlite3_column_text(res, 13)) : NULL;
582 - alarm_config.units = sqlite3_column_bytes(res, 14) > 0 ? strdupz((char *)sqlite3_column_text(res, 14)) : NULL;
583 -
584 - alarm_config.green = sqlite3_column_bytes(res, 15) > 0 ? strdupz((char *)sqlite3_column_text(res, 15)) : NULL;
585 - alarm_config.red = sqlite3_column_bytes(res, 16) > 0 ? strdupz((char *)sqlite3_column_text(res, 16)) : NULL;
586 -
587 - alarm_config.calculation_expr = sqlite3_column_bytes(res, 17) > 0 ? strdupz((char *)sqlite3_column_text(res, 17)) : NULL;
588 - alarm_config.warning_expr = sqlite3_column_bytes(res, 18) > 0 ? strdupz((char *)sqlite3_column_text(res, 18)) : NULL;
589 - alarm_config.critical_expr = sqlite3_column_bytes(res, 19) > 0 ? strdupz((char *)sqlite3_column_text(res, 19)) : NULL;
590 -
591 - alarm_config.recipient = sqlite3_column_bytes(res, 20) > 0 ? strdupz((char *)sqlite3_column_text(res, 20)) : NULL;
592 - alarm_config.exec = sqlite3_column_bytes(res, 21) > 0 ? strdupz((char *)sqlite3_column_text(res, 21)) : NULL;
593 - alarm_config.delay = sqlite3_column_bytes(res, 22) > 0 ? strdupz((char *)sqlite3_column_text(res, 22)) : NULL;
594 - alarm_config.repeat = sqlite3_column_bytes(res, 23) > 0 ? strdupz((char *)sqlite3_column_text(res, 23)) : NULL;
595 - alarm_config.info = sqlite3_column_bytes(res, 24) > 0 ? strdupz((char *)sqlite3_column_text(res, 24)) : NULL;
596 - alarm_config.options = sqlite3_column_bytes(res, 25) > 0 ? strdupz((char *)sqlite3_column_text(res, 25)) : NULL;
597 - alarm_config.host_labels = sqlite3_column_bytes(res, 26) > 0 ? strdupz((char *)sqlite3_column_text(res, 26)) : NULL;
586 + int param = 0;
587 + alarm_config.alarm = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
588 + alarm_config.tmpl = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
589 + alarm_config.on_chart = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
590 + alarm_config.classification = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
591 + alarm_config.type = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
592 + alarm_config.component = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
593 + alarm_config.os = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
594 + alarm_config.hosts = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
595 + alarm_config.plugin = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
596 + alarm_config.module = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
597 + alarm_config.charts = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
598 + alarm_config.families = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
599 + alarm_config.lookup = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
600 + alarm_config.every = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
601 + alarm_config.units = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
602 + alarm_config.green = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
603 + alarm_config.red = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
604 + alarm_config.calculation_expr = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
605 + alarm_config.warning_expr = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
606 + alarm_config.critical_expr = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
607 + alarm_config.recipient = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
608 + alarm_config.exec = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
609 + alarm_config.delay = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
610 + alarm_config.repeat = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
611 + alarm_config.info = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
612 + alarm_config.options = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++);
613 + alarm_config.host_labels = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++); // Current param 26
614
615 alarm_config.p_db_lookup_dimensions = NULL;
616 alarm_config.p_db_lookup_method = NULL;
@@ -604,8 +620,10 @@ int aclk_push_alert_config_event(char *node_id __maybe_unused, char *config_hash
620
621 if (sqlite3_column_bytes(res, 30) > 0) {
622
607 - alarm_config.p_db_lookup_dimensions = sqlite3_column_bytes(res, 27) > 0 ? strdupz((char *)sqlite3_column_text(res, 27)) : NULL;
608 - alarm_config.p_db_lookup_method = sqlite3_column_bytes(res, 28) > 0 ? strdupz((char *)sqlite3_column_text(res, 28)) : NULL;
623 + alarm_config.p_db_lookup_dimensions = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++); // Current param 27
624 + alarm_config.p_db_lookup_method = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, param++); // Current param 28
625 + if (param != 29)
626 + netdata_log_error("aclk_push_alert_config_event: Unexpected param number %d", param);
627
628 BUFFER *tmp_buf = buffer_create(1024, &netdata_buffers_statistics.buffers_sqlite);
629 buffer_data_options2string(tmp_buf, sqlite3_column_int(res, 29));
@@ -618,7 +636,7 @@ int aclk_push_alert_config_event(char *node_id __maybe_unused, char *config_hash
636
637 alarm_config.p_update_every = sqlite3_column_int(res, 32);
638
621 - alarm_config.chart_labels = sqlite3_column_bytes(res, 33) > 0 ? strdupz((char *)sqlite3_column_text(res, 33)) : NULL;
639 + alarm_config.chart_labels = SQLITE3_COLUMN_STRDUPZ_OR_NULL(res, 33);
640
641 p_alarm_config.cfg_hash = strdupz((char *) config_hash);
642 p_alarm_config.cfg = alarm_config;
@@ -648,11 +666,9 @@ bind_fail:
666 // Start streaming alerts
667 void aclk_start_alert_streaming(char *node_id, bool resets)
668 {
651 - if (unlikely(!node_id))
652 - return;
653 -
669 uuid_t node_uuid;
655 - if (uuid_parse(node_id, node_uuid))
670 +
671 + if (unlikely(!node_id || uuid_parse(node_id, node_uuid)))
672 return;
673
674 RRDHOST *host = find_host_by_node_id(node_id);
database/sqlite/sqlite_aclk_alert.h
+1 -3
@@ -15,7 +15,6 @@ struct proto_alert_status {
15 uint64_t last_submitted_sequence_id;
16 };
17
18 -int aclk_add_alert_event(struct aclk_sync_host_config *wc, struct aclk_database_cmd cmd);
18 void aclk_push_alert_event(struct aclk_sync_host_config *wc);
19 void aclk_send_alarm_configuration (char *config_hash);
20 int aclk_push_alert_config_event(char *node_id, char *config_hash);
@@ -28,8 +27,7 @@ void aclk_push_alarm_checkpoint(RRDHOST *host);
27 void aclk_push_alert_snapshot_event(char *node_id);
28 void aclk_process_send_alarm_snapshot(char *node_id, char *claim_id, char *snapshot_uuid);
29 int get_proto_alert_status(RRDHOST *host, struct proto_alert_status *proto_alert_status);
31 -int sql_queue_alarm_to_aclk(RRDHOST *host, ALARM_ENTRY *ae, int skip_filter);
30 +void sql_queue_alarm_to_aclk(RRDHOST *host, ALARM_ENTRY *ae, bool skip_filter);
31 void aclk_push_alert_events_for_all_hosts(void);
32
34 -
33 #endif //NETDATA_SQLITE_ACLK_ALERT_H
database/sqlite/sqlite_functions.c
+3 -2
@@ -55,10 +55,9 @@ const char *database_config[] = {
55 "info text, exec_code int, new_status real, old_status real, delay int, "
56 "new_value double, old_value double, last_repeat int, transition_id blob, global_id int);",
57
58 - "CREATE INDEX IF NOT EXISTS health_log_d_ind_1 ON health_log_detail (unique_id);",
58 "CREATE INDEX IF NOT EXISTS health_log_d_ind_2 ON health_log_detail (global_id);",
59 "CREATE INDEX IF NOT EXISTS health_log_d_ind_3 ON health_log_detail (transition_id);",
61 - "CREATE INDEX IF NOT EXISTS health_log_d_ind_4 ON health_log_detail (health_log_id);",
60 + "CREATE INDEX IF NOT EXISTS health_log_d_ind_5 ON health_log_detail (health_log_id, unique_id DESC);",
61
62 NULL
63 };
@@ -74,6 +73,8 @@ const char *database_cleanup[] = {
73 "DROP INDEX IF EXISTS ind_c1;",
74 "DROP INDEX IF EXISTS ind_c2;",
75 "DROP INDEX IF EXISTS alert_hash_index;",
76 + "DROP INDEX IF EXISTS health_log_d_ind_4;",
77 + "DROP INDEX IF EXISTS health_log_d_ind_1;",
78 NULL
79 };
80
database/sqlite/sqlite_health.c
+306 -411
@@ -5,13 +5,26 @@
5 #include "sqlite_db_migration.h"
6
7 #define MAX_HEALTH_SQL_SIZE 2048
8 -#define sqlite3_bind_string_or_null(res,key,param) ((key) ? sqlite3_bind_text(res, param, string2str(key), -1, SQLITE_STATIC) : sqlite3_bind_null(res, param))
8 +#define SQLITE3_BIND_STRING_OR_NULL(res, key, param) \
9 + ((key) ? sqlite3_bind_text(res, param, string2str(key), -1, SQLITE_STATIC) : sqlite3_bind_null(res, param))
10 +
11 +#define SQLITE3_COLUMN_STRINGDUP_OR_NULL(res, param) \
12 + ({ \
13 + int _param = (param); \
14 + sqlite3_column_type((res), (_param)) != SQLITE_NULL ? \
15 + string_strdupz((char *)sqlite3_column_text((res), (_param))) : \
16 + NULL; \
17 + })
18
19 /* Health related SQL queries
20 Updates an entry in the table
21 */
13 -#define SQL_UPDATE_HEALTH_LOG "UPDATE health_log_detail set updated_by_id = ?, flags = ?, exec_run_timestamp = ?, exec_code = ? where unique_id = ? AND alarm_id = ? and transition_id = ?;"
14 -void sql_health_alarm_log_update(RRDHOST *host, ALARM_ENTRY *ae) {
22 +#define SQL_UPDATE_HEALTH_LOG \
23 + "UPDATE health_log_detail SET updated_by_id = @updated_by, flags = @flags, exec_run_timestamp = @exec_time, " \
24 + "exec_code = @exec_code WHERE unique_id = @unique_id AND alarm_id = @alarm_id AND transition_id = @transaction"
25 +
26 +static void sql_health_alarm_log_update(RRDHOST *host, ALARM_ENTRY *ae)
27 +{
28 sqlite3_stmt *res = NULL;
29 int rc;
30
@@ -82,17 +95,20 @@ failed:
95 /* Health related SQL queries
96 Inserts an entry in the table
97 */
85 -#define SQL_INSERT_HEALTH_LOG "INSERT INTO health_log (host_id, alarm_id, " \
86 - "config_hash_id, name, chart, family, exec, recipient, units, chart_context, last_transition_id, chart_name) " \
87 - "VALUES (?,?,?,?,?,?,?,?,?,?,?,?) " \
88 - "ON CONFLICT (host_id, alarm_id) DO UPDATE SET last_transition_id = excluded.last_transition_id, " \
98 +#define SQL_INSERT_HEALTH_LOG \
99 + "INSERT INTO health_log (host_id, alarm_id, " \
100 + "config_hash_id, name, chart, family, exec, recipient, units, chart_context, last_transition_id, chart_name) " \
101 + "VALUES (?,?,?,?,?,?,?,?,?,?,?,?) " \
102 + "ON CONFLICT (host_id, alarm_id) DO UPDATE SET last_transition_id = excluded.last_transition_id, " \
103 "chart_name = excluded.chart_name RETURNING health_log_id; "
104
91 -#define SQL_INSERT_HEALTH_LOG_DETAIL "INSERT INTO health_log_detail (health_log_id, unique_id, alarm_id, alarm_event_id, " \
105 +#define SQL_INSERT_HEALTH_LOG_DETAIL \
106 + "INSERT INTO health_log_detail (health_log_id, unique_id, alarm_id, alarm_event_id, " \
107 "updated_by_id, updates_id, when_key, duration, non_clear_duration, flags, exec_run_timestamp, delay_up_to_timestamp, " \
93 - "info, exec_code, new_status, old_status, delay, new_value, old_value, last_repeat, transition_id, global_id) " \
108 + "info, exec_code, new_status, old_status, delay, new_value, old_value, last_repeat, transition_id, global_id) " \
109 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,@global_id); "
95 -void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae) {
110 +
111 +static void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae) {
112 sqlite3_stmt *res = NULL;
113 int rc;
114 uint64_t health_log_id = 0;
@@ -127,43 +143,43 @@ void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae) {
143 goto failed;
144 }
145
130 - rc = sqlite3_bind_string_or_null(res, ae->name, 4);
146 + rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->name, 4);
147 if (unlikely(rc != SQLITE_OK)) {
148 error_report("Failed to bind name parameter for SQL_INSERT_HEALTH_LOG");
149 goto failed;
150 }
151
136 - rc = sqlite3_bind_string_or_null(res, ae->chart, 5);
152 + rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->chart, 5);
153 if (unlikely(rc != SQLITE_OK)) {
154 error_report("Failed to bind chart parameter for SQL_INSERT_HEALTH_LOG");
155 goto failed;
156 }
157
142 - rc = sqlite3_bind_string_or_null(res, ae->family, 6);
158 + rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->family, 6);
159 if (unlikely(rc != SQLITE_OK)) {
160 error_report("Failed to bind family parameter for SQL_INSERT_HEALTH_LOG");
161 goto failed;
162 }
163
148 - rc = sqlite3_bind_string_or_null(res, ae->exec, 7);
164 + rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->exec, 7);
165 if (unlikely(rc != SQLITE_OK)) {
166 error_report("Failed to bind exec parameter for SQL_INSERT_HEALTH_LOG");
167 goto failed;
168 }
169
154 - rc = sqlite3_bind_string_or_null(res, ae->recipient, 8);
170 + rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->recipient, 8);
171 if (unlikely(rc != SQLITE_OK)) {
172 error_report("Failed to bind recipient parameter for SQL_INSERT_HEALTH_LOG");
173 goto failed;
174 }
175
160 - rc = sqlite3_bind_string_or_null(res, ae->units, 9);
176 + rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->units, 9);
177 if (unlikely(rc != SQLITE_OK)) {
178 error_report("Failed to bind host_id parameter to store node instance information");
179 goto failed;
180 }
181
166 - rc = sqlite3_bind_string_or_null(res, ae->chart_context, 10);
182 + rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->chart_context, 10);
183 if (unlikely(rc != SQLITE_OK)) {
184 error_report("Failed to bind chart_context parameter for SQL_INSERT_HEALTH_LOG");
185 goto failed;
@@ -175,7 +191,7 @@ void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae) {
191 goto failed;
192 }
193
178 - rc = sqlite3_bind_string_or_null(res, ae->chart_name, 12);
194 + rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->chart_name, 12);
195 if (unlikely(rc != SQLITE_OK)) {
196 error_report("Failed to bind chart_name parameter for SQL_INSERT_HEALTH_LOG");
197 goto failed;
@@ -271,7 +287,7 @@ void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae) {
287 goto failed;
288 }
289
274 - rc = sqlite3_bind_string_or_null(res, ae->info, 13);
290 + rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->info, 13);
291 if (unlikely(rc != SQLITE_OK)) {
292 error_report("Failed to bind info parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
293 goto failed;
@@ -353,7 +369,7 @@ void sql_health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae)
369 sql_health_alarm_log_insert(host, ae);
370 #ifdef ENABLE_ACLK
371 if (netdata_cloud_enabled) {
356 - sql_queue_alarm_to_aclk(host, ae, 0);
372 + sql_queue_alarm_to_aclk(host, ae, false);
373 }
374 #endif
375 }
@@ -362,46 +378,67 @@ void sql_health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae)
378 /* Health related SQL queries
379 Get a count of rows from health log table
380 */
365 -#define SQL_COUNT_HEALTH_LOG_DETAIL "SELECT count(1) FROM health_log_detail hld, health_log hl where hl.host_id = @host_id and hl.health_log_id = hld.health_log_id;"
366 -void sql_health_alarm_log_count(RRDHOST *host) {
381 +#define SQL_COUNT_HEALTH_LOG_DETAIL "SELECT count(1) FROM health_log_detail hld, health_log hl " \
382 + "where hl.host_id = @host_id and hl.health_log_id = hld.health_log_id"
383 +
384 +static int sql_health_alarm_log_count(RRDHOST *host) {
385 sqlite3_stmt *res = NULL;
386 int rc;
387
388 if (unlikely(!db_meta)) {
389 if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
390 error_report("Database has not been initialized");
373 - return;
391 + return -1;
392 }
393
394 + int entries_in_db = -1;
395 +
396 rc = sqlite3_prepare_v2(db_meta, SQL_COUNT_HEALTH_LOG_DETAIL, -1, &res, 0);
397 if (unlikely(rc != SQLITE_OK)) {
398 error_report("Failed to prepare statement to count health log entries from db");
379 - return;
399 + goto done;
400 }
401
402 rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
403 if (unlikely(rc != SQLITE_OK)) {
404 error_report("Failed to bind host_id for SQL_COUNT_HEALTH_LOG.");
385 - sqlite3_finalize(res);
386 - return;
405 + goto done;
406 }
407
408 rc = sqlite3_step_monitored(res);
409 if (likely(rc == SQLITE_ROW))
391 - host->health.health_log_entries_written = (size_t) sqlite3_column_int64(res, 0);
410 + entries_in_db = (int) sqlite3_column_int64(res, 0);
411
412 +done:
413 rc = sqlite3_finalize(res);
414 if (unlikely(rc != SQLITE_OK))
415 error_report("Failed to finalize the prepared statement to count health log entries from db");
416
397 - netdata_log_info("HEALTH [%s]: Table health_log_detail contains %lu entries.", rrdhost_hostname(host), (unsigned long int) host->health.health_log_entries_written);
417 + return entries_in_db;
418 }
419
400 -/* Health related SQL queries
401 - Cleans up the health_log_detail table on a non-claimed host
402 -*/
403 -#define SQL_CLEANUP_HEALTH_LOG_DETAIL_NOT_CLAIMED "DELETE FROM health_log_detail WHERE health_log_id IN (SELECT health_log_id FROM health_log WHERE host_id = ?1) AND when_key + ?2 < unixepoch() AND updated_by_id <> 0 AND transition_id NOT IN (SELECT last_transition_id FROM health_log hl WHERE hl.host_id = ?3);"
404 -void sql_health_alarm_log_cleanup_not_claimed(RRDHOST *host) {
420 +/*
421 + *
422 + * Health related SQL queries
423 + * Cleans up the health_log_detail table on a non-claimed or claimed host
424 + *
425 + */
426 +
427 +#define SQL_CLEANUP_HEALTH_LOG_DETAIL_NOT_CLAIMED "DELETE FROM health_log_detail WHERE health_log_id IN " \
428 + "(SELECT health_log_id FROM health_log WHERE host_id = @host_id) AND when_key + @history < unixepoch() " \
429 + "AND updated_by_id <> 0 AND transition_id NOT IN " \
430 + "(SELECT last_transition_id FROM health_log hl WHERE hl.host_id = @host_id);"
431 +
432 +#define SQL_CLEANUP_HEALTH_LOG_DETAIL_CLAIMED(guid) "DELETE from health_log_detail WHERE unique_id NOT IN " \
433 + "(SELECT filtered_alert_unique_id FROM aclk_alert_%s) " \
434 + "AND unique_id IN (SELECT hld.unique_id FROM health_log hl, health_log_detail hld WHERE " \
435 + "hl.host_id = @host_id AND hl.health_log_id = hld.health_log_id) " \
436 + "AND health_log_id IN (SELECT health_log_id FROM health_log WHERE host_id = @host_id) " \
437 + "AND when_key + @history < unixepoch() " \
438 + "AND updated_by_id <> 0 AND transition_id NOT IN " \
439 + "(SELECT last_transition_id FROM health_log hl WHERE hl.host_id = @host_id);", guid
440 +
441 +void sql_health_alarm_log_cleanup(RRDHOST *host, bool claimed) {
442 sqlite3_stmt *res = NULL;
443 int rc;
444 char command[MAX_HEALTH_SQL_SIZE + 1];
@@ -414,77 +451,18 @@ void sql_health_alarm_log_cleanup_not_claimed(RRDHOST *host) {
451
452 char uuid_str[UUID_STR_LEN];
453 uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
417 -
418 - rc = sqlite3_prepare_v2(db_meta, SQL_CLEANUP_HEALTH_LOG_DETAIL_NOT_CLAIMED, -1, &res, 0);
419 - if (unlikely(rc != SQLITE_OK)) {
420 - error_report("Failed to prepare statement to cleanup health log detail table (un-claimed)");
421 - return;
422 - }
423 -
424 - rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
425 - if (unlikely(rc != SQLITE_OK)) {
426 - error_report("Failed to bind host_id for SQL_CLEANUP_HEALTH_LOG_NOT_CLAIMED.");
427 - sqlite3_finalize(res);
428 - return;
429 - }
430 -
431 - rc = sqlite3_bind_int64(res, 2, (sqlite3_int64)host->health_log.health_log_history);
432 - if (unlikely(rc != SQLITE_OK)) {
433 - error_report("Failed to bind health log history for SQL_CLEANUP_HEALTH_LOG_NOT_CLAIMED.");
434 - sqlite3_finalize(res);
435 - return;
436 - }
437 -
438 - rc = sqlite3_bind_blob(res, 3, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
439 - if (unlikely(rc != SQLITE_OK)) {
440 - error_report("Failed to bind host_id for SQL_CLEANUP_HEALTH_LOG_NOT_CLAIMED.");
441 - sqlite3_finalize(res);
442 - return;
443 - }
444 -
445 - rc = sqlite3_step_monitored(res);
446 - if (unlikely(rc != SQLITE_DONE))
447 - error_report("Failed to cleanup health log detail table, rc = %d", rc);
448 -
449 - rc = sqlite3_finalize(res);
450 - if (unlikely(rc != SQLITE_OK))
451 - error_report("Failed to finalize the prepared statement to cleanup health log detail table (un-claimed)");
452 -
453 - sql_health_alarm_log_count(host);
454 -
454 snprintfz(command, MAX_HEALTH_SQL_SIZE, "aclk_alert_%s", uuid_str);
456 - if (unlikely(table_exists_in_database(command))) {
457 - sql_aclk_alert_clean_dead_entries(host);
458 - }
459 -}
455
461 -/* Health related SQL queries
462 - Cleans up the health_log_detail table on a claimed host
463 -*/
464 -#define SQL_CLEANUP_HEALTH_LOG_DETAIL_CLAIMED(guid) "DELETE from health_log_detail WHERE unique_id NOT IN (SELECT filtered_alert_unique_id FROM aclk_alert_%s) AND unique_id IN (SELECT hld.unique_id FROM health_log hl, health_log_detail hld WHERE hl.host_id = ?1 AND hl.health_log_id = hld.health_log_id) AND health_log_id IN (SELECT health_log_id FROM health_log WHERE host_id = ?2) AND when_key + ?3 < unixepoch() AND updated_by_id <> 0 AND transition_id NOT IN (SELECT last_transition_id FROM health_log hl WHERE hl.host_id = ?4);", guid
465 -void sql_health_alarm_log_cleanup_claimed(RRDHOST *host) {
466 - sqlite3_stmt *res = NULL;
467 - int rc;
468 - char command[MAX_HEALTH_SQL_SIZE + 1];
456 + bool aclk_table_exists = table_exists_in_database(command);
457
470 - if (unlikely(!db_meta)) {
471 - if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
472 - error_report("Database has not been initialized");
473 - return;
474 - }
458 + char *sql = SQL_CLEANUP_HEALTH_LOG_DETAIL_NOT_CLAIMED;
459
476 - char uuid_str[UUID_STR_LEN];
477 - uuid_unparse_lower_fix(&host->host_uuid, uuid_str);
478 - snprintfz(command, MAX_HEALTH_SQL_SIZE, "aclk_alert_%s", uuid_str);
479 -
480 - if (!table_exists_in_database(command)) {
481 - sql_health_alarm_log_cleanup_not_claimed(host);
482 - return;
460 + if (claimed && aclk_table_exists) {
461 + snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_CLEANUP_HEALTH_LOG_DETAIL_CLAIMED(uuid_str));
462 + sql = command;
463 }
464
485 - snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_CLEANUP_HEALTH_LOG_DETAIL_CLAIMED(uuid_str));
486 -
487 - rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
465 + rc = sqlite3_prepare_v2(db_meta, sql, -1, &res, 0);
466 if (unlikely(rc != SQLITE_OK)) {
467 error_report("Failed to prepare statement to cleanup health log detail table (claimed)");
468 return;
@@ -492,59 +470,44 @@ void sql_health_alarm_log_cleanup_claimed(RRDHOST *host) {
470
471 rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
472 if (unlikely(rc != SQLITE_OK)) {
495 - error_report("Failed to bind first host_id for SQL_CLEANUP_HEALTH_LOG_CLAIMED.");
496 - sqlite3_finalize(res);
497 - return;
498 - }
499 -
500 - rc = sqlite3_bind_blob(res, 2, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
501 - if (unlikely(rc != SQLITE_OK)) {
502 - error_report("Failed to bind second host_id for SQL_CLEANUP_HEALTH_LOG_CLAIMED.");
503 - sqlite3_finalize(res);
504 - return;
505 - }
506 -
507 - rc = sqlite3_bind_int64(res, 3, (sqlite3_int64)host->health_log.health_log_history);
508 - if (unlikely(rc != SQLITE_OK)) {
509 - error_report("Failed to bind health log history for SQL_CLEANUP_HEALTH_LOG_CLAIMED.");
510 - sqlite3_finalize(res);
511 - return;
473 + error_report("Failed to bind first host_id for sql_health_alarm_log_cleanup.");
474 + goto done;
475 }
476
514 - rc = sqlite3_bind_blob(res, 4, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
477 + rc = sqlite3_bind_int64(res, 2, (sqlite3_int64)host->health_log.health_log_history);
478 if (unlikely(rc != SQLITE_OK)) {
516 - error_report("Failed to bind second host_id for SQL_CLEANUP_HEALTH_LOG_CLAIMED.");
517 - sqlite3_finalize(res);
518 - return;
479 + error_report("Failed to bind health log history for sql_health_alarm_log_cleanup.");
480 + goto done;
481 }
482
483 rc = sqlite3_step_monitored(res);
484 if (unlikely(rc != SQLITE_DONE))
485 error_report("Failed to cleanup health log detail table, rc = %d", rc);
486
487 + int rows = sql_health_alarm_log_count(host);
488 + if (rows >= 0)
489 + host->health.health_log_entries_written = rows;
490 +
491 + if (aclk_table_exists)
492 + sql_aclk_alert_clean_dead_entries(host);
493 +
494 +done:
495 rc = sqlite3_finalize(res);
496 if (unlikely(rc != SQLITE_OK))
497 error_report("Failed to finalize the prepared statement to cleanup health log detail table (claimed)");
528 -
529 - sql_health_alarm_log_count(host);
530 -
531 - sql_aclk_alert_clean_dead_entries(host);
532 -
498 }
499
535 -/* Health related SQL queries
536 - Cleans up the health_log table.
537 -*/
538 -void sql_health_alarm_log_cleanup(RRDHOST *host) {
539 - if (!claimed()) {
540 - sql_health_alarm_log_cleanup_not_claimed(host);
541 - } else
542 - sql_health_alarm_log_cleanup_claimed(host);
543 -}
500 +#define SQL_INJECT_REMOVED \
501 + "insert into health_log_detail (health_log_id, unique_id, alarm_id, alarm_event_id, updated_by_id, updates_id, when_key, " \
502 + "duration, non_clear_duration, flags, exec_run_timestamp, delay_up_to_timestamp, info, exec_code, new_status, old_status, " \
503 + "delay, new_value, old_value, last_repeat, transition_id, global_id) " \
504 + "select health_log_id, ?1, ?2, ?3, 0, ?4, unixepoch(), 0, 0, flags, exec_run_timestamp, unixepoch(), info, exec_code, -2, " \
505 + "new_status, delay, NULL, new_value, 0, ?5, now_usec(0) from health_log_detail where unique_id = ?6 and transition_id = ?7;"
506
545 -#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) 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;"
507 #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;"
508 +
509 #define SQL_INJECT_REMOVED_UPDATE_LOG "update health_log set last_transition_id = ?1 where alarm_id = ?2 and last_transition_id = ?3 and host_id = ?4;"
510 +
511 void sql_inject_removed_status(RRDHOST *host, uint32_t alarm_id, uint32_t alarm_event_id, uint32_t unique_id, uint32_t max_unique_id, uuid_t *prev_transition_id)
512 {
513 int rc;
@@ -682,10 +645,8 @@ void sql_inject_removed_status(RRDHOST *host, uint32_t alarm_id, uint32_t alarm_
645 }
646
647 rc = execute_insert(res);
685 - if (unlikely(rc != SQLITE_DONE)) {
648 + if (unlikely(rc != SQLITE_DONE))
649 error_report("HEALTH [N/A]: Failed to execute SQL_INJECT_REMOVED_UPDATE_DETAIL, rc = %d", rc);
687 - goto failed;
688 - }
650
651 failed:
652 if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
@@ -727,7 +688,10 @@ uint32_t sql_get_max_unique_id (RRDHOST *host)
688 return max_unique_id;
689 }
690
730 -#define SQL_SELECT_LAST_STATUSES "SELECT hld.new_status, hld.unique_id, hld.alarm_id, hld.alarm_event_id, hld.transition_id from health_log hl, health_log_detail hld where hl.host_id = @host_id and hl.last_transition_id = hld.transition_id;"
691 +#define SQL_SELECT_LAST_STATUSES \
692 + "SELECT hld.new_status, hld.unique_id, hld.alarm_id, hld.alarm_event_id, hld.transition_id FROM health_log hl, " \
693 + "health_log_detail hld WHERE hl.host_id = @host_id AND hl.last_transition_id = hld.transition_id"
694 +
695 void sql_check_removed_alerts_state(RRDHOST *host)
696 {
697 int rc;
@@ -752,21 +716,23 @@ void sql_check_removed_alerts_state(RRDHOST *host)
716 uint32_t alarm_id, alarm_event_id, unique_id;
717 RRDCALC_STATUS status;
718
755 - status = (RRDCALC_STATUS) sqlite3_column_int(res, 0);
756 - unique_id = (uint32_t) sqlite3_column_int64(res, 1);
757 - alarm_id = (uint32_t) sqlite3_column_int64(res, 2);
758 - alarm_event_id = (uint32_t) sqlite3_column_int64(res, 3);
759 - uuid_copy(transition_id, *((uuid_t *) sqlite3_column_blob(res, 4)));
719 + status = (RRDCALC_STATUS)sqlite3_column_int(res, 0);
720 + unique_id = (uint32_t)sqlite3_column_int64(res, 1);
721 + alarm_id = (uint32_t)sqlite3_column_int64(res, 2);
722 + alarm_event_id = (uint32_t)sqlite3_column_int64(res, 3);
723 + uuid_copy(transition_id, *((uuid_t *)sqlite3_column_blob(res, 4)));
724 +
725 if (unlikely(status != RRDCALC_STATUS_REMOVED)) {
761 - if (unlikely(!max_unique_id))
762 - max_unique_id = sql_get_max_unique_id (host);
763 - sql_inject_removed_status (host, alarm_id, alarm_event_id, unique_id, ++max_unique_id, &transition_id);
726 + if (unlikely(!max_unique_id))
727 + max_unique_id = sql_get_max_unique_id(host);
728 +
729 + sql_inject_removed_status(host, alarm_id, alarm_event_id, unique_id, ++max_unique_id, &transition_id);
730 }
731 }
732
767 - rc = sqlite3_finalize(res);
768 - if (unlikely(rc != SQLITE_OK))
769 - error_report("Failed to finalize the statement");
733 + rc = sqlite3_finalize(res);
734 + if (unlikely(rc != SQLITE_OK))
735 + error_report("Failed to finalize the statement");
736 }
737
738 /* Health related SQL queries
@@ -779,7 +745,9 @@ void sql_check_removed_alerts_state(RRDHOST *host)
745 "hld.last_repeat, ah.class, ah.component, ah.type, hl.chart_context, hld.transition_id, hld.global_id, hl.chart_name " \
746 "FROM health_log hl, alert_hash ah, health_log_detail hld " \
747 "WHERE hl.config_hash_id = ah.hash_id and hl.host_id = @host_id and hl.last_transition_id = hld.transition_id;"
782 -void sql_health_alarm_log_load(RRDHOST *host) {
748 +
749 +void sql_health_alarm_log_load(RRDHOST *host)
750 +{
751 sqlite3_stmt *res = NULL;
752 int ret;
753 ssize_t errored = 0, loaded = 0;
@@ -894,30 +862,11 @@ void sql_health_alarm_log_load(RRDHOST *host) {
862 ae->chart = string_strdupz((char *) sqlite3_column_text(res, 13));
863 ae->family = string_strdupz((char *) sqlite3_column_text(res, 14));
864
897 - if (sqlite3_column_type(res, 15) != SQLITE_NULL)
898 - ae->exec = string_strdupz((char *) sqlite3_column_text(res, 15));
899 - else
900 - ae->exec = NULL;
901 -
902 - if (sqlite3_column_type(res, 16) != SQLITE_NULL)
903 - ae->recipient = string_strdupz((char *) sqlite3_column_text(res, 16));
904 - else
905 - ae->recipient = NULL;
906 -
907 - if (sqlite3_column_type(res, 17) != SQLITE_NULL)
908 - ae->source = string_strdupz((char *) sqlite3_column_text(res, 17));
909 - else
910 - ae->source = NULL;
911 -
912 - if (sqlite3_column_type(res, 18) != SQLITE_NULL)
913 - ae->units = string_strdupz((char *) sqlite3_column_text(res, 18));
914 - else
915 - ae->units = NULL;
916 -
917 - if (sqlite3_column_type(res, 19) != SQLITE_NULL)
918 - ae->info = string_strdupz((char *) sqlite3_column_text(res, 19));
919 - else
920 - ae->info = NULL;
865 + ae->exec = SQLITE3_COLUMN_STRINGDUP_OR_NULL(res, 15);
866 + ae->recipient = SQLITE3_COLUMN_STRINGDUP_OR_NULL(res, 16);
867 + ae->source = SQLITE3_COLUMN_STRINGDUP_OR_NULL(res, 17);
868 + ae->units = SQLITE3_COLUMN_STRINGDUP_OR_NULL(res, 18);
869 + ae->info = SQLITE3_COLUMN_STRINGDUP_OR_NULL(res, 19);
870
871 ae->exec_code = (int) sqlite3_column_int(res, 20);
872 ae->new_status = (RRDCALC_STATUS) sqlite3_column_int(res, 21);
@@ -929,25 +878,10 @@ void sql_health_alarm_log_load(RRDHOST *host) {
878
879 ae->last_repeat = last_repeat;
880
932 - if (sqlite3_column_type(res, 27) != SQLITE_NULL)
933 - ae->classification = string_strdupz((char *) sqlite3_column_text(res, 27));
934 - else
935 - ae->classification = NULL;
936 -
937 - if (sqlite3_column_type(res, 28) != SQLITE_NULL)
938 - ae->component = string_strdupz((char *) sqlite3_column_text(res, 28));
939 - else
940 - ae->component = NULL;
941 -
942 - if (sqlite3_column_type(res, 29) != SQLITE_NULL)
943 - ae->type = string_strdupz((char *) sqlite3_column_text(res, 29));
944 - else
945 - ae->type = NULL;
946 -
947 - if (sqlite3_column_type(res, 30) != SQLITE_NULL)
948 - ae->chart_context = string_strdupz((char *) sqlite3_column_text(res, 30));
949 - else
950 - ae->chart_context = NULL;
881 + ae->classification = SQLITE3_COLUMN_STRINGDUP_OR_NULL(res, 27);
882 + ae->component = SQLITE3_COLUMN_STRINGDUP_OR_NULL(res, 28);
883 + ae->type = SQLITE3_COLUMN_STRINGDUP_OR_NULL(res, 29);
884 + ae->chart_context = SQLITE3_COLUMN_STRINGDUP_OR_NULL(res, 30);
885
886 if (sqlite3_column_type(res, 31) != SQLITE_NULL)
887 uuid_copy(ae->transition_id, *((uuid_t *)sqlite3_column_blob(res, 31)));
@@ -955,10 +889,7 @@ void sql_health_alarm_log_load(RRDHOST *host) {
889 if (sqlite3_column_type(res, 32) != SQLITE_NULL)
890 ae->global_id = sqlite3_column_int64(res, 32);
891
958 - if (sqlite3_column_type(res, 33) != SQLITE_NULL)
959 - ae->chart_name = string_strdupz((char *) sqlite3_column_text(res, 33));
960 - else
961 - ae->chart_name = NULL;
892 + ae->chart_name = SQLITE3_COLUMN_STRINGDUP_OR_NULL(res, 33);
893
894 char value_string[100 + 1];
895 string_freez(ae->old_value_string);
@@ -996,7 +927,10 @@ void sql_health_alarm_log_load(RRDHOST *host) {
927 if (unlikely(ret != SQLITE_OK))
928 error_report("Failed to finalize the health log read statement");
929
999 - sql_health_alarm_log_count(host);
930 + int rows = sql_health_alarm_log_count(host);
931 +
932 + if (rows >= 0)
933 + host->health.health_log_entries_written = rows;
934 }
935
936 /*
@@ -1033,120 +967,120 @@ int sql_store_alert_config_hash(uuid_t *hash_id, struct alert_config *cfg)
967 if (unlikely(rc != SQLITE_OK))
968 goto bind_fail;
969
1036 - rc = sqlite3_bind_string_or_null(res, cfg->alarm, ++param);
970 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->alarm, ++param);
971 if (unlikely(rc != SQLITE_OK))
972 goto bind_fail;
973
1040 - rc = sqlite3_bind_string_or_null(res, cfg->template_key, ++param);
974 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->template_key, ++param);
975 if (unlikely(rc != SQLITE_OK))
976 goto bind_fail;
977
1044 - rc = sqlite3_bind_string_or_null(res, cfg->on, ++param);
978 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->on, ++param);
979 if (unlikely(rc != SQLITE_OK))
980 goto bind_fail;
981
1048 - rc = sqlite3_bind_string_or_null(res, cfg->classification, ++param);
982 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->classification, ++param);
983 if (unlikely(rc != SQLITE_OK))
984 goto bind_fail;
985
1052 - rc = sqlite3_bind_string_or_null(res, cfg->component, ++param);
986 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->component, ++param);
987 if (unlikely(rc != SQLITE_OK))
988 goto bind_fail;
989
1056 - rc = sqlite3_bind_string_or_null(res, cfg->type, ++param);
990 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->type, ++param);
991 if (unlikely(rc != SQLITE_OK))
992 goto bind_fail;
993
1060 - rc = sqlite3_bind_string_or_null(res, cfg->os, ++param);
994 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->os, ++param);
995 if (unlikely(rc != SQLITE_OK))
996 goto bind_fail;
997
1064 - rc = sqlite3_bind_string_or_null(res, cfg->host, ++param);
998 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->host, ++param);
999 if (unlikely(rc != SQLITE_OK))
1000 goto bind_fail;
1001
1068 - rc = sqlite3_bind_string_or_null(res, cfg->lookup, ++param);
1002 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->lookup, ++param);
1003 if (unlikely(rc != SQLITE_OK))
1004 goto bind_fail;
1005
1072 - rc = sqlite3_bind_string_or_null(res, cfg->every, ++param);
1006 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->every, ++param);
1007 if (unlikely(rc != SQLITE_OK))
1008 goto bind_fail;
1009
1076 - rc = sqlite3_bind_string_or_null(res, cfg->units, ++param);
1010 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->units, ++param);
1011 if (unlikely(rc != SQLITE_OK))
1012 goto bind_fail;
1013
1080 - rc = sqlite3_bind_string_or_null(res, cfg->calc, ++param);
1014 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->calc, ++param);
1015 if (unlikely(rc != SQLITE_OK))
1016 goto bind_fail;
1017
1084 - rc = sqlite3_bind_string_or_null(res, cfg->families, ++param);
1018 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->families, ++param);
1019 if (unlikely(rc != SQLITE_OK))
1020 goto bind_fail;
1021
1088 - rc = sqlite3_bind_string_or_null(res, cfg->plugin, ++param);
1022 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->plugin, ++param);
1023 if (unlikely(rc != SQLITE_OK))
1024 goto bind_fail;
1025
1092 - rc = sqlite3_bind_string_or_null(res, cfg->module, ++param);
1026 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->module, ++param);
1027 if (unlikely(rc != SQLITE_OK))
1028 goto bind_fail;
1029
1096 - rc = sqlite3_bind_string_or_null(res, cfg->charts, ++param);
1030 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->charts, ++param);
1031 if (unlikely(rc != SQLITE_OK))
1032 goto bind_fail;
1033
1100 - rc = sqlite3_bind_string_or_null(res, cfg->green, ++param);
1034 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->green, ++param);
1035 if (unlikely(rc != SQLITE_OK))
1036 goto bind_fail;
1037
1104 - rc = sqlite3_bind_string_or_null(res, cfg->red, ++param);
1038 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->red, ++param);
1039 if (unlikely(rc != SQLITE_OK))
1040 goto bind_fail;
1041
1108 - rc = sqlite3_bind_string_or_null(res, cfg->warn, ++param);
1042 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->warn, ++param);
1043 if (unlikely(rc != SQLITE_OK))
1044 goto bind_fail;
1045
1112 - rc = sqlite3_bind_string_or_null(res, cfg->crit, ++param);
1046 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->crit, ++param);
1047 if (unlikely(rc != SQLITE_OK))
1048 goto bind_fail;
1049
1116 - rc = sqlite3_bind_string_or_null(res, cfg->exec, ++param);
1050 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->exec, ++param);
1051 if (unlikely(rc != SQLITE_OK))
1052 goto bind_fail;
1053
1120 - rc = sqlite3_bind_string_or_null(res, cfg->to, ++param);
1054 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->to, ++param);
1055 if (unlikely(rc != SQLITE_OK))
1056 goto bind_fail;
1057
1124 - rc = sqlite3_bind_string_or_null(res, cfg->info, ++param);
1058 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->info, ++param);
1059 if (unlikely(rc != SQLITE_OK))
1060 goto bind_fail;
1061
1128 - rc = sqlite3_bind_string_or_null(res, cfg->delay, ++param);
1062 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->delay, ++param);
1063 if (unlikely(rc != SQLITE_OK))
1064 goto bind_fail;
1065
1132 - rc = sqlite3_bind_string_or_null(res, cfg->options, ++param);
1066 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->options, ++param);
1067 if (unlikely(rc != SQLITE_OK))
1068 goto bind_fail;
1069
1136 - rc = sqlite3_bind_string_or_null(res, cfg->repeat, ++param);
1070 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->repeat, ++param);
1071 if (unlikely(rc != SQLITE_OK))
1072 goto bind_fail;
1073
1140 - rc = sqlite3_bind_string_or_null(res, cfg->host_labels, ++param);
1074 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->host_labels, ++param);
1075 if (unlikely(rc != SQLITE_OK))
1076 goto bind_fail;
1077
1078 if (cfg->p_db_lookup_after) {
1145 - rc = sqlite3_bind_string_or_null(res, cfg->p_db_lookup_dimensions, ++param);
1079 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->p_db_lookup_dimensions, ++param);
1080 if (unlikely(rc != SQLITE_OK))
1081 goto bind_fail;
1082
1149 - rc = sqlite3_bind_string_or_null(res, cfg->p_db_lookup_method, ++param);
1083 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->p_db_lookup_method, ++param);
1084 if (unlikely(rc != SQLITE_OK))
1085 goto bind_fail;
1086
@@ -1187,11 +1121,11 @@ int sql_store_alert_config_hash(uuid_t *hash_id, struct alert_config *cfg)
1121 if (unlikely(rc != SQLITE_OK))
1122 goto bind_fail;
1123
1190 - rc = sqlite3_bind_string_or_null(res, cfg->source, ++param);
1124 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->source, ++param);
1125 if (unlikely(rc != SQLITE_OK))
1126 goto bind_fail;
1127
1194 - rc = sqlite3_bind_string_or_null(res, cfg->chart_labels, ++param);
1128 + rc = SQLITE3_BIND_STRING_OR_NULL(res, cfg->chart_labels, ++param);
1129 if (unlikely(rc != SQLITE_OK))
1130 goto bind_fail;
1131
@@ -1282,16 +1216,17 @@ int alert_hash_and_store_config(
1216 return 1;
1217 }
1218
1285 -#define SQL_SELECT_HEALTH_LAST_EXECUTED_EVENT "SELECT hld.new_status FROM health_log hl, health_log_detail hld WHERE hl.alarm_id = %u AND hld.unique_id != %u AND hld.flags & %u AND hl.host_id = @host_id and hl.health_log_id = hld.health_log_id ORDER BY hld.unique_id DESC LIMIT 1;"
1219 +#define SQL_SELECT_HEALTH_LAST_EXECUTED_EVENT \
1220 + "SELECT hld.new_status FROM health_log hl, health_log_detail hld " \
1221 + "WHERE hl.host_id = @host_id AND hl.alarm_id = @alarm_id AND hld.unique_id != @unique_id AND hld.flags & @flags " \
1222 + "AND hl.health_log_id = hld.health_log_id ORDER BY hld.unique_id DESC LIMIT 1;"
1223 +
1224 int sql_health_get_last_executed_event(RRDHOST *host, ALARM_ENTRY *ae, RRDCALC_STATUS *last_executed_status)
1225 {
1226 int rc = 0, ret = -1;
1289 - char command[MAX_HEALTH_SQL_SIZE + 1];
1227 sqlite3_stmt *res = NULL;
1228
1292 - snprintfz(command, MAX_HEALTH_SQL_SIZE, SQL_SELECT_HEALTH_LAST_EXECUTED_EVENT, ae->alarm_id, ae->unique_id, (uint32_t) HEALTH_ENTRY_FLAG_EXEC_RUN);
1293 -
1294 - rc = sqlite3_prepare_v2(db_meta, command, -1, &res, 0);
1229 + rc = sqlite3_prepare_v2(db_meta, SQL_SELECT_HEALTH_LAST_EXECUTED_EVENT, -1, &res, 0);
1230 if (rc != SQLITE_OK) {
1231 error_report("Failed to prepare statement when trying to get last executed status");
1232 return ret;
@@ -1300,8 +1235,25 @@ int sql_health_get_last_executed_event(RRDHOST *host, ALARM_ENTRY *ae, RRDCALC_S
1235 rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
1236 if (unlikely(rc != SQLITE_OK)) {
1237 error_report("Failed to bind host_id parameter for SQL_SELECT_HEALTH_LAST_EXECUTED_EVENT.");
1303 - sqlite3_finalize(res);
1304 - return ret;
1238 + goto done;
1239 + }
1240 +
1241 + rc = sqlite3_bind_int(res, 2, (int) ae->alarm_id);
1242 + if (unlikely(rc != SQLITE_OK)) {
1243 + error_report("Failed to bind alarm_id parameter for SQL_SELECT_HEALTH_LAST_EXECUTED_EVENT.");
1244 + goto done;
1245 + }
1246 +
1247 + rc = sqlite3_bind_int(res, 3, (int) ae->unique_id);
1248 + if (unlikely(rc != SQLITE_OK)) {
1249 + error_report("Failed to bind unique_id parameter for SQL_SELECT_HEALTH_LAST_EXECUTED_EVENT.");
1250 + goto done;
1251 + }
1252 +
1253 + rc = sqlite3_bind_int(res, 4, (uint32_t) HEALTH_ENTRY_FLAG_EXEC_RUN);
1254 + if (unlikely(rc != SQLITE_OK)) {
1255 + error_report("Failed to bind unique_id parameter for SQL_SELECT_HEALTH_LAST_EXECUTED_EVENT.");
1256 + goto done;
1257 }
1258
1259 ret = 0;
@@ -1310,6 +1262,7 @@ int sql_health_get_last_executed_event(RRDHOST *host, ALARM_ENTRY *ae, RRDCALC_S
1262 ret = 1;
1263 }
1264
1265 +done:
1266 rc = sqlite3_finalize(res);
1267 if (unlikely(rc != SQLITE_OK))
1268 error_report("Failed to finalize the statement.");
@@ -1317,7 +1270,15 @@ int sql_health_get_last_executed_event(RRDHOST *host, ALARM_ENTRY *ae, RRDCALC_S
1270 return ret;
1271 }
1272
1320 -#define SQL_SELECT_HEALTH_LOG "SELECT hld.unique_id, hld.alarm_id, hld.alarm_event_id, hl.config_hash_id, hld.updated_by_id, 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 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 "
1273 +#define SQL_SELECT_HEALTH_LOG \
1274 + "SELECT hld.unique_id, hld.alarm_id, hld.alarm_event_id, hl.config_hash_id, hld.updated_by_id, hld.updates_id, " \
1275 + "hld.when_key, hld.duration, hld.non_clear_duration, hld.flags, hld.exec_run_timestamp, " \
1276 + "hld.delay_up_to_timestamp, hl.name, hl.chart, hl.family, hl.exec, hl.recipient, ah.source, " \
1277 + "hl.units, hld.info, hld.exec_code, hld.new_status, hld.old_status, hld.delay, hld.new_value, hld.old_value, " \
1278 + "hld.last_repeat, ah.class, ah.component, ah.type, hl.chart_context, hld.transition_id FROM health_log hl, " \
1279 + "alert_hash ah, health_log_detail hld WHERE hl.config_hash_id = ah.hash_id and " \
1280 + "hl.health_log_id = hld.health_log_id and hl.host_id = @host_id "
1281 +
1282 void sql_health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after, char *chart) {
1283
1284 buffer_strcat(wb, "[");
@@ -1647,8 +1608,50 @@ int health_migrate_old_health_log_table(char *table) {
1608 return 1;
1609 }
1610
1650 -#define SQL_GET_ALARM_ID "select alarm_id, health_log_id from health_log where host_id = @host_id and chart = @chart and name = @name and config_hash_id = @config_hash_id"
1651 -#define SQL_GET_EVENT_ID "select max(alarm_event_id) + 1 from health_log_detail where health_log_id = @health_log_id and alarm_id = @alarm_id"
1611 +#define SQL_GET_EVENT_ID \
1612 + "SELECT MAX(alarm_event_id)+1 FROM health_log_detail WHERE health_log_id = @health_log_id AND alarm_id = @alarm_id"
1613 +
1614 +static uint32_t get_next_alarm_event_id(uint64_t health_log_id, uint32_t alarm_id)
1615 +{
1616 + int rc;
1617 + sqlite3_stmt *res = NULL;
1618 + uint32_t next_event_id = 0;
1619 +
1620 + rc = sqlite3_prepare_v2(db_meta, SQL_GET_EVENT_ID, -1, &res, 0);
1621 + if (rc != SQLITE_OK) {
1622 + error_report("Failed to prepare statement when trying to get an event id");
1623 + return alarm_id;
1624 + }
1625 +
1626 + rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) health_log_id);
1627 + if (unlikely(rc != SQLITE_OK)) {
1628 + error_report("Failed to bind host_id parameter for SQL_GET_EVENT_ID.");
1629 + sqlite3_finalize(res);
1630 + return alarm_id;
1631 + }
1632 +
1633 + rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) alarm_id);
1634 + if (unlikely(rc != SQLITE_OK)) {
1635 + error_report("Failed to bind char parameter for SQL_GET_EVENT_ID.");
1636 + sqlite3_finalize(res);
1637 + return alarm_id;
1638 + }
1639 +
1640 + while (sqlite3_step_monitored(res) == SQLITE_ROW) {
1641 + next_event_id = (uint32_t) sqlite3_column_int64(res, 0);
1642 + }
1643 +
1644 + rc = sqlite3_finalize(res);
1645 + if (unlikely(rc != SQLITE_OK))
1646 + error_report("Failed to finalize the statement while getting an alarm id.");
1647 +
1648 + return next_event_id;
1649 +}
1650 +
1651 +#define SQL_GET_ALARM_ID \
1652 + "SELECT alarm_id, health_log_id FROM health_log WHERE host_id = @host_id AND chart = @chart " \
1653 + "AND name = @name AND config_hash_id = @config_hash_id"
1654 +
1655 uint32_t sql_get_alarm_id(RRDHOST *host, STRING *chart, STRING *name, uint32_t *next_event_id, uuid_t *config_hash_id)
1656 {
1657 int rc = 0;
@@ -1669,14 +1672,14 @@ uint32_t sql_get_alarm_id(RRDHOST *host, STRING *chart, STRING *name, uint32_t *
1672 return alarm_id;
1673 }
1674
1672 - rc = sqlite3_bind_string_or_null(res, chart, 2);
1675 + rc = SQLITE3_BIND_STRING_OR_NULL(res, chart, 2);
1676 if (unlikely(rc != SQLITE_OK)) {
1677 error_report("Failed to bind char parameter for SQL_GET_ALARM_ID.");
1678 sqlite3_finalize(res);
1679 return alarm_id;
1680 }
1681
1679 - rc = sqlite3_bind_string_or_null(res, name, 3);
1682 + rc = SQLITE3_BIND_STRING_OR_NULL(res, name, 3);
1683 if (unlikely(rc != SQLITE_OK)) {
1684 error_report("Failed to bind name parameter for SQL_GET_ALARM_ID.");
1685 sqlite3_finalize(res);
@@ -1699,40 +1702,16 @@ uint32_t sql_get_alarm_id(RRDHOST *host, STRING *chart, STRING *name, uint32_t *
1702 if (unlikely(rc != SQLITE_OK))
1703 error_report("Failed to finalize the statement while getting an alarm id.");
1704
1702 - if (alarm_id) {
1703 - rc = sqlite3_prepare_v2(db_meta, SQL_GET_EVENT_ID, -1, &res, 0);
1704 - if (rc != SQLITE_OK) {
1705 - error_report("Failed to prepare statement when trying to get an event id");
1706 - return alarm_id;
1707 - }
1708 -
1709 - rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) health_log_id);
1710 - if (unlikely(rc != SQLITE_OK)) {
1711 - error_report("Failed to bind host_id parameter for SQL_GET_EVENT_ID.");
1712 - sqlite3_finalize(res);
1713 - return alarm_id;
1714 - }
1715 -
1716 - rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) alarm_id);
1717 - if (unlikely(rc != SQLITE_OK)) {
1718 - error_report("Failed to bind char parameter for SQL_GET_EVENT_ID.");
1719 - sqlite3_finalize(res);
1720 - return alarm_id;
1721 - }
1722 -
1723 - while (sqlite3_step_monitored(res) == SQLITE_ROW) {
1724 - *next_event_id = (uint32_t) sqlite3_column_int64(res, 0);
1725 - }
1726 -
1727 - rc = sqlite3_finalize(res);
1728 - if (unlikely(rc != SQLITE_OK))
1729 - error_report("Failed to finalize the statement while getting an alarm id.");
1730 - }
1705 + if (alarm_id)
1706 + *next_event_id = get_next_alarm_event_id(health_log_id, alarm_id);
1707
1708 return alarm_id;
1709 }
1710
1735 -#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"
1711 +#define SQL_UPDATE_ALARM_ID_WITH_CONFIG_HASH \
1712 + "UPDATE health_log SET config_hash_id = @config_hash_id WHERE host_id = @host_id AND alarm_id = @alarm_id " \
1713 + "AND health_log_id = @health_log_id"
1714 +
1715 void sql_update_alarm_with_config_hash(RRDHOST *host, uint32_t alarm_id, uint64_t health_log_id, uuid_t *config_hash_id)
1716 {
1717 int rc = 0;
@@ -1747,42 +1726,42 @@ void sql_update_alarm_with_config_hash(RRDHOST *host, uint32_t alarm_id, uint64_
1726 rc = sqlite3_bind_blob(res, 1, config_hash_id, sizeof(*config_hash_id), SQLITE_STATIC);
1727 if (unlikely(rc != SQLITE_OK)) {
1728 error_report("Failed to bind config_hash_id parameter for SQL_UPDATE_ALARM_ID_WITH_CONFIG_HASH.");
1750 - sqlite3_finalize(res);
1751 - return;
1729 + goto done;
1730 }
1731
1732 rc = sqlite3_bind_blob(res, 2, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
1733 if (unlikely(rc != SQLITE_OK)) {
1734 error_report("Failed to bind host_id parameter for SQL_UPDATE_ALARM_ID_WITH_CONFIG_HASH.");
1757 - sqlite3_finalize(res);
1758 - return;
1735 + goto done;
1736 }
1737
1738 rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) alarm_id);
1739 if (unlikely(rc != SQLITE_OK)) {
1740 error_report("Failed to bind alarm_id parameter for SQL_GET_ALARM_ID.");
1764 - sqlite3_finalize(res);
1765 - return;
1741 + goto done;
1742 }
1743
1744 rc = sqlite3_bind_int64(res, 4, (sqlite3_int64) health_log_id);
1745 if (unlikely(rc != SQLITE_OK)) {
1746 error_report("Failed to bind alarm_id parameter for SQL_GET_ALARM_ID.");
1771 - sqlite3_finalize(res);
1772 - return;
1747 + goto done;
1748 }
1749
1750 rc = execute_insert(res);
1776 - if (unlikely(rc != SQLITE_DONE)) {
1751 + if (unlikely(rc != SQLITE_DONE))
1752 error_report("Failed to execute SQL_UPDATE_ALARM_ID_WITH_CONFIG_HASH, rc = %d", rc);
1778 - rc = sqlite3_finalize(res);
1779 - if (unlikely(rc != SQLITE_OK))
1780 - error_report("Failed to reset statement to update health log detail table with config hash ids, rc = %d", rc);
1781 - return;
1782 - }
1753 +
1754 +done:
1755 + rc = sqlite3_finalize(res);
1756 + if (unlikely(rc != SQLITE_OK))
1757 + error_report("Failed to reset statement to update health log detail table with config hash ids, rc = %d", rc);
1758 +
1759 }
1760
1785 -#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))"
1761 +#define SQL_GET_ALARM_ID_CHECK_ZERO_HASH \
1762 + "SELECT alarm_id, health_log_id FROM health_log WHERE host_id = @host_id AND chart = @chart " \
1763 + "AND name = @name AND (config_hash_id IS NULL OR config_hash_id = ZEROBLOB(16))"
1764 +
1765 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)
1766 {
1767 int rc = 0;
@@ -1803,14 +1782,14 @@ uint32_t sql_get_alarm_id_check_zero_hash(RRDHOST *host, STRING *chart, STRING *
1782 return alarm_id;
1783 }
1784
1806 - rc = sqlite3_bind_string_or_null(res, chart, 2);
1785 + rc = SQLITE3_BIND_STRING_OR_NULL(res, chart, 2);
1786 if (unlikely(rc != SQLITE_OK)) {
1787 error_report("Failed to bind char parameter for SQL_GET_ALARM_ID_CHECK_ZERO_HASH.");
1788 sqlite3_finalize(res);
1789 return alarm_id;
1790 }
1791
1813 - rc = sqlite3_bind_string_or_null(res, name, 3);
1792 + rc = SQLITE3_BIND_STRING_OR_NULL(res, name, 3);
1793 if (unlikely(rc != SQLITE_OK)) {
1794 error_report("Failed to bind name parameter for SQL_GET_ALARM_ID_CHECK_ZERO_HASH.");
1795 sqlite3_finalize(res);
@@ -1828,44 +1807,21 @@ uint32_t sql_get_alarm_id_check_zero_hash(RRDHOST *host, STRING *chart, STRING *
1807
1808 if (alarm_id) {
1809 sql_update_alarm_with_config_hash(host, alarm_id, health_log_id, config_hash_id);
1831 -
1832 - rc = sqlite3_prepare_v2(db_meta, SQL_GET_EVENT_ID, -1, &res, 0);
1833 - if (rc != SQLITE_OK) {
1834 - error_report("Failed to prepare statement when trying to get an event id");
1835 - return alarm_id;
1836 - }
1837 -
1838 - rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) health_log_id);
1839 - if (unlikely(rc != SQLITE_OK)) {
1840 - error_report("Failed to bind host_id parameter for SQL_GET_EVENT_ID.");
1841 - sqlite3_finalize(res);
1842 - return alarm_id;
1843 - }
1844 -
1845 - rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) alarm_id);
1846 - if (unlikely(rc != SQLITE_OK)) {
1847 - error_report("Failed to bind char parameter for SQL_GET_EVENT_ID.");
1848 - sqlite3_finalize(res);
1849 - return alarm_id;
1850 - }
1851 -
1852 - while (sqlite3_step_monitored(res) == SQLITE_ROW) {
1853 - *next_event_id = (uint32_t) sqlite3_column_int64(res, 0);
1854 - }
1855 -
1856 - rc = sqlite3_finalize(res);
1857 - if (unlikely(rc != SQLITE_OK))
1858 - error_report("Failed to finalize the statement while getting an alarm id.");
1810 + *next_event_id = get_next_alarm_event_id(health_log_id, alarm_id);
1811 }
1812
1813 return alarm_id;
1814 }
1815
1864 -#define SQL_GET_ALARM_ID_FROM_TRANSITION_ID "SELECT hld.alarm_id, hl.host_id, hl.chart_context FROM " \
1865 - "health_log_detail hld, health_log hl WHERE hld.transition_id = @transition_id " \
1866 - "and hld.health_log_id = hl.health_log_id"
1816 +#define SQL_GET_ALARM_ID_FROM_TRANSITION_ID \
1817 + "SELECT hld.alarm_id, hl.host_id, hl.chart_context FROM health_log_detail hld, health_log hl " \
1818 + "WHERE hld.transition_id = @transition_id " \
1819 + "AND hld.health_log_id = hl.health_log_id"
1820
1868 -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)
1821 +bool sql_find_alert_transition(
1822 + const char *transition,
1823 + void (*cb)(const char *machine_guid, const char *context, time_t alert_id, void *data),
1824 + void *data)
1825 {
1826 static __thread sqlite3_stmt *res = NULL;
1827
@@ -1889,7 +1845,7 @@ bool sql_find_alert_transition(const char *transition, void (*cb)(const char *ma
1845 rc = sqlite3_bind_blob(res, 1, &transition_uuid, sizeof(transition_uuid), SQLITE_STATIC);
1846 if (unlikely(rc != SQLITE_OK)) {
1847 error_report("Failed to bind transition");
1892 - goto fail;
1848 + goto done;
1849 }
1850
1851 while (sqlite3_step_monitored(res) == SQLITE_ROW) {
@@ -1898,7 +1854,7 @@ bool sql_find_alert_transition(const char *transition, void (*cb)(const char *ma
1854 cb(machine_guid, (const char *) sqlite3_column_text(res, 2), sqlite3_column_int(res, 0), data);
1855 }
1856
1901 -fail:
1857 +done:
1858 rc = sqlite3_reset(res);
1859 if (unlikely(rc != SQLITE_OK))
1860 error_report("Failed to reset the statement when trying to find transition");
@@ -1910,20 +1866,24 @@ fail:
1866
1867 #define SQL_POPULATE_TEMP_ALERT_TRANSITION_TABLE "INSERT INTO v_%p (host_id) VALUES (@host_id)"
1868
1913 -#define SQL_SEARCH_ALERT_TRANSITION_SELECT "SELECT " \
1914 - "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, " \
1915 - "h.chart_context, d.when_key, d.duration, d.non_clear_duration, d.flags, d.delay_up_to_timestamp, " \
1916 - "d.info, d.exec_code, d.new_status, d.old_status, d.delay, d.new_value, d.old_value, d.last_repeat, " \
1869 +#define SQL_SEARCH_ALERT_TRANSITION_SELECT \
1870 + "SELECT 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, " \
1871 + "h.chart_context, d.when_key, d.duration, d.non_clear_duration, d.flags, d.delay_up_to_timestamp, " \
1872 + "d.info, d.exec_code, d.new_status, d.old_status, d.delay, d.new_value, d.old_value, d.last_repeat, " \
1873 "d.transition_id, d.global_id, ah.class, ah.type, ah.component, d.exec_run_timestamp"
1874
1919 -#define SQL_SEARCH_ALERT_TRANSITION_COMMON_WHERE \
1920 - "h.config_hash_id = ah.hash_id AND h.health_log_id = d.health_log_id"
1875 +#define SQL_SEARCH_ALERT_TRANSITION_COMMON_WHERE "h.config_hash_id = ah.hash_id AND h.health_log_id = d.health_log_id"
1876
1922 -#define SQL_SEARCH_ALERT_TRANSITION SQL_SEARCH_ALERT_TRANSITION_SELECT " FROM health_log h, health_log_detail d, v_%p t, alert_hash ah " \
1923 - " WHERE h.host_id = t.host_id AND " SQL_SEARCH_ALERT_TRANSITION_COMMON_WHERE " AND ( d.new_status > 2 OR d.old_status > 2 ) AND d.global_id BETWEEN @after AND @before "
1877 +#define SQL_SEARCH_ALERT_TRANSITION \
1878 + SQL_SEARCH_ALERT_TRANSITION_SELECT \
1879 + " FROM health_log h, health_log_detail d, v_%p t, alert_hash ah " \
1880 + " WHERE h.host_id = t.host_id AND " SQL_SEARCH_ALERT_TRANSITION_COMMON_WHERE \
1881 + " AND ( d.new_status > 2 OR d.old_status > 2 ) AND d.global_id BETWEEN @after AND @before "
1882
1925 -#define SQL_SEARCH_ALERT_TRANSITION_DIRECT SQL_SEARCH_ALERT_TRANSITION_SELECT " FROM health_log h, health_log_detail d, alert_hash ah " \
1926 - " WHERE " SQL_SEARCH_ALERT_TRANSITION_COMMON_WHERE " AND transition_id = @transition "
1883 +#define SQL_SEARCH_ALERT_TRANSITION_DIRECT \
1884 + SQL_SEARCH_ALERT_TRANSITION_SELECT " FROM health_log h, health_log_detail d, alert_hash ah " \
1885 + " WHERE " SQL_SEARCH_ALERT_TRANSITION_COMMON_WHERE \
1886 + " AND transition_id = @transition "
1887
1888 void sql_alert_transitions(
1889 DICTIONARY *nodes,
@@ -1956,7 +1916,7 @@ void sql_alert_transitions(
1916 rc = sqlite3_bind_blob(res, 1, &transition_uuid, sizeof(transition_uuid), SQLITE_STATIC);
1917 if (unlikely(rc != SQLITE_OK)) {
1918 error_report("Failed to bind transition_id parameter");
1959 - goto fail;
1919 + goto done;
1920 }
1921 goto run_query;
1922 }
@@ -1972,7 +1932,7 @@ void sql_alert_transitions(
1932 rc = sqlite3_prepare_v2(db_meta, sql, -1, &res, 0);
1933 if (unlikely(rc != SQLITE_OK)) {
1934 error_report("Failed to prepare statement to INSERT into v_%p", nodes);
1975 - goto fail_only_drop;
1935 + goto done_only_drop;
1936 }
1937
1938 void *t;
@@ -2015,27 +1975,27 @@ void sql_alert_transitions(
1975 rc = sqlite3_prepare_v2(db_meta, buffer_tostring(command), -1, &res, 0);
1976 if (unlikely(rc != SQLITE_OK)) {
1977 error_report("Failed to prepare statement sql_alert_transitions");
2018 - goto fail_only_drop;
1978 + goto done_only_drop;
1979 }
1980
1981 int param = 1;
1982 rc = sqlite3_bind_int64(res, param++, (sqlite3_int64)(after * USEC_PER_SEC));
1983 if (unlikely(rc != SQLITE_OK)) {
1984 error_report("Failed to bind after parameter");
2025 - goto fail;
1985 + goto done;
1986 }
1987
1988 rc = sqlite3_bind_int64(res, param++, (sqlite3_int64)(before * USEC_PER_SEC));
1989 if (unlikely(rc != SQLITE_OK)) {
1990 error_report("Failed to bind before parameter");
2031 - goto fail;
1991 + goto done;
1992 }
1993
1994 if (context) {
1995 rc = sqlite3_bind_text(res, param++, context, -1, SQLITE_STATIC);
1996 if (unlikely(rc != SQLITE_OK)) {
1997 error_report("Failed to bind context parameter");
2038 - goto fail;
1998 + goto done;
1999 }
2000 }
2001
@@ -2043,7 +2003,7 @@ void sql_alert_transitions(
2003 rc = sqlite3_bind_text(res, param++, alert_name, -1, SQLITE_STATIC);
2004 if (unlikely(rc != SQLITE_OK)) {
2005 error_report("Failed to bind alert_name parameter");
2046 - goto fail;
2006 + goto done;
2007 }
2008 }
2009
@@ -2086,12 +2046,12 @@ run_query:;
2046 cb(&atd, data);
2047 }
2048
2089 -fail:
2049 +done:
2050 rc = sqlite3_finalize(res);
2051 if (unlikely(rc != SQLITE_OK))
2052 error_report("Failed to finalize statement for sql_alert_transitions");
2053
2094 -fail_only_drop:
2054 +done_only_drop:
2055 if (likely(!transition)) {
2056 (void)snprintfz(sql, 511, "DROP TABLE IF EXISTS v_%p", nodes);
2057 (void)db_execute(db_meta, sql);
@@ -2103,10 +2063,11 @@ fail_only_drop:
2063
2064 #define SQL_POPULATE_TEMP_CONFIG_TARGET_TABLE "INSERT INTO c_%p (hash_id) VALUES (@hash_id)"
2065
2106 -#define SQL_SEARCH_CONFIG_LIST "SELECT ah.hash_id, alarm, template, on_key, class, component, type, os, hosts, lookup, every, " \
2107 - " units, calc, families, plugin, module, charts, green, red, warn, crit, " \
2108 - " exec, to_key, info, delay, options, repeat, host_labels, p_db_lookup_dimensions, p_db_lookup_method, " \
2109 - " p_db_lookup_options, p_db_lookup_after, p_db_lookup_before, p_update_every, source, chart_labels " \
2066 +#define SQL_SEARCH_CONFIG_LIST \
2067 + "SELECT ah.hash_id, alarm, template, on_key, class, component, type, os, hosts, lookup, every, " \
2068 + " units, calc, families, plugin, module, charts, green, red, warn, crit, " \
2069 + " exec, to_key, info, delay, options, repeat, host_labels, p_db_lookup_dimensions, p_db_lookup_method, " \
2070 + " p_db_lookup_options, p_db_lookup_after, p_db_lookup_before, p_update_every, source, chart_labels " \
2071 " FROM alert_hash ah, c_%p t where ah.hash_id = t.hash_id"
2072
2073 int sql_get_alert_configuration(
@@ -2230,69 +2191,3 @@ fail_only_drop:
2191 buffer_free(command);
2192 return added;
2193 }
2233 -
2234 -#define SQL_FETCH_CHART_NAME "SELECT chart_name FROM health_log where host_id = @host_id LIMIT 1;"
2235 -bool is_chart_name_populated(uuid_t *host_uuid)
2236 -{
2237 - sqlite3_stmt *res = NULL;
2238 - int rc;
2239 -
2240 - bool status = true;
2241 -
2242 - rc = sqlite3_prepare_v2(db_meta, SQL_FETCH_CHART_NAME, -1, &res, 0);
2243 - if (unlikely(rc != SQLITE_OK)) {
2244 - error_report("Failed to prepare statement to check health_log chart_name");
2245 - return true;
2246 - }
2247 -
2248 - rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
2249 - if (unlikely(rc != SQLITE_OK)) {
2250 - error_report("Failed to bind host_id for health_log chart_name check");
2251 - goto fail;
2252 - }
2253 -
2254 - rc = sqlite3_step_monitored(res);
2255 - if (likely(rc == SQLITE_ROW))
2256 - status = sqlite3_column_type(res, 0) != SQLITE_NULL;
2257 -fail:
2258 -
2259 - rc = sqlite3_finalize(res);
2260 - if (unlikely(rc != SQLITE_OK))
2261 - error_report("Failed to finalize the prepared statement for health_log chart_name check");
2262 -
2263 - return status;
2264 -}
2265 -
2266 -#define SQL_POPULATE_CHART_NAME " UPDATE health_log SET chart_name = upd.chart_name FROM " \
2267 - "(SELECT c.type || '.' || IFNULL(c.name, c.id) AS chart_name, hl.host_id, hl.health_log_id FROM " \
2268 - "chart c, health_log hl WHERE (c.type || '.' || c.id) = hl.chart AND c.host_id = hl.host_id " \
2269 - "AND hl.host_id = @host_id) AS upd WHERE health_log.host_id = upd.host_id " \
2270 - "AND health_log.health_log_id = upd.health_log_id"
2271 -
2272 -void chart_name_populate(uuid_t *host_uuid)
2273 -{
2274 - sqlite3_stmt *res = NULL;
2275 - int rc;
2276 -
2277 - rc = sqlite3_prepare_v2(db_meta, SQL_POPULATE_CHART_NAME, -1, &res, 0);
2278 - if (unlikely(rc != SQLITE_OK)) {
2279 - error_report("Failed to prepare statement to update health_log chart_name");
2280 - return;
2281 - }
2282 -
2283 - rc = sqlite3_bind_blob(res, 1, host_uuid, sizeof(*host_uuid), SQLITE_STATIC);
2284 - if (unlikely(rc != SQLITE_OK)) {
2285 - error_report("Failed to bind host_id for health_log chart_name update");
2286 - goto fail;
2287 - }
2288 -
2289 - rc = execute_insert(res);
2290 - if (unlikely(rc != SQLITE_DONE))
2291 - error_report("Failed to update chart name in health_log, rc = %d", rc);
2292 -
2293 -fail:
2294 -
2295 - rc = sqlite3_finalize(res);
2296 - if (unlikely(rc != SQLITE_OK))
2297 - error_report("Failed to finalize the prepared statement for health_log chart_name update");
2298 -}
database/sqlite/sqlite_health.h
+1 -6
@@ -7,12 +7,9 @@
7
8 struct sql_alert_transition_data;
9 struct sql_alert_config_data;
10 -extern sqlite3 *db_meta;
10 void sql_health_alarm_log_load(RRDHOST *host);
12 -void sql_health_alarm_log_update(RRDHOST *host, ALARM_ENTRY *ae);
13 -void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae);
11 void sql_health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae);
15 -void sql_health_alarm_log_cleanup(RRDHOST *host);
12 +void sql_health_alarm_log_cleanup(RRDHOST *host, bool claimed);
13 int alert_hash_and_store_config(uuid_t hash_id, struct alert_config *cfg, int store_hash);
14 void sql_aclk_alert_clean_dead_entries(RRDHOST *host);
15 int sql_health_get_last_executed_event(RRDHOST *host, ALARM_ENTRY *ae, RRDCALC_STATUS *last_executed_status);
@@ -38,6 +35,4 @@ int sql_get_alert_configuration(
35 bool debug __maybe_unused);
36
37 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);
41 -bool is_chart_name_populated(uuid_t *host_uuid);
42 -void chart_name_populate(uuid_t *host_uuid);
38 #endif //NETDATA_SQLITE_HEALTH_H
database/sqlite/sqlite_metadata.c
+4 -2
@@ -728,10 +728,12 @@ skip_run:
728 static void cleanup_health_log(void)
729 {
730 RRDHOST *host;
731 - dfe_start_reentrant(rrdhost_root_index, host) {
731 +
732 + bool is_claimed = claimed();
733 + dfe_start_reentrant(rrdhost_root_index, host){
734 if (rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED))
735 continue;
734 - sql_health_alarm_log_cleanup(host);
736 + sql_health_alarm_log_cleanup(host, is_claimed);
737 }
738 dfe_done(host);
739 }
health/health.c
+1 -4
@@ -844,9 +844,6 @@ static void initialize_health(RRDHOST *host)
844 host->health.health_default_exec = string_strdupz(config_get(CONFIG_SECTION_HEALTH, "script to execute on alarm", filename));
845 host->health.health_default_recipient = string_strdupz("root");
846
847 - //if (!is_chart_name_populated(&host->host_uuid))
848 - // chart_name_populate(&host->host_uuid);
849 -
847 sql_health_alarm_log_load(host);
848
849 // ------------------------------------------------------------------------
@@ -1187,7 +1184,7 @@ void *health_main(void *ptr) {
1184
1185 #ifdef ENABLE_ACLK
1186 if (netdata_cloud_enabled)
1190 - sql_queue_alarm_to_aclk(host, ae, 1);
1187 + sql_queue_alarm_to_aclk(host, ae, true);
1188 #endif
1189 }
1190 }