Maintain node's last connected timestamp in the db (#15979)
* Maintain node's last connected timestamp in the db * Rebase -- switch to version database v14
Stelios Fragkakis committed
Sep 26, 2023 at 20:39 UTC
94a3e42b9687821d7a1f4b5dd7a12dee8d819cb4
5 files changed
+35
-9
database/rrd.h
+1
@@ -1178,6 +1178,7 @@ struct rrdhost {
1178
// ------------------------------------------------------------------------
1179
// streaming of data from remote hosts - rrdpush receiver
1180
1181
+ time_t last_connected; // last time child connected (stored in db)
1182
time_t child_connect_time; // the time the last sender was connected
1183
time_t child_last_chart_command; // the time of the last CHART streaming command
1184
time_t child_disconnected_time; // the time the last sender was disconnected
database/rrdhost.c
+3
@@ -336,6 +336,7 @@ int is_legacy = 1;
336
337
if (likely(!archived)) {
338
rrdfunctions_host_init(host);
339
+ host->last_connected = now_realtime_sec();
340
host->rrdlabels = rrdlabels_create();
341
rrdhost_initialize_rrdpush_sender(
342
host, rrdpush_enabled, rrdpush_destination, rrdpush_api_key, rrdpush_send_charts_matching);
@@ -662,6 +663,8 @@ static void rrdhost_update(RRDHOST *host
663
if(!host->rrdvars)
664
host->rrdvars = rrdvariables_create();
665
666
+ host->last_connected = now_realtime_sec();
667
+
668
if (rrdhost_flag_check(host, RRDHOST_FLAG_ARCHIVED)) {
669
rrdhost_flag_clear(host, RRDHOST_FLAG_ARCHIVED);
670
database/sqlite/sqlite_db_migration.c
+17
@@ -104,6 +104,11 @@ const char *database_migrate_v12_v13_hash[] = {
104
NULL
105
};
106
107
+const char *database_migrate_v13_v14[] = {
108
+ "ALTER TABLE host ADD last_connected INT NOT NULL DEFAULT 0;",
109
+ NULL
110
+};
111
+
112
static int do_migration_v1_v2(sqlite3 *database, const char *name)
113
{
114
UNUSED(name);
@@ -365,6 +370,17 @@ static int do_migration_v12_v13(sqlite3 *database, const char *name)
370
return rc;
371
}
372
373
+static int do_migration_v13_v14(sqlite3 *database, const char *name)
374
+{
375
+ netdata_log_info("Running \"%s\" database migration", name);
376
+
377
+ if (!column_exists_in_table("host", "last_connected"))
378
+ return init_database_batch(database, &database_migrate_v13_v14[0]);
379
+
380
+ return 0;
381
+}
382
+
383
+
384
static int do_migration_noop(sqlite3 *database, const char *name)
385
{
386
UNUSED(database);
@@ -421,6 +437,7 @@ DATABASE_FUNC_MIGRATION_LIST migration_action[] = {
437
{.name = "v10 to v11", .func = do_migration_v10_v11},
438
{.name = "v11 to v12", .func = do_migration_v11_v12},
439
{.name = "v12 to v13", .func = do_migration_v12_v13},
440
+ {.name = "v13 to v14", .func = do_migration_v13_v14},
441
// the terminator of this array
442
{.name = NULL, .func = NULL}
443
};
database/sqlite/sqlite_functions.c
+2
-2
@@ -4,7 +4,7 @@
4
#include "sqlite3recover.h"
5
#include "sqlite_db_migration.h"
6
7
-#define DB_METADATA_VERSION 13
7
+#define DB_METADATA_VERSION 14
8
9
const char *database_config[] = {
10
"CREATE TABLE IF NOT EXISTS host(host_id BLOB PRIMARY KEY, hostname TEXT NOT NULL, "
@@ -14,7 +14,7 @@ const char *database_config[] = {
14
"memory_mode INT DEFAULT 0, abbrev_timezone TEXT DEFAULT '', utc_offset INT NOT NULL DEFAULT 0,"
15
"program_name TEXT NOT NULL DEFAULT 'unknown', program_version TEXT NOT NULL DEFAULT 'unknown', "
16
"entries INT NOT NULL DEFAULT 0,"
17
- "health_enabled INT NOT NULL DEFAULT 0);",
17
+ "health_enabled INT NOT NULL DEFAULT 0, last_connected INT NOT NULL DEFAULT 0);",
18
19
"CREATE TABLE IF NOT EXISTS chart(chart_id blob PRIMARY KEY, host_id blob, type text, id text, name text, "
20
"family text, context text, title text, unit text, plugin text, module text, priority int, update_every int, "
database/sqlite/sqlite_metadata.c
+12
-7
@@ -20,13 +20,14 @@
20
21
#define DELETE_DIMENSION_UUID "DELETE FROM dimension WHERE dim_id = @uuid;"
22
23
-#define SQL_STORE_HOST_INFO "INSERT OR REPLACE INTO host " \
24
- "(host_id, hostname, registry_hostname, update_every, os, timezone," \
25
- "tags, hops, memory_mode, abbrev_timezone, utc_offset, program_name, program_version," \
26
- "entries, health_enabled) " \
27
- "values (@host_id, @hostname, @registry_hostname, @update_every, @os, @timezone, @tags, @hops, @memory_mode, " \
28
- "@abbrev_timezone, @utc_offset, @program_name, @program_version, " \
29
- "@entries, @health_enabled);"
23
+#define SQL_STORE_HOST_INFO \
24
+ "INSERT OR REPLACE INTO host " \
25
+ "(host_id, hostname, registry_hostname, update_every, os, timezone, tags, hops, memory_mode, " \
26
+ "abbrev_timezone, utc_offset, program_name, program_version," \
27
+ "entries, health_enabled, last_connected) " \
28
+ "VALUES (@host_id, @hostname, @registry_hostname, @update_every, @os, @timezone, @tags, @hops, @memory_mode, " \
29
+ "@abbrev_timezone, @utc_offset, @program_name, @program_version, " \
30
+ "@entries, @health_enabled, @last_connected);"
31
32
#define SQL_STORE_CHART "insert or replace into chart (chart_id, host_id, type, id, " \
33
"name, family, context, title, unit, plugin, module, priority, update_every , chart_type , memory_mode , " \
@@ -364,6 +365,10 @@ static int store_host_metadata(RRDHOST *host)
365
if (unlikely(rc != SQLITE_OK))
366
goto bind_fail;
367
368
+ rc = sqlite3_bind_int(res, ++param, (int ) host->last_connected);
369
+ if (unlikely(rc != SQLITE_OK))
370
+ goto bind_fail;
371
+
372
int store_rc = sqlite3_step_monitored(res);
373
if (unlikely(store_rc != SQLITE_DONE))
374
error_report("Failed to store host %s, rc = %d", rrdhost_hostname(host), rc);