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
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;
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;
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;
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;
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
}
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];
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;
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;
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))
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;
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
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;
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);
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)));
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);
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
/*
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
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
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;
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;
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.");
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, "[");
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;
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);
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;
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;
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);
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
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) {
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");
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,
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
}
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;
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
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
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);
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(
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
-}