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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
/*
* Copyright (c) 2015 BalaBit
* 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 "grouping-parser.h"
#include "scratch-buffers.h"
#include "str-utils.h"
void
grouping_parser_set_key_template(LogParser *s, LogTemplate *key_template)
{
GroupingParser *self = (GroupingParser *) s;
log_template_unref(self->key_template);
self->key_template = log_template_ref(key_template);
}
void
grouping_parser_set_sort_key_template(LogParser *s, LogTemplate *sort_key)
{
GroupingParser *self = (GroupingParser *) s;
log_template_unref(self->sort_key_template);
self->sort_key_template = log_template_ref(sort_key);
}
void
grouping_parser_set_scope(LogParser *s, CorrelationScope scope)
{
GroupingParser *self = (GroupingParser *) s;
self->scope = scope;
}
void
grouping_parser_set_timeout(LogParser *s, gint timeout)
{
GroupingParser *self = (GroupingParser *) s;
self->timeout = timeout;
}
void
grouping_parser_clone_settings(GroupingParser *self, GroupingParser *cloned)
{
stateful_parser_clone_settings(&self->super, &cloned->super);
grouping_parser_set_key_template(&cloned->super.super, self->key_template);
grouping_parser_set_sort_key_template(&cloned->super.super, self->sort_key_template);
grouping_parser_set_timeout(&cloned->super.super, self->timeout);
grouping_parser_set_scope(&cloned->super.super, self->scope);
}
/*
* This function can be called any time when pattern-db is not processing
* messages, but we expect the correlation timer to move forward. It
* doesn't need to be called absolutely regularly as it'll use the current
* system time to determine how much time has passed since the last
* invocation. See the timing comment at pattern_db_process() for more
* information.
*/
static void
_advance_time_by_timer_tick(GroupingParser *self)
{
StatefulParserEmittedMessages emitted_messages = STATEFUL_PARSER_EMITTED_MESSAGES_INIT;
if (correlation_state_timer_tick(self->correlation, &emitted_messages))
{
msg_debug("grouping-parser: advancing current time because of timer tick",
evt_tag_long("utc", correlation_state_get_time(self->correlation)),
log_pipe_location_tag(&self->super.super.super));
}
stateful_parser_emitted_messages_flush(&emitted_messages, &self->super);
}
static void
_timer_tick(gpointer s)
{
GroupingParser *self = (GroupingParser *) s;
_advance_time_by_timer_tick(self);
iv_validate_now();
self->tick.expires = iv_now;
self->tick.expires.tv_sec++;
iv_timer_register(&self->tick);
}
/* NOTE: lock is acquired within correlation_state_set_time() */
void
_advance_time_based_on_message(GroupingParser *self, const UnixTime *ls,
StatefulParserEmittedMessages *emitted_messages)
{
correlation_state_set_time(self->correlation, ls->ut_sec, emitted_messages);
msg_debug("grouping-parser: advancing current time because of an incoming message",
evt_tag_long("utc", correlation_state_get_time(self->correlation)),
log_pipe_location_tag(&self->super.super.super));
}
static void
_load_correlation_state(GroupingParser *self, GlobalConfig *cfg)
{
CorrelationState *persisted_correlation = cfg_persist_config_fetch(cfg,
log_pipe_get_persist_name(&self->super.super.super));
if (persisted_correlation)
{
correlation_state_unref(self->correlation);
self->correlation = persisted_correlation;
}
timer_wheel_set_associated_data(self->correlation->timer_wheel, log_pipe_ref((LogPipe *)self),
(GDestroyNotify)log_pipe_unref);
}
static void
_store_data_in_persist(GroupingParser *self, GlobalConfig *cfg)
{
cfg_persist_config_add(cfg, log_pipe_get_persist_name(&self->super.super.super),
correlation_state_ref(self->correlation),
(GDestroyNotify) correlation_state_unref);
}
LogMessage *
grouping_parser_aggregate_context(GroupingParser *self, CorrelationContext *context)
{
if (context->messages->len == 0)
return NULL;
if (self->sort_key_template)
correlation_context_sort(context, self->sort_key_template);
LogMessage *msg = self->aggregate_context(self, context);
correlation_context_clear(context);
/* correlation_context_free is automatically called when returning from
this function by the timerwheel code as a destroy notify
callback. */
return msg;
}
static void
_expire_entry(TimerWheel *wheel, guint64 now, gpointer user_data, gpointer caller_context)
{
CorrelationContext *context = user_data;
StatefulParserEmittedMessages *emitted_messages = caller_context;
GroupingParser *self = (GroupingParser *) timer_wheel_get_associated_data(wheel);
msg_debug("grouping-parser: Expiring correlation context",
evt_tag_long("utc", correlation_state_get_time(self->correlation)),
evt_tag_str("context-id", context->key.session_id),
log_pipe_location_tag(&self->super.super.super));
context->timer = NULL;
LogMessage *msg = grouping_parser_aggregate_context(self, context);
correlation_state_tx_remove_context(self->correlation, context);
if (msg)
{
stateful_parser_emitted_messages_add(emitted_messages, msg);
log_msg_unref(msg);
}
}
CorrelationContext *
grouping_parser_lookup_or_create_context(GroupingParser *self, LogMessage *msg)
{
CorrelationContext *context;
CorrelationKey key;
GString *buffer = scratch_buffers_alloc();
log_template_format(self->key_template, msg, &DEFAULT_TEMPLATE_EVAL_OPTIONS, buffer);
correlation_key_init(&key, self->scope, msg, buffer->str);
context = correlation_state_tx_lookup_context(self->correlation, &key);
if (!context)
{
msg_debug("grouping-parser: Correlation context lookup failure, starting a new context",
evt_tag_str("key", key.session_id),
evt_tag_int("timeout", self->timeout),
evt_tag_int("expiration", correlation_state_get_time(self->correlation) + self->timeout),
log_pipe_location_tag(&self->super.super.super));
context = grouping_parser_construct_context(self, &key);
correlation_state_tx_store_context(self->correlation, context, self->timeout);
g_string_steal(buffer);
}
else
{
msg_debug("grouping-parser: Correlation context lookup successful",
evt_tag_str("key", key.session_id),
evt_tag_int("timeout", self->timeout),
evt_tag_int("expiration", correlation_state_get_time(self->correlation) + self->timeout),
evt_tag_int("num_messages", context->messages->len),
log_pipe_location_tag(&self->super.super.super));
}
return context;
}
static void
_aggregate_and_emit(GroupingParser *self, CorrelationContext *context, StatefulParserEmittedMessages *emitted_messages)
{
LogMessage *genmsg = grouping_parser_aggregate_context(self, context);
correlation_state_tx_update_context(self->correlation, context, self->timeout);
correlation_state_tx_end(self->correlation);
if (genmsg)
{
stateful_parser_emitted_messages_add(emitted_messages, genmsg);
log_msg_unref(genmsg);
}
}
void
grouping_parser_perform_grouping(GroupingParser *self, LogMessage *msg, StatefulParserEmittedMessages *emitted_messages)
{
correlation_state_tx_begin(self->correlation);
CorrelationContext *context = grouping_parser_lookup_or_create_context(self, msg);
GroupingParserUpdateContextResult r = grouping_parser_update_context(self, context, msg);
if (r == GP_CONTEXT_UPDATED)
{
msg_debug("grouping-parser: Correlation context update successful",
evt_tag_str("key", context->key.session_id),
evt_tag_int("num_messages", context->messages->len),
evt_tag_int("expiration", correlation_state_get_time(self->correlation) + self->timeout),
log_pipe_location_tag(&self->super.super.super));
correlation_state_tx_update_context(self->correlation, context, self->timeout);
correlation_state_tx_end(self->correlation);
}
else if (r == GP_CONTEXT_COMPLETE)
{
msg_debug("grouping-parser: Correlation finished, aggregating context",
evt_tag_str("key", context->key.session_id),
evt_tag_int("num_messages", context->messages->len),
evt_tag_int("expiration", correlation_state_get_time(self->correlation) + self->timeout),
log_pipe_location_tag(&self->super.super.super));
_aggregate_and_emit(self, context, emitted_messages);
}
else if (r == GP_STARTS_NEW_CONTEXT)
{
msg_debug("grouping-parser: Correlation finished, aggregating and starting a new context",
evt_tag_str("key", context->key.session_id),
evt_tag_int("num_messages", context->messages->len),
evt_tag_int("expiration", correlation_state_get_time(self->correlation) + self->timeout),
log_pipe_location_tag(&self->super.super.super));
_aggregate_and_emit(self, context, emitted_messages);
grouping_parser_perform_grouping(self, msg, emitted_messages);
}
}
gboolean
grouping_parser_process_method(LogParser *s,
LogMessage **pmsg, const LogPathOptions *path_options,
const char *input, gsize input_len)
{
GroupingParser *self = (GroupingParser *) s;
if (grouping_parser_filter_messages(self, pmsg, path_options))
{
LogMessage *msg = *pmsg;
StatefulParserEmittedMessages emitted_messages = STATEFUL_PARSER_EMITTED_MESSAGES_INIT;
_advance_time_based_on_message(self, &msg->timestamps[LM_TS_STAMP], &emitted_messages);
grouping_parser_perform_grouping(self, msg, &emitted_messages);
stateful_parser_emitted_messages_flush(&emitted_messages, &self->super);
}
return (self->super.inject_mode != LDBP_IM_AGGREGATE_ONLY);
}
gboolean
grouping_parser_init_method(LogPipe *s)
{
GroupingParser *self = (GroupingParser *) s;
GlobalConfig *cfg = log_pipe_get_config(s);
iv_validate_now();
IV_TIMER_INIT(&self->tick);
self->tick.cookie = self;
self->tick.handler = _timer_tick;
self->tick.expires = iv_now;
self->tick.expires.tv_sec++;
self->tick.expires.tv_nsec = 0;
iv_timer_register(&self->tick);
_load_correlation_state(self, cfg);
return stateful_parser_init_method(s);
}
gboolean
grouping_parser_deinit_method(LogPipe *s)
{
GroupingParser *self = (GroupingParser *) s;
GlobalConfig *cfg = log_pipe_get_config(s);
if (iv_timer_registered(&self->tick))
{
iv_timer_unregister(&self->tick);
}
_store_data_in_persist(self, cfg);
return stateful_parser_deinit_method(s);
}
void
grouping_parser_free_method(LogPipe *s)
{
GroupingParser *self = (GroupingParser *) s;
log_template_unref(self->key_template);
log_template_unref(self->sort_key_template);
correlation_state_unref(self->correlation);
stateful_parser_free_method(s);
}
void
grouping_parser_init_instance(GroupingParser *self, GlobalConfig *cfg)
{
stateful_parser_init_instance(&self->super, cfg);
self->super.super.super.free_fn = grouping_parser_free_method;
self->super.super.super.init = grouping_parser_init_method;
self->super.super.super.deinit = grouping_parser_deinit_method;
self->super.super.process = grouping_parser_process_method;
self->scope = RCS_GLOBAL;
self->timeout = -1;
self->correlation = correlation_state_new(_expire_entry);
}
void
grouping_parser_global_init(void)
{
}
|