Skip to content

Commit 75cf7a4

Browse files
authored
Merge pull request #1 from awmackowiak/am-logging
Add variable for logging time and add examples how to use it to README
2 parents e695492 + 8052510 commit 75cf7a4

4 files changed

+47
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,27 @@ using the same unique identificator.
176176

177177
String can contain variables.
178178

179+
# Format Variables
180+
ModSecurity-nginx provide times variables for particular phases that you can uses in nginx *log_format*:
181+
182+
*$modsecurity_req_headers_phase_time*
183+
request headers processing time in seconds with a microseconds resolution; time elapsed for processing headers in first phase by ModSecurity
184+
*$modsecurity_req_body_phase_time*
185+
request body processing time in seconds with a microseconds resolution; time elapsed for processing request body in second phase by ModSecurity
186+
*$modsecurity_resp_headers_phase_time*
187+
resposnse headers processing time in seconds with a microseconds resolution; time elapsed for processing response headers in third phase by ModSecurity
188+
*$modsecurity_resp_body_phase_time*
189+
response body processing time in seconds with a microseconds resolution; time elapsed for processing response body in fourth phase by ModSecurity
190+
*$modsecurity_logging_phase_time*
191+
logging processing time in seconds with a microseconds resolution; time elapsed for processing logging in fifth phase by ModSecurity
192+
193+
The following example show how to configure custom *log_format* with variables above and use them with custom *access.log*:
194+
195+
```nginx
196+
log_format format_modsecurity 'modsecurity_req_headers_phase_time: $modsecurity_req_headers_phase_time, modsecurity_req_body_phase_time: $modsecurity_req_body_phase_time, modsecurity_resp_headers_phase_time: $modsecurity_resp_headers_phase_time, modsecurity_resp_body_phase_time: $modsecurity_resp_body_phase_time, modsecurity_logging_phase_time: $modsecurity_logging_phase_time';
197+
198+
access_log /usr/local/nginx/logs/modsecurity.log format_modsecurity;
199+
```
179200

180201
# Contributing
181202

src/ngx_http_modsecurity_common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ typedef struct {
103103
ngx_msec_int_t req_body_phase_time;
104104
ngx_msec_int_t resp_headers_phase_time;
105105
ngx_msec_int_t resp_body_phase_time;
106+
ngx_msec_int_t logging_phase_time;
106107
} ngx_http_modsecurity_ctx_t;
107108

108109

src/ngx_http_modsecurity_log.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,14 @@ ngx_http_modsecurity_log_handler(ngx_http_request_t *r)
7171
dd("already logged earlier");
7272
return NGX_OK;
7373
}
74+
struct timeval start_tv;
75+
ngx_gettimeofday(&start_tv);
7476

7577
dd("calling msc_process_logging for %p", ctx);
7678
old_pool = ngx_http_modsecurity_pcre_malloc_init(r->pool);
7779
msc_process_logging(ctx->modsec_transaction);
7880
ngx_http_modsecurity_pcre_malloc_done(old_pool);
81+
ctx->logging_phase_time = ngx_http_modsecurity_compute_processing_time(start_tv);
7982

8083
return NGX_OK;
8184
}

src/ngx_http_modsecurity_module.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ static ngx_int_t ngx_http_modsecurity_resp_headers_phase_time(ngx_http_request_t
4141
ngx_http_variable_value_t *v, uintptr_t data);
4242
static ngx_int_t ngx_http_modsecurity_resp_body_phase_time(ngx_http_request_t *r,
4343
ngx_http_variable_value_t *v, uintptr_t data);
44+
static ngx_int_t ngx_http_modsecurity_logging_phase_time(ngx_http_request_t *r,
45+
ngx_http_variable_value_t *v, uintptr_t data);
4446
static ngx_int_t ngx_http_modsecurity_time_variable(ngx_http_request_t *r,
4547
ngx_http_variable_value_t *v, uintptr_t data, ngx_msec_int_t usec);
4648

@@ -283,6 +285,7 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
283285
ctx->req_body_phase_time = -1;
284286
ctx->resp_headers_phase_time = -1;
285287
ctx->resp_body_phase_time = -1;
288+
ctx->logging_phase_time = -1;
286289

287290
mmcf = ngx_http_get_module_main_conf(r, ngx_http_modsecurity_module);
288291
mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module);
@@ -553,6 +556,10 @@ static ngx_http_variable_t ngx_http_modsecurity_vars[] = {
553556
ngx_http_modsecurity_resp_body_phase_time, 0,
554557
NGX_HTTP_VAR_NOCACHEABLE, 0 },
555558

559+
{ ngx_string("modsecurity_logging_phase_time"), NULL,
560+
ngx_http_modsecurity_logging_phase_time, 0,
561+
NGX_HTTP_VAR_NOCACHEABLE, 0 },
562+
556563
ngx_http_null_variable
557564
};
558565

@@ -897,6 +904,21 @@ ngx_http_modsecurity_resp_body_phase_time(ngx_http_request_t *r,
897904
return ngx_http_modsecurity_time_variable(r, v, data, ctx->resp_body_phase_time);
898905
}
899906

907+
908+
static ngx_int_t
909+
ngx_http_modsecurity_logging_phase_time(ngx_http_request_t *r,
910+
ngx_http_variable_value_t *v, uintptr_t data)
911+
{
912+
ngx_http_modsecurity_ctx_t *ctx;
913+
914+
ctx = ngx_http_get_module_ctx(r, ngx_http_modsecurity_module);
915+
if (ctx == NULL) {
916+
return NGX_ERROR;
917+
}
918+
return ngx_http_modsecurity_time_variable(r, v, data, ctx->logging_phase_time);
919+
}
920+
921+
900922
static ngx_int_t
901923
ngx_http_modsecurity_time_variable(ngx_http_request_t *r,
902924
ngx_http_variable_value_t *v, uintptr_t data, ngx_msec_int_t usec)

0 commit comments

Comments
 (0)