Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions modules/event_flatstore/doc/event_flatstore_admin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,25 @@ modparam("event_flatstore", "suffix", "$time(%Y)")
</programlisting>
</example>
</section>
<section id="param_header" xreflabel="header">
<title><varname>header</varname> (string)</title>
<para>
If set, the string is written as the very first line of
every new log file created by the module.
Useful for column names.
</para>
<para>
<emphasis>Default value is <quote>""</quote> (disabled)</emphasis>.
</para>
<example>
<title>Set <varname>header</varname> parameter</title>
<programlisting format="linespecific">
...
modparam("event_flatstore", "header", "event,time,param1,param2")
...
</programlisting>
</example>
</section>
</section>
<section id="exported_functions" xreflabel="exported_functions">
<title>Exported Functions</title>
Expand Down
29 changes: 25 additions & 4 deletions modules/event_flatstore/event_flatstore.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ static unsigned long file_rotate_size;
static str file_suffix;
static pv_elem_p file_suffix_format;
static str escape_delimiter = {0, 0};
static str file_header;

static void raise_rotation_event(struct flat_file *file, const char *reason);
static void update_counters_and_rotate(struct flat_file *file,
Expand Down Expand Up @@ -116,6 +117,7 @@ static const param_export_t mod_params[] = {
{"rotate_size", INT_PARAM|STR_PARAM|USE_FUNC_PARAM, (void*)rotate_size_param},
{"suffix", STR_PARAM, &file_suffix.s},
{"escape_delimiter", STR_PARAM, &escape_delimiter.s},
{"header", STR_PARAM, &file_header.s},
{0,0,0}
};

Expand Down Expand Up @@ -283,6 +285,9 @@ static int mod_init(void) {
LM_DBG("The delimiter for separating columns in files was set at %.*s\n", delimiter.len, delimiter.s);
}

if (file_header.s)
file_header.len = strlen(file_header.s);

if (escape_delimiter.s) {
escape_delimiter.len = strlen(escape_delimiter.s);
if (escape_delimiter.len != delimiter.len) {
Expand Down Expand Up @@ -467,6 +472,7 @@ mi_response_t *mi_rotate(const mi_params_t *params,
found_fd->rotate_version++;
found_fd->record_count = 0;
found_fd->bytes_written = 0;
found_fd->header_written = 0;
ensure_file_path(found_fd, 1);

lock_release(global_lock);
Expand Down Expand Up @@ -503,6 +509,7 @@ static int insert_in_list(struct flat_file *entry) {
*list_files = entry;
entry->prev = NULL;
entry->next = NULL;
entry->header_written = 0;
return 0;
}

Expand All @@ -512,6 +519,7 @@ static int insert_in_list(struct flat_file *entry) {
entry->file_index_process = head->file_index_process + 1;
entry->prev = NULL;
entry->next = head;
entry->header_written = 0;
head->prev = entry;
*list_files = entry;
return 0;
Expand All @@ -524,6 +532,7 @@ static int insert_in_list(struct flat_file *entry) {
entry->file_index_process = expected;
entry->prev = aux->prev;
entry->next = aux;
entry->header_written = 0;
aux->prev =entry;
entry->prev->next = entry;
return 0;
Expand All @@ -537,6 +546,7 @@ static int insert_in_list(struct flat_file *entry) {
entry->file_index_process = expected;
entry->prev = parent;
entry->next = NULL;
entry->header_written = 0;
parent->next = entry;
return 0;
}
Expand Down Expand Up @@ -777,6 +787,17 @@ static void rotating(struct flat_file *file){
file->counter_open++;
LM_DBG("File %s is opened %d time\n", file->pathname, file->counter_open);

/* write header */
if (file_header.s && !file->header_written) {
if (write(opened_fds[index], file_header.s,
file_header.len) == file_header.len &&
write(opened_fds[index], "\n", 1) == 1) {
file->header_written = 1;
} else {
LM_ERR("failed to write header to %s\n", file->pathname);
}
}

lock_release(global_lock);
return;

Expand Down Expand Up @@ -1032,6 +1053,7 @@ static void event_flatstore_timer(unsigned int ticks, void *param)
file->rotate_version++;
file->record_count = 0;
file->bytes_written = 0;
file->header_written = 0;
ensure_file_path(file, 1);
raise_rotation_event(file, ROTATE_REASON_PERIOD);
LM_DBG("File %s is being rotated at %u - new file is %s\n",
Expand Down Expand Up @@ -1179,18 +1201,17 @@ static void update_counters_and_rotate(struct flat_file *file, ssize_t bytes_inc
file->record_count++;
file->bytes_written += (unsigned long)bytes_inc;

if (file_rotate_count &&
file->record_count >= file_rotate_count)
if (file_rotate_count && file->record_count >= file_rotate_count)
hit_cnt = 1;

if (!hit_cnt && file_rotate_size &&
file->bytes_written >= file_rotate_size)
if (!hit_cnt && file_rotate_size && file->bytes_written >= file_rotate_size)
hit_sz = 1;

if (hit_cnt || hit_sz) {
file->rotate_version++;
file->record_count = 0;
file->bytes_written = 0;
file->header_written = 0;
ensure_file_path(file, 1);
}
lock_release(global_lock);
Expand Down
1 change: 1 addition & 0 deletions modules/event_flatstore/event_flatstore.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct flat_file {
unsigned int counter_open;
unsigned int rotate_version;
unsigned int flat_socket_ref;
int header_written;
struct flat_file *next;
struct flat_file *prev;
};
Expand Down