blob: b80969ff1d1bc8d42fbd7228af466007150a08fd [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreauc8f24f82007-11-30 18:38:35 +010013#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020014#include <stdarg.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <syslog.h>
19#include <time.h>
20#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010021#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022
23#include <sys/time.h>
24
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020025#include <common/config.h>
Willy Tarreaud6d06902009-08-19 11:22:33 +020026#include <common/compat.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020027#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020028#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <types/global.h>
William Lallemand723b73a2012-02-08 16:37:49 +010031#include <types/log.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020032
33#include <proto/log.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010034#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
Willy Tarreaubaaee002006-06-26 02:48:02 +020036const char *log_facilities[NB_LOG_FACILITIES] = {
37 "kern", "user", "mail", "daemon",
38 "auth", "syslog", "lpr", "news",
39 "uucp", "cron", "auth2", "ftp",
40 "ntp", "audit", "alert", "cron2",
41 "local0", "local1", "local2", "local3",
42 "local4", "local5", "local6", "local7"
43};
44
45
46const char *log_levels[NB_LOG_LEVELS] = {
47 "emerg", "alert", "crit", "err",
48 "warning", "notice", "info", "debug"
49};
50
51const char *monthname[12] = {
52 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
53 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
54};
55
Willy Tarreaua2a64e92011-09-07 23:01:56 +020056const char sess_term_cond[10] = "-cCsSPRIDK"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed */
Willy Tarreaub8750a82006-09-03 09:56:00 +020057const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
William Lallemand723b73a2012-02-08 16:37:49 +010059
60/* log_format */
61struct logformat_type {
62 char *name;
63 int type;
64};
65
66/* log_format variable names */
67static const struct logformat_type logformat_keywords[] = {
68 { "o", LOG_GLOBAL }, /* global option */
69 { "Ci", LOG_CLIENTIP }, /* client ip */
70 { "Cp", LOG_CLIENTPORT }, /* client port */
71 { "t", LOG_DATE }, /* date */
72 { "T", LOG_DATEGMT }, /* date GMT */
73 { "ms", LOG_MS }, /* accept date millisecond */
74 { "f", LOG_FRONTEND }, /* frontend */
75 { "b", LOG_BACKEND }, /* backend */
76 { "s", LOG_SERVER }, /* server */
77 { "B", LOG_BYTES }, /* bytes read */
78 { "Tq", LOG_TQ }, /* Tq */
79 { "Tw", LOG_TW }, /* Tw */
80 { "Tc", LOG_TC }, /* Tc */
81 { "Tr", LOG_TR }, /* Tr */
82 { "Tt", LOG_TT }, /* Tt */
83 { "st", LOG_STATUS }, /* status code */
84 { "cc", LOG_CCLIENT }, /* client cookie */
85 { "cs", LOG_CSERVER }, /* server cookie */
86 { "ts", LOG_TERMSTATE },/* terminaison state */
87 { "ac", LOG_ACTCONN }, /* actconn */
88 { "fc", LOG_FECONN }, /* feconn */
89 { "bc", LOG_BECONN }, /* beconn */
90 { "sc", LOG_SRVCONN }, /* srv_conn */
91 { "rc", LOG_RETRIES }, /* retries */
92 { "sq", LOG_SRVQUEUE }, /* srv_queue */
93 { "bq", LOG_BCKQUEUE }, /* backend_queue */
94 { "hr", LOG_HDRREQUEST }, /* header request */
95 { "hs", LOG_HDRRESPONS }, /* header response */
96 { "hrl", LOG_HDRREQUESTLIST }, /* header request list */
97 { "hsl", LOG_HDRRESPONSLIST }, /* header response list */
98 { "r", LOG_REQ }, /* request */
99 { 0, 0 }
100};
101
102char default_http_log_format[] = "%Ci:%Cp [%t] %f %b/%s %Tq/%Tw/%Tc/%Tr/%Tt %st %B %cc %cs %ts %ac/%fc/%bc/%sc/%rc %sq/%bq %hr %hs %{+Q}r"; // default format
103char clf_http_log_format[] = "%{+Q}o %{-Q}Ci - - [%T] %r %st %B \"\" \"\" %Cp %ms %f %b %s %Tq %Tw %Tc %Tr %Tt %ts %ac %fc %bc %sc %rc %sq %bq %cc %cs %hrl %hsl";
104char *log_format = NULL;
105
106struct logformat_var_args {
107 char *name;
108 int mask;
109};
110
111struct logformat_var_args var_args_list[] = {
112// global
113 { "M", LOG_OPT_MANDATORY },
114 { "Q", LOG_OPT_QUOTE },
115 { 0, 0 }
116};
117
118/*
119 * Parse args in a logformat_var
120 */
121int parse_logformat_var_args(char *args, struct logformat_node *node)
122{
123 int i = 0;
124 int end = 0;
125 int flags = 0; // 1 = + 2 = -
126 char *sp = NULL; // start pointer
127
128 if (args == NULL)
129 return 1;
130
131 while (1) {
132 if (*args == '\0')
133 end = 1;
134
135 if (*args == '+') {
136 // add flag
137 sp = args + 1;
138 flags = 1;
139 }
140 if (*args == '-') {
141 // delete flag
142 sp = args + 1;
143 flags = 2;
144 }
145
146 if (*args == '\0' || *args == ',') {
147 *args = '\0';
148 for (i = 0; var_args_list[i].name; i++) {
149 if (strcmp(sp, var_args_list[i].name) == 0) {
150 if (flags == 1) {
151 node->options |= var_args_list[i].mask;
152 break;
153 } else if (flags == 2) {
154 node->options &= ~var_args_list[i].mask;
155 break;
156 }
157 }
158 }
159 sp = NULL;
160 if (end)
161 break;
162 }
163 args++;
164 }
165 return 0;
166}
167
168/*
169 * Parse a variable '%varname' or '%{args}varname' in logformat
170 *
171 */
172int parse_logformat_var(char *str, size_t len, struct proxy *curproxy)
173{
174 int i, j;
175 char *arg = NULL; // arguments
176 int fparam = 0;
177 char *name = NULL;
178 struct logformat_node *node = NULL;
179 char varname[255] = { 0 }; // variable name
180 int logformat_options = 0x00000000;
181
182
183 for (i = 1; i < len; i++) { // escape first char %
184 if (!arg && str[i] == '{') {
185 arg = str + i;
186 fparam = 1;
187 } else if (arg && str[i] == '}') {
188 char *tmp = arg;
189 arg = calloc(str + i - tmp, 1); // without {}
190 strncpy(arg, tmp + 1, str + i - tmp - 1); // copy without { and }
191 arg[str + i - tmp - 1] = '\0';
192 fparam = 0;
193 } else if (!name && !fparam) {
194 strncpy(varname, str + i, len - i + 1);
195 varname[len - i] = '\0';
196 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
197 if (strcmp(varname, logformat_keywords[j].name) == 0) {
198 node = calloc(1, sizeof(struct logformat_node));
199 node->type = logformat_keywords[j].type;
200 node->options = logformat_options;
201 node->arg = arg;
202 parse_logformat_var_args(node->arg, node);
203 if (node->type == LOG_GLOBAL) {
204 logformat_options = node->options;
205 free(node);
206 } else {
207 LIST_ADDQ(&curproxy->logformat, &node->list);
208 }
209 return 0;
210 }
211 }
212 Warning("Warning: No such variable name '%s' in logformat\n", varname);
213 if (arg)
214 free(arg);
215 return -1;
216 }
217 }
218 return -1;
219}
220
221/*
222 * push to the logformat linked list
223 *
224 * start: start pointer
225 * end: end text pointer
226 * type: string type
227 *
228 * LOG_TEXT: copy chars from start to end excluding end.
229 *
230*/
231void add_to_logformat_list(char *start, char *end, int type, struct proxy *curproxy)
232{
233 char *str;
234
235 if (type == LOG_TEXT) { /* type text */
236 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
237
238 str = calloc(end - start + 1, 1);
239 strncpy(str, start, end - start);
240
241 str[end - start] = '\0';
242 node->arg = str;
243 node->type = LOG_TEXT; // type string
244 LIST_ADDQ(&curproxy->logformat, &node->list);
245 } else if (type == LOG_VARIABLE) { /* type variable */
246 parse_logformat_var(start, end - start, curproxy);
247 } else if (type == LOG_SEPARATOR) {
248 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
249 node->type = LOG_SEPARATOR;
250 LIST_ADDQ(&curproxy->logformat, &node->list);
251 }
252}
253
254/*
255 * Parse the log_format string and fill a linked list.
256 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
257 * You can set arguments using { } : %{many arguments}varname
258 */
259void parse_logformat_string(char *str, struct proxy *curproxy)
260{
261 char *sp = str; /* start pointer */
262 int cformat = -1; /* current token format : LOG_TEXT, LOG_SEPARATOR, LOG_VARIABLE */
263 int pformat = -1; /* previous token format */
264 struct logformat_node *tmplf, *back;
265
266 /* flush the list first. */
267 list_for_each_entry_safe(tmplf, back, &curproxy->logformat, list) {
268 LIST_DEL(&tmplf->list);
269 free(tmplf);
270 }
271
272 while (1) {
273
274 // push the variable only if formats are different, not
275 // within a variable, and not the first iteration
276 if ((cformat != pformat && cformat != -1 && pformat != -1) || *str == '\0') {
277 if (((pformat != LF_STARTVAR && cformat != LF_VAR) &&
278 (pformat != LF_STARTVAR && cformat != LF_STARG) &&
279 (pformat != LF_STARG && cformat != LF_VAR)) || *str == '\0') {
280 if (pformat > LF_VAR) // unfinished string
281 pformat = LF_TEXT;
282 add_to_logformat_list(sp, str, pformat, curproxy);
283 sp = str;
284 if (*str == '\0')
285 break;
286 }
287 }
288
289 if (cformat != -1)
290 str++; // consume the string, except on the first tour
291
292 pformat = cformat;
293
294 if (*str == '\0') {
295 cformat = LF_STARTVAR; // for breaking in all cases
296 continue;
297 }
298
299 if (pformat == LF_STARTVAR) { // after a %
300 if ( (*str >= 'a' && *str <= 'z') || // parse varname
301 (*str >= 'A' && *str <= 'Z') ||
302 (*str >= '0' && *str <= '9')) {
303 cformat = LF_VAR; // varname
304 continue;
305 } else if (*str == '{') {
306 cformat = LF_STARG; // variable arguments
307 continue;
308 } else { // another unexpected token
309 pformat = LF_TEXT; // redefine the format of the previous token to TEXT
310 cformat = LF_TEXT;
311 continue;
312 }
313
314 } else if (pformat == LF_VAR) { // after a varname
315 if ( (*str >= 'a' && *str <= 'z') || // parse varname
316 (*str >= 'A' && *str <= 'Z') ||
317 (*str >= '0' && *str <= '9')) {
318 cformat = LF_VAR;
319 continue;
320 }
321 } else if (pformat == LF_STARG) { // inside variable arguments
322 if (*str == '}') { // end of varname
323 cformat = LF_EDARG;
324 continue;
325 } else { // all tokens are acceptable within { }
326 cformat = LF_STARG;
327 continue;
328 }
329 } else if (pformat == LF_EDARG) { // after arguments
330 if ( (*str >= 'a' && *str <= 'z') || // parse a varname
331 (*str >= 'A' && *str <= 'Z') ||
332 (*str >= '0' && *str <= '9')) {
333 cformat = LF_VAR;
334 continue;
335 } else { // if no varname after arguments, transform in TEXT
336 pformat = LF_TEXT;
337 cformat = LF_TEXT;
338 }
339 }
340
341 // others tokens that don't match previous conditions
342 if (*str == '%') {
343 cformat = LF_STARTVAR;
344 } else if (*str == ' ') {
345 cformat = LF_SEPARATOR;
346 } else {
347 cformat = LF_TEXT;
348 }
349 }
350}
351
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352/*
353 * Displays the message on stderr with the date and pid. Overrides the quiet
354 * mode during startup.
355 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200356void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357{
358 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200359 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200360
361 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
362 va_start(argp, fmt);
363
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200364 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200366 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200367 vfprintf(stderr, fmt, argp);
368 fflush(stderr);
369 va_end(argp);
370 }
371}
372
373
374/*
375 * Displays the message on stderr with the date and pid.
376 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200377void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200378{
379 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200380 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381
382 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
383 va_start(argp, fmt);
384
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200385 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200387 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200388 vfprintf(stderr, fmt, argp);
389 fflush(stderr);
390 va_end(argp);
391 }
392}
393
394/*
395 * Displays the message on <out> only if quiet mode is not set.
396 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200397void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200398{
399 va_list argp;
400
401 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
402 va_start(argp, fmt);
403 vfprintf(out, fmt, argp);
404 fflush(out);
405 va_end(argp);
406 }
407}
408
409/*
410 * returns log level for <lev> or -1 if not found.
411 */
412int get_log_level(const char *lev)
413{
414 int level;
415
416 level = NB_LOG_LEVELS - 1;
417 while (level >= 0 && strcmp(log_levels[level], lev))
418 level--;
419
420 return level;
421}
422
423
424/*
425 * returns log facility for <fac> or -1 if not found.
426 */
427int get_log_facility(const char *fac)
428{
429 int facility;
430
431 facility = NB_LOG_FACILITIES - 1;
432 while (facility >= 0 && strcmp(log_facilities[facility], fac))
433 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100434
Willy Tarreaubaaee002006-06-26 02:48:02 +0200435 return facility;
436}
437
William Lallemand2a4a44f2012-02-06 16:00:33 +0100438/* generate the syslog header once a second */
439char *hdr_log(char *dst)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200440{
William Lallemand2a4a44f2012-02-06 16:00:33 +0100441 int hdr_len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200442 static long tvsec = -1; /* to force the string to be initialized */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200443 static char *dataptr = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200444
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200445 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200446 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200447 struct tm tm;
448
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200449 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200450 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200451
William Lallemand2a4a44f2012-02-06 16:00:33 +0100452 hdr_len = snprintf(dst, MAX_SYSLOG_LEN,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100453 "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200454 monthname[tm.tm_mon],
455 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100456 global.log_send_hostname ? global.log_send_hostname : "",
Kevinm48936af2010-12-22 16:08:21 +0000457 global.log_tag, pid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200458 /* WARNING: depending upon implementations, snprintf may return
459 * either -1 or the number of bytes that would be needed to store
460 * the total message. In both cases, we must adjust it.
461 */
William Lallemand2a4a44f2012-02-06 16:00:33 +0100462 if (hdr_len < 0 || hdr_len > MAX_SYSLOG_LEN)
463 hdr_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200464
William Lallemand2a4a44f2012-02-06 16:00:33 +0100465 dataptr = dst + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200466 }
467
William Lallemand2a4a44f2012-02-06 16:00:33 +0100468 return dataptr;
469}
470
471/*
472 * This function adds a header to the message and sends the syslog message
473 * using a printf format string
474 */
475void send_log(struct proxy *p, int level, const char *format, ...)
476{
477 va_list argp;
478 static char logmsg[MAX_SYSLOG_LEN];
479 static char *dataptr = NULL;
480 int data_len = 0;
481
482 if (level < 0 || format == NULL)
483 return;
484
485 dataptr = hdr_log(logmsg); /* create header */
486 data_len = dataptr - logmsg;
487
488 va_start(argp, format);
489 data_len += vsnprintf(dataptr, MAX_SYSLOG_LEN, format, argp);
490 if (data_len < 0 || data_len > MAX_SYSLOG_LEN)
491 data_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200492 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100493
494 __send_log(p, level, logmsg, data_len);
495}
496
497/*
498 * This function sends a syslog message.
499 * It doesn't care about errors nor does it report them.
500 */
501void __send_log(struct proxy *p, int level, char *message, size_t size)
502{
503 static int logfdunix = -1; /* syslog to AF_UNIX socket */
504 static int logfdinet = -1; /* syslog to AF_INET socket */
505 static char *dataptr = NULL;
506 int fac_level;
507 struct list *logsrvs = NULL;
508 struct logsrv *tmp = NULL;
509 int nblogger;
510 char *log_ptr;
511
512 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200513
514 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200515 if (!LIST_ISEMPTY(&global.logsrvs)) {
516 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200517 }
518 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200519 if (!LIST_ISEMPTY(&p->logsrvs)) {
520 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200521 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100522 }
523
William Lallemand0f99e342011-10-12 17:50:54 +0200524 if (!logsrvs)
525 return;
526
William Lallemand2a4a44f2012-02-06 16:00:33 +0100527 message[size - 1] = '\n';
528
Robert Tsai81ae1952007-12-05 10:47:29 +0100529 /* Lazily set up syslog sockets for protocol families of configured
530 * syslog servers. */
William Lallemand0f99e342011-10-12 17:50:54 +0200531 nblogger = 0;
532 list_for_each_entry(tmp, logsrvs, list) {
533 const struct logsrv *logsrv = tmp;
Robert Tsai81ae1952007-12-05 10:47:29 +0100534 int proto, *plogfd;
William Lallemand0f99e342011-10-12 17:50:54 +0200535
David du Colombier11bcb6c2011-03-24 12:23:00 +0100536 if (logsrv->addr.ss_family == AF_UNIX) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100537 proto = 0;
538 plogfd = &logfdunix;
539 } else {
Robert Tsai81ae1952007-12-05 10:47:29 +0100540 proto = IPPROTO_UDP;
541 plogfd = &logfdinet;
542 }
543 if (*plogfd >= 0) {
544 /* socket already created. */
545 continue;
546 }
David du Colombier11bcb6c2011-03-24 12:23:00 +0100547 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
Robert Tsai81ae1952007-12-05 10:47:29 +0100548 proto)) < 0) {
549 Alert("socket for logger #%d failed: %s (errno=%d)\n",
550 nblogger + 1, strerror(errno), errno);
551 return;
552 }
553 /* we don't want to receive anything on this socket */
554 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
555 /* does nothing under Linux, maybe needed for others */
556 shutdown(*plogfd, SHUT_RD);
William Lallemand0f99e342011-10-12 17:50:54 +0200557 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200558 }
559
Robert Tsai81ae1952007-12-05 10:47:29 +0100560 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200561 nblogger = 0;
562 list_for_each_entry(tmp, logsrvs, list) {
563 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100564 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100565 &logfdunix : &logfdinet;
566 int sent;
567
Willy Tarreaubaaee002006-06-26 02:48:02 +0200568 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200569 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200570 continue;
571
572 /* For each target, we may have a different facility.
573 * We can also have a different log level for each message.
574 * This induces variations in the message header length.
575 * Since we don't want to recompute it each time, nor copy it every
576 * time, we only change the facility in the pre-computed header,
577 * and we change the pointer to the header accordingly.
578 */
William Lallemand0f99e342011-10-12 17:50:54 +0200579 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100580 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200581 do {
582 *log_ptr = '0' + fac_level % 10;
583 fac_level /= 10;
584 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100585 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200586 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100587
588 sent = sendto(*plogfd, log_ptr, size,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200589 MSG_DONTWAIT | MSG_NOSIGNAL,
590 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Robert Tsai81ae1952007-12-05 10:47:29 +0100591 if (sent < 0) {
592 Alert("sendto logger #%d failed: %s (errno=%d)\n",
593 nblogger, strerror(errno), errno);
594 }
William Lallemand0f99e342011-10-12 17:50:54 +0200595 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200596 }
597}
598
599
600/*
601 * send a log for the session when we have enough info about it
602 */
Willy Tarreau42250582007-04-01 01:30:43 +0200603void tcp_sess_log(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200604{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100605 char pn[INET6_ADDRSTRLEN];
Willy Tarreau73de9892006-11-30 11:40:23 +0100606 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100607 struct proxy *be = s->be;
608 struct proxy *prx_log;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200609 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200610 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200611 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200612
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200613 /* if we don't want to log normal traffic, return now */
Willy Tarreauee28de02010-06-01 09:51:00 +0200614 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || (s->req->cons->conn_retries != be->conn_retries);
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200615 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
616 return;
617
Willy Tarreau6471afb2011-09-23 10:54:59 +0200618 addr_to_str(&s->si[0].addr.from, pn, sizeof(pn));
Willy Tarreaufe944602007-10-25 10:34:16 +0200619 get_localtime(s->logs.tv_accept.tv_sec, &tm);
Willy Tarreauddb358d2006-12-17 22:55:52 +0100620
William Lallemand0f99e342011-10-12 17:50:54 +0200621 if(LIST_ISEMPTY(&fe->logsrvs))
Willy Tarreaue7ded1f2009-08-09 10:11:45 +0200622 return;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100623
Willy Tarreaue7ded1f2009-08-09 10:11:45 +0200624 prx_log = fe;
Willy Tarreau42250582007-04-01 01:30:43 +0200625 tolog = fe->to_log;
Willy Tarreau71904a42011-02-13 14:30:26 +0100626
627 if (!(tolog & LW_SVID))
628 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200629 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100630 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200631 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100632 break;
633 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200634 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100635 break;
636 default:
637 svid = "<NOSRV>";
638 break;
639 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200640
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200641 level = LOG_INFO;
642 if (err && (fe->options2 & PR_O2_LOGERRORS))
643 level = LOG_ERR;
644
645 send_log(prx_log, level, "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +0200646 " %s %s/%s %ld/%ld/%s%ld %s%lld"
647 " %c%c %d/%d/%d/%d/%s%u %ld/%ld\n",
Willy Tarreau6471afb2011-09-23 10:54:59 +0200648 s->si[0].addr.from.ss_family == AF_UNIX ? "unix" : pn,
649 s->si[0].addr.from.ss_family == AF_UNIX ? s->listener->luid : get_host_port(&s->si[0].addr.from),
Willy Tarreaufe944602007-10-25 10:34:16 +0200650 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +0200651 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.tv_accept.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200652 fe->id, be->id, svid,
653 (s->logs.t_queue >= 0) ? s->logs.t_queue : -1,
654 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
655 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100656 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200657 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
658 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
Willy Tarreau827aee92011-03-10 16:55:02 +0100659 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100660 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +0200661 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200662 s->logs.srv_queue_size, s->logs.prx_queue_size);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200663
664 s->logs.logwait = 0;
665}
666
Willy Tarreaubaaee002006-06-26 02:48:02 +0200667
668/*
669 * Local variables:
670 * c-indent-level: 8
671 * c-basic-offset: 8
672 * End:
673 */