Skip to content

Commit

Permalink
syslog-ng-ctl: review changes.
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 authored and mehul-m-prajapati committed Oct 3, 2024
1 parent 338c29c commit cc735c7
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 85 deletions.
105 changes: 70 additions & 35 deletions lib/afinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ struct _AFInterSource

static void afinter_source_update_watches(AFInterSource *self);
void afinter_message_posted(LogMessage *msg);
static void afinter_reset_live_collection(void);

static void
afinter_source_post(gpointer s)
Expand Down Expand Up @@ -481,7 +482,7 @@ afinter_sd_deinit(LogPipe *s)
self->source = NULL;
}

afinter_stop_live_collection();
afinter_stop_live_collection(NULL);

if (!log_src_driver_deinit_method(s))
return FALSE;
Expand Down Expand Up @@ -651,17 +652,21 @@ afinter_get_metrics(void)
AFInterLive
afinter_start_live_collection(void)
{
AFInterSource *self;

g_mutex_lock(&internal_msg_lock);

if (is_live_collection)
return AFINTER_LIVE_COLLECTION_RUNNING;
{
g_mutex_unlock(&internal_msg_lock);
return AFINTER_LIVE_COLLECTION_RUNNING;
}

if (current_internal_source != NULL)
return AFINTER_INTERNAL_SRC_PRESENT;
{
g_mutex_unlock(&internal_msg_lock);
return AFINTER_INTERNAL_SRC_PRESENT;
}

self = g_new0(AFInterSource, 1);
AFInterSource *self = g_new0(AFInterSource, 1);
AFInterSourceOptions *options = g_new0(AFInterSourceOptions, 1);

afinter_source_options_defaults(options);
Expand All @@ -676,50 +681,30 @@ afinter_start_live_collection(void)
return AFINTER_LIVE_COLLECTION_STARTED;
}

void
static void
afinter_reset_live_collection(void)
{
afinter_stop_live_collection();
afinter_stop_live_collection(NULL);

g_mutex_lock(&internal_msg_lock);
if (is_live_collection)
{
if (internal_msg_queue)
_release_internal_msg_queue();
}
}

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;

