Fix alert transition (#19507)
* Fix alert transition from WARNING,CRITICAL to REMOVED to have a short delay (10 seconds) instead of 10 mins * date_scheduled when adding the alert queue will be from the trigger time of the alert * When adding newly generated transitions use the AE when time as trigger time * When mass populating removed states use the current time (as it is now)
Stelios Fragkakis committed
Jan 28, 2025 at 19:13 UTC
3d49d3270c6052c96692553210ce953b9e952d75
1 file changed
+9
-7
src/database/sqlite/sqlite_health.c
+9
-7
@@ -128,11 +128,11 @@ int calculate_delay(RRDCALC_STATUS old_status, RRDCALC_STATUS new_status)
128
case RRDCALC_STATUS_WARNING:
129
case RRDCALC_STATUS_CRITICAL:
130
switch (new_status) {
131
- case RRDCALC_STATUS_REMOVED:
131
case RRDCALC_STATUS_UNINITIALIZED:
132
case RRDCALC_STATUS_UNDEFINED:
133
delay = ALERT_TRANSITION_DELAY_LONG;
134
break;
135
+ case RRDCALC_STATUS_REMOVED:
136
case RRDCALC_STATUS_CLEAR:
137
delay = ALERT_TRANSITION_DELAY_SHORT;
138
break;
@@ -150,7 +150,7 @@ int calculate_delay(RRDCALC_STATUS old_status, RRDCALC_STATUS new_status)
150
151
#define SQL_INSERT_ALERT_PENDING_QUEUE \
152
"INSERT INTO alert_queue (host_id, health_log_id, unique_id, alarm_id, status, date_scheduled)" \
153
- " VALUES (@host_id, @health_log_id, @unique_id, @alarm_id, @new_status, UNIXEPOCH() + @delay)" \
153
+ " VALUES (@host_id, @health_log_id, @unique_id, @alarm_id, @new_status, @delay)" \
154
" ON CONFLICT (host_id, health_log_id, alarm_id)" \
155
" DO UPDATE SET status = excluded.status, unique_id = excluded.unique_id, " \
156
" date_scheduled = MIN(date_scheduled, excluded.date_scheduled)"
@@ -161,7 +161,8 @@ static void insert_alert_queue(
161
int64_t unique_id,
162
uint32_t alarm_id,
163
RRDCALC_STATUS old_status,
164
- RRDCALC_STATUS new_status)
164
+ RRDCALC_STATUS new_status,
165
+ time_t trigger_time)
166
{
167
static __thread sqlite3_stmt *compiled_res = NULL;
168
sqlite3_stmt *res = NULL;
@@ -185,7 +186,7 @@ static void insert_alert_queue(
186
if (!PREPARE_STATEMENT(db_meta, SQL_INSERT_ALERT_PENDING_QUEUE, &res))
187
return;
188
188
- int submit_delay = calculate_delay(old_status, new_status);
189
+ time_t submit_delay = trigger_time + calculate_delay(old_status, new_status);
190
191
int param = 0;
192
SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, &host->host_id.uuid, sizeof(host->host_id.uuid), SQLITE_STATIC));
@@ -193,7 +194,7 @@ static void insert_alert_queue(
194
SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, unique_id));
195
SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, alarm_id));
196
SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, new_status));
196
- SQLITE_BIND_FAIL(done, sqlite3_bind_int(res, ++param, submit_delay));
197
+ SQLITE_BIND_FAIL(done, sqlite3_bind_int64(res, ++param, submit_delay));
198
199
param = 0;
200
rc = sqlite3_step_monitored(res);
@@ -321,7 +322,7 @@ static void sql_health_alarm_log_insert(RRDHOST *host, ALARM_ENTRY *ae)
322
health_log_id = (size_t)sqlite3_column_int64(res, 0);
323
sql_health_alarm_log_insert_detail(host, health_log_id, ae);
324
insert_alert_queue(
324
- host, health_log_id, (int64_t)ae->unique_id, (int64_t)ae->alarm_id, ae->old_status, ae->new_status);
325
+ host, health_log_id, (int64_t)ae->unique_id, (int64_t)ae->alarm_id, ae->old_status, ae->new_status, ae->when);
326
} else
327
error_report("HEALTH [%s]: Failed to execute SQL_INSERT_HEALTH_LOG, rc = %d", rrdhost_hostname(host), rc);
328
@@ -473,6 +474,7 @@ static void sql_inject_removed_status(
474
SQLITE_BIND_FAIL(done, sqlite3_bind_blob(res, ++param, last_transition, sizeof(*last_transition), SQLITE_STATIC));
475
476
param = 0;
477
+ time_t now = now_realtime_sec();
478
while (sqlite3_step_monitored(res) == SQLITE_ROW) {
479
//update the old entry in health_log_detail
480
sql_set_updated_by_in_health_log_detail(unique_id, max_unique_id, last_transition);
@@ -482,7 +484,7 @@ static void sql_inject_removed_status(
484
int64_t health_log_id = sqlite3_column_int64(res, 0);
485
RRDCALC_STATUS old_status = (RRDCALC_STATUS)sqlite3_column_double(res, 1);
486
insert_alert_queue(
485
- host, health_log_id, (int64_t)max_unique_id, (int64_t)alarm_id, old_status, RRDCALC_STATUS_REMOVED);
487
+ host, health_log_id, (int64_t)max_unique_id, (int64_t)alarm_id, old_status, RRDCALC_STATUS_REMOVED, now);
488
}
489
490
done: