Skip to content

Commit

Permalink
Merge pull request #29 from m-kress/configdir
Browse files Browse the repository at this point in the history
read additional config files from a directory configured by configdir
  • Loading branch information
m-kress authored Jul 19, 2023
2 parents 0586af2 + ef0f7a0 commit bb7c007
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
- m-kress:
-- Do not substitute trailing control characters of a log line, but eliminate them completelly.
Be aware, that this change will break existing filters that rely on the trailing underscore!
-- note in config file and man page: comments must be started with # only at the beginning of a line
-- add "configdir" to config, which allows reading additional config files ending with *.conf

* Version 20230707
- m-kress:
Expand Down
5 changes: 5 additions & 0 deletions man/metalog.conf.5.in
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ if the rate of messages has been well below the limit before.
The specified message rate will not be exceeded.
The default value of 1 disables special treatment for bursts.
Values less than 1 are invalid and will be rejected.
.TP
\fBconfigdir = \fI"/path/to/directory/with/more/config/files"\fR
This optional directory can contain more config files that will get interpreted at
metalog start. Only file names ending with ".conf" are interpreted. This option is
only allowed once, all repetitions will be ignored.
.SH "FILES"
.LP
Note that the exact paths depend on the build settings. These are the standard paths.
Expand Down
3 changes: 3 additions & 0 deletions metalog.conf
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ maxtime = 2592000
# Number of archive files per directory
maxfiles = 5

# directory with more config files to load; files names must end with ".conf"
#configdir = /etc/metalog.d

# Permissions for log directories. 0750 allows group to read logs. 0700 is default.
#perms = 0750

Expand Down
62 changes: 61 additions & 1 deletion src/metalog.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,17 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
(*cur_block)->output->perms = (*cur_block)->perms;
}
}
else if (strcasecmp(keyword, "configdir") == 0) {
/* allow only once to avoid recursion */
if (config_dir != NULL) {
warnp("Ignore repeated \"configdir\" statement");
}
else {
if ((config_dir = wstrdup(value)) == NULL) {
return -3;
}
}
}
else if (strcasecmp(keyword, "logdir") == 0) {
char *logdir = NULL;
Output *outputs_scan = outputs;
Expand Down Expand Up @@ -406,7 +417,7 @@ static int parseLine(char * const line, ConfigBlock **cur_block,
static int configParser(const char * const file)
{
char line[LINE_MAX];
FILE *fp;
FILE *fp = NULL;
pcre2_code *re_newblock;
pcre2_code *re_newstmt;
pcre2_code *re_comment;
Expand Down Expand Up @@ -459,6 +470,55 @@ static int configParser(const char * const file)
break;
}
}

/* read all config files of an eventually configured directory */
if (config_dir != NULL) {
DIR *dp;
struct dirent *ep;

dp = opendir(config_dir);
if (dp == NULL) {
retcode = -1;
goto rtn;
}

/* config file names must end with ".conf" */
while ((ep = readdir(dp)) != NULL) {
FILE *fp2 = NULL;
char *p = NULL;
char *file_path = NULL;

p = strchr(ep->d_name, (int) '.');
if (p == NULL || !strstr(p, ".conf")) {
continue;
}

if ((file_path = wmalloc(strlen(ep->d_name) + strlen(config_dir) + 2)) == NULL) {
retcode = -1;
goto rtn;
}
sprintf(file_path, "%s/%s", config_dir, ep->d_name);
if ((fp2 = fopen(file_path, "r")) == NULL) {
warnp("Can't open the config file %s", file_path);
retcode = -1;
free(file_path);
goto rtn;
}

while (fgets(line, sizeof line, fp2) != NULL) {
if ((retcode = parseLine(line, &cur_block, &default_block,
re_newblock, re_newstmt,
re_comment, re_null)) != 0) {
break;
}
}

free(file_path);
fclose(fp2);
}
closedir(dp);
}

rtn:
if (re_newblock != NULL) {
pcre2_code_free(re_newblock);
Expand Down
1 change: 1 addition & 0 deletions src/metalog_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static bool do_kernel_log = true;
static signed char daemonize;
static const char *pid_file = DEFAULT_PID_FILE;
static const char *config_file = DEFAULT_CONFIG_FILE;
static const char *config_dir = NULL;
static const char *group_name = NULL;

#endif

0 comments on commit bb7c007

Please sign in to comment.