1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/*
* Copyright (c) 2023 One Identity LLC.
* Copyright (c) 2023 Balazs Scheidler <bazsi77@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; 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 "group-lines.h"
#include "correlation.h"
#include "correlation-context.h"
#include "scratch-buffers.h"
#include "str-utils.h"
#include "messages.h"
#include "grouping-parser.h"
#include "id-counter.h"
#include <iv.h>
typedef struct _GroupLinesContext
{
CorrelationContext super;
MultiLineLogic *multi_line;
GString *line_buffer;
} GroupLinesContext;
static void
group_lines_context_update(GroupLinesContext *self, LogMessage *msg)
{
if (self->super.messages->len > 0)
{
LogMessage *old_msg = g_ptr_array_index(self->super.messages, 0);
log_msg_unref(old_msg);
g_ptr_array_index(self->super.messages, 0) = log_msg_ref(msg);
}
else
g_ptr_array_add(self->super.messages, log_msg_ref(msg));
}
static void
group_lines_context_clear(CorrelationContext *s)
{
GroupLinesContext *self = (GroupLinesContext *) s;
g_string_truncate(self->line_buffer, 0);
correlation_context_clear_method(s);
}
static void
group_lines_context_free(CorrelationContext *s)
{
GroupLinesContext *self = (GroupLinesContext *) s;
multi_line_logic_free(self->multi_line);
g_string_free(self->line_buffer, TRUE);
correlation_context_free_method(&self->super);
}
GroupLinesContext *
group_lines_context_new(const CorrelationKey *key, MultiLineLogic *multi_line)
{
GroupLinesContext *self = g_new0(GroupLinesContext, 1);
correlation_context_init(&self->super, key);
self->super.free_fn = group_lines_context_free;
self->super.clear = group_lines_context_clear;
self->line_buffer = g_string_sized_new(1024);
self->multi_line = multi_line;
return self;
}
typedef struct _GroupLines
{
GroupingParser super;
guint clone_id;
IdCounter *id_counter;
gchar *separator;
gsize separator_len;
MultiLineOptions multi_line_options;
} GroupLines;
/* public functions */
MultiLineOptions *
group_lines_get_multi_line_options(LogParser *s)
{
GroupLines *self = (GroupLines *) s;
return &self->multi_line_options;
}
void
group_lines_set_separator(LogParser *s, const gchar *separator)
{
GroupLines *self = (GroupLines *) s;
g_free(self->separator);
self->separator = g_strdup(separator);
self->separator_len = strlen(self->separator);
}
static CorrelationContext *
_construct_context(GroupingParser *s, CorrelationKey *key)
{
GroupLines *self = (GroupLines *) s;
return &group_lines_context_new(key, multi_line_factory_construct(&self->multi_line_options))->super;
}
static void
_update_context_add_message(GroupLines *self, GroupLinesContext *context, LogMessage *msg, const gchar *line,
gsize line_len)
{
group_lines_context_update(context, msg);
msg_debug("group-lines: accumulating new segment into the line",
evt_tag_str("key", context->super.key.session_id),
evt_tag_mem("segment", line, line_len),
evt_tag_str("line", context->line_buffer->str));
if (context->line_buffer->len)
g_string_append_len(context->line_buffer, self->separator, self->separator_len);
g_string_append_len(context->line_buffer, line, line_len);
}
static const gchar *
_get_payload(GroupLines *self, LogMessage *msg, gssize *len)
{
LogTemplate *template = self->super.super.super.template_obj;
if (!template)
return log_msg_get_value(msg, LM_V_MESSAGE, len);
if (log_template_is_trivial(template))
return log_template_get_trivial_value(template, msg, len);
GString *buf = scratch_buffers_alloc();
log_template_format(template, msg, &DEFAULT_TEMPLATE_EVAL_OPTIONS, buf);
*len = buf->len;
return buf->str;
}
static GroupingParserUpdateContextResult
_update_context(GroupingParser *s, CorrelationContext *c, LogMessage *msg)
{
GroupLines *self = (GroupLines *) s;
GroupLinesContext *context = (GroupLinesContext *) c;
gssize line_len;
const gchar *line = _get_payload(self, msg, &line_len);
gint verdict = multi_line_logic_accumulate_line(context->multi_line, (const guchar *) context->line_buffer->str,
context->line_buffer->len, (const guchar *) line, line_len);
if (verdict & MLL_EXTRACTED)
{
if (verdict & MLL_CONSUME_SEGMENT)
{
gint drop_length = (verdict & MLL_CONSUME_PARTIAL_AMOUNT_MASK) >> MLL_CONSUME_PARTIAL_AMOUNT_SHIFT;
_update_context_add_message(self, context, msg, line, line_len - drop_length);
msg_debug("group-lines: accumulated line extracted",
evt_tag_str("key", context->super.key.session_id),
evt_tag_str("line", context->line_buffer->str));
return GP_CONTEXT_COMPLETE;
}
else if (verdict & MLL_REWIND_SEGMENT)
{
msg_debug("group-lines: accumulated line extracted",
evt_tag_str("key", context->super.key.session_id),
evt_tag_str("line", context->line_buffer->str));
return GP_STARTS_NEW_CONTEXT;
}
else
g_assert_not_reached();
}
else if (verdict & MLL_WAITING)
{
if (verdict & MLL_CONSUME_SEGMENT)
{
_update_context_add_message(self, context, msg, line, line_len);
return GP_CONTEXT_UPDATED;
}
else
g_assert_not_reached();
return FALSE;
}
else
g_assert_not_reached();
}
static LogMessage *
_generate_synthetic_msg(GroupLines *self, GroupLinesContext *context)
{
LogPathOptions path_options = LOG_PATH_OPTIONS_INIT;
path_options.ack_needed = FALSE;
LogMessage *msg = log_msg_ref(correlation_context_get_last_message(&context->super));
log_msg_make_writable(&msg, &path_options);
return msg;
}
static LogMessage *
_aggregate_context(GroupingParser *s, CorrelationContext *c)
{
GroupLines *self = (GroupLines *) s;
GroupLinesContext *context = (GroupLinesContext *) c;
LogMessage *msg = _generate_synthetic_msg(self, context);
log_msg_set_value(msg, LM_V_MESSAGE, context->line_buffer->str, context->line_buffer->len);
return msg;
}
static const gchar *
_format_persist_name(const LogPipe *s)
{
static gchar persist_name[512];
GroupLines *self = (GroupLines *)s;
if (s->persist_name)
g_snprintf(persist_name, sizeof(persist_name), "group-lines.%s(clone=%d)", s->persist_name, self->clone_id);
else
g_snprintf(persist_name, sizeof(persist_name), "group-lines(%s,scope=%d,clone=%d)",
self->super.key_template->template_str,
self->super.scope, self->clone_id);
return persist_name;
}
static gboolean
_init(LogPipe *s)
{
GroupLines *self = (GroupLines *) s;
self->id_counter = NULL;
if (self->super.timeout < 1)
{
msg_error("timeout() needs to be specified explicitly and must be greater than 0 in the group-lines() parser",
log_pipe_location_tag(s));
return FALSE;
}
if (!self->super.key_template)
{
msg_error("The key() option is mandatory for the group-lines() parser",
log_pipe_location_tag(s));
return FALSE;
}
if (!multi_line_options_validate(&self->multi_line_options))
return FALSE;
return grouping_parser_init_method(s);
}
static LogPipe *
_clone(LogPipe *s)
{
GroupLines *self = (GroupLines *) s;
GroupLines *cloned;
cloned = (GroupLines *) group_lines_new(s->cfg);
grouping_parser_clone_settings(&self->super, &cloned->super);
group_lines_set_separator(&cloned->super.super.super, self->separator);
multi_line_options_copy(&cloned->multi_line_options, &self->multi_line_options);
id_counter_unref(cloned->id_counter);
cloned->id_counter = id_counter_ref(self->id_counter);
cloned->clone_id = id_counter_get_next_id(cloned->id_counter);
return &cloned->super.super.super.super;
}
static void
_free(LogPipe *s)
{
GroupLines *self = (GroupLines *) s;
id_counter_unref(self->id_counter);
multi_line_options_destroy(&self->multi_line_options);
g_free(self->separator);
grouping_parser_free_method(s);
}
LogParser *
group_lines_new(GlobalConfig *cfg)
{
GroupLines *self = g_new0(GroupLines, 1);
self->id_counter = id_counter_new();
self->clone_id = id_counter_get_next_id(self->id_counter);
grouping_parser_init_instance(&self->super, cfg);
self->super.super.super.super.init = _init;
self->super.super.super.super.free_fn = _free;
self->super.super.super.super.clone = _clone;
self->super.super.super.super.generate_persist_name = _format_persist_name;
self->super.scope = RCS_GLOBAL;
self->super.construct_context = _construct_context;
self->super.update_context = _update_context;
self->super.aggregate_context = _aggregate_context;
self->super.super.inject_mode = LDBP_IM_AGGREGATE_ONLY;
group_lines_set_separator(&self->super.super.super, "\n");
multi_line_options_defaults(&self->multi_line_options);
return &self->super.super.super;
}
|