Skip to content

Commit

Permalink
syslog-ng-ctl: add get size of internal logs command.
Browse files Browse the repository at this point in the history
Signed-off-by: Mehul Prajapati <mehul.encs@gmail.com>
  • Loading branch information
Mehul Prajapati committed Sep 18, 2024
1 parent 1e70245 commit 43a763d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lib/afinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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;
}

Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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");
Expand Down
1 change: 1 addition & 0 deletions lib/afinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions lib/mainloop-control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 4 additions & 0 deletions syslog-ng-ctl/syslog-ng-ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
};

Expand All @@ -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");
Expand Down

0 comments on commit 43a763d

Please sign in to comment.