Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * General logging functions. |
| 3 | * |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 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 Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 13 | #include <fcntl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 14 | #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 Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 21 | #include <errno.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 22 | |
| 23 | #include <sys/time.h> |
| 24 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 25 | #include <common/config.h> |
Willy Tarreau | d6d0690 | 2009-08-19 11:22:33 +0200 | [diff] [blame] | 26 | #include <common/compat.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 27 | #include <common/standard.h> |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 28 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | #include <types/global.h> |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 31 | #include <types/log.h> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 32 | |
| 33 | #include <proto/log.h> |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 34 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | const 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 | |
| 46 | const char *log_levels[NB_LOG_LEVELS] = { |
| 47 | "emerg", "alert", "crit", "err", |
| 48 | "warning", "notice", "info", "debug" |
| 49 | }; |
| 50 | |
Willy Tarreau | a2a64e9 | 2011-09-07 23:01:56 +0200 | [diff] [blame] | 51 | const char sess_term_cond[10] = "-cCsSPRIDK"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down, Killed */ |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 52 | const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 54 | |
| 55 | /* log_format */ |
| 56 | struct logformat_type { |
| 57 | char *name; |
| 58 | int type; |
| 59 | }; |
| 60 | |
| 61 | /* log_format variable names */ |
| 62 | static 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 | |
| 97 | char 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 |
| 98 | char 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"; |
| 99 | char *log_format = NULL; |
| 100 | |
| 101 | struct logformat_var_args { |
| 102 | char *name; |
| 103 | int mask; |
| 104 | }; |
| 105 | |
| 106 | struct 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 | */ |
| 116 | int 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 | */ |
| 167 | int 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 | */ |
| 226 | void 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 | */ |
| 254 | void 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 347 | /* |
| 348 | * Displays the message on stderr with the date and pid. Overrides the quiet |
| 349 | * mode during startup. |
| 350 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 351 | void Alert(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 352 | { |
| 353 | va_list argp; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 354 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 355 | |
| 356 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
| 357 | va_start(argp, fmt); |
| 358 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 359 | get_localtime(date.tv_sec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 360 | fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 361 | tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid()); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 362 | 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 Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 372 | void Warning(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 373 | { |
| 374 | va_list argp; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 375 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 376 | |
| 377 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 378 | va_start(argp, fmt); |
| 379 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 380 | get_localtime(date.tv_sec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 381 | fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 382 | tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid()); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 383 | 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 Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 392 | void qfprintf(FILE *out, const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 393 | { |
| 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 | */ |
| 407 | int 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 | */ |
| 422 | int 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 Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 429 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 430 | return facility; |
| 431 | } |
| 432 | |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 433 | /* |
| 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 | */ |
| 439 | char *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 Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 487 | /* generate the syslog header once a second */ |
| 488 | char *hdr_log(char *dst) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 489 | { |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 490 | int hdr_len = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 491 | static long tvsec = -1; /* to force the string to be initialized */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 492 | static char *dataptr = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 493 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 494 | if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 495 | /* this string is rebuild only once a second */ |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 496 | struct tm tm; |
| 497 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 498 | tvsec = date.tv_sec; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 499 | get_localtime(tvsec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 500 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 501 | hdr_len = snprintf(dst, MAX_SYSLOG_LEN, |
Joe Williams | df5b38f | 2010-12-29 17:05:48 +0100 | [diff] [blame] | 502 | "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 503 | monthname[tm.tm_mon], |
| 504 | tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Joe Williams | df5b38f | 2010-12-29 17:05:48 +0100 | [diff] [blame] | 505 | global.log_send_hostname ? global.log_send_hostname : "", |
Kevinm | 48936af | 2010-12-22 16:08:21 +0000 | [diff] [blame] | 506 | global.log_tag, pid); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 507 | /* 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 Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 511 | if (hdr_len < 0 || hdr_len > MAX_SYSLOG_LEN) |
| 512 | hdr_len = MAX_SYSLOG_LEN; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 513 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 514 | dataptr = dst + hdr_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 515 | } |
| 516 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 517 | return dataptr; |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * This function adds a header to the message and sends the syslog message |
Willy Tarreau | 53bf6af | 2012-02-24 11:46:54 +0100 | [diff] [blame] | 522 | * using a printf format string. It expects an LF-terminated message. |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 523 | */ |
| 524 | void 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 541 | va_end(argp); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 542 | |
| 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 Tarreau | 53bf6af | 2012-02-24 11:46:54 +0100 | [diff] [blame] | 549 | * It overrides the last byte (message[size-1]) with an LF character. |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 550 | */ |
| 551 | void __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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 563 | |
| 564 | if (p == NULL) { |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 565 | if (!LIST_ISEMPTY(&global.logsrvs)) { |
| 566 | logsrvs = &global.logsrvs; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 567 | } |
| 568 | } else { |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 569 | if (!LIST_ISEMPTY(&p->logsrvs)) { |
| 570 | logsrvs = &p->logsrvs; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 571 | } |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 572 | } |
| 573 | |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 574 | if (!logsrvs) |
| 575 | return; |
| 576 | |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 577 | message[size - 1] = '\n'; |
| 578 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 579 | /* Lazily set up syslog sockets for protocol families of configured |
| 580 | * syslog servers. */ |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 581 | nblogger = 0; |
| 582 | list_for_each_entry(tmp, logsrvs, list) { |
| 583 | const struct logsrv *logsrv = tmp; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 584 | int proto, *plogfd; |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 585 | |
David du Colombier | 11bcb6c | 2011-03-24 12:23:00 +0100 | [diff] [blame] | 586 | if (logsrv->addr.ss_family == AF_UNIX) { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 587 | proto = 0; |
| 588 | plogfd = &logfdunix; |
| 589 | } else { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 590 | proto = IPPROTO_UDP; |
| 591 | plogfd = &logfdinet; |
| 592 | } |
| 593 | if (*plogfd >= 0) { |
| 594 | /* socket already created. */ |
| 595 | continue; |
| 596 | } |
David du Colombier | 11bcb6c | 2011-03-24 12:23:00 +0100 | [diff] [blame] | 597 | if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 598 | 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 Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 607 | nblogger++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 608 | } |
| 609 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 610 | /* Send log messages to syslog server. */ |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 611 | nblogger = 0; |
| 612 | list_for_each_entry(tmp, logsrvs, list) { |
| 613 | const struct logsrv *logsrv = tmp; |
David du Colombier | 11bcb6c | 2011-03-24 12:23:00 +0100 | [diff] [blame] | 614 | int *plogfd = logsrv->addr.ss_family == AF_UNIX ? |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 615 | &logfdunix : &logfdinet; |
| 616 | int sent; |
| 617 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 618 | /* we can filter the level of the messages that are sent to each logger */ |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 619 | if (level > logsrv->level) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 620 | 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 Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 629 | fac_level = (logsrv->facility << 3) + MAX(level, logsrv->minlvl); |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 630 | log_ptr = dataptr + 3; /* last digit of the log level */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 631 | do { |
| 632 | *log_ptr = '0' + fac_level % 10; |
| 633 | fac_level /= 10; |
| 634 | log_ptr--; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 635 | } while (fac_level && log_ptr > dataptr); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 636 | *log_ptr = '<'; |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 637 | |
| 638 | sent = sendto(*plogfd, log_ptr, size, |
Willy Tarreau | 1b4b7ce | 2011-04-05 16:56:50 +0200 | [diff] [blame] | 639 | MSG_DONTWAIT | MSG_NOSIGNAL, |
| 640 | (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr)); |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 641 | if (sent < 0) { |
| 642 | Alert("sendto logger #%d failed: %s (errno=%d)\n", |
| 643 | nblogger, strerror(errno), errno); |
| 644 | } |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 645 | nblogger++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
| 649 | |
| 650 | /* |
| 651 | * send a log for the session when we have enough info about it |
| 652 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 653 | void tcp_sess_log(struct session *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 654 | { |
Cyril Bonté | acd7d63 | 2010-11-01 19:26:02 +0100 | [diff] [blame] | 655 | char pn[INET6_ADDRSTRLEN]; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 656 | struct proxy *fe = s->fe; |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 657 | struct proxy *be = s->be; |
| 658 | struct proxy *prx_log; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 659 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 660 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 661 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 662 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 663 | /* if we don't want to log normal traffic, return now */ |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 664 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || (s->req->cons->conn_retries != be->conn_retries); |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 665 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 666 | return; |
| 667 | |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 668 | addr_to_str(&s->si[0].addr.from, pn, sizeof(pn)); |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 669 | get_localtime(s->logs.tv_accept.tv_sec, &tm); |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 670 | |
William Lallemand | 0f99e34 | 2011-10-12 17:50:54 +0200 | [diff] [blame] | 671 | if(LIST_ISEMPTY(&fe->logsrvs)) |
Willy Tarreau | e7ded1f | 2009-08-09 10:11:45 +0200 | [diff] [blame] | 672 | return; |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 673 | |
Willy Tarreau | e7ded1f | 2009-08-09 10:11:45 +0200 | [diff] [blame] | 674 | prx_log = fe; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 675 | tolog = fe->to_log; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 676 | |
| 677 | if (!(tolog & LW_SVID)) |
| 678 | svid = "-"; |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 679 | else switch (s->target.type) { |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 680 | case TARG_TYPE_SERVER: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 681 | svid = s->target.ptr.s->id; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 682 | break; |
| 683 | case TARG_TYPE_APPLET: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 684 | svid = s->target.ptr.a->name; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 685 | break; |
| 686 | default: |
| 687 | svid = "<NOSRV>"; |
| 688 | break; |
| 689 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 690 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 691 | 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 Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 696 | " %s %s/%s %ld/%ld/%s%ld %s%lld" |
| 697 | " %c%c %d/%d/%d/%d/%s%u %ld/%ld\n", |
Willy Tarreau | 6471afb | 2011-09-23 10:54:59 +0200 | [diff] [blame] | 698 | 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 Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 700 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 701 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.tv_accept.tv_usec/1000, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 702 | 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 Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 706 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 707 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 708 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 709 | actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0, |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 710 | (s->flags & SN_REDISP)?"+":"", |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 711 | (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 712 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 713 | |
| 714 | s->logs.logwait = 0; |
| 715 | } |
| 716 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 717 | |
| 718 | /* |
| 719 | * Local variables: |
| 720 | * c-indent-level: 8 |
| 721 | * c-basic-offset: 8 |
| 722 | * End: |
| 723 | */ |