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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
/*
* Copyright (c) 2023 Balazs Scheidler <bazsi77@gmail.com>
*
* 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 "smart-multi-line.h"
#include "regexp-multi-line.h"
#include "reloc.h"
#include "messages.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
enum
{
SMLS_NONE,
SMLS_START_STATE,
};
typedef struct _SmartMultiLineRule SmartMultiLineRule;
struct _SmartMultiLineRule
{
gint from_states[4];
gchar *regexp;
gint to_state;
MultiLinePattern *compiled_regexp;
};
typedef struct _SmartMultiLine
{
MultiLineLogic super;
GMutex lock;
gint current_state;
gboolean last_segment_rewound;
gboolean rewound_segment_is_trace;
gboolean consumed_message_is_trace;
} SmartMultiLine;
GHashTable *state_map;
gint last_state_id = SMLS_START_STATE;
GArray *rules;
GPtrArray *rules_by_from_state[64];
static void
_reshuffle_rules_by_from_state(void)
{
for (gint rule_ndx = 0; rule_ndx < rules->len; rule_ndx++)
{
SmartMultiLineRule *rule = &g_array_index(rules, SmartMultiLineRule, rule_ndx);
rule->compiled_regexp = multi_line_pattern_compile(rule->regexp, NULL);
g_assert(rule->compiled_regexp != NULL);
for (gint i = 0; rule->from_states[i]; i++)
{
g_assert(i < G_N_ELEMENTS(rule->from_states));
gint from_state = rule->from_states[i];
if (rules_by_from_state[from_state] == NULL)
rules_by_from_state[from_state] = g_ptr_array_new();
g_ptr_array_add(rules_by_from_state[from_state], rule);
}
}
}
static gint
_map_state(const gchar *state)
{
if (!state_map)
{
state_map = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
g_hash_table_insert(state_map, g_strdup("start_state"), GINT_TO_POINTER(SMLS_START_STATE));
}
gpointer value = g_hash_table_lookup(state_map, state);
if (value)
return GPOINTER_TO_INT(value);
if (last_state_id >= G_N_ELEMENTS(rules_by_from_state))
{
msg_error("smart-multi-line: too many states used in smart-multi-line.fsm, running with a partial a rule-set",
evt_tag_int("fsm-max-state", G_N_ELEMENTS(rules_by_from_state)),
evt_tag_str("state", state));
return 0;
}
last_state_id++;
g_hash_table_insert(state_map, g_strdup(state), GINT_TO_POINTER(last_state_id));
return last_state_id;
}
static gchar *
_extract_regexp(const gchar *regexp)
{
gint len = strlen(regexp);
if (regexp[0] == regexp[len - 1])
return g_strndup(®exp[1], len - 2);
return g_strdup(regexp);
}
static void
_parse_rule(const gchar *from_states, const gchar *regexp, const gchar *to_state)
{
SmartMultiLineRule new_rule = {0};
gchar **from_state_list = g_strsplit(from_states, ",", -1);
/* leave one slot for a terminating element */
for (gint i = 0; from_state_list[i] && i < G_N_ELEMENTS(new_rule.from_states) - 1; i++)
new_rule.from_states[i] = _map_state(from_state_list[i]);
new_rule.regexp = _extract_regexp(regexp);
new_rule.to_state = _map_state(to_state);
g_strfreev(from_state_list);
g_array_append_val(rules, new_rule);
}
static void
_read_rules(const gchar *filename, FILE *sml_file)
{
gchar line[1024];
gint lineno = 0;
while (fgets(line, sizeof(line), sml_file))
{
lineno++;
gint len = strlen(line);
if (len > 0 && line[len - 1] == '\n')
{
line[len - 1] = 0;
len--;
}
if (len == 0 || line[0] == '#')
continue;
gchar **parts = g_strsplit(line, "\t", 3);
if (g_strv_length(parts) != 3)
{
msg_error("smart-multi-line: error parsing line in pattern file, lines need to be in the form <from_states>\t<regexp>\t<to_state>",
evt_tag_str("filename", filename),
evt_tag_int("lineno", lineno));
continue;
}
gchar *from_states = parts[0];
gchar *regexp = parts[1];
gchar *to_state = parts[2];
msg_trace("smart-multi-line: processing pattern",
evt_tag_str("from_states", from_states),
evt_tag_str("regexp", regexp),
evt_tag_str("to_state", to_state));
_parse_rule(from_states, regexp, to_state);
g_strfreev(parts);
}
}
static void
_load_tsv_file(const gchar *sml_file_name)
{
FILE *sml_file = fopen(sml_file_name, "r");
if (!sml_file)
{
msg_error("smart-multi-line: error opening smart-multi-line.fsm file",
evt_tag_str("filename", sml_file_name),
evt_tag_error("error"));
return;
}
_read_rules(sml_file_name, sml_file);
fclose(sml_file);
}
static void
_load_rules(const gchar *sml_file_name)
{
if (rules)
return;
rules = g_array_new(FALSE, TRUE, sizeof(SmartMultiLineRule));
_load_tsv_file(sml_file_name);
_reshuffle_rules_by_from_state();
if (state_map)
{
g_hash_table_unref(state_map);
state_map = NULL;
}
if (rules_by_from_state[SMLS_START_STATE] == NULL)
{
msg_warning("smart-multi-line: your smart-multi-line.fsm seems to be empty or non-existent, "
"automatic multi-line log extraction will probably not work",
evt_tag_str("filename", sml_file_name));
}
}
static void
_free_rules(void)
{
for (gint state_ndx = 0; state_ndx < G_N_ELEMENTS(rules_by_from_state); state_ndx++)
{
if (rules_by_from_state[state_ndx])
{
g_ptr_array_free(rules_by_from_state[state_ndx], TRUE);
rules_by_from_state[state_ndx] = NULL;
}
}
for (gint rule_ndx = 0; rule_ndx < rules->len; rule_ndx++)
{
SmartMultiLineRule *rule = &g_array_index(rules, SmartMultiLineRule, rule_ndx);
multi_line_pattern_unref(rule->compiled_regexp);
g_free(rule->regexp);
}
g_array_free(rules, TRUE);
rules = NULL;
}
gboolean
_fsm_transition(SmartMultiLine *self, const gchar *segment, gsize segment_len)
{
GPtrArray *applicable_rules = rules_by_from_state[self->current_state];
for (gint i = 0; applicable_rules && i < applicable_rules->len; i++)
{
SmartMultiLineRule *rule = g_ptr_array_index(applicable_rules, i);
gboolean match = multi_line_pattern_match(rule->compiled_regexp, (const guchar *) segment, segment_len);
msg_trace_printf("smart-multi-line: Matching against pattern: %s in state %d, matched %d", rule->regexp,
self->current_state, match);
if (match)
{
self->current_state = rule->to_state;
/* the current segment is part of a sequence */
return TRUE;
}
}
self->current_state = SMLS_START_STATE;
return FALSE;
}
void
_process_segment(SmartMultiLine *self, const gchar *segment, gsize segment_len,
gboolean *segment_is_part_of_trace,
gboolean *segment_starts_a_new_trace,
gboolean *segment_ends_trace)
{
*segment_is_part_of_trace = FALSE;
*segment_starts_a_new_trace = FALSE;
*segment_ends_trace = FALSE;
gboolean last_segment_ended_the_trace = self->current_state == SMLS_START_STATE;
gboolean segment_is_trace = _fsm_transition(self, segment, segment_len);
msg_trace_printf("smart-multi-line: [STEP1] >>%.*s<<, result=%d, state=%d", (int) segment_len, segment,
segment_is_trace, self->current_state);
*segment_is_part_of_trace = segment_is_trace;
if (!(*segment_is_part_of_trace))
{
/* try again from the start state, the current segment is may be part of a new trace */
segment_is_trace = _fsm_transition(self, segment, segment_len);
msg_trace_printf("smart-multi-line: [STEP2]: >>%.*s<<, result=%d, state=%d", (int) segment_len, segment,
segment_is_trace, self->current_state);
*segment_is_part_of_trace = segment_is_trace;
if (*segment_is_part_of_trace)
*segment_starts_a_new_trace = TRUE;
}
else
{
if (last_segment_ended_the_trace)
*segment_starts_a_new_trace = TRUE;
*segment_ends_trace = (self->current_state == SMLS_START_STATE);
}
}
static gint
_accumulate_line_unlocked(MultiLineLogic *s,
const guchar *msg, gsize msg_len,
const guchar *segment, gsize segment_len)
{
SmartMultiLine *self = (SmartMultiLine *) s;
if (self->last_segment_rewound)
{
/* we rewound the last segment, processing it again. The previous
* message was extracted we are starting with a fresh start, but we
* don't run the current segment through the FSM, we simply reuse the
* result the last time we've run it. */
g_assert(msg_len == 0);
self->last_segment_rewound = FALSE;
if (self->rewound_segment_is_trace)
{
self->consumed_message_is_trace = TRUE;
return MLL_CONSUME_SEGMENT | MLL_WAITING;
}
else
{
self->consumed_message_is_trace = FALSE;
return MLL_CONSUME_SEGMENT | MLL_EXTRACTED;
}
}
else
{
gboolean segment_is_part_of_trace, segment_starts_a_new_trace, segment_ends_trace;
_process_segment(self, (const gchar *) segment, segment_len,
&segment_is_part_of_trace,
&segment_starts_a_new_trace,
&segment_ends_trace);
if (msg_len == 0)
{
/* just starting up, first segment is to be consumed */
if (!segment_is_part_of_trace)
{
/* not recognized as part of a trace, just return it */
self->consumed_message_is_trace = FALSE;
return MLL_CONSUME_SEGMENT | MLL_EXTRACTED;
}
/* first line of the trace, remember that we are within a trace
* and let's wait for more */
self->consumed_message_is_trace = TRUE;
return MLL_CONSUME_SEGMENT | MLL_WAITING;
}
else
{
/* we already have data in @msg, so processing subsequent lines */
if (self->consumed_message_is_trace && segment_is_part_of_trace)
{
/* ok, @msg is a trace and the new segment as well. */
if (segment_starts_a_new_trace)
{
/* the new segment is not actually part of the consumed
* @msg, it starts a new trace */
self->last_segment_rewound = TRUE;
self->rewound_segment_is_trace = TRUE;
return MLL_REWIND_SEGMENT | MLL_EXTRACTED;
}
else if (segment_ends_trace)
{
/* the new segment is trace, and is the last part of @msg */
return MLL_CONSUME_SEGMENT | MLL_EXTRACTED;
}
else
{
/* the new segment is a trace and is part of the partially
* consumed @msg */
return MLL_CONSUME_SEGMENT | MLL_WAITING;
}
}
else if (self->consumed_message_is_trace && !segment_is_part_of_trace)
{
/* ok, we have a consumed trace message in @msg and the new
* segment is not part of that. */
self->last_segment_rewound = TRUE;
self->rewound_segment_is_trace = FALSE;
self->consumed_message_is_trace = FALSE;
return MLL_REWIND_SEGMENT | MLL_EXTRACTED;
}
else
{
/* we can only be here with consumed_message_is_trace == TRUE */
g_assert_not_reached();
}
}
}
g_assert_not_reached();
}
static gint
_accumulate_line(MultiLineLogic *s,
const guchar *msg, gsize msg_len,
const guchar *segment, gsize segment_len)
{
SmartMultiLine *self = (SmartMultiLine *) s;
g_mutex_lock(&self->lock);
gint result = _accumulate_line_unlocked(s, msg, msg_len, segment, segment_len);
g_mutex_unlock(&self->lock);
return result;
}
static void
_free(MultiLineLogic *s)
{
SmartMultiLine *self = (SmartMultiLine *) s;
g_mutex_clear(&self->lock);
multi_line_logic_free_method(s);
}
MultiLineLogic *
smart_multi_line_new(void)
{
SmartMultiLine *self = g_new0(SmartMultiLine, 1);
multi_line_logic_init_instance(&self->super);
self->super.free_fn = _free;
self->super.accumulate_line = _accumulate_line;
self->last_segment_rewound = FALSE;
self->current_state = SMLS_START_STATE;
g_mutex_init(&self->lock);
return &self->super;
}
void
smart_multi_line_global_init(void)
{
const gchar *sml_file_name = get_installation_path_for("${pkgdatadir}/smart-multi-line.fsm");
_load_rules(sml_file_name);
}
void
smart_multi_line_global_deinit(void)
{
_free_rules();
}
|