@cryptotaxi247 / netdata-1 / commits / 9688012a1

Alert transitions code cleanup (#17103)

* Separate function to add entries in health_log_detail * Code cleanup * Function returns void * Use prepared statements * Remove unused definitions * Simplify bind params with a macro * Switch macro params * Change param order of SQLITE3_BIND_STRING_OR_NULL * Use macro in more places * More code changes * Rebase

Stelios Fragkakis committed Apr 16, 2024 at 18:56 UTC 9688012a15bff3ae80b010a328e980c2f4c60061
7 files changed +314 -672
src/database/sqlite/sqlite_aclk_alert.c
+18 -34
@@ -29,23 +29,18 @@ static void update_filtered(ALARM_ENTRY *ae, int64_t unique_id, char *uuid_str)
29 return;
30 }
31
32 - rc = sqlite3_bind_int64(res, 1, ae->unique_id);
33 - if (unlikely(rc != SQLITE_OK)) {
34 - error_report("Failed to bind ae unique_id for update_filtered");
35 - goto done;
36 - }
37 -
38 - rc = sqlite3_bind_int64(res, 2, unique_id);
39 - if (unlikely(rc != SQLITE_OK)) {
40 - error_report("Failed to bind unique_id for update_filtered");
41 - goto done;
42 - }
32 + int param = 0;
33 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, ae->unique_id));
34 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, unique_id));
35
36 + param = 0;
37 rc = sqlite3_step_monitored(res);
38 if (likely(rc == SQLITE_DONE))
39 ae->flags |= HEALTH_ENTRY_FLAG_ACLK_QUEUED;
40
41 done:
42 + REPORT_BIND_FAIL(res, param);
43 +
44 rc = sqlite3_finalize(res);
45 if (unlikely(rc != SQLITE_OK))
46 error_report("Failed to finalize statement when trying to update_filtered, rc = %d", rc);
@@ -68,23 +63,18 @@ static inline bool is_event_from_alert_variable_config(int64_t unique_id, uuid_t
63
64 bool ret = false;
65
71 - rc = sqlite3_bind_int64(res, 1, unique_id);
72 - if (unlikely(rc != SQLITE_OK)) {
73 - error_report("Failed to bind unique_id for checking alert variable.");
74 - goto done;
75 - }
76 -
77 - rc = sqlite3_bind_blob(res, 2, host_id, sizeof(*host_id), SQLITE_STATIC);
78 - if (unlikely(rc != SQLITE_OK)) {
79 - error_report("Failed to bind host_id for checking alert variable.");
80 - goto done;
81 - }
66 + int param = 0;
67 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, unique_id));
68 + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, host_id, sizeof(*host_id), SQLITE_STATIC));
69
70 + param = 0;
71 rc = sqlite3_step_monitored(res);
72 if (likely(rc == SQLITE_ROW))
73 ret = true;
74
75 done:
76 + REPORT_BIND_FAIL(res, param);
77 +
78 rc = sqlite3_finalize(res);
79 if (unlikely(rc != SQLITE_OK))
80 error_report("Failed to finalize statement when trying to check for alert variables, rc = %d", rc);
@@ -125,20 +115,12 @@ static bool should_send_to_cloud(RRDHOST *host, ALARM_ENTRY *ae)
115
116 bool send = false;
117
128 - rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
129 - if (unlikely(rc != SQLITE_OK)) {
130 - error_report("Failed to bind host_id for checking should_send_to_cloud");
131 - goto done;
132 - }
133 -
134 - rc = sqlite3_bind_int(res, 2, (int) ae->alarm_id);
135 - if (unlikely(rc != SQLITE_OK)) {
136 - error_report("Failed to bind alarm_id for checking should_send_to_cloud");
137 - goto done;
138 - }
118 + int param = 0;
119 + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC));
120 + SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, (int) ae->alarm_id));
121
122 + param = 0;
123 rc = sqlite3_step_monitored(res);
141 -
124 if (likely(rc == SQLITE_ROW)) {
125 uuid_t config_hash_id;
126 RRDCALC_STATUS status = (RRDCALC_STATUS)sqlite3_column_int(res, 0);
@@ -156,6 +138,8 @@ static bool should_send_to_cloud(RRDHOST *host, ALARM_ENTRY *ae)
138 send = true;
139
140 done:
141 + REPORT_BIND_FAIL(res, param);
142 +
143 rc = sqlite3_finalize(res);
144 if (unlikely(rc != SQLITE_OK))
145 error_report("Failed to finalize statement when trying should_send_to_cloud, rc = %d", rc);
src/database/sqlite/sqlite_context.h
-1
@@ -66,6 +66,5 @@ int ctx_delete_context(uuid_t *host_id, VERSIONED_CONTEXT_DATA *context_data);
66
67 int sql_init_context_database(int memory);
68 uint64_t sqlite_get_context_space(void);
69 -void sql_close_context_database(void);
69 int ctx_unittest(void);
70 #endif //NETDATA_SQLITE_CONTEXT_H
src/database/sqlite/sqlite_functions.h
+20
@@ -8,6 +8,26 @@
8
9 void analytics_set_data_str(char **name, const char *value);
10
11 +#define SQLITE_BIND_FAIL(label, rc) \
12 + do { \
13 + if ((rc) != SQLITE_OK) \
14 + goto label; \
15 + } while (0)
16 +
17 +#define REPORT_BIND_FAIL(res, param) \
18 + do { \
19 + if (unlikely((param))) { \
20 + const char *failed_param = sqlite3_bind_parameter_name((res), (param)); \
21 + nd_log( \
22 + NDLS_DAEMON, \
23 + NDLP_ERR, \
24 + "Failed to bind parameter %d (%s) in %s", \
25 + (param), \
26 + failed_param ? failed_param : "?", \
27 + __FUNCTION__); \
28 + } \
29 + } while (0)
30 +
31 #define SQL_MAX_RETRY (100)
32 #define SQLITE_INSERT_DELAY (10) // Insert delay in case of lock
33
src/database/sqlite/sqlite_health.c
+274 -634
@@ -6,8 +6,8 @@
6 #include "health/health_internals.h"
7
8 #define MAX_HEALTH_SQL_SIZE 2048
9 -#define SQLITE3_BIND_STRING_OR_NULL(res, key, param) \
10 - ((key) ? sqlite3_bind_text(res, param, string2str(key), -1, SQLITE_STATIC) : sqlite3_bind_null(res, param))
9 +#define SQLITE3_BIND_STRING_OR_NULL(res, param, key) \
10 + ((key) ? sqlite3_bind_text((res), (param), string2str(key), -1, SQLITE_STATIC) : sqlite3_bind_null((res), (param)))
11
12 #define SQLITE3_COLUMN_STRINGDUP_OR_NULL(res, param) \
13 ({ \
@@ -26,7 +26,7 @@
26
27 static void sql_health_alarm_log_update(RRDHOST *host, ALARM_ENTRY *ae)
28 {
29 - sqlite3_stmt *res = NULL;
29 + static __thread sqlite3_stmt *res = NULL;
30 int rc;
31
32 if (unlikely(!db_meta)) {
@@ -35,75 +35,40 @@ static void sql_health_alarm_log_update(RRDHOST *host, ALARM_ENTRY *ae)
35 return;
36 }
37
38 - rc = sqlite3_prepare_v2(db_meta, SQL_UPDATE_HEALTH_LOG, -1, &res, 0);
39 - if (unlikely(rc != SQLITE_OK)) {
40 - error_report("HEALTH [%s]: Failed to prepare statement for SQL_UPDATE_HEALTH_LOG", rrdhost_hostname(host));
41 - return;
42 - }
43 -
44 - rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) ae->updated_by_id);
45 - if (unlikely(rc != SQLITE_OK)) {
46 - error_report("Failed to bind updated_by_id parameter for SQL_UPDATE_HEALTH_LOG");
47 - goto failed;
48 - }
49 -
50 - rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) ae->flags);
51 - if (unlikely(rc != SQLITE_OK)) {
52 - error_report("Failed to bind flags parameter for SQL_UPDATE_HEALTH_LOG");
53 - goto failed;
54 - }
55 -
56 - rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) ae->exec_run_timestamp);
57 - if (unlikely(rc != SQLITE_OK)) {
58 - error_report("Failed to bind exec_run_timestamp parameter for SQL_UPDATE_HEALTH_LOG");
59 - goto failed;
60 - }
61 -
62 - rc = sqlite3_bind_int(res, 4, ae->exec_code);
63 - if (unlikely(rc != SQLITE_OK)) {
64 - error_report("Failed to bind exec_code parameter for SQL_UPDATE_HEALTH_LOG");
65 - goto failed;
66 - }
67 -
68 - rc = sqlite3_bind_int64(res, 5, (sqlite3_int64) ae->unique_id);
69 - if (unlikely(rc != SQLITE_OK)) {
70 - error_report("Failed to bind unique_id parameter for SQL_UPDATE_HEALTH_LOG");
71 - goto failed;
72 - }
73 -
74 - rc = sqlite3_bind_int64(res, 6, (sqlite3_int64) ae->alarm_id);
75 - if (unlikely(rc != SQLITE_OK)) {
76 - error_report("Failed to bind unique_id parameter for SQL_UPDATE_HEALTH_LOG");
77 - goto failed;
38 + if (unlikely(!res)) {
39 + rc = prepare_statement(db_meta, SQL_UPDATE_HEALTH_LOG, &res);
40 + if (unlikely(rc != SQLITE_OK)) {
41 + error_report("HEALTH [%s]: Failed to prepare statement for SQL_UPDATE_HEALTH_LOG", rrdhost_hostname(host));
42 + return;
43 + }
44 }
45
80 - rc = sqlite3_bind_blob(res, 7, &ae->transition_id, sizeof(ae->transition_id), SQLITE_STATIC);
81 - if (unlikely(rc != SQLITE_OK)) {
82 - error_report("Failed to bind host_id for SQL_UPDATE_HEALTH_LOG.");
83 - goto failed;
84 - }
46 + int param = 0;
47 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64) ae->updated_by_id));
48 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64) ae->flags));
49 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64) ae->exec_run_timestamp));
50 + SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, ae->exec_code));
51 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64) ae->unique_id));
52 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64) ae->alarm_id));
53 + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, &ae->transition_id, sizeof(ae->transition_id), SQLITE_STATIC));
54
55 + param = 0;
56 rc = execute_insert(res);
57 if (unlikely(rc != SQLITE_DONE)) {
58 error_report("HEALTH [%s]: Failed to update health log, rc = %d", rrdhost_hostname(host), rc);
59 }
60
91 -failed:
92 - if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
93 - error_report("HEALTH [%s]: Failed to finalize the prepared statement for updating health log.", rrdhost_hostname(host));
61 +done:
62 + REPORT_BIND_FAIL(res, param);
63 +
64 + if (unlikely(sqlite3_reset(res) != SQLITE_OK))
65 + error_report("HEALTH [%s]: Failed to reset statement for updating health log.", rrdhost_hostname(host));
66 }
67
68 /* Health related SQL queries
69 Inserts an entry in the table
70 */
71
100 -#define SQL_INSERT_HEALTH_LOG \
101 - "INSERT INTO health_log (host_id, alarm_id, " \
102 - "config_hash_id, name, chart, exec, recipient, units, chart_context, last_transition_id, chart_name) " \
103 - "VALUES (@host_id,@alarm_id, @config_hash_id,@name,@chart,@exec,@recipient,@units,@chart_context," \
104 - "@last_transition_id,@chart_name) ON CONFLICT (host_id, alarm_id) DO UPDATE " \
105 - "SET last_transition_id = excluded.last_transition_id, chart_name = excluded.chart_name, " \
106 - "config_hash_id=excluded.config_hash_id RETURNING health_log_id"
72
73 #define SQL_INSERT_HEALTH_LOG_DETAIL \
74 "INSERT INTO health_log_detail (health_log_id, unique_id, alarm_id, alarm_event_id, " \
@@ -113,256 +78,114 @@ failed:
78 "@non_clear_duration,@flags,@exec_run_timestamp,@delay_up_to_timestamp, @info,@exec_code,@new_status,@old_status," \
79 "@delay,@new_value,@old_value,@last_repeat,@transition_id,@global_id,@summary)"
80
116 -static void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae) {
117 - sqlite3_stmt *res = NULL;
81 +static void sql_health_alarm_log_insert_detail(RRDHOST *host, uint64_t health_log_id, ALARM_ENTRY *ae)
82 +{
83 + static __thread sqlite3_stmt *res = NULL;
84 int rc;
119 - uint64_t health_log_id = 0;
120 -
121 - if (unlikely(!db_meta)) {
122 - if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
123 - error_report("HEALTH [%s]: Database has not been initialized", rrdhost_hostname(host));
124 - return;
125 - }
126 -
127 - rc = sqlite3_prepare_v2(db_meta, SQL_INSERT_HEALTH_LOG, -1, &res, 0);
128 - if (unlikely(rc != SQLITE_OK)) {
129 - error_report("HEALTH [%s]: Failed to prepare statement for SQL_INSERT_HEALTH_LOG", rrdhost_hostname(host));
130 - return;
131 - }
85
133 - rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
134 - if (unlikely(rc != SQLITE_OK)) {
135 - error_report("Failed to bind host_id for SQL_INSERT_HEALTH_LOG.");
136 - goto failed;
137 - }
138 -
139 - rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) ae->alarm_id);
140 - if (unlikely(rc != SQLITE_OK)) {
141 - error_report("Failed to bind alarm_id parameter for SQL_INSERT_HEALTH_LOG");
142 - goto failed;
143 - }
144 -
145 - rc = sqlite3_bind_blob(res, 3, &ae->config_hash_id, sizeof(ae->config_hash_id), SQLITE_STATIC);
146 - if (unlikely(rc != SQLITE_OK)) {
147 - error_report("Failed to bind config_hash_id parameter for SQL_INSERT_HEALTH_LOG");
148 - goto failed;
149 - }
150 -
151 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->name, 4);
152 - if (unlikely(rc != SQLITE_OK)) {
153 - error_report("Failed to bind name parameter for SQL_INSERT_HEALTH_LOG");
154 - goto failed;
86 + if (unlikely(!res)) {
87 + rc = prepare_statement(db_meta, SQL_INSERT_HEALTH_LOG_DETAIL, &res);
88 + if (unlikely(rc != SQLITE_OK)) {
89 + error_report(
90 + "HEALTH [%s]: Failed to prepare statement for SQL_INSERT_HEALTH_LOG_DETAIL", rrdhost_hostname(host));
91 + return;
92 + }
93 }
94
157 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->chart, 5);
158 - if (unlikely(rc != SQLITE_OK)) {
159 - error_report("Failed to bind chart parameter for SQL_INSERT_HEALTH_LOG");
160 - goto failed;
161 - }
95 + int param = 0;
96 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)health_log_id));
97 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)ae->unique_id));
98 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)ae->alarm_id));
99 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)ae->alarm_event_id));
100 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)ae->updated_by_id));
101 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)ae->updates_id));
102 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)ae->when));
103 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)ae->duration));
104 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)ae->non_clear_duration));
105 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)ae->flags));
106 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)ae->exec_run_timestamp));
107 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)ae->delay_up_to_timestamp));
108 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ae->info));
109 + SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, ae->exec_code));
110 + SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, ae->new_status));
111 + SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, ae->old_status));
112 + SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, ae->delay));
113 + SQLITE_BIND_FAIL(done, sqlite3_bind_double(res, ++param, ae->new_value));
114 + SQLITE_BIND_FAIL(done, sqlite3_bind_double(res, ++param, ae->old_value));
115 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)ae->last_repeat));
116 + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, &ae->transition_id, sizeof(ae->transition_id), SQLITE_STATIC));
117 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)ae->global_id));
118 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ae->summary));
119 +
120 + param = 0;
121 + rc = execute_insert(res);
122 + if (rc == SQLITE_DONE)
123 + ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
124 + else
125 + error_report(
126 + "HEALTH [%s]: Failed to execute SQL_INSERT_HEALTH_LOG_DETAIL, rc = %d", rrdhost_hostname(host), rc);
127
163 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->exec, 6);
164 - if (unlikely(rc != SQLITE_OK)) {
165 - error_report("Failed to bind exec parameter for SQL_INSERT_HEALTH_LOG");
166 - goto failed;
167 - }
128 +done:
129 + REPORT_BIND_FAIL(res, param);
130
169 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->recipient, 7);
170 - if (unlikely(rc != SQLITE_OK)) {
171 - error_report("Failed to bind recipient parameter for SQL_INSERT_HEALTH_LOG");
172 - goto failed;
173 - }
131 + if (unlikely(sqlite3_reset(res) != SQLITE_OK))
132 + error_report("HEALTH [%s]: Failed to reset statement for inserting to health log detail", rrdhost_hostname(host));
133 +}
134
175 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->units, 8);
176 - if (unlikely(rc != SQLITE_OK)) {
177 - error_report("Failed to bind host_id parameter to store node instance information");
178 - goto failed;
179 - }
135 +#define SQL_INSERT_HEALTH_LOG \
136 + "INSERT INTO health_log (host_id, alarm_id, " \
137 + "config_hash_id, name, chart, exec, recipient, units, chart_context, last_transition_id, chart_name) " \
138 + "VALUES (@host_id,@alarm_id, @config_hash_id,@name,@chart,@exec,@recipient,@units,@chart_context," \
139 + "@last_transition_id,@chart_name) ON CONFLICT (host_id, alarm_id) DO UPDATE " \
140 + "SET last_transition_id = excluded.last_transition_id, chart_name = excluded.chart_name, " \
141 + "config_hash_id=excluded.config_hash_id RETURNING health_log_id"
142
181 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->chart_context, 9);
182 - if (unlikely(rc != SQLITE_OK)) {
183 - error_report("Failed to bind chart_context parameter for SQL_INSERT_HEALTH_LOG");
184 - goto failed;
185 - }
143 +static void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae)
144 +{
145 + static __thread sqlite3_stmt *res = NULL;
146 + int rc;
147 + uint64_t health_log_id;
148
187 - rc = sqlite3_bind_blob(res, 10, &ae->transition_id, sizeof(ae->transition_id), SQLITE_STATIC);
188 - if (unlikely(rc != SQLITE_OK)) {
189 - error_report("Failed to bind transition_id parameter for SQL_INSERT_HEALTH_LOG");
190 - goto failed;
149 + if (unlikely(!db_meta)) {
150 + if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
151 + error_report("HEALTH [%s]: Database has not been initialized", rrdhost_hostname(host));
152 + return;
153 }
154
193 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->chart_name, 11);
194 - if (unlikely(rc != SQLITE_OK)) {
195 - error_report("Failed to bind chart_name parameter for SQL_INSERT_HEALTH_LOG");
196 - goto failed;
155 + if (unlikely(!res)) {
156 + rc = prepare_statement(db_meta, SQL_INSERT_HEALTH_LOG, &res);
157 + if (unlikely(rc != SQLITE_OK)) {
158 + error_report("HEALTH [%s]: Failed to prepare statement for SQL_INSERT_HEALTH_LOG", rrdhost_hostname(host));
159 + return;
160 + }
161 }
162
163 + int param = 0;
164 + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC));
165 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64) ae->alarm_id));
166 + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, &ae->config_hash_id, sizeof(ae->config_hash_id), SQLITE_STATIC));
167 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ae->name));
168 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ae->chart));
169 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ae->exec));
170 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ae->recipient));
171 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ae->units));
172 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ae->chart_context));
173 + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, &ae->transition_id, sizeof(ae->transition_id), SQLITE_STATIC));
174 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ae->chart_name));
175 +
176 + param = 0;
177 rc = sqlite3_step_monitored(res);
200 - if (likely(rc == SQLITE_ROW))
201 - health_log_id = (size_t) sqlite3_column_int64(res, 0);
202 - else {
178 + if (rc == SQLITE_ROW) {
179 + health_log_id = (size_t)sqlite3_column_int64(res, 0);
180 + sql_health_alarm_log_insert_detail(host, health_log_id, ae);
181 + } else
182 error_report("HEALTH [%s]: Failed to execute SQL_INSERT_HEALTH_LOG, rc = %d", rrdhost_hostname(host), rc);
204 - goto failed;
205 - }
183
207 - rc = sqlite3_finalize(res);
208 - if (unlikely(rc != SQLITE_OK))
209 - error_report("HEALTH [%s]: Failed to finalize the prepared statement for inserting to health log.", rrdhost_hostname(host));
210 -
211 - rc = sqlite3_prepare_v2(db_meta, SQL_INSERT_HEALTH_LOG_DETAIL, -1, &res, 0);
212 - if (unlikely(rc != SQLITE_OK)) {
213 - error_report("HEALTH [%s]: Failed to prepare statement for SQL_INSERT_HEALTH_LOG_DETAIL", rrdhost_hostname(host));
214 - return;
215 - }
216 -
217 - rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) health_log_id);
218 - if (unlikely(rc != SQLITE_OK)) {
219 - error_report("Failed to bind unique_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
220 - goto failed;
221 - }
222 -
223 - rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) ae->unique_id);
224 - if (unlikely(rc != SQLITE_OK)) {
225 - error_report("Failed to bind unique_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
226 - goto failed;
227 - }
228 -
229 - rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) ae->alarm_id);
230 - if (unlikely(rc != SQLITE_OK)) {
231 - error_report("Failed to bind unique_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
232 - goto failed;
233 - }
234 -
235 - rc = sqlite3_bind_int64(res, 4, (sqlite3_int64) ae->alarm_event_id);
236 - if (unlikely(rc != SQLITE_OK)) {
237 - error_report("Failed to bind alarm_event_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
238 - goto failed;
239 - }
240 -
241 - rc = sqlite3_bind_int64(res, 5, (sqlite3_int64) ae->updated_by_id);
242 - if (unlikely(rc != SQLITE_OK)) {
243 - error_report("Failed to bind updated_by_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
244 - goto failed;
245 - }
246 -
247 - rc = sqlite3_bind_int64(res, 6, (sqlite3_int64) ae->updates_id);
248 - if (unlikely(rc != SQLITE_OK)) {
249 - error_report("Failed to bind updates_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
250 - goto failed;
251 - }
252 -
253 - rc = sqlite3_bind_int64(res, 7, (sqlite3_int64) ae->when);
254 - if (unlikely(rc != SQLITE_OK)) {
255 - error_report("Failed to bind when parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
256 - goto failed;
257 - }
258 -
259 - rc = sqlite3_bind_int64(res, 8, (sqlite3_int64) ae->duration);
260 - if (unlikely(rc != SQLITE_OK)) {
261 - error_report("Failed to bind duration parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
262 - goto failed;
263 - }
264 -
265 - rc = sqlite3_bind_int64(res, 9, (sqlite3_int64) ae->non_clear_duration);
266 - if (unlikely(rc != SQLITE_OK)) {
267 - error_report("Failed to bind non_clear_duration parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
268 - goto failed;
269 - }
270 -
271 - rc = sqlite3_bind_int64(res, 10, (sqlite3_int64) ae->flags);
272 - if (unlikely(rc != SQLITE_OK)) {
273 - error_report("Failed to bind flags parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
274 - goto failed;
275 - }
276 -
277 - rc = sqlite3_bind_int64(res, 11, (sqlite3_int64) ae->exec_run_timestamp);
278 - if (unlikely(rc != SQLITE_OK)) {
279 - error_report("Failed to bind exec_run_timestamp parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
280 - goto failed;
281 - }
282 -
283 - rc = sqlite3_bind_int64(res, 12, (sqlite3_int64) ae->delay_up_to_timestamp);
284 - if (unlikely(rc != SQLITE_OK)) {
285 - error_report("Failed to bind delay_up_to_timestamp parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
286 - goto failed;
287 - }
288 -
289 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->info, 13);
290 - if (unlikely(rc != SQLITE_OK)) {
291 - error_report("Failed to bind info parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
292 - goto failed;
293 - }
294 -
295 - rc = sqlite3_bind_int(res, 14, ae->exec_code);
296 - if (unlikely(rc != SQLITE_OK)) {
297 - error_report("Failed to bind exec_code parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
298 - goto failed;
299 - }
300 -
301 - rc = sqlite3_bind_int(res, 15, ae->new_status);
302 - if (unlikely(rc != SQLITE_OK)) {
303 - error_report("Failed to bind new_status parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
304 - goto failed;
305 - }
306 -
307 - rc = sqlite3_bind_int(res, 16, ae->old_status);
308 - if (unlikely(rc != SQLITE_OK)) {
309 - error_report("Failed to bind old_status parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
310 - goto failed;
311 - }
312 -
313 - rc = sqlite3_bind_int(res, 17, ae->delay);
314 - if (unlikely(rc != SQLITE_OK)) {
315 - error_report("Failed to bind delay parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
316 - goto failed;
317 - }
318 -
319 - rc = sqlite3_bind_double(res, 18, ae->new_value);
320 - if (unlikely(rc != SQLITE_OK)) {
321 - error_report("Failed to bind new_value parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
322 - goto failed;
323 - }
324 -
325 - rc = sqlite3_bind_double(res, 19, ae->old_value);
326 - if (unlikely(rc != SQLITE_OK)) {
327 - error_report("Failed to bind old_value parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
328 - goto failed;
329 - }
330 -
331 - rc = sqlite3_bind_int64(res, 20, (sqlite3_int64) ae->last_repeat);
332 - if (unlikely(rc != SQLITE_OK)) {
333 - error_report("Failed to bind last_repeat parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
334 - goto failed;
335 - }
336 -
337 - rc = sqlite3_bind_blob(res, 21, &ae->transition_id, sizeof(ae->transition_id), SQLITE_STATIC);
338 - if (unlikely(rc != SQLITE_OK)) {
339 - error_report("Failed to bind transition_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
340 - goto failed;
341 - }
342 -
343 - rc = sqlite3_bind_int64(res, 22, (sqlite3_int64) ae->global_id);
344 - if (unlikely(rc != SQLITE_OK)) {
345 - error_report("Failed to bind global_id parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
346 - goto failed;
347 - }
348 -
349 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ae->summary, 23);
350 - if (unlikely(rc != SQLITE_OK)) {
351 - error_report("Failed to bind summary parameter for SQL_INSERT_HEALTH_LOG_DETAIL");
352 - goto failed;
353 - }
354 -
355 - rc = execute_insert(res);
356 - if (unlikely(rc != SQLITE_DONE)) {
357 - error_report("HEALTH [%s]: Failed to execute SQL_INSERT_HEALTH_LOG_DETAIL, rc = %d", rrdhost_hostname(host), rc);
358 - goto failed;
359 - }
360 -
361 - ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
184 +done:
185 + REPORT_BIND_FAIL(res, param);
186
363 -failed:
364 - if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
365 - error_report("HEALTH [%s]: Failed to finalize the prepared statement for inserting to health log.", rrdhost_hostname(host));
187 + if (unlikely(sqlite3_reset(res) != SQLITE_OK))
188 + error_report("HEALTH [%s]: Failed to reset statement for inserting to health log", rrdhost_hostname(host));
189 }
190
191 void sql_health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae)
@@ -433,18 +256,11 @@ void sql_health_alarm_log_cleanup(RRDHOST *host, bool claimed) {
256 return;
257 }
258
436 - rc = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
437 - if (unlikely(rc != SQLITE_OK)) {
438 - error_report("Failed to bind first host_id for sql_health_alarm_log_cleanup.");
439 - goto done;
440 - }
441 -
442 - rc = sqlite3_bind_int64(res, 2, (sqlite3_int64)host->health_log.health_log_history);
443 - if (unlikely(rc != SQLITE_OK)) {
444 - error_report("Failed to bind health log history for sql_health_alarm_log_cleanup.");
445 - goto done;
446 - }
259 + int param = 0;
260 + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC));
261 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)host->health_log.health_log_history));
262
263 + param = 0;
264 rc = sqlite3_step_monitored(res);
265 if (unlikely(rc != SQLITE_DONE))
266 error_report("Failed to cleanup health log detail table, rc = %d", rc);
@@ -453,6 +269,8 @@ void sql_health_alarm_log_cleanup(RRDHOST *host, bool claimed) {
269 sql_aclk_alert_clean_dead_entries(host);
270
271 done:
272 + REPORT_BIND_FAIL(res, param);
273 +
274 rc = sqlite3_finalize(res);
275 if (unlikely(rc != SQLITE_OK))
276 error_report("Failed to finalize the prepared statement to cleanup health log detail table (claimed)");
@@ -471,153 +289,115 @@ done:
289 #define SQL_INJECT_REMOVED_UPDATE_LOG \
290 "UPDATE health_log SET last_transition_id = ?1 WHERE alarm_id = ?2 AND last_transition_id = ?3 AND host_id = ?4"
291
474 -void sql_inject_removed_status(
475 - RRDHOST *host,
476 - uint32_t alarm_id,
477 - uint32_t alarm_event_id,
478 - uint32_t unique_id,
479 - uint32_t max_unique_id,
480 - uuid_t *prev_transition_id)
292 +bool sql_update_removed_in_health_log(RRDHOST *host, uint32_t alarm_id, uuid_t *transition_id, uuid_t *last_transition)
293 {
294 int rc;
295 + sqlite3_stmt *res;
296
484 - if (!alarm_id || !alarm_event_id || !unique_id || !max_unique_id)
485 - return;
486 -
487 - sqlite3_stmt *res = NULL;
488 -
489 - rc = sqlite3_prepare_v2(db_meta, SQL_INJECT_REMOVED, -1, &res, 0);
297 + rc = sqlite3_prepare_v2(db_meta, SQL_INJECT_REMOVED_UPDATE_LOG, -1, &res, 0);
298 if (rc != SQLITE_OK) {
491 - error_report("Failed to prepare statement when trying to inject removed event");
492 - return;
299 + error_report("Failed to prepare statement when trying to update health_log during inject removed event");
300 + return false;
301 }
302
495 - rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) max_unique_id);
496 - if (unlikely(rc != SQLITE_OK)) {
497 - error_report("Failed to bind max_unique_id parameter for SQL_INJECT_REMOVED");
498 - goto failed;
499 - }
303 + int param = 0;
304 + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, transition_id, sizeof(*transition_id), SQLITE_STATIC));
305 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)alarm_id));
306 + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, last_transition, sizeof(*last_transition), SQLITE_STATIC));
307 + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC));
308
501 - rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) alarm_id);
502 - if (unlikely(rc != SQLITE_OK)) {
503 - error_report("Failed to bind alarm_id parameter for SQL_INJECT_REMOVED");
504 - goto failed;
505 - }
309 + param = 0;
310 + rc = execute_insert(res);
311 + if (unlikely(rc != SQLITE_DONE))
312 + error_report("HEALTH [N/A]: Failed to execute SQL_INJECT_REMOVED_UPDATE_DETAIL, rc = %d", rc);
313
507 - rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) alarm_event_id + 1);
508 - if (unlikely(rc != SQLITE_OK)) {
509 - error_report("Failed to bind alarm_event_id parameter for SQL_INJECT_REMOVED");
510 - goto failed;
511 - }
314 +done:
315 + REPORT_BIND_FAIL(res, param);
316
513 - rc = sqlite3_bind_int64(res, 4, (sqlite3_int64) unique_id);
514 - if (unlikely(rc != SQLITE_OK)) {
515 - error_report("Failed to bind unique_id parameter for SQL_INJECT_REMOVED");
516 - goto failed;
517 - }
317 + if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
318 + error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event.");
319
519 - uuid_t transition_id;
520 - uuid_generate_random(transition_id);
521 - rc = sqlite3_bind_blob(res, 5, &transition_id, sizeof(transition_id), SQLITE_STATIC);
522 - if (unlikely(rc != SQLITE_OK)) {
523 - error_report("Failed to bind config_hash_id parameter for SQL_INJECT_REMOVED");
524 - goto failed;
525 - }
320 + return (param == 0 && rc == SQLITE_DONE);
321 +}
322
527 - rc = sqlite3_bind_int64(res, 6, (sqlite3_int64) unique_id);
528 - if (unlikely(rc != SQLITE_OK)) {
529 - error_report("Failed to bind unique_id parameter for SQL_INJECT_REMOVED");
530 - goto failed;
531 - }
323 +bool sql_update_removed_in_health_log_detail(uint32_t unique_id, uint32_t max_unique_id, uuid_t *prev_transition_id)
324 +{
325 + int rc;
326 + sqlite3_stmt *res;
327
533 - rc = sqlite3_bind_blob(res, 7, prev_transition_id, sizeof(*prev_transition_id), SQLITE_STATIC);
534 - if (unlikely(rc != SQLITE_OK)) {
535 - error_report("Failed to bind host_id parameter for SQL_INJECT_REMOVED.");
536 - goto failed;
328 + rc = sqlite3_prepare_v2(db_meta, SQL_INJECT_REMOVED_UPDATE_DETAIL, -1, &res, 0);
329 + if (rc != SQLITE_OK) {
330 + error_report("Failed to prepare statement when trying to update health_log_detail during inject removed event");
331 + return false;
332 }
333
334 + int param = 0;
335 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64) HEALTH_ENTRY_FLAG_UPDATED));
336 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64) max_unique_id));
337 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64) unique_id));
338 + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, prev_transition_id, sizeof(*prev_transition_id), SQLITE_STATIC));
339 +
340 + param = 0;
341 rc = execute_insert(res);
540 - if (unlikely(rc != SQLITE_DONE)) {
541 - error_report("HEALTH [N/A]: Failed to execute SQL_INJECT_REMOVED, rc = %d", rc);
542 - goto failed;
543 - }
342 + if (unlikely(rc != SQLITE_DONE))
343 + error_report("HEALTH [N/A]: Failed to execute SQL_INJECT_REMOVED_UPDATE_DETAIL, rc = %d", rc);
344 +
345 +done:
346 + REPORT_BIND_FAIL(res, param);
347
348 if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
349 error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event.");
350
548 - //update the old entry in health_log_detail
549 - rc = sqlite3_prepare_v2(db_meta, SQL_INJECT_REMOVED_UPDATE_DETAIL, -1, &res, 0);
550 - if (rc != SQLITE_OK) {
551 - error_report("Failed to prepare statement when trying to update health_log_detail during inject removed event");
552 - return;
553 - }
554 -
555 - rc = sqlite3_bind_int64(res, 1, (sqlite3_int64) HEALTH_ENTRY_FLAG_UPDATED);
556 - if (unlikely(rc != SQLITE_OK)) {
557 - error_report("Failed to bind flags parameter for SQL_INJECT_REMOVED_UPDATE_DETAIL");
558 - goto failed;
559 - }
560 -
561 - rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) max_unique_id);
562 - if (unlikely(rc != SQLITE_OK)) {
563 - error_report("Failed to bind max_unique_id parameter for SQL_INJECT_REMOVED_UPDATE_DETAIL");
564 - goto failed;
565 - }
351 + return (param == 0 && rc == SQLITE_DONE);
352 +}
353
567 - rc = sqlite3_bind_int64(res, 3, (sqlite3_int64) unique_id);
568 - if (unlikely(rc != SQLITE_OK)) {
569 - error_report("Failed to bind unique_id parameter for SQL_INJECT_REMOVED_UPDATE_DETAIL");
570 - goto failed;
571 - }
354 +void sql_inject_removed_status(
355 + RRDHOST *host,
356 + uint32_t alarm_id,
357 + uint32_t alarm_event_id,
358 + uint32_t unique_id,
359 + uint32_t max_unique_id,
360 + uuid_t *last_transition)
361 +{
362 + int rc;
363
573 - rc = sqlite3_bind_blob(res, 4, prev_transition_id, sizeof(*prev_transition_id), SQLITE_STATIC);
574 - if (unlikely(rc != SQLITE_OK)) {
575 - error_report("Failed to bind host_id parameter for SQL_INJECT_REMOVED_UPDATE_DETAIL");
576 - goto failed;
577 - }
364 + if (!alarm_id || !alarm_event_id || !unique_id || !max_unique_id)
365 + return;
366
579 - rc = execute_insert(res);
580 - if (unlikely(rc != SQLITE_DONE)) {
581 - error_report("HEALTH [N/A]: Failed to execute SQL_INJECT_REMOVED_UPDATE_DETAIL, rc = %d", rc);
582 - goto failed;
583 - }
367 + sqlite3_stmt *res = NULL;
368
585 - //update the health_log_table
586 - rc = sqlite3_prepare_v2(db_meta, SQL_INJECT_REMOVED_UPDATE_LOG, -1, &res, 0);
369 + rc = sqlite3_prepare_v2(db_meta, SQL_INJECT_REMOVED, -1, &res, 0);
370 if (rc != SQLITE_OK) {
588 - error_report("Failed to prepare statement when trying to update health_log during inject removed event");
371 + error_report("Failed to prepare statement when trying to inject removed event");
372 return;
373 }
374
592 - rc = sqlite3_bind_blob(res, 1, &transition_id, sizeof(transition_id), SQLITE_STATIC);
593 - if (unlikely(rc != SQLITE_OK)) {
594 - error_report("Failed to bind host_id parameter for SQL_INJECT_REMOVED_UPDATE_LOG");
595 - goto failed;
596 - }
597 -
598 - rc = sqlite3_bind_int64(res, 2, (sqlite3_int64) alarm_id);
599 - if (unlikely(rc != SQLITE_OK)) {
600 - error_report("Failed to bind unique_id parameter for SQL_INJECT_REMOVED_UPDATE_DETAIL");
601 - goto failed;
602 - }
375 + uuid_t transition_id;
376 + uuid_generate_random(transition_id);
377
604 - rc = sqlite3_bind_blob(res, 3, prev_transition_id, sizeof(*prev_transition_id), SQLITE_STATIC);
605 - if (unlikely(rc != SQLITE_OK)) {
606 - error_report("Failed to bind host_id parameter for SQL_INJECT_REMOVED_UPDATE_LOG");
607 - goto failed;
608 - }
378 + int param = 0;
379 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64) max_unique_id));
380 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64) alarm_id));
381 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64) alarm_event_id + 1));
382 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64) unique_id));
383 + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, &transition_id, sizeof(transition_id), SQLITE_STATIC));
384 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64) unique_id));
385 + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, last_transition, sizeof(*last_transition), SQLITE_STATIC));
386
610 - rc = sqlite3_bind_blob(res, 4, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
611 - if (unlikely(rc != SQLITE_OK)) {
612 - error_report("Failed to bind host_id parameter for SQL_INJECT_REMOVED_UPDATE_DETAIL");
613 - goto failed;
387 + param = 0;
388 + rc = execute_insert(res);
389 + if (rc == SQLITE_DONE) {
390 + //update the old entry in health_log_detail
391 + sql_update_removed_in_health_log_detail(unique_id, max_unique_id, last_transition);
392 + //update the old entry in health_log
393 + sql_update_removed_in_health_log(host, alarm_id, &transition_id, last_transition);
394 }
395 + else
396 + error_report("HEALTH [N/A]: Failed to execute SQL_INJECT_REMOVED, rc = %d", rc);
397
616 - rc = execute_insert(res);
617 - if (unlikely(rc != SQLITE_DONE))
618 - error_report("HEALTH [N/A]: Failed to execute SQL_INJECT_REMOVED_UPDATE_DETAIL, rc = %d", rc);
398 +done:
399 + REPORT_BIND_FAIL(res, param);
400
620 -failed:
401 if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
402 error_report("HEALTH [N/A]: Failed to finalize the prepared statement for injecting removed event.");
403 }
@@ -979,121 +759,64 @@ void sql_health_alarm_log_load(RRDHOST *host)
759 "@p_db_lookup_before,@p_update_every,@source,@chart_labels,@summary, @time_group_condition, " \
760 "@time_group_value, @dims_group, @data_source)"
761
982 -int sql_alert_store_config(RRD_ALERT_PROTOTYPE *ap __maybe_unused)
762 +void sql_alert_store_config(RRD_ALERT_PROTOTYPE *ap __maybe_unused)
763 {
764 static __thread sqlite3_stmt *res = NULL;
765 int rc, param = 0;
766
987 - if (unlikely(!db_meta)) {
988 - if (default_rrd_memory_mode != RRD_MEMORY_MODE_DBENGINE)
989 - return 0;
990 - error_report("Database has not been initialized");
991 - return 1;
992 - }
993 -
767 if (unlikely(!res)) {
768 rc = prepare_statement(db_meta, SQL_STORE_ALERT_CONFIG_HASH, &res);
769 if (unlikely(rc != SQLITE_OK)) {
770 error_report("Failed to prepare statement to store alert configuration, rc = %d", rc);
998 - return 1;
771 + return;
772 }
773 }
1001 - BUFFER *buf = buffer_create(128, NULL);
774
1003 - rc = sqlite3_bind_blob(res, ++param, &ap->config.hash_id, sizeof(ap->config.hash_id), SQLITE_STATIC);
1004 - if (unlikely(rc != SQLITE_OK))
1005 - goto bind_fail;
1006 -
1007 - if (ap->match.is_template)
1008 - rc = SQLITE3_BIND_STRING_OR_NULL(res, NULL, ++param);
1009 - else
1010 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->config.name, ++param);
1011 -
1012 - if (unlikely(rc != SQLITE_OK))
1013 - goto bind_fail;
1014 -
1015 - if (ap->match.is_template)
1016 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->config.name, ++param);
1017 - else
1018 - rc = SQLITE3_BIND_STRING_OR_NULL(res, NULL, ++param);
1019 -
1020 - if (unlikely(rc != SQLITE_OK))
1021 - goto bind_fail;
1022 -
1023 - if (ap->match.is_template)
1024 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->match.on.context, ++param);
1025 - else
1026 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->match.on.chart, ++param);
1027 - if (unlikely(rc != SQLITE_OK))
1028 - goto bind_fail;
1029 -
1030 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->config.classification, ++param);
1031 - if (unlikely(rc != SQLITE_OK))
1032 - goto bind_fail;
1033 -
1034 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->config.component, ++param);
1035 - if (unlikely(rc != SQLITE_OK))
1036 - goto bind_fail;
1037 -
1038 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->config.type, ++param);
1039 - if (unlikely(rc != SQLITE_OK))
1040 - goto bind_fail;
775 + BUFFER *buf = buffer_create(128, NULL);
776
1042 - // Rebuild lookup
1043 - rc = SQLITE3_BIND_STRING_OR_NULL(res, NULL, ++param); // lookup line
1044 - if (unlikely(rc != SQLITE_OK))
1045 - goto bind_fail;
777 + SQLITE_BIND_FAIL(
778 + done, rc = sqlite3_bind_blob(res, ++param, &ap->config.hash_id, sizeof(ap->config.hash_id), SQLITE_STATIC));
779
1047 - rc = sqlite3_bind_int(res, ++param, ap->config.update_every);
1048 - if (unlikely(rc != SQLITE_OK))
1049 - goto bind_fail;
780 + if (ap->match.is_template) {
781 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, NULL));
782 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->config.name));
783 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->match.on.context));
784 + }
785 + else {
786 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->config.name));
787 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, NULL));
788 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->match.on.chart));
789 + }
790
1051 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->config.units, ++param);
1052 - if (unlikely(rc != SQLITE_OK))
1053 - goto bind_fail;
791 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->config.classification));
792 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->config.component));
793 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->config.type));
794 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, NULL)); // lookup
795 + SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, ap->config.update_every));
796 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->config.units));
797
798 if (ap->config.calculation)
1056 - rc = sqlite3_bind_text(res, ++param, expression_source(ap->config.calculation), -1, SQLITE_STATIC);
799 + SQLITE_BIND_FAIL(done, sqlite3_bind_text(res, ++param, expression_source(ap->config.calculation), -1, SQLITE_STATIC));
800 else
1058 - rc = sqlite3_bind_null(res, ++param);
1059 - if (unlikely(rc != SQLITE_OK))
1060 - goto bind_fail;
801 + SQLITE_BIND_FAIL(done,sqlite3_bind_null(res, ++param));
802
1062 - NETDATA_DOUBLE green = NAN;
1063 - rc = sqlite3_bind_double(res, ++param, green);
1064 - if (unlikely(rc != SQLITE_OK))
1065 - goto bind_fail;
1066 -
1067 - NETDATA_DOUBLE red = NAN;
1068 - rc = sqlite3_bind_double(res, ++param, red);
1069 - if (unlikely(rc != SQLITE_OK))
1070 - goto bind_fail;
803 + NETDATA_DOUBLE nan_value = NAN;
804 + SQLITE_BIND_FAIL(done, sqlite3_bind_double(res, ++param, nan_value));
805 + SQLITE_BIND_FAIL(done, sqlite3_bind_double(res, ++param, nan_value));
806
807 if (ap->config.warning)
1073 - rc = sqlite3_bind_text(res, ++param, expression_source(ap->config.warning), -1, SQLITE_STATIC);
808 + SQLITE_BIND_FAIL(done, sqlite3_bind_text(res, ++param, expression_source(ap->config.warning), -1, SQLITE_STATIC));
809 else
1075 - rc = sqlite3_bind_null(res, ++param);
1076 - if (unlikely(rc != SQLITE_OK))
1077 - goto bind_fail;
810 + SQLITE_BIND_FAIL(done, sqlite3_bind_null(res, ++param));
811
812 if (ap->config.critical)
1080 - rc = sqlite3_bind_text(res, ++param, expression_source(ap->config.critical), -1, SQLITE_STATIC);
813 + SQLITE_BIND_FAIL(done, sqlite3_bind_text(res, ++param, expression_source(ap->config.critical), -1, SQLITE_STATIC));
814 else
1082 - rc = sqlite3_bind_null(res, ++param);
1083 - if (unlikely(rc != SQLITE_OK))
1084 - goto bind_fail;
1085 -
1086 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->config.exec, ++param);
1087 - if (unlikely(rc != SQLITE_OK))
1088 - goto bind_fail;
815 + SQLITE_BIND_FAIL(done, sqlite3_bind_null(res, ++param));
816
1090 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->config.recipient, ++param);
1091 - if (unlikely(rc != SQLITE_OK))
1092 - goto bind_fail;
1093 -
1094 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->config.info, ++param);
1095 - if (unlikely(rc != SQLITE_OK))
1096 - goto bind_fail;
817 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->config.exec));
818 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->config.recipient));
819 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->config.info));
820
821 if (ap->config.delay_up_duration)
822 buffer_sprintf(buf, "up %ds ", ap->config.delay_up_duration);
@@ -1108,117 +831,50 @@ int sql_alert_store_config(RRD_ALERT_PROTOTYPE *ap __maybe_unused)
831 buffer_sprintf(buf, "max %ds", ap->config.delay_max_duration);
832
833 // delay
1111 - rc = sqlite3_bind_text(res, ++param, buffer_tostring(buf), -1, SQLITE_STATIC);
1112 - if (unlikely(rc != SQLITE_OK))
1113 - goto bind_fail;
834 + SQLITE_BIND_FAIL(done, sqlite3_bind_text(res, ++param, buffer_tostring(buf), -1, SQLITE_STATIC));
835
836 if (ap->config.alert_action_options & ALERT_ACTION_OPTION_NO_CLEAR_NOTIFICATION)
1116 - rc = sqlite3_bind_text(res, ++param, "no-clear-notification", -1, SQLITE_STATIC);
837 + SQLITE_BIND_FAIL(done, sqlite3_bind_text(res, ++param, "no-clear-notification", -1, SQLITE_STATIC));
838 else
1118 - rc = sqlite3_bind_null(res, ++param);
1119 - if (unlikely(rc != SQLITE_OK))
1120 - goto bind_fail;
1121 -
1122 - rc = sqlite3_bind_int(res, ++param, ap->config.update_every);
1123 - if (unlikely(rc != SQLITE_OK))
1124 - goto bind_fail;
839 + SQLITE_BIND_FAIL(done, sqlite3_bind_null(res, ++param));
840
1126 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->match.host_labels, ++param);
1127 - if (unlikely(rc != SQLITE_OK))
1128 - goto bind_fail;
841 + SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, ap->config.update_every));
842 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->match.host_labels));
843
844 if (ap->config.after) {
1131 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->config.dimensions, ++param);
1132 - if (unlikely(rc != SQLITE_OK))
1133 - goto bind_fail;
1134 -
1135 - rc = sqlite3_bind_text(res, ++param, time_grouping_id2txt(ap->config.time_group), -1, SQLITE_STATIC);
1136 - if (unlikely(rc != SQLITE_OK))
1137 - goto bind_fail;
1138 -
1139 - rc = sqlite3_bind_int(res, ++param, (int) RRDR_OPTIONS_REMOVE_OVERLAPPING(ap->config.options));
1140 - if (unlikely(rc != SQLITE_OK))
1141 - goto bind_fail;
1142 -
1143 - rc = sqlite3_bind_int64(res, ++param, (int) ap->config.after);
1144 - if (unlikely(rc != SQLITE_OK))
1145 - goto bind_fail;
1146 -
1147 - rc = sqlite3_bind_int64(res, ++param, (int) ap->config.before);
1148 - if (unlikely(rc != SQLITE_OK))
1149 - goto bind_fail;
845 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->config.dimensions));
846 + SQLITE_BIND_FAIL(done, sqlite3_bind_text(res, ++param, time_grouping_id2txt(ap->config.time_group), -1, SQLITE_STATIC));
847 + SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, (int) RRDR_OPTIONS_REMOVE_OVERLAPPING(ap->config.options)));
848 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (int) ap->config.after));
849 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (int) ap->config.before));
850 } else {
1151 - rc = sqlite3_bind_null(res, ++param);
1152 - if (unlikely(rc != SQLITE_OK))
1153 - goto bind_fail;
1154 -
1155 - rc = sqlite3_bind_null(res, ++param);
1156 - if (unlikely(rc != SQLITE_OK))
1157 - goto bind_fail;
1158 -
1159 - rc = sqlite3_bind_null(res, ++param);
1160 - if (unlikely(rc != SQLITE_OK))
1161 - goto bind_fail;
1162 -
1163 - rc = sqlite3_bind_null(res, ++param);
1164 - if (unlikely(rc != SQLITE_OK))
1165 - goto bind_fail;
1166 -
1167 - rc = sqlite3_bind_null(res, ++param);
1168 - if (unlikely(rc != SQLITE_OK))
1169 - goto bind_fail;
851 + SQLITE_BIND_FAIL(done, sqlite3_bind_null(res, ++param));
852 + SQLITE_BIND_FAIL(done, sqlite3_bind_null(res, ++param));
853 + SQLITE_BIND_FAIL(done, sqlite3_bind_null(res, ++param));
854 + SQLITE_BIND_FAIL(done, sqlite3_bind_null(res, ++param));
855 + SQLITE_BIND_FAIL(done, sqlite3_bind_null(res, ++param));
856 }
857
1172 - rc = sqlite3_bind_int(res, ++param, ap->config.update_every);
1173 - if (unlikely(rc != SQLITE_OK))
1174 - goto bind_fail;
1175 -
1176 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->config.source, ++param);
1177 - if (unlikely(rc != SQLITE_OK))
1178 - goto bind_fail;
1179 -
1180 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->match.chart_labels, ++param);
1181 - if (unlikely(rc != SQLITE_OK))
1182 - goto bind_fail;
1183 -
1184 - rc = SQLITE3_BIND_STRING_OR_NULL(res, ap->config.summary, ++param);
1185 - if (unlikely(rc != SQLITE_OK))
1186 - goto bind_fail;
1187 -
1188 - rc = sqlite3_bind_int(res, ++param, ap->config.time_group_condition);
1189 - if (unlikely(rc != SQLITE_OK))
1190 - goto bind_fail;
1191 -
1192 - rc = sqlite3_bind_double(res, ++param, ap->config.time_group_value);
1193 - if (unlikely(rc != SQLITE_OK))
1194 - goto bind_fail;
858 + SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, ap->config.update_every));
859 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->config.source));
860 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->match.chart_labels));
861 + SQLITE_BIND_FAIL(done, SQLITE3_BIND_STRING_OR_NULL(res, ++param, ap->config.summary));
862
1196 - rc = sqlite3_bind_int(res, ++param, ap->config.dims_group);
1197 - if (unlikely(rc != SQLITE_OK))
1198 - goto bind_fail;
1199 -
1200 - rc = sqlite3_bind_int(res, ++param, ap->config.data_source);
1201 - if (unlikely(rc != SQLITE_OK))
1202 - goto bind_fail;
863 + SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, ap->config.time_group_condition));
864 + SQLITE_BIND_FAIL(done, sqlite3_bind_double(res, ++param, ap->config.time_group_value));
865 + SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, ap->config.dims_group));
866 + SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, ap->config.data_source));
867
868 + param = 0;
869 rc = execute_insert(res);
870 if (unlikely(rc != SQLITE_DONE))
871 error_report("Failed to store alert config, rc = %d", rc);
872
1208 - rc = sqlite3_reset(res);
1209 - if (unlikely(rc != SQLITE_OK))
1210 - error_report("Failed to reset statement in alert hash_id store function, rc = %d", rc);
1211 -
1212 - buffer_free(buf);
1213 - return 0;
1214 -
1215 -bind_fail:
873 +done:
874 + REPORT_BIND_FAIL(res, param);
875 buffer_free(buf);
1217 - error_report("Failed to bind parameter %d to store alert hash_id, rc = %d", param, rc);
1218 - rc = sqlite3_reset(res);
1219 - if (unlikely(rc != SQLITE_OK))
876 + if (unlikely(sqlite3_reset(res) != SQLITE_OK))
877 error_report("Failed to reset statement in alert hash_id store function, rc = %d", rc);
1221 - return 1;
878 }
879
880 #define SQL_SELECT_HEALTH_LAST_EXECUTED_EVENT \
@@ -1646,14 +1302,14 @@ uint32_t sql_get_alarm_id(RRDHOST *host, STRING *chart, STRING *name, uint32_t *
1302 return alarm_id;
1303 }
1304
1649 - rc = SQLITE3_BIND_STRING_OR_NULL(res, chart, 2);
1305 + rc = SQLITE3_BIND_STRING_OR_NULL(res, 2, chart);
1306 if (unlikely(rc != SQLITE_OK)) {
1307 error_report("Failed to bind char parameter for SQL_GET_ALARM_ID.");
1308 sqlite3_finalize(res);
1309 return alarm_id;
1310 }
1311
1656 - rc = SQLITE3_BIND_STRING_OR_NULL(res, name, 3);
1312 + rc = SQLITE3_BIND_STRING_OR_NULL(res, 3, name);
1313 if (unlikely(rc != SQLITE_OK)) {
1314 error_report("Failed to bind name parameter for SQL_GET_ALARM_ID.");
1315 sqlite3_finalize(res);
@@ -1704,12 +1360,10 @@ bool sql_find_alert_transition(
1360
1361 bool ok = false;
1362
1707 - rc = sqlite3_bind_blob(res, 1, &transition_uuid, sizeof(transition_uuid), SQLITE_STATIC);
1708 - if (unlikely(rc != SQLITE_OK)) {
1709 - error_report("Failed to bind transition");
1710 - goto done;
1711 - }
1363 + int param = 0;
1364 + SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, &transition_uuid, sizeof(transition_uuid), SQLITE_STATIC));
1365
1366 + param = 0;
1367 while (sqlite3_step_monitored(res) == SQLITE_ROW) {
1368 ok = true;
1369 uuid_unparse_lower(*(uuid_t *) sqlite3_column_blob(res, 1), machine_guid);
@@ -1717,6 +1371,8 @@ bool sql_find_alert_transition(
1371 }
1372
1373 done:
1374 + REPORT_BIND_FAIL(res, param);
1375 +
1376 rc = sqlite3_reset(res);
1377 if (unlikely(rc != SQLITE_OK))
1378 error_report("Failed to reset the statement when trying to find transition");
@@ -1840,39 +1496,21 @@ void sql_alert_transitions(
1496 goto done_only_drop;
1497 }
1498
1843 - int param = 1;
1844 - rc = sqlite3_bind_int64(res, param++, (sqlite3_int64)(after * USEC_PER_SEC));
1845 - if (unlikely(rc != SQLITE_OK)) {
1846 - error_report("Failed to bind after parameter");
1847 - goto done;
1848 - }
1499 + int param = 0;
1500 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)(after * USEC_PER_SEC)));
1501 + SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, (sqlite3_int64)(before * USEC_PER_SEC)));
1502
1850 - rc = sqlite3_bind_int64(res, param++, (sqlite3_int64)(before * USEC_PER_SEC));
1851 - if (unlikely(rc != SQLITE_OK)) {
1852 - error_report("Failed to bind before parameter");
1853 - goto done;
1854 - }
1855 -
1856 - if (context) {
1857 - rc = sqlite3_bind_text(res, param++, context, -1, SQLITE_STATIC);
1858 - if (unlikely(rc != SQLITE_OK)) {
1859 - error_report("Failed to bind context parameter");
1860 - goto done;
1861 - }
1862 - }
1503 + if (context)
1504 + SQLITE_BIND_FAIL(done, sqlite3_bind_text(res, ++param, context, -1, SQLITE_STATIC));
1505
1864 - if (alert_name) {
1865 - rc = sqlite3_bind_text(res, param++, alert_name, -1, SQLITE_STATIC);
1866 - if (unlikely(rc != SQLITE_OK)) {
1867 - error_report("Failed to bind alert_name parameter");
1868 - goto done;
1869 - }
1870 - }
1506 + if (alert_name)
1507 + SQLITE_BIND_FAIL(done, sqlite3_bind_text(res, ++param, alert_name, -1, SQLITE_STATIC));
1508
1509 run_query:;
1510
1511 struct sql_alert_transition_data atd = {0 };
1512
1513 + param = 0;
1514 while (sqlite3_step(res) == SQLITE_ROW) {
1515 atd.host_id = (uuid_t *) sqlite3_column_blob(res, 0);
1516 atd.alarm_id = sqlite3_column_int64(res, 1);
@@ -1910,6 +1548,8 @@ run_query:;
1548 }
1549
1550 done:
1551 + REPORT_BIND_FAIL(res, param);
1552 +
1553 rc = sqlite3_finalize(res);
1554 if (unlikely(rc != SQLITE_OK))
1555 error_report("Failed to finalize statement for sql_alert_transitions");
src/database/sqlite/sqlite_health.h
+1 -1
@@ -12,7 +12,7 @@ struct rrd_alert_prototype;
12 void sql_health_alarm_log_load(RRDHOST *host);
13 void sql_health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae);
14 void sql_health_alarm_log_cleanup(RRDHOST *host, bool claimed);
15 -int sql_alert_store_config(struct rrd_alert_prototype *ap);
15 +void sql_alert_store_config(struct rrd_alert_prototype *ap);
16 void sql_aclk_alert_clean_dead_entries(RRDHOST *host);
17 int sql_health_get_last_executed_event(RRDHOST *host, ALARM_ENTRY *ae, RRDCALC_STATUS *last_executed_status);
18 void sql_health_alarm_log2json(RRDHOST *host, BUFFER *wb, time_t after, const char *chart);
src/database/sqlite/sqlite_metadata.h
-1
@@ -52,7 +52,6 @@ RRDLABELS *sql_load_host_labels(uuid_t *host_id);
52
53 uint64_t sqlite_get_meta_space(void);
54 int sql_init_meta_database(db_check_action_type_t rebuild, int memory);
55 -void sql_close_meta_database(void);
55
56 // UNIT TEST
57 int metadata_unittest(void);
src/health/health_prototypes.c
+1 -1
@@ -377,7 +377,7 @@ void health_prototype_hash_id(RRD_ALERT_PROTOTYPE *ap) {
377 UUID uuid = UUID_generate_from_hash(buffer_tostring(wb), buffer_strlen(wb));
378 uuid_copy(ap->config.hash_id, uuid.uuid);
379
380 - (void) sql_alert_store_config(ap);
380 + sql_alert_store_config(ap);
381 }
382
383 bool health_prototype_add(RRD_ALERT_PROTOTYPE *ap) {