diff options
Diffstat (limited to 'utility/logparse.c')
-rw-r--r-- | utility/logparse.c | 61 |
1 files changed, 37 insertions, 24 deletions
diff --git a/utility/logparse.c b/utility/logparse.c index 4b5ab40..f4afb52 100644 --- a/utility/logparse.c +++ b/utility/logparse.c | |||
@@ -11,7 +11,6 @@ | |||
11 | #include "ap_pcre.h" | 11 | #include "ap_pcre.h" |
12 | #include "database.h" | 12 | #include "database.h" |
13 | 13 | ||
14 | |||
15 | apr_hash_t *g_parser_funcs; | 14 | apr_hash_t *g_parser_funcs; |
16 | 15 | ||
17 | static apr_status_t parser_func_regexmatch(apr_pool_t *p, config_t *cfg, | 16 | static apr_status_t parser_func_regexmatch(apr_pool_t *p, config_t *cfg, |
@@ -20,23 +19,26 @@ static apr_status_t parser_func_regexmatch(apr_pool_t *p, config_t *cfg, | |||
20 | struct { | 19 | struct { |
21 | ap_regex_t *rx; | 20 | ap_regex_t *rx; |
22 | const char *substr; | 21 | const char *substr; |
23 | } *data; | 22 | }*data; |
24 | ap_regmatch_t regm[AP_MAX_REG_MATCH]; | 23 | ap_regmatch_t regm[AP_MAX_REG_MATCH]; |
25 | // Check if a regular expression configured | 24 | // Check if a regular expression configured |
26 | if (!field->args[0]) return APR_EINVAL; | 25 | if (!field->args[0]) |
26 | return APR_EINVAL; | ||
27 | if (!field->data) { | 27 | if (!field->data) { |
28 | // pre compile the regex | 28 | // pre compile the regex |
29 | data = apr_palloc(cfg->pool, sizeof(ap_regex_t)+sizeof(const char *)); | 29 | data = apr_palloc(cfg->pool, sizeof(ap_regex_t)+sizeof(const char *)); |
30 | data->rx = ap_pregcomp(cfg->pool, field->args[0], | 30 | data->rx = ap_pregcomp(cfg->pool, field->args[0], |
31 | AP_REG_EXTENDED|AP_REG_ICASE); | 31 | AP_REG_EXTENDED|AP_REG_ICASE); |
32 | if (field->args[1]) { | 32 | if (field->args[1]) { |
33 | data->substr = field->args[1]; | 33 | data->substr = field->args[1]; |
34 | } else { | 34 | } else { |
35 | data->substr = "$1"; | 35 | data->substr = "$1"; |
36 | } | 36 | } |
37 | if (!data->rx) return APR_EINVAL; | 37 | if (!data->rx) |
38 | return APR_EINVAL; | ||
38 | field->data = data; | 39 | field->data = data; |
39 | } else data = field->data; | 40 | } else |
41 | data = field->data; | ||
40 | 42 | ||
41 | if (!ap_regexec(data->rx, value, AP_MAX_REG_MATCH, regm, 0)) { | 43 | if (!ap_regexec(data->rx, value, AP_MAX_REG_MATCH, regm, 0)) { |
42 | *ret = ap_pregsub(p, data->substr, value, AP_MAX_REG_MATCH, regm); | 44 | *ret = ap_pregsub(p, data->substr, value, AP_MAX_REG_MATCH, regm); |
@@ -66,7 +68,7 @@ static apr_status_t parser_func_machineid(apr_pool_t *p, config_t *cfg, | |||
66 | config_output_field_t *field, const char *value, const char **ret) | 68 | config_output_field_t *field, const char *value, const char **ret) |
67 | { | 69 | { |
68 | if (cfg->machineid) { | 70 | if (cfg->machineid) { |
69 | *ret = apr_pstrdup(p,cfg->machineid); | 71 | *ret = apr_pstrdup(p, cfg->machineid); |
70 | } else { | 72 | } else { |
71 | *ret = field->def; | 73 | *ret = field->def; |
72 | } | 74 | } |
@@ -81,7 +83,7 @@ parser_func_t parser_get_func(const char *name) | |||
81 | } | 83 | } |
82 | 84 | ||
83 | static void parser_add_func(apr_pool_t *p, const char *const name, | 85 | static void parser_add_func(apr_pool_t *p, const char *const name, |
84 | parser_func_t func) | 86 | parser_func_t func) |
85 | { | 87 | { |
86 | if (!g_parser_funcs) { | 88 | if (!g_parser_funcs) { |
87 | g_parser_funcs = apr_hash_make(p); | 89 | g_parser_funcs = apr_hash_make(p); |
@@ -103,6 +105,7 @@ void parser_find_logs(config_t *cfg) | |||
103 | apr_finfo_t finfo; | 105 | apr_finfo_t finfo; |
104 | char **newp; | 106 | char **newp; |
105 | 107 | ||
108 | logging_log(cfg, LOGLEVEL_NOTICE, "Find Log files"); | ||
106 | if (!cfg->input_dir) | 109 | if (!cfg->input_dir) |
107 | return; | 110 | return; |
108 | apr_pool_create(&tp, cfg->pool); | 111 | apr_pool_create(&tp, cfg->pool); |
@@ -113,7 +116,7 @@ void parser_find_logs(config_t *cfg) | |||
113 | continue; | 116 | continue; |
114 | newp = (char **)apr_array_push(cfg->input_files); | 117 | newp = (char **)apr_array_push(cfg->input_files); |
115 | apr_filepath_merge(newp, cfg->input_dir, finfo.name, | 118 | apr_filepath_merge(newp, cfg->input_dir, finfo.name, |
116 | APR_FILEPATH_TRUENAME, cfg->pool); | 119 | APR_FILEPATH_TRUENAME, cfg->pool); |
117 | } | 120 | } |
118 | apr_dir_close(dir); | 121 | apr_dir_close(dir); |
119 | } | 122 | } |
@@ -261,11 +264,12 @@ apr_status_t parse_logfile(config_t *cfg, const char *filename) | |||
261 | apr_pool_create(&tp, cfg->pool); | 264 | apr_pool_create(&tp, cfg->pool); |
262 | apr_pool_create(&targp, tp); | 265 | apr_pool_create(&targp, tp); |
263 | 266 | ||
267 | logging_log(cfg, LOGLEVEL_NOTICE, "Begin Parsing Log File '%s'", filename); | ||
268 | |||
264 | rv = apr_file_open(&file, filename, APR_FOPEN_READ | APR_BUFFERED, | 269 | rv = apr_file_open(&file, filename, APR_FOPEN_READ | APR_BUFFERED, |
265 | APR_OS_DEFAULT, tp); | 270 | APR_OS_DEFAULT, tp); |
266 | if (rv != APR_SUCCESS) { | 271 | if (rv != APR_SUCCESS) { |
267 | logging_log(cfg, LOGLEVEL_ERROR, "Could not open %s",filename); | 272 | logging_log(cfg, LOGLEVEL_NOISE, "Could not open %s", filename); |
268 | printf("Could not open %s\n", filename); | ||
269 | return rv; | 273 | return rv; |
270 | } | 274 | } |
271 | 275 | ||
@@ -280,44 +284,52 @@ apr_status_t parse_logfile(config_t *cfg, const char *filename) | |||
280 | apr_pool_clear(targp); | 284 | apr_pool_clear(targp); |
281 | tokenize_logline(buff, &targv, targp, 0); | 285 | tokenize_logline(buff, &targv, targp, 0); |
282 | targc = 0; | 286 | targc = 0; |
283 | while (targv[targc]) targc++; | 287 | while (targv[targc]) |
288 | targc++; | ||
284 | /** @todo Run Line Filters here */ | 289 | /** @todo Run Line Filters here */ |
285 | rv = parse_processline(targp, cfg, targv, targc); | 290 | rv = parse_processline(targp, cfg, targv, targc); |
286 | if (rv != APR_SUCCESS) { | 291 | if (rv != APR_SUCCESS) { |
287 | int i; | 292 | int i; |
288 | printf("Line %d(%d): %s\n",line, targc, buff); | 293 | logging_log(cfg, LOGLEVEL_ERROR, "Line %d(%d): %s", line, |
294 | targc, buff); | ||
289 | for (i = 0; targv[i]; i++) { | 295 | for (i = 0; targv[i]; i++) { |
290 | printf("Arg (%d): '%s'\n", i, targv[i]); | 296 | logging_log(cfg, LOGLEVEL_ERROR, "Arg (%d): '%s'", i, |
297 | targv[i]); | ||
291 | } | 298 | } |
292 | } | 299 | } |
293 | } | 300 | } |
294 | } while (rv == APR_SUCCESS); | 301 | } while (rv == APR_SUCCESS); |
295 | printf("Total Lines: %d\n", line); | ||
296 | apr_file_close(file); | 302 | apr_file_close(file); |
297 | apr_pool_destroy(tp); | 303 | apr_pool_destroy(tp); |
304 | logging_log(cfg, LOGLEVEL_NOTICE, | ||
305 | "Finish Parsing Log File '%s'. Lines: %d", filename, line); | ||
306 | |||
298 | return APR_SUCCESS; | 307 | return APR_SUCCESS; |
299 | } | 308 | } |
300 | 309 | ||
301 | apr_status_t parse_processline(apr_pool_t *ptemp, config_t *cfg, char **argv, int argc) | 310 | apr_status_t parse_processline(apr_pool_t *ptemp, config_t *cfg, char **argv, |
311 | int argc) | ||
302 | { | 312 | { |
303 | config_logformat_t *fmt; | 313 | config_logformat_t *fmt; |
304 | config_logformat_field_t *ifields; | 314 | config_logformat_field_t *ifields; |
305 | config_output_field_t *ofields; | 315 | config_output_field_t *ofields; |
306 | apr_table_t *datain; | 316 | apr_table_t *datain; |
307 | apr_table_t *dataout; | 317 | apr_table_t *dataout; |
308 | apr_status_t rv = APR_SUCCESS; | 318 | apr_status_t rv= APR_SUCCESS; |
309 | int i; | 319 | int i; |
310 | 320 | ||
311 | fmt = apr_hash_get(cfg->log_formats, cfg->logformat, APR_HASH_KEY_STRING); | 321 | fmt = apr_hash_get(cfg->log_formats, cfg->logformat, APR_HASH_KEY_STRING); |
312 | if (!fmt) return APR_EINVAL; | 322 | if (!fmt) |
313 | if (fmt->fields->nelts != argc) return APR_EINVAL; | 323 | return APR_EINVAL; |
324 | if (fmt->fields->nelts != argc) | ||
325 | return APR_EINVAL; | ||
314 | 326 | ||
315 | datain = apr_table_make(ptemp, fmt->fields->nelts); | 327 | datain = apr_table_make(ptemp, fmt->fields->nelts); |
316 | dataout = apr_table_make(ptemp, cfg->output_fields->nelts); | 328 | dataout = apr_table_make(ptemp, cfg->output_fields->nelts); |
317 | 329 | ||
318 | ifields = (config_logformat_field_t *)fmt->fields->elts; | 330 | ifields = (config_logformat_field_t *)fmt->fields->elts; |
319 | for (i=0; i<fmt->fields->nelts; i++) { | 331 | for (i=0; i<fmt->fields->nelts; i++) { |
320 | apr_table_setn(datain,ifields[i].name,argv[i]); | 332 | apr_table_setn(datain, ifields[i].name, argv[i]); |
321 | } | 333 | } |
322 | /** @todo Run Pre Filters here */ | 334 | /** @todo Run Pre Filters here */ |
323 | 335 | ||
@@ -332,12 +344,13 @@ apr_status_t parse_processline(apr_pool_t *ptemp, config_t *cfg, char **argv, in | |||
332 | continue; | 344 | continue; |
333 | } | 345 | } |
334 | if (!ofields[i].func) { | 346 | if (!ofields[i].func) { |
335 | apr_table_setn(dataout,ofields[i].field, val); | 347 | apr_table_setn(dataout, ofields[i].field, val); |
336 | } else { | 348 | } else { |
337 | const char *ret = NULL; | 349 | const char *ret= NULL; |
338 | rv = ((parser_func_t)ofields[i].func)(ptemp, cfg, &ofields[i], val, | 350 | rv = ((parser_func_t)ofields[i].func)(ptemp, cfg, &ofields[i], val, |
339 | &ret); | 351 | &ret); |
340 | if (rv) return rv; | 352 | if (rv) |
353 | return rv; | ||
341 | apr_table_setn(dataout, ofields[i].field, ret); | 354 | apr_table_setn(dataout, ofields[i].field, ret); |
342 | } | 355 | } |
343 | } | 356 | } |