current_internal_source = NULL;

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
static void
afinter_get_collected_messages(GString *result)
{
LogMessage *log_msg;
GString *msg;
LogTemplate *template = NULL;

if (result == NULL)
return;

g_mutex_lock(&internal_msg_lock);
if (internal_msg_queue != NULL)
{
Expand All @@ -730,10 +715,9 @@ afinter_get_collected_messages(GString *result)
log_msg = g_queue_pop_head(internal_msg_queue);
/* format log */
template = log_template_new(configuration, NULL);
log_template_compile(template, "[$DATE] $HOST $MSGHDR$MSG\n", NULL);
log_template_compile(template, "[$DATE] $HOST $MSGHDR$MSG", NULL);
log_template_format(template, log_msg, &DEFAULT_TEMPLATE_EVAL_OPTIONS, msg);
g_string_append_printf(result, "%s\n", msg->str);

log_msg_unref(log_msg);
log_msg = NULL;
stats_counter_dec(internal_queue_length);
Expand All @@ -747,3 +731,54 @@ afinter_get_collected_messages(GString *result)
if (!strlen(result->str))
g_string_assign(result, "No live messages collected");
}

AFInterLive
afinter_stop_live_collection(GString *result)
{
g_mutex_lock(&internal_msg_lock);
if (is_live_collection)
{
if (current_internal_source != NULL)
{
AFInterSource *self = current_internal_source;

current_internal_source = NULL;
is_live_collection = FALSE;
g_free(self);
}
g_mutex_unlock(&internal_msg_lock);
afinter_get_collected_messages(result);

return AFINTER_LIVE_COLLECTION_NONE;
}
else
{
g_mutex_unlock(&internal_msg_lock);
return AFINTER_LIVE_COLLECTION_INIT;
}
}

AFInterLive
afinter_get_size_of_internal_logs(GString *result)
{
g_mutex_lock(&internal_msg_lock);

if (is_live_collection)
{

if (internal_msg_queue != NULL)
g_string_append_printf(result, "Size of internal logs: %d", g_queue_get_length(internal_msg_queue));
else
g_string_append_printf(result, "Size of internal logs: 0");

g_mutex_unlock(&internal_msg_lock);

return AFINTER_LIVE_COLLECTION_NONE;
}
else
{
g_mutex_unlock(&internal_msg_lock);

return AFINTER_LIVE_COLLECTION_INIT;
}
}
8 changes: 4 additions & 4 deletions lib/afinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ typedef struct AFInterSourceOptions

typedef enum
{
AFINTER_LIVE_COLLECTION_INIT,
AFINTER_LIVE_COLLECTION_STARTED,
AFINTER_LIVE_COLLECTION_RUNNING,
AFINTER_INTERNAL_SRC_PRESENT,
AFINTER_LIVE_COLLECTION_NONE,
} AFInterLive;

/*
Expand Down Expand Up @@ -69,8 +71,6 @@ void afinter_global_deinit(void);
AFInterMetrics afinter_get_metrics(void);

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);
AFInterLive afinter_stop_live_collection(GString *result);
AFInterLive afinter_get_size_of_internal_logs(GString *result);
#endif
22 changes: 12 additions & 10 deletions lib/mainloop-control.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,20 +457,19 @@ control_internal_logs(ControlConnection *cc, GString *command, gpointer user_dat
{
GString *result = g_string_new("");
gchar **arguments = g_strsplit(command->str, " ", 0);
AFInterLive status = AFINTER_LIVE_COLLECTION_INIT;

if (g_str_equal(arguments[1], "START"))
{
AFInterLive ret;

if (g_process_get_mode() == G_PM_FOREGROUND)
{
g_string_assign(result, "FAIL Error: Cannot collect internal logs while syslog-ng is running in foreground");
goto exit;
}

ret = afinter_start_live_collection();
status = afinter_start_live_collection();

switch (ret)
switch (status)
{
case AFINTER_LIVE_COLLECTION_STARTED:
g_string_assign(result, "OK Started to collect internal messages");
Expand All @@ -490,17 +489,20 @@ control_internal_logs(ControlConnection *cc, GString *command, gpointer user_dat
}
else if (g_str_equal(arguments[1], "STOP"))
{
afinter_stop_live_collection();
afinter_get_collected_messages(result);
status = afinter_stop_live_collection(result);

if (status == AFINTER_LIVE_COLLECTION_INIT)
g_string_assign(result, "FAIL Error: 'STOP' command received without a prior 'START' command.");
}
else if (g_str_equal(arguments[1], "SIZE"))
{
afinter_get_size_of_internal_logs(result);
status = afinter_get_size_of_internal_logs(result);

if (status == AFINTER_LIVE_COLLECTION_INIT)
g_string_assign(result, "FAIL Error: 'SIZE' command received without a prior 'START' command.");
}
else
{
g_string_assign(result, "FAIL Invalid arguments received");
}
g_string_assign(result, "FAIL Invalid arguments received");

exit:
g_strfreev(arguments);
Expand Down
2 changes: 2 additions & 0 deletions syslog-ng-ctl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ set(SYSLOG_NG_CTL_SOURCES
commands/config.c
commands/healthcheck.h
commands/healthcheck.c
commands/internal-logs.h
commands/internal-logs.c
control-client.c
)

Expand Down
2 changes: 2 additions & 0 deletions syslog-ng-ctl/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ syslog_ng_ctl_syslog_ng_ctl_SOURCES = \
syslog-ng-ctl/commands/license.c \
syslog-ng-ctl/commands/healthcheck.h \
syslog-ng-ctl/commands/healthcheck.c \
syslog-ng-ctl/commands/internal-logs.h \
syslog-ng-ctl/commands/internal-logs.c \
syslog-ng-ctl/control-client.h \
syslog-ng-ctl/control-client.c

Expand Down
59 changes: 59 additions & 0 deletions syslog-ng-ctl/commands/internal-logs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2024 Balabit
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "internal-logs.h"
#include "commands.h"

static gboolean config_options_start = FALSE;
static gboolean config_options_stop = FALSE;
static gboolean config_options_size = FALSE;

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 }
};

gint
slng_internal_logs(int argc, char *argv[], const gchar *mode, GOptionContext *ctx)
{
GString *cmd = g_string_new("");

if (config_options_start)
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");
return 1;
}

gint res = dispatch_command(cmd->str);
g_string_free(cmd, TRUE);

return res;
}
32 changes: 32 additions & 0 deletions syslog-ng-ctl/commands/internal-logs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2024 Balabit
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/

#ifndef SYSLOG_NG_CTL_INTERNAL_LOGS_H
#define SYSLOG_NG_CTL_INTERNAL_LOGS_H

#include "commands.h"

extern GOptionEntry internal_options[];
gint slng_internal_logs(int argc, char *argv[], const gchar *mode, GOptionContext *ctx);

#endif
Loading

0 comments on commit cc735c7

Please sign in to comment.