-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathngx_http_syslog_module.c
365 lines (282 loc) · 9.57 KB
/
ngx_http_syslog_module.c
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
/*
* Copyright (C) 2010 Valery Kholodkov
*
* NOTE: Some small fragments have been copied from original nginx log module due to exports problem.
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <nginx.h>
#include <syslog.h>
#define NGX_SYSLOG_SEVERITY_INFO 6
typedef struct ngx_http_log_op_s ngx_http_log_op_t;
typedef u_char *(*ngx_http_log_op_run_pt) (ngx_http_request_t *r, u_char *buf,
ngx_http_log_op_t *op);
typedef size_t (*ngx_http_log_op_getlen_pt) (ngx_http_request_t *r,
uintptr_t data);
struct ngx_http_log_op_s {
size_t len;
ngx_http_log_op_getlen_pt getlen;
ngx_http_log_op_run_pt run;
uintptr_t data;
};
typedef struct {
ngx_str_t name;
#if defined nginx_version && nginx_version >= 7018
ngx_array_t *flushes;
#endif
ngx_array_t *ops; /* array of ngx_http_log_op_t */
} ngx_http_log_fmt_t;
typedef struct {
ngx_array_t formats; /* array of ngx_http_log_fmt_t */
ngx_uint_t combined_used; /* unsigned combined_used:1 */
} ngx_http_log_main_conf_t;
typedef struct {
ngx_str_t name;
ngx_uint_t number;
} ngx_syslog_severity_t;
typedef struct {
ngx_array_t *endpoints;
} ngx_http_syslog_main_conf_t;
typedef struct {
ngx_http_log_fmt_t *format;
ngx_uint_t severity;
unsigned off;
} ngx_http_syslog_conf_t;
static void *ngx_http_syslog_create_main_conf(ngx_conf_t *cf);
static void *ngx_http_syslog_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_syslog_merge_loc_conf(ngx_conf_t *cf, void *parent,
void *child);
static char *ngx_http_syslog_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static char *ngx_http_syslog_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static ngx_int_t ngx_http_syslog_init(ngx_conf_t *cf);
static char* ngx_syslog_init_conf(ngx_cycle_t *cycle, void *conf);
static ngx_int_t ngx_http_syslog_init_process(ngx_cycle_t *cycle);
static void ngx_http_syslog_exit_process(ngx_cycle_t *cycle);
static ngx_command_t ngx_http_syslog_commands[] = {
{ ngx_string("access_syslog"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF
|NGX_HTTP_LMT_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1,
ngx_http_syslog_set_log,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
{ ngx_string("syslog_priority"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_http_syslog_set_priority,
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL },
ngx_null_command
};
static ngx_http_module_t ngx_http_syslog_module_ctx = {
NULL, /* preconfiguration */
ngx_http_syslog_init, /* postconfiguration */
ngx_http_syslog_create_main_conf, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_syslog_create_loc_conf, /* create location configration */
ngx_http_syslog_merge_loc_conf /* merge location configration */
};
extern ngx_module_t ngx_http_log_module;
ngx_module_t ngx_http_syslog_module = {
NGX_MODULE_V1,
&ngx_http_syslog_module_ctx, /* module context */
ngx_http_syslog_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
ngx_http_syslog_init_process, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
ngx_http_syslog_exit_process, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_syslog_severity_t ngx_syslog_severities[] = {
{ ngx_string("emerg"), 0 },
{ ngx_string("alert"), 1 },
{ ngx_string("crit"), 2 },
{ ngx_string("err"), 3 },
{ ngx_string("warning"), 4 },
{ ngx_string("notice"), 5 },
{ ngx_string("info"), 6 },
{ ngx_string("debug"), 7 },
{ ngx_null_string, 0 }
};
ngx_int_t
ngx_http_syslog_handler(ngx_http_request_t *r)
{
u_char *line, *p;
size_t len;
ngx_uint_t i;
ngx_http_log_fmt_t *format;
ngx_http_log_op_t *op;
ngx_http_syslog_conf_t *slcf;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"http syslog handler");
slcf = ngx_http_get_module_loc_conf(r, ngx_http_syslog_module);
if(slcf->off || slcf->format == NULL) {
return NGX_OK;
}
format = slcf->format;
#if defined nginx_version && nginx_version >= 7018
ngx_http_script_flush_no_cacheable_variables(r, format->flushes);
#endif
len = 0;
op = format->ops->elts;
for (i = 0; i < format->ops->nelts; i++) {
if (op[i].len == 0) {
len += op[i].getlen(r, op[i].data);
} else {
len += op[i].len;
}
}
#if defined nginx_version && nginx_version >= 7003
line = ngx_pnalloc(r->pool, len + 1);
#else
line = ngx_palloc(r->pool, len + 1);
#endif
if (line == NULL) {
return NGX_ERROR;
}
p = line;
for (i = 0; i < format->ops->nelts; i++) {
p = op[i].run(r, p, &op[i]);
}
*p++ = '\0';
syslog(slcf->severity, "%s", line);
return NGX_OK;
}
static void *
ngx_http_syslog_create_main_conf(ngx_conf_t *cf)
{
ngx_http_syslog_main_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_syslog_main_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
return conf;
}
static void *
ngx_http_syslog_create_loc_conf(ngx_conf_t *cf)
{
ngx_http_syslog_conf_t *conf;
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_syslog_conf_t));
if (conf == NULL) {
return NGX_CONF_ERROR;
}
conf->severity = NGX_CONF_UNSET_UINT;
return conf;
}
static char *
ngx_http_syslog_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_syslog_conf_t *prev = parent;
ngx_http_syslog_conf_t *conf = child;
ngx_conf_merge_uint_value(conf->severity,
prev->severity, NGX_SYSLOG_SEVERITY_INFO);
if(conf->format != NULL || conf->off) {
return NGX_CONF_OK;
}
conf->format = prev->format;
conf->off = prev->off;
return NGX_CONF_OK;
}
static char *
ngx_http_syslog_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_syslog_conf_t *slcf = conf;
ngx_uint_t i;
ngx_str_t *value, name;
ngx_http_log_fmt_t *fmt;
ngx_http_log_main_conf_t *lmcf;
value = cf->args->elts;
if (ngx_strcmp(value[1].data, "off") == 0) {
slcf->off = 1;
return NGX_CONF_OK;
}
if (slcf->format != NULL) {
return "is duplicate";
}
lmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_log_module);
if(lmcf == NULL) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"syslog module requires log module to be compiled in");
return NGX_CONF_ERROR;
}
if (cf->args->nelts > 1) {
name = value[1];
if (ngx_strcmp(name.data, "combined") == 0) {
lmcf->combined_used = 1;
}
} else {
name.len = sizeof("combined") - 1;
name.data = (u_char *) "combined";
lmcf->combined_used = 1;
}
fmt = lmcf->formats.elts;
for (i = 0; i < lmcf->formats.nelts; i++) {
if (fmt[i].name.len == name.len
&& ngx_strcasecmp(fmt[i].name.data, name.data) == 0)
{
slcf->format = &fmt[i];
goto done;
}
}
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"unknown log format \"%V\"", &name);
return NGX_CONF_ERROR;
done:
return NGX_CONF_OK;
}
static char *
ngx_http_syslog_set_priority(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_syslog_conf_t *slcf = conf;
ngx_str_t *value;
ngx_syslog_severity_t *s;
value = cf->args->elts;
s = ngx_syslog_severities;
while(s->name.data != NULL) {
if(ngx_strncmp(s->name.data, value[1].data, s->name.len) == 0)
break;
s++;
}
if(s->name.data != NULL) {
slcf->severity = s->number;
}
else {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"unknown severity \"%V\"", &value[2]);
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
static ngx_int_t
ngx_http_syslog_init(ngx_conf_t *cf)
{
ngx_http_core_main_conf_t *cmcf;
ngx_http_syslog_main_conf_t *smcf;
ngx_http_handler_pt *h;
smcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_syslog_module);
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
h = ngx_array_push(&cmcf->phases[NGX_HTTP_LOG_PHASE].handlers);
if (h == NULL) {
return NGX_ERROR;
}
*h = ngx_http_syslog_handler;
return NGX_OK;
}
static ngx_int_t
ngx_http_syslog_init_process(ngx_cycle_t *cycle)
{
openlog("nginx", LOG_NDELAY, LOG_DAEMON);
return NGX_OK;
}
static void
ngx_http_syslog_exit_process(ngx_cycle_t *cycle)
{
closelog();
}