@cryptotaxi247 / netdata-1 / commits / 69361a721

Add option to cleanup health_log table (#17385)

Add option to cleanup health_log table (-W sqlite-alert-cleanup)

Stelios Fragkakis committed Apr 13, 2024 at 20:11 UTC 69361a721c5c21012ef6488fec6fd55f232a8e36
6 files changed +80
src/daemon/main.c
+6
@@ -795,6 +795,7 @@ int help(int exitcode) {
795 " -W sqlite-meta-recover Run recovery on the metadata database and exit.\n\n"
796 " -W sqlite-compact Reclaim metadata database unused space and exit.\n\n"
797 " -W sqlite-analyze Run update statistics and exit.\n\n"
798 + " -W sqlite-alert-cleanup Perform maintenance on the alerts table.\n\n"
799 #ifdef ENABLE_DBENGINE
800 " -W createdataset=N Create a DB engine dataset of N seconds and exit.\n\n"
801 " -W stresstest=A,B,C,D,E,F,G\n"
@@ -1515,6 +1516,11 @@ int main(int argc, char **argv) {
1516 return 0;
1517 }
1518
1519 + if(strcmp(optarg, "sqlite-alert-cleanup") == 0) {
1520 + sql_alert_cleanup(true);
1521 + return 0;
1522 + }
1523 +
1524 if(strcmp(optarg, "unittest") == 0) {
1525 unittest_running = true;
1526
src/database/sqlite/sqlite_context.c
+1
@@ -43,6 +43,7 @@ int sql_init_context_database(int memory)
43 return 1;
44 }
45
46 + errno = 0;
47 netdata_log_info("SQLite database %s initialization", sqlite_database);
48
49 char buf[1024 + 1] = "";
src/database/sqlite/sqlite_db_migration.c
+1
@@ -530,6 +530,7 @@ static int migrate_database(sqlite3 *database, int target_version, char *db_name
530 }
531
532 if (likely(user_version == target_version)) {
533 + errno = 0;
534 netdata_log_info("%s database version is %d (no migration needed)", db_name, target_version);
535 return target_version;
536 }
src/database/sqlite/sqlite_health.c
+70
@@ -704,6 +704,76 @@ void sql_check_removed_alerts_state(RRDHOST *host)
704 error_report("Failed to finalize the statement");
705 }
706
707 +#define SQL_DELETE_MISSING_CHART_ALERT \
708 + "DELETE FROM health_log WHERE host_id = @host_id AND chart NOT IN " \
709 + "(SELECT type||'.'||id FROM chart WHERE host_id = @host_id)"
710 +
711 +static void sql_remove_alerts_from_deleted_charts(RRDHOST *host, uuid_t *host_id)
712 +{
713 + sqlite3_stmt *res = NULL;
714 + int ret;
715 +
716 + ret = sqlite3_prepare_v2(db_meta, SQL_DELETE_MISSING_CHART_ALERT, -1, &res, 0);
717 + if (unlikely(ret != SQLITE_OK)) {
718 + error_report("HEALTH [%s]: Failed to prepare sql statement to sql_remove_alerts_from_deleted_charts", rrdhost_hostname(host));
719 + return;
720 + }
721 +
722 + if (host)
723 + ret = sqlite3_bind_blob(res, 1, &host->host_uuid, sizeof(host->host_uuid), SQLITE_STATIC);
724 + else
725 + ret = sqlite3_bind_blob(res, 1, host_id, sizeof(*host_id), SQLITE_STATIC);
726 +
727 + if (unlikely(ret != SQLITE_OK)) {
728 + error_report("Failed to bind host_id parameter for sql_remove_alerts_from_deleted_charts.");
729 + sqlite3_finalize(res);
730 + return;
731 + }
732 +
733 + ret = execute_insert(res);
734 + if (ret != SQLITE_DONE)
735 + error_report("Failed to execute command to delete missing charts from health_log");
736 +
737 + ret = sqlite3_finalize(res);
738 + if (unlikely(ret != SQLITE_OK))
739 + error_report("Failed to finalize statement when deleting missing charts from health_log");
740 +}
741 +
742 +static int clean_host_alerts(void *data, int argc, char **argv, char **column)
743 +{
744 + UNUSED(argc);
745 + UNUSED(data);
746 + UNUSED(column);
747 +
748 + char guid[UUID_STR_LEN];
749 + uuid_unparse_lower(*(uuid_t *)argv[0], guid);
750 +
751 + netdata_log_info("Checking host %s (%s)", guid, (const char *) argv[1]);
752 + sql_remove_alerts_from_deleted_charts(NULL, (uuid_t *)argv[0]);
753 +
754 + return 0;
755 +}
756 +
757 +
758 +#define SQL_HEALTH_CHECK_ALL_HOSTS "SELECT host_id, hostname FROM host"
759 +
760 +void sql_alert_cleanup(bool cli)
761 +{
762 + UNUSED(cli);
763 +
764 + errno = 0;
765 + if (sql_init_meta_database(DB_CHECK_NONE, 0)) {
766 + netdata_log_error("Failed to open database");
767 + return;
768 + }
769 + netdata_log_info("Alert cleanup running ...");
770 + int rc = sqlite3_exec_monitored(db_meta, SQL_HEALTH_CHECK_ALL_HOSTS, clean_host_alerts, NULL, NULL);
771 + if (rc != SQLITE_OK)
772 + netdata_log_error("Failed to check host alerts");
773 + else
774 + netdata_log_info("Alert cleanup done");
775 +
776 +}
777 /* Health related SQL queries
778 Load from the health log table
779 */
src/database/sqlite/sqlite_health.h
+1
@@ -36,4 +36,5 @@ int sql_get_alert_configuration(
36 bool debug __maybe_unused);
37
38 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);
39 +void sql_alert_cleanup(bool cli);
40 #endif //NETDATA_SQLITE_HEALTH_H
src/database/sqlite/sqlite_metadata.c
+1
@@ -747,6 +747,7 @@ int sql_init_meta_database(db_check_action_type_t rebuild, int memory)
747 return 1;
748 }
749
750 + errno = 0;
751 netdata_log_info("SQLite database %s initialization", sqlite_database);
752
753 rc = sqlite3_create_function(db_meta, "u2h", 1, SQLITE_ANY | SQLITE_DETERMINISTIC, 0, sqlite_uuid_parse, 0, 0);