@cryptotaxi247 / netdata-1 / commits / 754b54224

Store host system information in the database (#13402)

* Hold host info in the database Add functions to store / fetch * Store the system info * Delete the relevant host_info where the host is deleted * Remove redundant call to store system info Use const in parameters to sql_store_host_system_info_key_value and sql_store_host_system_info * Do not crash if no system info is given * Fix missing finalize

Stelios Fragkakis committed Jul 20, 2022 at 13:08 UTC 754b54224240629c853f222e174e01e9b57064e1
3 files changed +171
database/rrdhost.c
+2
@@ -487,6 +487,7 @@ RRDHOST *rrdhost_create(const char *hostname,
487 , host->health_default_exec
488 , host->health_default_recipient
489 );
490 + sql_store_host_system_info(&host->host_uuid, system_info);
491
492 rrd_hosts_available++;
493
@@ -526,6 +527,7 @@ void rrdhost_update(RRDHOST *host
527
528 rrdhost_system_info_free(host->system_info);
529 host->system_info = system_info;
530 + sql_store_host_system_info(&host->host_uuid, system_info);
531
532 rrdhost_init_os(host, os);
533 rrdhost_init_timezone(host, timezone, abbrev_timezone, utc_offset);
database/sqlite/sqlite_functions.c
+168
@@ -32,6 +32,9 @@ const char *database_config[] = {
32 "repeat text, host_labels text, p_db_lookup_dimensions text, p_db_lookup_method text, p_db_lookup_options int, "
33 "p_db_lookup_after int, p_db_lookup_before int, p_update_every int);",
34
35 + "CREATE TABLE IF NOT EXISTS host_info(host_id blob, system_key text NOT NULL, system_value text NOT NULL, "
36 + "date_created INT, PRIMARY KEY(host_id, system_key));"
37 +
38 "CREATE TABLE IF NOT EXISTS chart_hash_map(chart_id blob , hash_id blob, UNIQUE (chart_id, hash_id));",
39
40 "CREATE TABLE IF NOT EXISTS chart_hash(hash_id blob PRIMARY KEY,type text, id text, name text, "
@@ -63,6 +66,7 @@ const char *database_cleanup[] = {
66 "DELETE FROM chart_hash_map WHERE chart_id NOT IN (SELECT chart_id FROM chart);",
67 "DELETE FROM chart_hash WHERE hash_id NOT IN (SELECT hash_id FROM chart_hash_map);",
68 "DELETE FROM node_instance WHERE host_id NOT IN (SELECT host_id FROM host);",
69 + "DELETE FROM host_info WHERE host_id NOT IN (SELECT host_id FROM host);",
70 NULL
71 };
72
@@ -2313,3 +2317,167 @@ failed:
2317
2318 return;
2319 };
2320 +
2321 +#define SELECT_HOST_INFO "SELECT system_key, system_value FROM host_info WHERE host_id = @host_id;"
2322 +
2323 +void sql_build_host_system_info(uuid_t *host_id, struct rrdhost_system_info *system_info)
2324 +{
2325 + int rc;
2326 +
2327 + sqlite3_stmt *res = NULL;
2328 +
2329 + rc = sqlite3_prepare_v2(db_meta, SELECT_HOST_INFO, -1, &res, 0);
2330 + if (unlikely(rc != SQLITE_OK)) {
2331 + error_report("Failed to prepare statement to read host information");
2332 + return;
2333 + }
2334 +
2335 + rc = sqlite3_bind_blob(res, 1, host_id, sizeof(*host_id), SQLITE_STATIC);
2336 + if (unlikely(rc != SQLITE_OK)) {
2337 + error_report("Failed to bind host parameter host information");
2338 + goto skip_loading;
2339 + }
2340 +
2341 + while (sqlite3_step(res) == SQLITE_ROW) {
2342 + rrdhost_set_system_info_variable(system_info, (char *) sqlite3_column_text(res, 0),
2343 + (char *) sqlite3_column_text(res, 1));
2344 + }
2345 +
2346 +skip_loading:
2347 + if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
2348 + error_report("Failed to finalize the prepared statement when reading host information");
2349 + return;
2350 +}
2351 +
2352 +
2353 +#define SQL_INS_HOST_SYSTEM_INFO "INSERT OR REPLACE INTO host_info " \
2354 + "(host_id, system_key, system_value, date_created) " \
2355 + "VALUES (@host, @key, @value, unixepoch());"
2356 +
2357 +void sql_store_host_system_info_key_value(uuid_t *host_id, const char *name, const char *value)
2358 +{
2359 + sqlite3_stmt *res = NULL;
2360 + int rc;
2361 +
2362 + if (unlikely(!db_meta)) {
2363 + if (default_rrd_memory_mode == RRD_MEMORY_MODE_DBENGINE)
2364 + error_report("Database has not been initialized");
2365 + return;
2366 + }
2367 +
2368 + rc = sqlite3_prepare_v2(db_meta, SQL_INS_HOST_SYSTEM_INFO, -1, &res, 0);
2369 + if (unlikely(rc != SQLITE_OK)) {
2370 + error_report("Failed to prepare statement to store system info");
2371 + return;
2372 + }
2373 +
2374 + rc = sqlite3_bind_blob(res, 1, host_id, sizeof(*host_id), SQLITE_STATIC);
2375 + if (unlikely(rc != SQLITE_OK)) {
2376 + error_report("Failed to bind host parameter to store system information");
2377 + goto skip_store;
2378 + }
2379 +
2380 + rc = sqlite3_bind_text(res, 2, name, -1, SQLITE_STATIC);
2381 + if (unlikely(rc != SQLITE_OK)) {
2382 + error_report("Failed to bind label parameter to store name information");
2383 + goto skip_store;
2384 + }
2385 +
2386 + rc = sqlite3_bind_text(res, 3, value, -1, SQLITE_STATIC);
2387 + if (unlikely(rc != SQLITE_OK)) {
2388 + error_report("Failed to bind value parameter to store value information");
2389 + goto skip_store;
2390 + }
2391 +
2392 + rc = execute_insert(res);
2393 + if (unlikely(rc != SQLITE_DONE))
2394 + error_report("Failed to store host system info, rc = %d", rc);
2395 +
2396 +skip_store:
2397 + if (unlikely(sqlite3_finalize(res) != SQLITE_OK))
2398 + error_report("Failed to finalize the prepared statement when storing host system information");
2399 +
2400 + return;
2401 +}
2402 +
2403 +
2404 +void sql_store_host_system_info(uuid_t *host_id, const struct rrdhost_system_info *system_info)
2405 +{
2406 + if (unlikely(!system_info))
2407 + return;
2408 +
2409 + if (system_info->container_os_name)
2410 + sql_store_host_system_info_key_value(host_id, "NETDATA_CONTAINER_OS_NAME", system_info->container_os_name);
2411 +
2412 + if (system_info->container_os_id)
2413 + sql_store_host_system_info_key_value(host_id, "NETDATA_CONTAINER_OS_ID", system_info->container_os_id);
2414 +
2415 + if (system_info->container_os_id_like)
2416 + sql_store_host_system_info_key_value(host_id, "NETDATA_CONTAINER_OS_ID_LIKE", system_info->container_os_id_like);
2417 +
2418 + if (system_info->container_os_version)
2419 + sql_store_host_system_info_key_value(host_id, "NETDATA_CONTAINER_OS_VERSION", system_info->container_os_version);
2420 +
2421 + if (system_info->container_os_version_id)
2422 + sql_store_host_system_info_key_value(host_id, "NETDATA_CONTAINER_OS_VERSION_ID", system_info->container_os_version_id);
2423 +
2424 + if (system_info->host_os_detection)
2425 + sql_store_host_system_info_key_value(host_id, "NETDATA_CONTAINER_OS_DETECTION", system_info->host_os_detection);
2426 +
2427 + if (system_info->host_os_name)
2428 + sql_store_host_system_info_key_value(host_id, "NETDATA_HOST_OS_NAME", system_info->host_os_name);
2429 +
2430 + if (system_info->host_os_id)
2431 + sql_store_host_system_info_key_value(host_id, "NETDATA_HOST_OS_ID", system_info->host_os_id);
2432 +
2433 + if (system_info->host_os_id_like)
2434 + sql_store_host_system_info_key_value(host_id, "NETDATA_HOST_OS_ID_LIKE", system_info->host_os_id_like);
2435 +
2436 + if (system_info->host_os_version)
2437 + sql_store_host_system_info_key_value(host_id, "NETDATA_HOST_OS_VERSION", system_info->host_os_version);
2438 +
2439 + if (system_info->host_os_version_id)
2440 + sql_store_host_system_info_key_value(host_id, "NETDATA_HOST_OS_VERSION_ID", system_info->host_os_version_id);
2441 +
2442 + if (system_info->host_os_detection)
2443 + sql_store_host_system_info_key_value(host_id, "NETDATA_HOST_OS_DETECTION", system_info->host_os_detection);
2444 +
2445 + if (system_info->kernel_name)
2446 + sql_store_host_system_info_key_value(host_id, "NETDATA_SYSTEM_KERNEL_NAME", system_info->kernel_name);
2447 +
2448 + if (system_info->host_cores)
2449 + sql_store_host_system_info_key_value(host_id, "NETDATA_SYSTEM_CPU_LOGICAL_CPU_COUNT", system_info->host_cores);
2450 +
2451 + if (system_info->host_cpu_freq)
2452 + sql_store_host_system_info_key_value(host_id, "NETDATA_SYSTEM_CPU_FREQ", system_info->host_cpu_freq);
2453 +
2454 + if (system_info->host_ram_total)
2455 + sql_store_host_system_info_key_value(host_id, "NETDATA_SYSTEM_TOTAL_RAM", system_info->host_ram_total);
2456 +
2457 + if (system_info->host_disk_space)
2458 + sql_store_host_system_info_key_value(host_id, "NETDATA_SYSTEM_TOTAL_DISK_SIZE", system_info->host_disk_space);
2459 +
2460 + if (system_info->kernel_version)
2461 + sql_store_host_system_info_key_value(host_id, "NETDATA_SYSTEM_KERNEL_VERSION", system_info->kernel_version);
2462 +
2463 + if (system_info->architecture)
2464 + sql_store_host_system_info_key_value(host_id, "NETDATA_SYSTEM_ARCHITECTURE", system_info->architecture);
2465 +
2466 + if (system_info->virtualization)
2467 + sql_store_host_system_info_key_value(host_id, "NETDATA_SYSTEM_VIRTUALIZATION", system_info->virtualization);
2468 +
2469 + if (system_info->virt_detection)
2470 + sql_store_host_system_info_key_value(host_id, "NETDATA_SYSTEM_VIRT_DETECTION", system_info->virt_detection);
2471 +
2472 + if (system_info->container)
2473 + sql_store_host_system_info_key_value(host_id, "NETDATA_SYSTEM_CONTAINER", system_info->container);
2474 +
2475 + if (system_info->container_detection)
2476 + sql_store_host_system_info_key_value(host_id, "NETDATA_SYSTEM_CONTAINER_DETECTION", system_info->container_detection);
2477 +
2478 + if (system_info->is_k8s_node)
2479 + sql_store_host_system_info_key_value(host_id, "NETDATA_HOST_IS_K8S_NODE", system_info->is_k8s_node);
2480 +
2481 + return;
2482 +}
2483 +
database/sqlite/sqlite_functions.h
+1
@@ -106,4 +106,5 @@ char *get_hostname_by_node_id(char *node_id);
106 void free_temporary_host(RRDHOST *host);
107 int init_database_batch(int rebuild, int init_type, const char *batch[]);
108 void migrate_localhost(uuid_t *host_uuid);
109 +extern void sql_store_host_system_info(uuid_t *host_id, const struct rrdhost_system_info *system_info);
110 #endif //NETDATA_SQLITE_FUNCTIONS_H