| 1 |
/** |
| 2 |
* collectd - src/logparser.c |
| 3 |
* |
| 4 |
* Copyright(c) 2018 Intel Corporation. All rights reserved. |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and associated documentation files (the "Software"), to deal |
| 8 |
* in the Software without restriction, including without limitation the rights |
| 9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
* copies of the Software, and to permit persons to whom the Software is |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
* SOFTWARE. |
| 23 |
* |
| 24 |
* Authors: |
| 25 |
* Marcin Mozejko <[email protected]> |
| 26 |
**/ |
| 27 |
|
| 28 |
#include "collectd.h" |
| 29 |
|
| 30 |
#include "utils/common/common.h" |
| 31 |
#include "utils/message_parser/message_parser.h" |
| 32 |
#include "utils_llist.h" |
| 33 |
|
| 34 |
#define PLUGIN_NAME "logparser" |
| 35 |
|
| 36 |
#define LOGPARSER_SEV_OK_STR "OK" |
| 37 |
#define LOGPARSER_SEV_WARN_STR "WARNING" |
| 38 |
#define LOGPARSER_SEV_FAIL_STR "FAILURE" |
| 39 |
|
| 40 |
#define LOGPARSER_PLUGIN_INST_STR "PluginInstance" |
| 41 |
#define LOGPARSER_TYPE_STR "Type" |
| 42 |
#define LOGPARSER_TYPE_INST_STR "TypeInstance" |
| 43 |
#define LOGPARSER_SEVERITY_STR "Severity" |
| 44 |
|
| 45 |
#define MAX_STR_LEN 128 |
| 46 |
#define MAX_FIELDS 4 /* PluginInstance, Type, TypeInstance, Severity */ |
| 47 |
|
| 48 |
#define START_IDX 0 |
| 49 |
#define STOP_IDX (parser->patterns_len - 1) |
| 50 |
|
| 51 |
typedef enum message_item_type_e { |
| 52 |
MSG_ITEM_PLUGIN_INST = 0, |
| 53 |
MSG_ITEM_TYPE, |
| 54 |
MSG_ITEM_TYPE_INST, |
| 55 |
MSG_ITEM_SEVERITY |
| 56 |
} message_item_type_t; |
| 57 |
|
| 58 |
typedef struct message_item_info_s { |
| 59 |
/* Type of message item used for special processing */ |
| 60 |
message_item_type_t type; |
| 61 |
union { |
| 62 |
/* If set, will override message item string with this one */ |
| 63 |
char *str_override; |
| 64 |
/* Used only if type is MSG_ITEM_SEVERITY */ |
| 65 |
int severity; |
| 66 |
} val; |
| 67 |
} message_item_info_t; |
| 68 |
|
| 69 |
typedef struct message_item_user_data_s { |
| 70 |
/* Information telling what to do when match found */ |
| 71 |
message_item_info_t infos[MAX_FIELDS]; |
| 72 |
size_t infos_len; |
| 73 |
} message_item_user_data_t; |
| 74 |
|
| 75 |
typedef struct log_parser_s { |
| 76 |
char *name; |
| 77 |
parser_job_data_t *job; |
| 78 |
message_pattern_t *patterns; |
| 79 |
size_t patterns_len; |
| 80 |
bool first_read; |
| 81 |
char *filename; |
| 82 |
char *def_plugin_inst; |
| 83 |
char *def_type; |
| 84 |
char *def_type_inst; |
| 85 |
int def_severity; |
| 86 |
} log_parser_t; |
| 87 |
|
| 88 |
typedef struct logparser_ctx_s { |
| 89 |
log_parser_t *parsers; |
| 90 |
size_t parsers_len; |
| 91 |
} logparser_ctx_t; |
| 92 |
|
| 93 |
static logparser_ctx_t logparser_ctx; |
| 94 |
|
| 95 |
static int logparser_shutdown(void); |
| 96 |
|
| 97 |
static void logparser_free_user_data(void *data) { |
| 98 |
message_item_user_data_t *user_data = (message_item_user_data_t *)data; |
| 99 |
|
| 100 |
if (user_data == NULL) |
| 101 |
return; |
| 102 |
|
| 103 |
for (size_t i = 0; i < user_data->infos_len; i++) { |
| 104 |
if (user_data->infos[i].type != MSG_ITEM_SEVERITY) |
| 105 |
sfree(user_data->infos[i].val.str_override); |
| 106 |
} |
| 107 |
sfree(user_data); |
| 108 |
} |
| 109 |
|
| 110 |
static int logparser_config_msg_item_type(oconfig_item_t *ci, |
| 111 |
message_item_user_data_t **user_data, |
| 112 |
message_item_type_t type) { |
| 113 |
bool val; |
| 114 |
int ret; |
| 115 |
char *str = NULL; |
| 116 |
|
| 117 |
if (*user_data == NULL) { |
| 118 |
*user_data = calloc(1, sizeof(**user_data)); |
| 119 |
if (*user_data == NULL) { |
| 120 |
ERROR(PLUGIN_NAME ": Could not allocate memory"); |
| 121 |
return -1; |
| 122 |
} |
| 123 |
(*user_data)->infos_len = 0; |
| 124 |
} |
| 125 |
|
| 126 |
size_t i = (*user_data)->infos_len; |
| 127 |
|
| 128 |
switch (ci->values[0].type) { |
| 129 |
case OCONFIG_TYPE_STRING: |
| 130 |
ret = cf_util_get_string(ci, &str); |
| 131 |
break; |
| 132 |
case OCONFIG_TYPE_BOOLEAN: |
| 133 |
ret = cf_util_get_boolean(ci, &val); |
| 134 |
if (val == false || type == MSG_ITEM_SEVERITY) |
| 135 |
goto wrong_value; |
| 136 |
break; |
| 137 |
default: |
| 138 |
ERROR(PLUGIN_NAME ": Wrong type for option %s", ci->key); |
| 139 |
goto error; |
| 140 |
} |
| 141 |
|
| 142 |
if (ret != 0) { |
| 143 |
ERROR(PLUGIN_NAME ": Error getting %s option", ci->key); |
| 144 |
goto error; |
| 145 |
} |
| 146 |
|
| 147 |
if (type == MSG_ITEM_SEVERITY) { |
| 148 |
if (strcasecmp(LOGPARSER_SEV_OK_STR, str) == 0) |
| 149 |
(*user_data)->infos[i].val.severity = NOTIF_OKAY; |
| 150 |
else if (strcasecmp(LOGPARSER_SEV_WARN_STR, str) == 0) |
| 151 |
(*user_data)->infos[i].val.severity = NOTIF_WARNING; |
| 152 |
else if (strcasecmp(LOGPARSER_SEV_FAIL_STR, str) == 0) |
| 153 |
(*user_data)->infos[i].val.severity = NOTIF_FAILURE; |
| 154 |
else { |
| 155 |
sfree(str); |
| 156 |
goto wrong_value; |
| 157 |
} |
| 158 |
sfree(str); |
| 159 |
} else |
| 160 |
(*user_data)->infos[i].val.str_override = str; |
| 161 |
|
| 162 |
(*user_data)->infos[i].type = type; |
| 163 |
(*user_data)->infos_len++; |
| 164 |
return 0; |
| 165 |
|
| 166 |
wrong_value: |
| 167 |
ERROR(PLUGIN_NAME ": Wrong value for option %s", ci->key); |
| 168 |
error: |
| 169 |
sfree(*user_data); |
| 170 |
return -1; |
| 171 |
} |
| 172 |
|
| 173 |
static int logparser_config_match(oconfig_item_t *ci, log_parser_t *parser) { |
| 174 |
int ret; |
| 175 |
message_item_user_data_t *user_data = NULL; |
| 176 |
message_pattern_t *ptr = NULL; |
| 177 |
|
| 178 |
ptr = realloc(parser->patterns, sizeof(*ptr) * (parser->patterns_len + 1)); |
| 179 |
if (ptr == NULL) { |
| 180 |
ERROR(PLUGIN_NAME ": Error reallocating memory for message patterns."); |
| 181 |
return -1; |
| 182 |
} |
| 183 |
|
| 184 |
message_pattern_t *pattern = ptr + parser->patterns_len; |
| 185 |
|
| 186 |
memset(pattern, 0, sizeof(*pattern)); |
| 187 |
pattern->is_mandatory = 1; |
| 188 |
ret = cf_util_get_string(ci, &pattern->name); |
| 189 |
|
| 190 |
if (ret != 0) { |
| 191 |
ERROR(PLUGIN_NAME ": Error getting match name"); |
| 192 |
goto free_ptr; |
| 193 |
} |
| 194 |
|
| 195 |
for (int i = 0; i < ci->children_num; i++) { |
| 196 |
oconfig_item_t *child = ci->children + i; |
| 197 |
|
| 198 |
if (strcasecmp("Regex", child->key) == 0) |
| 199 |
ret = cf_util_get_string(child, &pattern->regex); |
| 200 |
else if (strcasecmp("SubmatchIdx", child->key) == 0) |
| 201 |
ret = cf_util_get_int(child, &pattern->submatch_idx); |
| 202 |
else if (strcasecmp("ExcludeRegex", child->key) == 0) |
| 203 |
ret = cf_util_get_string(child, &pattern->excluderegex); |
| 204 |
else if (strcasecmp("IsMandatory", child->key) == 0) |
| 205 |
ret = cf_util_get_boolean(child, &pattern->is_mandatory); |
| 206 |
else if (strcasecmp(LOGPARSER_PLUGIN_INST_STR, child->key) == 0) |
| 207 |
ret = logparser_config_msg_item_type(child, &user_data, |
| 208 |
MSG_ITEM_PLUGIN_INST); |
| 209 |
else if (strcasecmp(LOGPARSER_TYPE_STR, child->key) == 0) |
| 210 |
ret = logparser_config_msg_item_type(child, &user_data, MSG_ITEM_TYPE); |
| 211 |
else if (strcasecmp(LOGPARSER_TYPE_INST_STR, child->key) == 0) |
| 212 |
ret = |
| 213 |
logparser_config_msg_item_type(child, &user_data, MSG_ITEM_TYPE_INST); |
| 214 |
else if (strcasecmp(LOGPARSER_SEVERITY_STR, child->key) == 0) |
| 215 |
ret = |
| 216 |
logparser_config_msg_item_type(child, &user_data, MSG_ITEM_SEVERITY); |
| 217 |
else { |
| 218 |
ERROR(PLUGIN_NAME ": Invalid configuration option \"%s\".", child->key); |
| 219 |
goto free_user_data; |
| 220 |
} |
| 221 |
if (ret != 0) { |
| 222 |
ERROR(PLUGIN_NAME ": Error getting %s option", child->key); |
| 223 |
goto free_user_data; |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
if (user_data != NULL) { |
| 228 |
pattern->user_data = (void *)user_data; |
| 229 |
pattern->free_user_data = logparser_free_user_data; |
| 230 |
} |
| 231 |
|
| 232 |
parser->patterns = ptr; |
| 233 |
parser->patterns_len++; |
| 234 |
|
| 235 |
return 0; |
| 236 |
|
| 237 |
free_user_data: |
| 238 |
if (user_data != NULL) |
| 239 |
logparser_free_user_data(user_data); |
| 240 |
free_ptr: |
| 241 |
sfree(ptr); |
| 242 |
return -1; |
| 243 |
} |
| 244 |
|
| 245 |
static int logparser_config_message(const oconfig_item_t *ci, char *filename, |
| 246 |
bool first_read) { |
| 247 |
char *msg_name = NULL; |
| 248 |
char *severity = NULL; |
| 249 |
int ret; |
| 250 |
log_parser_t *ptr; |
| 251 |
|
| 252 |
ret = cf_util_get_string(ci, &msg_name); |
| 253 |
if (ret != 0) { |
| 254 |
ERROR(PLUGIN_NAME ": Error getting message name"); |
| 255 |
return -1; |
| 256 |
} |
| 257 |
|
| 258 |
ptr = realloc(logparser_ctx.parsers, |
| 259 |
sizeof(*ptr) * (logparser_ctx.parsers_len + 1)); |
| 260 |
|
| 261 |
if (ptr == NULL) { |
| 262 |
ERROR(PLUGIN_NAME ": Error reallocating memory for message parsers."); |
| 263 |
goto error; |
| 264 |
} |
| 265 |
|
| 266 |
logparser_ctx.parsers = ptr; |
| 267 |
|
| 268 |
log_parser_t *parser = ptr + logparser_ctx.parsers_len; |
| 269 |
|
| 270 |
memset(parser, 0, sizeof(*parser)); |
| 271 |
parser->name = msg_name; |
| 272 |
parser->first_read = first_read; |
| 273 |
parser->filename = filename; |
| 274 |
parser->def_severity = NOTIF_OKAY; |
| 275 |
|
| 276 |
for (int i = 0; i < ci->children_num; i++) { |
| 277 |
oconfig_item_t *child = ci->children + i; |
| 278 |
if (strcasecmp("Match", child->key) == 0) |
| 279 |
ret = logparser_config_match(child, parser); |
| 280 |
else if (strcasecmp("DefaultPluginInstance", child->key) == 0) |
| 281 |
ret = cf_util_get_string(child, &parser->def_plugin_inst); |
| 282 |
else if (strcasecmp("DefaultType", child->key) == 0) |
| 283 |
ret = cf_util_get_string(child, &parser->def_type); |
| 284 |
else if (strcasecmp("DefaultTypeInstance", child->key) == 0) |
| 285 |
ret = cf_util_get_string(child, &parser->def_type_inst); |
| 286 |
else if (strcasecmp("DefaultSeverity", child->key) == 0) { |
| 287 |
ret = cf_util_get_string(child, &severity); |
| 288 |
if (strcasecmp(LOGPARSER_SEV_OK_STR, severity) == 0) |
| 289 |
parser->def_severity = NOTIF_OKAY; |
| 290 |
else if (strcasecmp(LOGPARSER_SEV_WARN_STR, severity) == 0) |
| 291 |
parser->def_severity = NOTIF_WARNING; |
| 292 |
else if (strcasecmp(LOGPARSER_SEV_FAIL_STR, severity) == 0) |
| 293 |
parser->def_severity = NOTIF_FAILURE; |
| 294 |
else { |
| 295 |
ERROR(PLUGIN_NAME ": Invalid severity value: \"%s\".", severity); |
| 296 |
sfree(severity); |
| 297 |
goto error; |
| 298 |
} |
| 299 |
sfree(severity); |
| 300 |
} else { |
| 301 |
ERROR(PLUGIN_NAME ": Invalid configuration option \"%s\".", child->key); |
| 302 |
goto error; |
| 303 |
} |
| 304 |
if (ret != 0) { |
| 305 |
ERROR(PLUGIN_NAME ": Error getting %s option", child->key); |
| 306 |
goto error; |
| 307 |
} |
| 308 |
} |
| 309 |
logparser_ctx.parsers_len++; |
| 310 |
|
| 311 |
return 0; |
| 312 |
|
| 313 |
error: |
| 314 |
sfree(msg_name); |
| 315 |
return -1; |
| 316 |
} |
| 317 |
|
| 318 |
static int logparser_config_logfile(oconfig_item_t *ci) { |
| 319 |
char *filename = NULL; |
| 320 |
bool first_read = false; // First full read |
| 321 |
int ret = 0; |
| 322 |
|
| 323 |
ret = cf_util_get_string(ci, &filename); |
| 324 |
if (ret != 0) { |
| 325 |
ERROR(PLUGIN_NAME ": Error getting filename"); |
| 326 |
return -1; |
| 327 |
} |
| 328 |
|
| 329 |
for (int i = 0; i < ci->children_num; i++) { |
| 330 |
oconfig_item_t *child = ci->children + i; |
| 331 |
if (strcasecmp("FirstFullRead", child->key) == 0) |
| 332 |
ret = cf_util_get_boolean(child, &first_read); |
| 333 |
else if (strcasecmp("Message", child->key) == 0) |
| 334 |
ret = logparser_config_message(child, filename, first_read); |
| 335 |
else { |
| 336 |
ERROR(PLUGIN_NAME ": Invalid configuration option \"%s\".", child->key); |
| 337 |
goto error; |
| 338 |
} |
| 339 |
|
| 340 |
if (ret != 0) { |
| 341 |
ERROR(PLUGIN_NAME ": Error getting %s option", child->key); |
| 342 |
goto error; |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
return 0; |
| 347 |
|
| 348 |
error: |
| 349 |
sfree(filename); |
| 350 |
return -1; |
| 351 |
} |
| 352 |
|
| 353 |
static int logparser_validate_config(void) { |
| 354 |
for (size_t i = 0; i < logparser_ctx.parsers_len; i++) { |
| 355 |
log_parser_t *parser = logparser_ctx.parsers + i; |
| 356 |
|
| 357 |
if (strlen(parser->filename) < 1) { |
| 358 |
ERROR(PLUGIN_NAME ": Log filename in \"%s\" message can't be empty", |
| 359 |
parser->name); |
| 360 |
return -1; |
| 361 |
} |
| 362 |
|
| 363 |
if (parser->def_plugin_inst != NULL && |
| 364 |
strlen(parser->def_plugin_inst) < 1) { |
| 365 |
ERROR(PLUGIN_NAME |
| 366 |
": DefaultPluginInstance in \"%s\" message can't be empty", |
| 367 |
parser->name); |
| 368 |
return -1; |
| 369 |
} |
| 370 |
|
| 371 |
if (parser->def_type != NULL && strlen(parser->def_type) < 1) { |
| 372 |
ERROR(PLUGIN_NAME ": DefaultType in \"%s\" message can't be empty", |
| 373 |
parser->name); |
| 374 |
return -1; |
| 375 |
} |
| 376 |
|
| 377 |
if (parser->def_type_inst != NULL && strlen(parser->def_type_inst) < 1) { |
| 378 |
ERROR(PLUGIN_NAME |
| 379 |
": DefaultTypeInstance in \"%s\" message can't be empty", |
| 380 |
parser->name); |
| 381 |
return -1; |
| 382 |
} |
| 383 |
|
| 384 |
if (parser->patterns_len < 2) { |
| 385 |
ERROR(PLUGIN_NAME ": Message \"%s\" should have at least 2 matches", |
| 386 |
parser->name); |
| 387 |
return -1; |
| 388 |
} |
| 389 |
|
| 390 |
if (parser->patterns[START_IDX].is_mandatory != 1) { |
| 391 |
ERROR(PLUGIN_NAME |
| 392 |
": Start match \"%s\" in message \"%s\" can't be optional", |
| 393 |
parser->patterns[START_IDX].name, parser->name); |
| 394 |
return -1; |
| 395 |
} |
| 396 |
|
| 397 |
if (parser->patterns[STOP_IDX].is_mandatory != 1) { |
| 398 |
ERROR(PLUGIN_NAME |
| 399 |
": Stop match \"%s\" in message \"%s\" can't be optional", |
| 400 |
parser->patterns[STOP_IDX].name, parser->name); |
| 401 |
return -1; |
| 402 |
} |
| 403 |
|
| 404 |
for (int j = 0; j < parser->patterns_len; j++) { |
| 405 |
message_pattern_t *pattern = parser->patterns + j; |
| 406 |
|
| 407 |
if (pattern->regex == NULL) { |
| 408 |
ERROR(PLUGIN_NAME |
| 409 |
": Regex must be set (message: \"%s\", match: \"%s\")", |
| 410 |
parser->name, pattern->name); |
| 411 |
return -1; |
| 412 |
} else if (strlen(pattern->regex) < 1) { |
| 413 |
ERROR(PLUGIN_NAME |
| 414 |
": Regex can't be empty (message: \"%s\", match: \"%s\")", |
| 415 |
parser->name, pattern->name); |
| 416 |
return -1; |
| 417 |
} |
| 418 |
|
| 419 |
if (pattern->excluderegex != NULL && strlen(pattern->excluderegex) < 1) { |
| 420 |
ERROR(PLUGIN_NAME |
| 421 |
": ExcludeRegex can't be empty (message: \"%s\", match: \"%s\")", |
| 422 |
parser->name, pattern->name); |
| 423 |
return -1; |
| 424 |
} |
| 425 |
|
| 426 |
if (pattern->submatch_idx < -1) { |
| 427 |
ERROR(PLUGIN_NAME ": SubmatchIdx must be in range [-1..n]"); |
| 428 |
return -1; |
| 429 |
} |
| 430 |
|
| 431 |
if (pattern->user_data != NULL && pattern->submatch_idx == -1) |
| 432 |
WARNING(PLUGIN_NAME ": Options [PluginInstance, Type, TypeInstance, " |
| 433 |
"Severity] are omitted when SubmatchIdx is set to " |
| 434 |
"-1 (message: \"%s\", match: \"%s\")", |
| 435 |
parser->name, pattern->name); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
return 0; |
| 440 |
} |
| 441 |
|
| 442 |
static int logparser_config(oconfig_item_t *ci) { |
| 443 |
int ret; |
| 444 |
|
| 445 |
for (int i = 0; i < ci->children_num; i++) { |
| 446 |
oconfig_item_t *child = ci->children + i; |
| 447 |
|
| 448 |
if (strcasecmp("Logfile", child->key) == 0) { |
| 449 |
ret = logparser_config_logfile(child); |
| 450 |
|
| 451 |
if (ret != 0) { |
| 452 |
return -1; |
| 453 |
} |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
return logparser_validate_config(); |
| 458 |
} |
| 459 |
|
| 460 |
#if COLLECT_DEBUG |
| 461 |
static void logparser_print_config(void) { |
| 462 |
const char *severity_desc[5] = {NULL, LOGPARSER_SEV_FAIL_STR, |
| 463 |
LOGPARSER_SEV_WARN_STR, NULL, |
| 464 |
LOGPARSER_SEV_OK_STR}; |
| 465 |
const char *item_type_desc[MAX_FIELDS] = { |
| 466 |
LOGPARSER_PLUGIN_INST_STR, LOGPARSER_TYPE_STR, LOGPARSER_TYPE_INST_STR, |
| 467 |
LOGPARSER_SEVERITY_STR}; |
| 468 |
const char *bool_str[2] = {"False", "True"}; |
| 469 |
|
| 470 |
DEBUG(PLUGIN_NAME ": ==========LOGPARSER CONFIG============="); |
| 471 |
DEBUG(PLUGIN_NAME ": Message configs count: %zu", logparser_ctx.parsers_len); |
| 472 |
|
| 473 |
for (size_t i = 0; i < logparser_ctx.parsers_len; i++) { |
| 474 |
log_parser_t *parser = logparser_ctx.parsers + i; |
| 475 |
DEBUG(PLUGIN_NAME ": Message: \"%s\"", parser->name); |
| 476 |
DEBUG(PLUGIN_NAME ": File: \"%s\"", parser->filename); |
| 477 |
if (parser->def_plugin_inst != NULL) |
| 478 |
DEBUG(PLUGIN_NAME ": DefaultPluginInstance: \"%s\"", |
| 479 |
parser->def_plugin_inst); |
| 480 |
if (parser->def_type != NULL) |
| 481 |
DEBUG(PLUGIN_NAME ": DefaultType: \"%s\"", parser->def_type); |
| 482 |
if (parser->def_type_inst != NULL) |
| 483 |
DEBUG(PLUGIN_NAME ": DefaultTypeInstance: \"%s\"", |
| 484 |
parser->def_type_inst); |
| 485 |
DEBUG(PLUGIN_NAME ": DefaultSeverity: %s", |
| 486 |
severity_desc[parser->def_severity]); |
| 487 |
DEBUG(PLUGIN_NAME ": Match configs count: %zu", parser->patterns_len); |
| 488 |
|
| 489 |
for (size_t j = 0; j < parser->patterns_len; j++) { |
| 490 |
message_pattern_t *pattern = parser->patterns + j; |
| 491 |
DEBUG(PLUGIN_NAME ": Match: \"%s\"", pattern->name); |
| 492 |
DEBUG(PLUGIN_NAME ": Regex: \"%s\"", pattern->regex); |
| 493 |
if (pattern->excluderegex != NULL) |
| 494 |
DEBUG(PLUGIN_NAME ": ExcludeRegex: \"%s\"", pattern->excluderegex); |
| 495 |
DEBUG(PLUGIN_NAME ": SubmatchIdx: %d", pattern->submatch_idx); |
| 496 |
DEBUG(PLUGIN_NAME ": IsMandatory: %s", |
| 497 |
bool_str[pattern->is_mandatory]); |
| 498 |
if (pattern->user_data != NULL) { |
| 499 |
message_item_user_data_t *ud = |
| 500 |
(message_item_user_data_t *)pattern->user_data; |
| 501 |
for (size_t k = 0; k < ud->infos_len; k++) { |
| 502 |
if (ud->infos[k].type == MSG_ITEM_SEVERITY) |
| 503 |
DEBUG(PLUGIN_NAME ": Severity: %s", |
| 504 |
severity_desc[ud->infos[k].val.severity]); |
| 505 |
else { |
| 506 |
if (ud->infos[k].val.str_override != NULL) |
| 507 |
DEBUG(PLUGIN_NAME ": %s: \"%s\"", |
| 508 |
item_type_desc[ud->infos[k].type], |
| 509 |
ud->infos[k].val.str_override); |
| 510 |
else |
| 511 |
DEBUG(PLUGIN_NAME ": %s: %s", |
| 512 |
item_type_desc[ud->infos[k].type], bool_str[1]); |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
} |
| 518 |
DEBUG(PLUGIN_NAME ": ======================================="); |
| 519 |
} |
| 520 |
#endif |
| 521 |
|
| 522 |
static int logparser_init(void) { |
| 523 |
#if COLLECT_DEBUG |
| 524 |
logparser_print_config(); |
| 525 |
#endif |
| 526 |
|
| 527 |
for (size_t i = 0; i < logparser_ctx.parsers_len; i++) { |
| 528 |
log_parser_t *parser = logparser_ctx.parsers + i; |
| 529 |
parser->job = message_parser_init(parser->filename, START_IDX, STOP_IDX, |
| 530 |
parser->patterns, parser->patterns_len); |
| 531 |
if (parser->job == NULL) { |
| 532 |
ERROR(PLUGIN_NAME ": Failed to initialize %s parser.", |
| 533 |
logparser_ctx.parsers[i].name); |
| 534 |
logparser_shutdown(); |
| 535 |
return -1; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
return 0; |
| 540 |
} |
| 541 |
|
| 542 |
static void logparser_dispatch_notification(notification_t *n) { |
| 543 |
sstrncpy(n->host, hostname_g, sizeof(n->host)); |
| 544 |
plugin_dispatch_notification(n); |
| 545 |
if (n->meta != NULL) |
| 546 |
plugin_notification_meta_free(n->meta); |
| 547 |
} |
| 548 |
|
| 549 |
static void logparser_process_msg(log_parser_t *parser, message_t *msg, |
| 550 |
unsigned int max_items) { |
| 551 |
notification_t n = {.severity = parser->def_severity, |
| 552 |
.time = cdtime(), |
| 553 |
.plugin = PLUGIN_NAME, |
| 554 |
.meta = NULL}; |
| 555 |
|
| 556 |
/* Writing default values if set */ |
| 557 |
if (parser->def_plugin_inst != NULL) |
| 558 |
sstrncpy(n.plugin_instance, parser->def_plugin_inst, |
| 559 |
sizeof(n.plugin_instance)); |
| 560 |
if (parser->def_type != NULL) |
| 561 |
sstrncpy(n.type, parser->def_type, sizeof(n.type)); |
| 562 |
if (parser->def_type_inst != NULL) |
| 563 |
sstrncpy(n.type_instance, parser->def_type_inst, sizeof(n.type_instance)); |
| 564 |
|
| 565 |
for (int i = 0; i < max_items; i++) { |
| 566 |
message_item_t *item = msg->message_items + i; |
| 567 |
if (!item->value[0]) |
| 568 |
break; |
| 569 |
|
| 570 |
DEBUG(PLUGIN_NAME ": [%02d] %s:%s", i, item->name, item->value); |
| 571 |
|
| 572 |
if (item->user_data != NULL) { |
| 573 |
message_item_user_data_t *user_data = |
| 574 |
(message_item_user_data_t *)item->user_data; |
| 575 |
|
| 576 |
for (size_t i = 0; i < user_data->infos_len; i++) { |
| 577 |
char *ptr = NULL; |
| 578 |
size_t size = 0; |
| 579 |
switch (user_data->infos[i].type) { |
| 580 |
case MSG_ITEM_SEVERITY: |
| 581 |
n.severity = user_data->infos[i].val.severity; |
| 582 |
break; |
| 583 |
case MSG_ITEM_PLUGIN_INST: |
| 584 |
ptr = n.plugin_instance; |
| 585 |
size = sizeof(n.plugin_instance); |
| 586 |
break; |
| 587 |
case MSG_ITEM_TYPE: |
| 588 |
ptr = n.type; |
| 589 |
size = sizeof(n.type); |
| 590 |
break; |
| 591 |
case MSG_ITEM_TYPE_INST: |
| 592 |
ptr = n.type_instance; |
| 593 |
size = sizeof(n.type_instance); |
| 594 |
break; |
| 595 |
default: |
| 596 |
ERROR(PLUGIN_NAME ": Message item has wrong type!"); |
| 597 |
return; |
| 598 |
} |
| 599 |
|
| 600 |
if (ptr != NULL && size > 0) { |
| 601 |
if (user_data->infos[i].val.str_override != NULL) |
| 602 |
sstrncpy(ptr, user_data->infos[i].val.str_override, size); |
| 603 |
else |
| 604 |
sstrncpy(ptr, item->value, size); |
| 605 |
} |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
if (plugin_notification_meta_add_string(&n, item->name, item->value)) |
| 610 |
ERROR(PLUGIN_NAME ": Failed to add notification meta data %s:%s", |
| 611 |
item->name, item->value); |
| 612 |
} |
| 613 |
|
| 614 |
logparser_dispatch_notification(&n); |
| 615 |
} |
| 616 |
|
| 617 |
static int logparser_parser_read(log_parser_t *parser) { |
| 618 |
message_t *messages_storage; |
| 619 |
unsigned int max_item_num; |
| 620 |
int msg_num = |
| 621 |
message_parser_read(parser->job, &messages_storage, parser->first_read); |
| 622 |
|
| 623 |
if (msg_num < 0) { |
| 624 |
notification_t n = {.severity = NOTIF_FAILURE, |
| 625 |
.time = cdtime(), |
| 626 |
.message = "Failed to read from log file", |
| 627 |
.plugin = PLUGIN_NAME, |
| 628 |
.meta = NULL}; |
| 629 |
logparser_dispatch_notification(&n); |
| 630 |
return -1; |
| 631 |
} |
| 632 |
|
| 633 |
max_item_num = STATIC_ARRAY_SIZE(messages_storage[0].message_items); |
| 634 |
|
| 635 |
DEBUG(PLUGIN_NAME ": read %d messages, %s", msg_num, parser->name); |
| 636 |
|
| 637 |
for (int i = 0; i < msg_num; i++) { |
| 638 |
message_t *msg = messages_storage + i; |
| 639 |
logparser_process_msg(parser, msg, max_item_num); |
| 640 |
} |
| 641 |
return 0; |
| 642 |
} |
| 643 |
|
| 644 |
static int logparser_read(__attribute__((unused)) user_data_t *ud) { |
| 645 |
int ret = 0; |
| 646 |
|
| 647 |
for (size_t i = 0; i < logparser_ctx.parsers_len; i++) { |
| 648 |
log_parser_t *parser = logparser_ctx.parsers + i; |
| 649 |
ret = logparser_parser_read(parser); |
| 650 |
if (parser->first_read) |
| 651 |
parser->first_read = false; |
| 652 |
|
| 653 |
if (ret < 0) { |
| 654 |
ERROR(PLUGIN_NAME ": Failed to parse %s messages from %s", parser->name, |
| 655 |
parser->filename); |
| 656 |
break; |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
return ret; |
| 661 |
} |
| 662 |
|
| 663 |
static int logparser_shutdown(void) { |
| 664 |
if (logparser_ctx.parsers == NULL) |
| 665 |
return 0; |
| 666 |
|
| 667 |
for (size_t i = 0; i < logparser_ctx.parsers_len; i++) { |
| 668 |
log_parser_t *parser = logparser_ctx.parsers + i; |
| 669 |
|
| 670 |
if (parser->job != NULL) |
| 671 |
message_parser_cleanup(parser->job); |
| 672 |
|
| 673 |
for (size_t j = 0; j < parser->patterns_len; j++) { |
| 674 |
if (parser->patterns[j].free_user_data != NULL) |
| 675 |
parser->patterns[j].free_user_data(parser->patterns[j].user_data); |
| 676 |
|
| 677 |
sfree(parser->patterns[j].name); |
| 678 |
sfree(parser->patterns[j].regex); |
| 679 |
sfree(parser->patterns[j].excluderegex); |
| 680 |
} |
| 681 |
|
| 682 |
sfree(parser->patterns); |
| 683 |
sfree(parser->filename); |
| 684 |
sfree(parser->def_plugin_inst); |
| 685 |
sfree(parser->def_type); |
| 686 |
sfree(parser->def_type_inst); |
| 687 |
sfree(parser->name); |
| 688 |
} |
| 689 |
|
| 690 |
sfree(logparser_ctx.parsers); |
| 691 |
|
| 692 |
return 0; |
| 693 |
} |
| 694 |
|
| 695 |
void module_register(void) { |
| 696 |
plugin_register_complex_config(PLUGIN_NAME, logparser_config); |
| 697 |
plugin_register_init(PLUGIN_NAME, logparser_init); |
| 698 |
plugin_register_complex_read(NULL, PLUGIN_NAME, logparser_read, 0, NULL); |
| 699 |
plugin_register_shutdown(PLUGIN_NAME, logparser_shutdown); |
| 700 |
} |