diff --git a/src/gvmd.c b/src/gvmd.c index af7bc1195..5faf0e330 100644 --- a/src/gvmd.c +++ b/src/gvmd.c @@ -2223,6 +2223,7 @@ gvmd (int argc, char** argv, char *env[]) static gboolean disable_password_policy = FALSE; static gboolean disable_scheduling = FALSE; static gboolean dump_vt_verification = FALSE; + static gboolean dump_asset_snapshot_counts = FALSE; static gchar *encryption_key_type = NULL; static int encryption_key_length = 0; static gchar *set_encryption_key = NULL; @@ -2407,6 +2408,10 @@ gvmd (int argc, char** argv, char *env[]) &dump_vt_verification, "Dump the string the VTs verification hash is calculated from.", NULL }, + { "dump-asset-snapshot-counts", '\0', 0, G_OPTION_ARG_NONE, + &dump_asset_snapshot_counts, + "Dump the string the Asset snapshot counts are calculated from.", + NULL }, { "encryption-key-length", '\0', 0, G_OPTION_ARG_INT, &encryption_key_length, "Set key length to bits when creating a new RSA" @@ -3311,6 +3316,25 @@ gvmd (int argc, char** argv, char *env[]) return EXIT_SUCCESS; } + if (dump_asset_snapshot_counts) + { + int ret; + + setproctitle ("--dump-asset-snapshot-counts"); + + if (option_lock (&lockfile_checking)) + return EXIT_FAILURE; + + ret = manage_dump_asset_snapshot_counts (log_config, &database); + log_config_free (); + if (ret) + { + printf ("Failed to dump Asset snapshot counts.\n"); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; + } + if (create_scanner) { int ret; diff --git a/src/manage.h b/src/manage.h index 71522fdea..7bf506a25 100644 --- a/src/manage.h +++ b/src/manage.h @@ -3530,6 +3530,9 @@ manage_rebuild (GSList *, const db_conn_info_t *); int manage_dump_vt_verification (GSList *, const db_conn_info_t *); +int +manage_dump_asset_snapshot_counts(GSList *, const db_conn_info_t *); + /* Wizards. */ diff --git a/src/manage_sql_assets.c b/src/manage_sql_assets.c index 9228b9cbc..56d12921e 100644 --- a/src/manage_sql_assets.c +++ b/src/manage_sql_assets.c @@ -1515,6 +1515,73 @@ asset_snapshots_container_image (report_t report, task_t task) } #endif /* ENABLE_CONTAINER_SCANNING */ +/** + * @brief Dump the string for Asset Snapshot counts to stdout. + * + * @param[in] log_config Log configuration. + * @param[in] database Location of manage database. + * + * @return 0 success, -1 error, -2 database is too old, + * -3 database needs to be initialised from server, + * -5 database is too new. + */ +int +manage_dump_asset_snapshot_counts (GSList *log_config, + const db_conn_info_t *database) +{ + int ret; + + int total_count = 0; + int target_count = 0; + int agent_count = 0; + int container_image_count = 0; + + ret = manage_option_setup (log_config, database, + 0 /* avoid_db_check_inserts */); + if (ret) + return ret; + + total_count = sql_int ( + "SELECT COUNT(DISTINCT asset_key) FROM asset_snapshots;"); + + target_count = sql_int ( + "SELECT COUNT(DISTINCT asset_key) FROM asset_snapshots" + " WHERE asset_type = %d;", + ASSET_TYPE_TARGET); + + agent_count = sql_int ( + "SELECT COUNT(DISTINCT asset_key) FROM asset_snapshots" + " WHERE asset_type = %d;", + ASSET_TYPE_AGENT); + + container_image_count = sql_int ( + "SELECT COUNT(DISTINCT asset_key) FROM asset_snapshots" + " WHERE asset_type = %d;", + ASSET_TYPE_CONTAINER_IMAGE); + + GString *out = g_string_new (NULL); + + g_string_append (out, "Asset Snapshot Counts (distinct asset_key)\n"); + g_string_append_printf ( + out, " Total: %d\n", total_count); + g_string_append_printf ( + out, " Targets (type=%d): %d\n", + ASSET_TYPE_TARGET, target_count); + g_string_append_printf ( + out, " Agents (type=%d): %d\n", + ASSET_TYPE_AGENT, agent_count); + g_string_append_printf ( + out, " Container images (type=%d): %d\n", + ASSET_TYPE_CONTAINER_IMAGE, container_image_count); + + printf ("%s", out->str); + + g_string_free (out, TRUE); + + manage_option_cleanup (); + return 0; +} + /** * @brief Setup hosts and their identifiers after a scan, from host details. *