From 43a763daa7f61742c886566bf7436092fb0c958a Mon Sep 17 00:00:00 2001 From: Mehul Prajapati Date: Mon, 16 Sep 2024 19:55:03 -0400 Subject: [PATCH] syslog-ng-ctl: add get size of internal logs command. Signed-off-by: Mehul Prajapati --- lib/afinter.c | 24 ++++++++++++++++-------- lib/afinter.h | 1 + lib/mainloop-control.c | 4 ++++ syslog-ng-ctl/syslog-ng-ctl.c | 4 ++++ 4 files changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/afinter.c b/lib/afinter.c index 8cf8ac4aeaa..db5710d71d5 100644 --- a/lib/afinter.c +++ b/lib/afinter.c @@ -481,6 +481,8 @@ afinter_sd_deinit(LogPipe *s) self->source = NULL; } + afinter_stop_live_collection(); + if (!log_src_driver_deinit_method(s)) return FALSE; @@ -651,6 +653,8 @@ afinter_start_live_collection(void) { AFInterSource *self; + g_mutex_lock(&internal_msg_lock); + if (is_live_collection) return AFINTER_LIVE_COLLECTION_RUNNING; @@ -664,12 +668,11 @@ afinter_start_live_collection(void) self->options = options; stats_counter_set(metrics.queue_capacity, self->options->queue_capacity); - g_mutex_lock(&internal_msg_lock); current_internal_source = self; - g_mutex_unlock(&internal_msg_lock); - is_live_collection = TRUE; + g_mutex_unlock(&internal_msg_lock); + return AFINTER_LIVE_COLLECTION_STARTED; } @@ -688,20 +691,26 @@ afinter_reset_live_collection(void) void afinter_stop_live_collection(void) { + g_mutex_lock(&internal_msg_lock); if (is_live_collection) { if (current_internal_source != NULL) { AFInterSource *self = current_internal_source; - g_mutex_lock(&internal_msg_lock); current_internal_source = NULL; - g_mutex_unlock(&internal_msg_lock); is_live_collection = FALSE; g_free(self); } } + g_mutex_unlock(&internal_msg_lock); +} + +void +afinter_get_size_of_internal_logs(GString *result) +{ + g_string_append_printf(result, "Size of internal logs: %d", g_queue_get_length(internal_msg_queue)); } void @@ -711,16 +720,14 @@ afinter_get_collected_messages(GString *result) GString *msg; LogTemplate *template = NULL; + g_mutex_lock(&internal_msg_lock); if (internal_msg_queue != NULL) { msg = g_string_sized_new(1024); while (!g_queue_is_empty(internal_msg_queue)) { - g_mutex_lock(&internal_msg_lock); log_msg = g_queue_pop_head(internal_msg_queue); - g_mutex_unlock(&internal_msg_lock); - /* format log */ template = log_template_new(configuration, NULL); log_template_compile(template, "[$DATE] $HOST $MSGHDR$MSG\n", NULL); @@ -735,6 +742,7 @@ afinter_get_collected_messages(GString *result) g_string_free(msg, TRUE); internal_msg_queue = NULL; } + g_mutex_unlock(&internal_msg_lock); if (!strlen(result->str)) g_string_assign(result, "No live messages collected"); diff --git a/lib/afinter.h b/lib/afinter.h index 3c18cf07935..05159654c3c 100644 --- a/lib/afinter.h +++ b/lib/afinter.h @@ -72,4 +72,5 @@ AFInterLive afinter_start_live_collection(void); void afinter_stop_live_collection(void); void afinter_reset_live_collection(void); void afinter_get_collected_messages(GString *result); +void afinter_get_size_of_internal_logs(GString *result); #endif diff --git a/lib/mainloop-control.c b/lib/mainloop-control.c index 5dbc18c6ad2..54654e5238c 100644 --- a/lib/mainloop-control.c +++ b/lib/mainloop-control.c @@ -492,6 +492,10 @@ control_internal_logs(ControlConnection *cc, GString *command, gpointer user_dat afinter_stop_live_collection(); afinter_get_collected_messages(result); } + else if (g_str_equal(arguments[1], "SIZE")) + { + afinter_get_size_of_internal_logs(result); + } else { g_string_assign(result, "FAIL Invalid arguments received"); diff --git a/syslog-ng-ctl/syslog-ng-ctl.c b/syslog-ng-ctl/syslog-ng-ctl.c index 1ec817b9735..12f02fab73a 100644 --- a/syslog-ng-ctl/syslog-ng-ctl.c +++ b/syslog-ng-ctl/syslog-ng-ctl.c @@ -65,11 +65,13 @@ slng_reload(int argc, char *argv[], const gchar *mode, GOptionContext *ctx) static gboolean config_options_start = FALSE; static gboolean config_options_stop = FALSE; +static gboolean config_options_size = FALSE; static GOptionEntry internal_options[] = { { "start", 's', 0, G_OPTION_ARG_NONE, &config_options_start, "start live collection of internal logs", NULL }, { "stop", 'x', 0, G_OPTION_ARG_NONE, &config_options_stop, "stop live collection of internal logs", NULL }, + { "size", 'l', 0, G_OPTION_ARG_NONE, &config_options_size, "get size of internal logs", NULL }, { NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL } }; @@ -82,6 +84,8 @@ slng_internal(int argc, char *argv[], const gchar *mode, GOptionContext *ctx) g_string_assign(cmd, "INTERLOGS START"); else if (config_options_stop) g_string_assign(cmd, "INTERLOGS STOP"); + else if (config_options_size) + g_string_assign(cmd, "INTERLOGS SIZE"); else { fprintf(stderr, "Error: Unknown command\n");