blob: 0d13329356a96a7c058d0213ccf9a4c0f0e87c0f [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
Willy Tarreaua2a64e92011-09-07 23:01:56 +020051const char sess_term_cond[10] = "-cCsSPRIDK"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed */
Willy Tarreaub8750a82006-09-03 09:56:00 +020052const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020053
William Lallemand723b73a2012-02-08 16:37:49 +010054
55/* log_format */
56struct logformat_type {
57 char *name;
58 int type;
59};
60
61/* log_format variable names */
62static const struct logformat_type logformat_keywords[] = {
63 { "o", LOG_GLOBAL }, /* global option */
64 { "Ci", LOG_CLIENTIP }, /* client ip */
65 { "Cp", LOG_CLIENTPORT }, /* client port */
66 { "t", LOG_DATE }, /* date */
67 { "T", LOG_DATEGMT }, /* date GMT */
68 { "ms", LOG_MS }, /* accept date millisecond */
69 { "f", LOG_FRONTEND }, /* frontend */
70 { "b", LOG_BACKEND }, /* backend */
71 { "s", LOG_SERVER }, /* server */
72 { "B", LOG_BYTES }, /* bytes read */
73 { "Tq", LOG_TQ }, /* Tq */
74 { "Tw", LOG_TW }, /* Tw */
75 { "Tc", LOG_TC }, /* Tc */
76 { "Tr", LOG_TR }, /* Tr */
77 { "Tt", LOG_TT }, /* Tt */
78 { "st", LOG_STATUS }, /* status code */
79 { "cc", LOG_CCLIENT }, /* client cookie */
80 { "cs", LOG_CSERVER }, /* server cookie */
81 { "ts", LOG_TERMSTATE },/* terminaison state */
82 { "ac", LOG_ACTCONN }, /* actconn */
83 { "fc", LOG_FECONN }, /* feconn */
84 { "bc", LOG_BECONN }, /* beconn */
85 { "sc", LOG_SRVCONN }, /* srv_conn */
86 { "rc", LOG_RETRIES }, /* retries */
87 { "sq", LOG_SRVQUEUE }, /* srv_queue */
88 { "bq", LOG_BCKQUEUE }, /* backend_queue */
89 { "hr", LOG_HDRREQUEST }, /* header request */
90 { "hs", LOG_HDRRESPONS }, /* header response */
91 { "hrl", LOG_HDRREQUESTLIST }, /* header request list */
92 { "hsl", LOG_HDRRESPONSLIST }, /* header response list */
93 { "r", LOG_REQ }, /* request */
94 { 0, 0 }
95};
96
97char 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
98char 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";
99char *log_format = NULL;
100
101struct logformat_var_args {
102 char *name;
103 int mask;
104};
105
106struct logformat_var_args var_args_list[] = {
107// global
108 { "M", LOG_OPT_MANDATORY },
109 { "Q", LOG_OPT_QUOTE },
110 { 0, 0 }
111};
112
113/*
114 * Parse args in a logformat_var
115 */
116int parse_logformat_var_args(char *args, struct logformat_node *node)
117{
118 int i = 0;
119 int end = 0;
120 int flags = 0; // 1 = + 2 = -
121 char *sp = NULL; // start pointer
122
123 if (args == NULL)
124 return 1;
125
126 while (1) {
127 if (*args == '\0')
128 end = 1;
129
130 if (*args == '+') {
131 // add flag
132 sp = args + 1;
133 flags = 1;
134 }
135 if (*args == '-') {
136 // delete flag
137 sp = args + 1;
138 flags = 2;
139 }
140
141 if (*args == '\0' || *args == ',') {
142 *args = '\0';
143 for (i = 0; var_args_list[i].name; i++) {
144 if (strcmp(sp, var_args_list[i].name) == 0) {
145 if (flags == 1) {
146 node->options |= var_args_list[i].mask;
147 break;
148 } else if (flags == 2) {
149 node->options &= ~var_args_list[i].mask;
150 break;
151 }
152 }
153 }
154 sp = NULL;
155 if (end)
156 break;
157 }
158 args++;
159 }
160 return 0;
161}
162
163/*
164 * Parse a variable '%varname' or '%{args}varname' in logformat
165 *
166 */
167int parse_logformat_var(char *str, size_t len, struct proxy *curproxy)
168{
169 int i, j;
170 char *arg = NULL; // arguments
171 int fparam = 0;
172 char *name = NULL;
173 struct logformat_node *node = NULL;
174 char varname[255] = { 0 }; // variable name
175 int logformat_options = 0x00000000;
176
177
178 for (i = 1; i < len; i++) { // escape first char %
179 if (!arg && str[i] == '{') {
180 arg = str + i;
181 fparam = 1;
182 } else if (arg && str[i] == '}') {
183 char *tmp = arg;
184 arg = calloc(str + i - tmp, 1); // without {}
185 strncpy(arg, tmp + 1, str + i - tmp - 1); // copy without { and }
186 arg[str + i - tmp - 1] = '\0';
187 fparam = 0;
188 } else if (!name && !fparam) {
189 strncpy(varname, str + i, len - i + 1);
190 varname[len - i] = '\0';
191 for (j = 0; logformat_keywords[j].name; j++) { // search a log type
192 if (strcmp(varname, logformat_keywords[j].name) == 0) {
193 node = calloc(1, sizeof(struct logformat_node));
194 node->type = logformat_keywords[j].type;
195 node->options = logformat_options;
196 node->arg = arg;
197 parse_logformat_var_args(node->arg, node);
198 if (node->type == LOG_GLOBAL) {
199 logformat_options = node->options;
200 free(node);
201 } else {
202 LIST_ADDQ(&curproxy->logformat, &node->list);
203 }
204 return 0;
205 }
206 }
207 Warning("Warning: No such variable name '%s' in logformat\n", varname);
208 if (arg)
209 free(arg);
210 return -1;
211 }
212 }
213 return -1;
214}
215
216/*
217 * push to the logformat linked list
218 *
219 * start: start pointer
220 * end: end text pointer
221 * type: string type
222 *
223 * LOG_TEXT: copy chars from start to end excluding end.
224 *
225*/
226void add_to_logformat_list(char *start, char *end, int type, struct proxy *curproxy)
227{
228 char *str;
229
230 if (type == LOG_TEXT) { /* type text */
231 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
232
233 str = calloc(end - start + 1, 1);
234 strncpy(str, start, end - start);
235
236 str[end - start] = '\0';
237 node->arg = str;
238 node->type = LOG_TEXT; // type string
239 LIST_ADDQ(&curproxy->logformat, &node->list);
240 } else if (type == LOG_VARIABLE) { /* type variable */
241 parse_logformat_var(start, end - start, curproxy);
242 } else if (type == LOG_SEPARATOR) {
243 struct logformat_node *node = calloc(1, sizeof(struct logformat_node));
244 node->type = LOG_SEPARATOR;
245 LIST_ADDQ(&curproxy->logformat, &node->list);
246 }
247}
248
249/*
250 * Parse the log_format string and fill a linked list.
251 * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname
252 * You can set arguments using { } : %{many arguments}varname
253 */
254void parse_logformat_string(char *str, struct proxy *curproxy)
255{
256 char *sp = str; /* start pointer */
257 int cformat = -1; /* current token format : LOG_TEXT, LOG_SEPARATOR, LOG_VARIABLE */
258 int pformat = -1; /* previous token format */
259 struct logformat_node *tmplf, *back;
260
261 /* flush the list first. */
262 list_for_each_entry_safe(tmplf, back, &curproxy->logformat, list) {
263 LIST_DEL(&tmplf->list);
264 free(tmplf);
265 }
266
267 while (1) {
268
269 // push the variable only if formats are different, not
270 // within a variable, and not the first iteration
271 if ((cformat != pformat && cformat != -1 && pformat != -1) || *str == '\0') {
272 if (((pformat != LF_STARTVAR && cformat != LF_VAR) &&
273 (pformat != LF_STARTVAR && cformat != LF_STARG) &&
274 (pformat != LF_STARG && cformat != LF_VAR)) || *str == '\0') {
275 if (pformat > LF_VAR) // unfinished string
276 pformat = LF_TEXT;
277 add_to_logformat_list(sp, str, pformat, curproxy);
278 sp = str;
279 if (*str == '\0')
280 break;
281 }
282 }
283
284 if (cformat != -1)
285 str++; // consume the string, except on the first tour
286
287 pformat = cformat;
288
289 if (*str == '\0') {
290 cformat = LF_STARTVAR; // for breaking in all cases
291 continue;
292 }
293
294 if (pformat == LF_STARTVAR) { // after a %
295 if ( (*str >= 'a' && *str <= 'z') || // parse varname
296 (*str >= 'A' && *str <= 'Z') ||
297 (*str >= '0' && *str <= '9')) {
298 cformat = LF_VAR; // varname
299 continue;
300 } else if (*str == '{') {
301 cformat = LF_STARG; // variable arguments
302 continue;
303 } else { // another unexpected token
304 pformat = LF_TEXT; // redefine the format of the previous token to TEXT
305 cformat = LF_TEXT;
306 continue;
307 }
308
309 } else if (pformat == LF_VAR) { // after a varname
310 if ( (*str >= 'a' && *str <= 'z') || // parse varname
311 (*str >= 'A' && *str <= 'Z') ||
312 (*str >= '0' && *str <= '9')) {
313 cformat = LF_VAR;
314 continue;
315 }
316 } else if (pformat == LF_STARG) { // inside variable arguments
317 if (*str == '}') { // end of varname
318 cformat = LF_EDARG;
319 continue;
320 } else { // all tokens are acceptable within { }
321 cformat = LF_STARG;
322 continue;
323 }
324 } else if (pformat == LF_EDARG) { // after arguments
325 if ( (*str >= 'a' && *str <= 'z') || // parse a varname
326 (*str >= 'A' && *str <= 'Z') ||
327 (*str >= '0' && *str <= '9')) {
328 cformat = LF_VAR;
329 continue;
330 } else { // if no varname after arguments, transform in TEXT
331 pformat = LF_TEXT;
332 cformat = LF_TEXT;
333 }
334 }
335
336 // others tokens that don't match previous conditions
337 if (*str == '%') {
338 cformat = LF_STARTVAR;
339 } else if (*str == ' ') {
340 cformat = LF_SEPARATOR;
341 } else {
342 cformat = LF_TEXT;
343 }
344 }
345}
346
Willy Tarreaubaaee002006-06-26 02:48:02 +0200347/*
348 * Displays the message on stderr with the date and pid. Overrides the quiet
349 * mode during startup.
350 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200351void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352{
353 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200354 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200355
356 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
357 va_start(argp, fmt);
358
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200359 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200360 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200361 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200362 vfprintf(stderr, fmt, argp);
363 fflush(stderr);
364 va_end(argp);
365 }
366}
367
368
369/*
370 * Displays the message on stderr with the date and pid.
371 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200372void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200373{
374 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +0200375 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200376
377 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
378 va_start(argp, fmt);
379
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200380 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200382 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +0200383 vfprintf(stderr, fmt, argp);
384 fflush(stderr);
385 va_end(argp);
386 }
387}
388
389/*
390 * Displays the message on <out> only if quiet mode is not set.
391 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200392void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200393{
394 va_list argp;
395
396 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
397 va_start(argp, fmt);
398 vfprintf(out, fmt, argp);
399 fflush(out);
400 va_end(argp);
401 }
402}
403
404/*
405 * returns log level for <lev> or -1 if not found.
406 */
407int get_log_level(const char *lev)
408{
409 int level;
410
411 level = NB_LOG_LEVELS - 1;
412 while (level >= 0 && strcmp(log_levels[level], lev))
413 level--;
414
415 return level;
416}
417
418
419/*
420 * returns log facility for <fac> or -1 if not found.
421 */
422int get_log_facility(const char *fac)
423{
424 int facility;
425
426 facility = NB_LOG_FACILITIES - 1;
427 while (facility >= 0 && strcmp(log_facilities[facility], fac))
428 facility--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100429
Willy Tarreaubaaee002006-06-26 02:48:02 +0200430 return facility;
431}
432
William Lallemanda1cc3812012-02-08 16:38:44 +0100433/*
434 * Write a string in the log string
435 * Take cares of mandatory and quote options
436 *
437 * Return the adress of the \0 character, or NULL on error
438 */
439char *logformat_write_string(char *dst, char *src, size_t size, struct logformat_node *node)
440{
441 char *orig = dst;
442
443 if (src == NULL || *src == '\0') {
444 if (node->options & LOG_OPT_QUOTE) {
445 if (size > 2) {
446 *(dst++) = '"';
447 *(dst++) = '"';
448 *dst = '\0';
449 node->options |= LOG_OPT_WRITTEN;
450 } else {
451 dst = NULL;
452 return dst;
453 }
454 } else {
455 if (size > 1) {
456 *(dst++) = '-';
457 *dst = '\0';
458 node->options |= LOG_OPT_WRITTEN;
459 } else { // error no space available
460 dst = NULL;
461 return dst;
462 }
463 }
464 } else {
465 if (node->options & LOG_OPT_QUOTE) {
466 if (size-- > 1 ) {
467 *(dst++) = '"';
468 } else {
469 dst = NULL;
470 return NULL;
471 }
472 dst += strlcpy2(dst, src, size);
473 size -= orig - dst + 1;
474 if (size > 1) {
475 *(dst++) = '"';
476 *dst = '\0';
477 } else {
478 dst = NULL;
479 }
480 } else {
481 dst += strlcpy2(dst, src, size);
482 }
483 }
484 return dst;
485}
486
William Lallemand2a4a44f2012-02-06 16:00:33 +0100487/* generate the syslog header once a second */
488char *hdr_log(char *dst)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200489{
William Lallemand2a4a44f2012-02-06 16:00:33 +0100490 int hdr_len = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200491 static long tvsec = -1; /* to force the string to be initialized */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200492 static char *dataptr = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200493
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200494 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200495 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200496 struct tm tm;
497
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200498 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200499 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200500
William Lallemand2a4a44f2012-02-06 16:00:33 +0100501 hdr_len = snprintf(dst, MAX_SYSLOG_LEN,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100502 "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200503 monthname[tm.tm_mon],
504 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100505 global.log_send_hostname ? global.log_send_hostname : "",
Kevinm48936af2010-12-22 16:08:21 +0000506 global.log_tag, pid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200507 /* WARNING: depending upon implementations, snprintf may return
508 * either -1 or the number of bytes that would be needed to store
509 * the total message. In both cases, we must adjust it.
510 */
William Lallemand2a4a44f2012-02-06 16:00:33 +0100511 if (hdr_len < 0 || hdr_len > MAX_SYSLOG_LEN)
512 hdr_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200513
William Lallemand2a4a44f2012-02-06 16:00:33 +0100514 dataptr = dst + hdr_len;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200515 }
516
William Lallemand2a4a44f2012-02-06 16:00:33 +0100517 return dataptr;
518}
519
520/*
521 * This function adds a header to the message and sends the syslog message
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100522 * using a printf format string. It expects an LF-terminated message.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100523 */
524void send_log(struct proxy *p, int level, const char *format, ...)
525{
526 va_list argp;
527 static char logmsg[MAX_SYSLOG_LEN];
528 static char *dataptr = NULL;
529 int data_len = 0;
530
531 if (level < 0 || format == NULL)
532 return;
533
534 dataptr = hdr_log(logmsg); /* create header */
535 data_len = dataptr - logmsg;
536
537 va_start(argp, format);
538 data_len += vsnprintf(dataptr, MAX_SYSLOG_LEN, format, argp);
539 if (data_len < 0 || data_len > MAX_SYSLOG_LEN)
540 data_len = MAX_SYSLOG_LEN;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200541 va_end(argp);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100542
543 __send_log(p, level, logmsg, data_len);
544}
545
546/*
547 * This function sends a syslog message.
548 * It doesn't care about errors nor does it report them.
Willy Tarreau53bf6af2012-02-24 11:46:54 +0100549 * It overrides the last byte (message[size-1]) with an LF character.
William Lallemand2a4a44f2012-02-06 16:00:33 +0100550 */
551void __send_log(struct proxy *p, int level, char *message, size_t size)
552{
553 static int logfdunix = -1; /* syslog to AF_UNIX socket */
554 static int logfdinet = -1; /* syslog to AF_INET socket */
555 static char *dataptr = NULL;
556 int fac_level;
557 struct list *logsrvs = NULL;
558 struct logsrv *tmp = NULL;
559 int nblogger;
560 char *log_ptr;
561
562 dataptr = message;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200563
564 if (p == NULL) {
William Lallemand0f99e342011-10-12 17:50:54 +0200565 if (!LIST_ISEMPTY(&global.logsrvs)) {
566 logsrvs = &global.logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200567 }
568 } else {
William Lallemand0f99e342011-10-12 17:50:54 +0200569 if (!LIST_ISEMPTY(&p->logsrvs)) {
570 logsrvs = &p->logsrvs;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200571 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100572 }
573
William Lallemand0f99e342011-10-12 17:50:54 +0200574 if (!logsrvs)
575 return;
576
William Lallemand2a4a44f2012-02-06 16:00:33 +0100577 message[size - 1] = '\n';
578
Robert Tsai81ae1952007-12-05 10:47:29 +0100579 /* Lazily set up syslog sockets for protocol families of configured
580 * syslog servers. */
William Lallemand0f99e342011-10-12 17:50:54 +0200581 nblogger = 0;
582 list_for_each_entry(tmp, logsrvs, list) {
583 const struct logsrv *logsrv = tmp;
Robert Tsai81ae1952007-12-05 10:47:29 +0100584 int proto, *plogfd;
William Lallemand0f99e342011-10-12 17:50:54 +0200585
David du Colombier11bcb6c2011-03-24 12:23:00 +0100586 if (logsrv->addr.ss_family == AF_UNIX) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100587 proto = 0;
588 plogfd = &logfdunix;
589 } else {
Robert Tsai81ae1952007-12-05 10:47:29 +0100590 proto = IPPROTO_UDP;
591 plogfd = &logfdinet;
592 }
593 if (*plogfd >= 0) {
594 /* socket already created. */
595 continue;
596 }
David du Colombier11bcb6c2011-03-24 12:23:00 +0100597 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
Robert Tsai81ae1952007-12-05 10:47:29 +0100598 proto)) < 0) {
599 Alert("socket for logger #%d failed: %s (errno=%d)\n",
600 nblogger + 1, strerror(errno), errno);
601 return;
602 }
603 /* we don't want to receive anything on this socket */
604 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
605 /* does nothing under Linux, maybe needed for others */
606 shutdown(*plogfd, SHUT_RD);
William Lallemand0f99e342011-10-12 17:50:54 +0200607 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200608 }
609
Robert Tsai81ae1952007-12-05 10:47:29 +0100610 /* Send log messages to syslog server. */
William Lallemand0f99e342011-10-12 17:50:54 +0200611 nblogger = 0;
612 list_for_each_entry(tmp, logsrvs, list) {
613 const struct logsrv *logsrv = tmp;
David du Colombier11bcb6c2011-03-24 12:23:00 +0100614 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100615 &logfdunix : &logfdinet;
616 int sent;
617
Willy Tarreaubaaee002006-06-26 02:48:02 +0200618 /* we can filter the level of the messages that are sent to each logger */
William Lallemand0f99e342011-10-12 17:50:54 +0200619 if (level > logsrv->level)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200620 continue;
621
622 /* For each target, we may have a different facility.
623 * We can also have a different log level for each message.
624 * This induces variations in the message header length.
625 * Since we don't want to recompute it each time, nor copy it every
626 * time, we only change the facility in the pre-computed header,
627 * and we change the pointer to the header accordingly.
628 */
William Lallemand0f99e342011-10-12 17:50:54 +0200629 fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl);
William Lallemand2a4a44f2012-02-06 16:00:33 +0100630 log_ptr = dataptr + 3; /* last digit of the log level */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200631 do {
632 *log_ptr = '0' + fac_level % 10;
633 fac_level /= 10;
634 log_ptr--;
William Lallemand2a4a44f2012-02-06 16:00:33 +0100635 } while (fac_level && log_ptr > dataptr);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200636 *log_ptr = '<';
William Lallemand2a4a44f2012-02-06 16:00:33 +0100637
638 sent = sendto(*plogfd, log_ptr, size,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200639 MSG_DONTWAIT | MSG_NOSIGNAL,
640 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Robert Tsai81ae1952007-12-05 10:47:29 +0100641 if (sent < 0) {
642 Alert("sendto logger #%d failed: %s (errno=%d)\n",
643 nblogger, strerror(errno), errno);
644 }
William Lallemand0f99e342011-10-12 17:50:54 +0200645 nblogger++;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200646 }
647}
648
649
650/*
651 * send a log for the session when we have enough info about it
652 */
Willy Tarreau42250582007-04-01 01:30:43 +0200653void tcp_sess_log(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200654{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100655 char pn[INET6_ADDRSTRLEN];
Willy Tarreau73de9892006-11-30 11:40:23 +0100656 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100657 struct proxy *be = s->be;
658 struct proxy *prx_log;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200659 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200660 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200661 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200662
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200663 /* if we don't want to log normal traffic, return now */
Willy Tarreauee28de02010-06-01 09:51:00 +0200664 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || (s->req->cons->conn_retries != be->conn_retries);
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200665 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
666 return;
667
Willy Tarreau6471afb2011-09-23 10:54:59 +0200668 addr_to_str(&s->si[0].addr.from, pn, sizeof(pn));
Willy Tarreaufe944602007-10-25 10:34:16 +0200669 get_localtime(s->logs.tv_accept.tv_sec, &tm);
Willy Tarreauddb358d2006-12-17 22:55:52 +0100670
William Lallemand0f99e342011-10-12 17:50:54 +0200671 if(LIST_ISEMPTY(&fe->logsrvs))
Willy Tarreaue7ded1f2009-08-09 10:11:45 +0200672 return;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100673
Willy Tarreaue7ded1f2009-08-09 10:11:45 +0200674 prx_log = fe;
Willy Tarreau42250582007-04-01 01:30:43 +0200675 tolog = fe->to_log;
Willy Tarreau71904a42011-02-13 14:30:26 +0100676
677 if (!(tolog & LW_SVID))
678 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200679 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100680 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200681 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100682 break;
683 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200684 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100685 break;
686 default:
687 svid = "<NOSRV>";
688 break;
689 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200690
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200691 level = LOG_INFO;
692 if (err && (fe->options2 & PR_O2_LOGERRORS))
693 level = LOG_ERR;
694
695 send_log(prx_log, level, "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +0200696 " %s %s/%s %ld/%ld/%s%ld %s%lld"
697 " %c%c %d/%d/%d/%d/%s%u %ld/%ld\n",
Willy Tarreau6471afb2011-09-23 10:54:59 +0200698 s->si[0].addr.from.ss_family == AF_UNIX ? "unix" : pn,
699 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 +0200700 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +0200701 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.tv_accept.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200702 fe->id, be->id, svid,
703 (s->logs.t_queue >= 0) ? s->logs.t_queue : -1,
704 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
705 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100706 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200707 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
708 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
Willy Tarreau827aee92011-03-10 16:55:02 +0100709 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100710 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +0200711 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200712 s->logs.srv_queue_size, s->logs.prx_queue_size);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200713
714 s->logs.logwait = 0;
715}
716
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717
718/*
719 * Local variables:
720 * c-indent-level: 8
721 * c-basic-offset: 8
722 * End:
723 */