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> |
Willy Tarreau | ec6c5df | 2008-07-15 00:22:45 +0200 | [diff] [blame] | 31 | |
| 32 | #include <proto/log.h> |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 33 | #include <proto/stream_interface.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 34 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | const char *log_facilities[NB_LOG_FACILITIES] = { |
| 36 | "kern", "user", "mail", "daemon", |
| 37 | "auth", "syslog", "lpr", "news", |
| 38 | "uucp", "cron", "auth2", "ftp", |
| 39 | "ntp", "audit", "alert", "cron2", |
| 40 | "local0", "local1", "local2", "local3", |
| 41 | "local4", "local5", "local6", "local7" |
| 42 | }; |
| 43 | |
| 44 | |
| 45 | const char *log_levels[NB_LOG_LEVELS] = { |
| 46 | "emerg", "alert", "crit", "err", |
| 47 | "warning", "notice", "info", "debug" |
| 48 | }; |
| 49 | |
| 50 | const char *monthname[12] = { |
| 51 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 52 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 53 | }; |
| 54 | |
Simon Horman | 752dc4a | 2011-06-21 14:34:59 +0900 | [diff] [blame] | 55 | const char sess_term_cond[9] = "-cCsSPRID"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down */ |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 56 | 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] | 57 | |
| 58 | /* |
| 59 | * Displays the message on stderr with the date and pid. Overrides the quiet |
| 60 | * mode during startup. |
| 61 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 62 | void Alert(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | { |
| 64 | va_list argp; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 65 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 66 | |
| 67 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
| 68 | va_start(argp, fmt); |
| 69 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 70 | get_localtime(date.tv_sec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 71 | fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 72 | 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] | 73 | vfprintf(stderr, fmt, argp); |
| 74 | fflush(stderr); |
| 75 | va_end(argp); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /* |
| 81 | * Displays the message on stderr with the date and pid. |
| 82 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 83 | void Warning(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | { |
| 85 | va_list argp; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 86 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 87 | |
| 88 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 89 | va_start(argp, fmt); |
| 90 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 91 | get_localtime(date.tv_sec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 92 | fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 93 | 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] | 94 | vfprintf(stderr, fmt, argp); |
| 95 | fflush(stderr); |
| 96 | va_end(argp); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Displays the message on <out> only if quiet mode is not set. |
| 102 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 103 | void qfprintf(FILE *out, const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | { |
| 105 | va_list argp; |
| 106 | |
| 107 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 108 | va_start(argp, fmt); |
| 109 | vfprintf(out, fmt, argp); |
| 110 | fflush(out); |
| 111 | va_end(argp); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * returns log level for <lev> or -1 if not found. |
| 117 | */ |
| 118 | int get_log_level(const char *lev) |
| 119 | { |
| 120 | int level; |
| 121 | |
| 122 | level = NB_LOG_LEVELS - 1; |
| 123 | while (level >= 0 && strcmp(log_levels[level], lev)) |
| 124 | level--; |
| 125 | |
| 126 | return level; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /* |
| 131 | * returns log facility for <fac> or -1 if not found. |
| 132 | */ |
| 133 | int get_log_facility(const char *fac) |
| 134 | { |
| 135 | int facility; |
| 136 | |
| 137 | facility = NB_LOG_FACILITIES - 1; |
| 138 | while (facility >= 0 && strcmp(log_facilities[facility], fac)) |
| 139 | facility--; |
| 140 | |
| 141 | return facility; |
| 142 | } |
| 143 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 144 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 145 | * This function sends a syslog message to both log servers of a proxy, |
| 146 | * or to global log servers if the proxy is NULL. |
| 147 | * It also tries not to waste too much time computing the message header. |
| 148 | * It doesn't care about errors nor does it report them. |
| 149 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 150 | void send_log(struct proxy *p, int level, const char *message, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 151 | { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 152 | static int logfdunix = -1; /* syslog to AF_UNIX socket */ |
| 153 | static int logfdinet = -1; /* syslog to AF_INET socket */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 154 | static long tvsec = -1; /* to force the string to be initialized */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 155 | va_list argp; |
| 156 | static char logmsg[MAX_SYSLOG_LEN]; |
| 157 | static char *dataptr = NULL; |
| 158 | int fac_level; |
| 159 | int hdr_len, data_len; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 160 | struct logsrv *logsrvs[2]; |
Willy Tarreau | f7edefa | 2009-05-10 17:20:05 +0200 | [diff] [blame] | 161 | int facilities[2], loglevel[2], minlvl[2]; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 162 | int nblogger; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 163 | int nbloggers = 0; |
| 164 | char *log_ptr; |
| 165 | |
Kevinm | 48936af | 2010-12-22 16:08:21 +0000 | [diff] [blame] | 166 | if (level < 0 || message == NULL) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 167 | return; |
| 168 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 169 | if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 170 | /* this string is rebuild only once a second */ |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 171 | struct tm tm; |
| 172 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 173 | tvsec = date.tv_sec; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 174 | get_localtime(tvsec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 175 | |
| 176 | hdr_len = snprintf(logmsg, sizeof(logmsg), |
Joe Williams | df5b38f | 2010-12-29 17:05:48 +0100 | [diff] [blame] | 177 | "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 178 | monthname[tm.tm_mon], |
| 179 | tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Joe Williams | df5b38f | 2010-12-29 17:05:48 +0100 | [diff] [blame] | 180 | global.log_send_hostname ? global.log_send_hostname : "", |
Kevinm | 48936af | 2010-12-22 16:08:21 +0000 | [diff] [blame] | 181 | global.log_tag, pid); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 182 | /* WARNING: depending upon implementations, snprintf may return |
| 183 | * either -1 or the number of bytes that would be needed to store |
| 184 | * the total message. In both cases, we must adjust it. |
| 185 | */ |
| 186 | if (hdr_len < 0 || hdr_len > sizeof(logmsg)) |
| 187 | hdr_len = sizeof(logmsg); |
| 188 | |
| 189 | dataptr = logmsg + hdr_len; |
| 190 | } |
| 191 | |
| 192 | va_start(argp, message); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 193 | /* |
| 194 | * FIXME: we take a huge performance hit here. We might have to replace |
| 195 | * vsnprintf() for a hard-coded log writer. |
| 196 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 197 | data_len = vsnprintf(dataptr, logmsg + sizeof(logmsg) - dataptr, message, argp); |
| 198 | if (data_len < 0 || data_len > (logmsg + sizeof(logmsg) - dataptr)) |
| 199 | data_len = logmsg + sizeof(logmsg) - dataptr; |
| 200 | va_end(argp); |
| 201 | dataptr[data_len - 1] = '\n'; /* force a break on ultra-long lines */ |
| 202 | |
| 203 | if (p == NULL) { |
| 204 | if (global.logfac1 >= 0) { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 205 | logsrvs[nbloggers] = &global.logsrv1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 206 | facilities[nbloggers] = global.logfac1; |
| 207 | loglevel[nbloggers] = global.loglev1; |
Willy Tarreau | f7edefa | 2009-05-10 17:20:05 +0200 | [diff] [blame] | 208 | minlvl[nbloggers] = global.minlvl1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 209 | nbloggers++; |
| 210 | } |
| 211 | if (global.logfac2 >= 0) { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 212 | logsrvs[nbloggers] = &global.logsrv2; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 213 | facilities[nbloggers] = global.logfac2; |
| 214 | loglevel[nbloggers] = global.loglev2; |
Willy Tarreau | f7edefa | 2009-05-10 17:20:05 +0200 | [diff] [blame] | 215 | minlvl[nbloggers] = global.minlvl2; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 216 | nbloggers++; |
| 217 | } |
| 218 | } else { |
| 219 | if (p->logfac1 >= 0) { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 220 | logsrvs[nbloggers] = &p->logsrv1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 221 | facilities[nbloggers] = p->logfac1; |
| 222 | loglevel[nbloggers] = p->loglev1; |
Willy Tarreau | f7edefa | 2009-05-10 17:20:05 +0200 | [diff] [blame] | 223 | minlvl[nbloggers] = p->minlvl1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 224 | nbloggers++; |
| 225 | } |
| 226 | if (p->logfac2 >= 0) { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 227 | logsrvs[nbloggers] = &p->logsrv2; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 228 | facilities[nbloggers] = p->logfac2; |
| 229 | loglevel[nbloggers] = p->loglev2; |
Willy Tarreau | f7edefa | 2009-05-10 17:20:05 +0200 | [diff] [blame] | 230 | minlvl[nbloggers] = p->minlvl2; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 231 | nbloggers++; |
| 232 | } |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /* Lazily set up syslog sockets for protocol families of configured |
| 236 | * syslog servers. */ |
| 237 | for (nblogger = 0; nblogger < nbloggers; nblogger++) { |
| 238 | const struct logsrv *logsrv = logsrvs[nblogger]; |
| 239 | int proto, *plogfd; |
David du Colombier | 11bcb6c | 2011-03-24 12:23:00 +0100 | [diff] [blame] | 240 | if (logsrv->addr.ss_family == AF_UNIX) { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 241 | proto = 0; |
| 242 | plogfd = &logfdunix; |
| 243 | } else { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 244 | proto = IPPROTO_UDP; |
| 245 | plogfd = &logfdinet; |
| 246 | } |
| 247 | if (*plogfd >= 0) { |
| 248 | /* socket already created. */ |
| 249 | continue; |
| 250 | } |
David du Colombier | 11bcb6c | 2011-03-24 12:23:00 +0100 | [diff] [blame] | 251 | if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM, |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 252 | proto)) < 0) { |
| 253 | Alert("socket for logger #%d failed: %s (errno=%d)\n", |
| 254 | nblogger + 1, strerror(errno), errno); |
| 255 | return; |
| 256 | } |
| 257 | /* we don't want to receive anything on this socket */ |
| 258 | setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero)); |
| 259 | /* does nothing under Linux, maybe needed for others */ |
| 260 | shutdown(*plogfd, SHUT_RD); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 263 | /* Send log messages to syslog server. */ |
| 264 | for (nblogger = 0; nblogger < nbloggers; nblogger++) { |
| 265 | const struct logsrv *logsrv = logsrvs[nblogger]; |
David du Colombier | 11bcb6c | 2011-03-24 12:23:00 +0100 | [diff] [blame] | 266 | int *plogfd = logsrv->addr.ss_family == AF_UNIX ? |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 267 | &logfdunix : &logfdinet; |
| 268 | int sent; |
| 269 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 270 | /* we can filter the level of the messages that are sent to each logger */ |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 271 | if (level > loglevel[nblogger]) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 272 | continue; |
| 273 | |
| 274 | /* For each target, we may have a different facility. |
| 275 | * We can also have a different log level for each message. |
| 276 | * This induces variations in the message header length. |
| 277 | * Since we don't want to recompute it each time, nor copy it every |
| 278 | * time, we only change the facility in the pre-computed header, |
| 279 | * and we change the pointer to the header accordingly. |
| 280 | */ |
Willy Tarreau | f7edefa | 2009-05-10 17:20:05 +0200 | [diff] [blame] | 281 | fac_level = (facilities[nblogger] << 3) + MAX(level, minlvl[nblogger]); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 282 | log_ptr = logmsg + 3; /* last digit of the log level */ |
| 283 | do { |
| 284 | *log_ptr = '0' + fac_level % 10; |
| 285 | fac_level /= 10; |
| 286 | log_ptr--; |
| 287 | } while (fac_level && log_ptr > logmsg); |
| 288 | *log_ptr = '<'; |
| 289 | |
| 290 | /* the total syslog message now starts at logptr, for dataptr+data_len-logptr */ |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 291 | sent = sendto(*plogfd, log_ptr, dataptr + data_len - log_ptr, |
Willy Tarreau | 1b4b7ce | 2011-04-05 16:56:50 +0200 | [diff] [blame] | 292 | MSG_DONTWAIT | MSG_NOSIGNAL, |
| 293 | (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr)); |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 294 | if (sent < 0) { |
| 295 | Alert("sendto logger #%d failed: %s (errno=%d)\n", |
| 296 | nblogger, strerror(errno), errno); |
| 297 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | |
| 301 | |
| 302 | /* |
| 303 | * send a log for the session when we have enough info about it |
| 304 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 305 | void tcp_sess_log(struct session *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 306 | { |
Cyril Bonté | acd7d63 | 2010-11-01 19:26:02 +0100 | [diff] [blame] | 307 | char pn[INET6_ADDRSTRLEN]; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 308 | struct proxy *fe = s->fe; |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 309 | struct proxy *be = s->be; |
| 310 | struct proxy *prx_log; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 311 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 312 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 313 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 314 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 315 | /* if we don't want to log normal traffic, return now */ |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 316 | 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] | 317 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 318 | return; |
| 319 | |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame^] | 320 | addr_to_str(&s->si[0].addr.c.from, pn, sizeof(pn)); |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 321 | get_localtime(s->logs.tv_accept.tv_sec, &tm); |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 322 | |
Willy Tarreau | e7ded1f | 2009-08-09 10:11:45 +0200 | [diff] [blame] | 323 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 324 | return; |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 325 | |
Willy Tarreau | e7ded1f | 2009-08-09 10:11:45 +0200 | [diff] [blame] | 326 | prx_log = fe; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 327 | tolog = fe->to_log; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 328 | |
| 329 | if (!(tolog & LW_SVID)) |
| 330 | svid = "-"; |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 331 | else switch (s->target.type) { |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 332 | case TARG_TYPE_SERVER: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 333 | svid = s->target.ptr.s->id; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 334 | break; |
| 335 | case TARG_TYPE_APPLET: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 336 | svid = s->target.ptr.a->name; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 337 | break; |
| 338 | default: |
| 339 | svid = "<NOSRV>"; |
| 340 | break; |
| 341 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 342 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 343 | level = LOG_INFO; |
| 344 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 345 | level = LOG_ERR; |
| 346 | |
| 347 | 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] | 348 | " %s %s/%s %ld/%ld/%s%ld %s%lld" |
| 349 | " %c%c %d/%d/%d/%d/%s%u %ld/%ld\n", |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 350 | s->si[0].addr.c.from.ss_family == AF_UNIX ? "unix" : pn, |
Willy Tarreau | 86ad42c | 2011-08-27 12:29:07 +0200 | [diff] [blame] | 351 | s->si[0].addr.c.from.ss_family == AF_UNIX ? s->listener->luid : get_host_port(&s->si[0].addr.c.from), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 352 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 353 | 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] | 354 | fe->id, be->id, svid, |
| 355 | (s->logs.t_queue >= 0) ? s->logs.t_queue : -1, |
| 356 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 357 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 358 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 359 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 360 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 361 | 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] | 362 | (s->flags & SN_REDISP)?"+":"", |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 363 | (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] | 364 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 365 | |
| 366 | s->logs.logwait = 0; |
| 367 | } |
| 368 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 369 | |
| 370 | /* |
| 371 | * Local variables: |
| 372 | * c-indent-level: 8 |
| 373 | * c-basic-offset: 8 |
| 374 | * End: |
| 375 | */ |