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 | |
| 55 | const char sess_term_cond[8] = "-cCsSPRI"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal */ |
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 | /* |
| 145 | * Return the length of the address endpoint, suitable for use with sendto(). |
| 146 | */ |
Willy Tarreau | 019767b | 2007-12-05 11:11:55 +0100 | [diff] [blame] | 147 | static inline int logsrv_addrlen(const struct logsrv *logsrv) |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 148 | { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 149 | switch (logsrv->u.addr.sa_family) { |
| 150 | case AF_UNIX: |
| 151 | return sizeof(logsrv->u.un); |
| 152 | case AF_INET: |
| 153 | return sizeof(logsrv->u.in); |
| 154 | default: |
| 155 | break; |
| 156 | } |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 157 | return -1; |
| 158 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 159 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 160 | /* |
| 161 | * This function sends a syslog message to both log servers of a proxy, |
| 162 | * or to global log servers if the proxy is NULL. |
| 163 | * It also tries not to waste too much time computing the message header. |
| 164 | * It doesn't care about errors nor does it report them. |
| 165 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 166 | void send_log(struct proxy *p, int level, const char *message, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 167 | { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 168 | static int logfdunix = -1; /* syslog to AF_UNIX socket */ |
| 169 | static int logfdinet = -1; /* syslog to AF_INET socket */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 170 | static long tvsec = -1; /* to force the string to be initialized */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 171 | va_list argp; |
| 172 | static char logmsg[MAX_SYSLOG_LEN]; |
| 173 | static char *dataptr = NULL; |
| 174 | int fac_level; |
| 175 | int hdr_len, data_len; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 176 | struct logsrv *logsrvs[2]; |
Willy Tarreau | f7edefa | 2009-05-10 17:20:05 +0200 | [diff] [blame] | 177 | int facilities[2], loglevel[2], minlvl[2]; |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 178 | int nblogger; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 179 | int nbloggers = 0; |
| 180 | char *log_ptr; |
| 181 | |
Kevinm | 48936af | 2010-12-22 16:08:21 +0000 | [diff] [blame] | 182 | if (level < 0 || message == NULL) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 183 | return; |
| 184 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 185 | if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 186 | /* this string is rebuild only once a second */ |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 187 | struct tm tm; |
| 188 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 189 | tvsec = date.tv_sec; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 190 | get_localtime(tvsec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 191 | |
| 192 | hdr_len = snprintf(logmsg, sizeof(logmsg), |
Joe Williams | df5b38f | 2010-12-29 17:05:48 +0100 | [diff] [blame] | 193 | "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 194 | monthname[tm.tm_mon], |
| 195 | tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Joe Williams | df5b38f | 2010-12-29 17:05:48 +0100 | [diff] [blame] | 196 | global.log_send_hostname ? global.log_send_hostname : "", |
Kevinm | 48936af | 2010-12-22 16:08:21 +0000 | [diff] [blame] | 197 | global.log_tag, pid); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 198 | /* WARNING: depending upon implementations, snprintf may return |
| 199 | * either -1 or the number of bytes that would be needed to store |
| 200 | * the total message. In both cases, we must adjust it. |
| 201 | */ |
| 202 | if (hdr_len < 0 || hdr_len > sizeof(logmsg)) |
| 203 | hdr_len = sizeof(logmsg); |
| 204 | |
| 205 | dataptr = logmsg + hdr_len; |
| 206 | } |
| 207 | |
| 208 | va_start(argp, message); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 209 | /* |
| 210 | * FIXME: we take a huge performance hit here. We might have to replace |
| 211 | * vsnprintf() for a hard-coded log writer. |
| 212 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 213 | data_len = vsnprintf(dataptr, logmsg + sizeof(logmsg) - dataptr, message, argp); |
| 214 | if (data_len < 0 || data_len > (logmsg + sizeof(logmsg) - dataptr)) |
| 215 | data_len = logmsg + sizeof(logmsg) - dataptr; |
| 216 | va_end(argp); |
| 217 | dataptr[data_len - 1] = '\n'; /* force a break on ultra-long lines */ |
| 218 | |
| 219 | if (p == NULL) { |
| 220 | if (global.logfac1 >= 0) { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 221 | logsrvs[nbloggers] = &global.logsrv1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 222 | facilities[nbloggers] = global.logfac1; |
| 223 | loglevel[nbloggers] = global.loglev1; |
Willy Tarreau | f7edefa | 2009-05-10 17:20:05 +0200 | [diff] [blame] | 224 | minlvl[nbloggers] = global.minlvl1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 225 | nbloggers++; |
| 226 | } |
| 227 | if (global.logfac2 >= 0) { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 228 | logsrvs[nbloggers] = &global.logsrv2; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 229 | facilities[nbloggers] = global.logfac2; |
| 230 | loglevel[nbloggers] = global.loglev2; |
Willy Tarreau | f7edefa | 2009-05-10 17:20:05 +0200 | [diff] [blame] | 231 | minlvl[nbloggers] = global.minlvl2; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 232 | nbloggers++; |
| 233 | } |
| 234 | } else { |
| 235 | if (p->logfac1 >= 0) { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 236 | logsrvs[nbloggers] = &p->logsrv1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 237 | facilities[nbloggers] = p->logfac1; |
| 238 | loglevel[nbloggers] = p->loglev1; |
Willy Tarreau | f7edefa | 2009-05-10 17:20:05 +0200 | [diff] [blame] | 239 | minlvl[nbloggers] = p->minlvl1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 240 | nbloggers++; |
| 241 | } |
| 242 | if (p->logfac2 >= 0) { |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 243 | logsrvs[nbloggers] = &p->logsrv2; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 244 | facilities[nbloggers] = p->logfac2; |
| 245 | loglevel[nbloggers] = p->loglev2; |
Willy Tarreau | f7edefa | 2009-05-10 17:20:05 +0200 | [diff] [blame] | 246 | minlvl[nbloggers] = p->minlvl2; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 247 | nbloggers++; |
| 248 | } |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* Lazily set up syslog sockets for protocol families of configured |
| 252 | * syslog servers. */ |
| 253 | for (nblogger = 0; nblogger < nbloggers; nblogger++) { |
| 254 | const struct logsrv *logsrv = logsrvs[nblogger]; |
| 255 | int proto, *plogfd; |
| 256 | if (logsrv->u.addr.sa_family == AF_UNIX) { |
| 257 | proto = 0; |
| 258 | plogfd = &logfdunix; |
| 259 | } else { |
| 260 | /* sa_family == AF_INET */ |
| 261 | proto = IPPROTO_UDP; |
| 262 | plogfd = &logfdinet; |
| 263 | } |
| 264 | if (*plogfd >= 0) { |
| 265 | /* socket already created. */ |
| 266 | continue; |
| 267 | } |
| 268 | if ((*plogfd = socket(logsrv->u.addr.sa_family, SOCK_DGRAM, |
| 269 | proto)) < 0) { |
| 270 | Alert("socket for logger #%d failed: %s (errno=%d)\n", |
| 271 | nblogger + 1, strerror(errno), errno); |
| 272 | return; |
| 273 | } |
| 274 | /* we don't want to receive anything on this socket */ |
| 275 | setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero)); |
| 276 | /* does nothing under Linux, maybe needed for others */ |
| 277 | shutdown(*plogfd, SHUT_RD); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 280 | /* Send log messages to syslog server. */ |
| 281 | for (nblogger = 0; nblogger < nbloggers; nblogger++) { |
| 282 | const struct logsrv *logsrv = logsrvs[nblogger]; |
| 283 | int *plogfd = logsrv->u.addr.sa_family == AF_UNIX ? |
| 284 | &logfdunix : &logfdinet; |
| 285 | int sent; |
| 286 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 287 | /* 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] | 288 | if (level > loglevel[nblogger]) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 289 | continue; |
| 290 | |
| 291 | /* For each target, we may have a different facility. |
| 292 | * We can also have a different log level for each message. |
| 293 | * This induces variations in the message header length. |
| 294 | * Since we don't want to recompute it each time, nor copy it every |
| 295 | * time, we only change the facility in the pre-computed header, |
| 296 | * and we change the pointer to the header accordingly. |
| 297 | */ |
Willy Tarreau | f7edefa | 2009-05-10 17:20:05 +0200 | [diff] [blame] | 298 | fac_level = (facilities[nblogger] << 3) + MAX(level, minlvl[nblogger]); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 299 | log_ptr = logmsg + 3; /* last digit of the log level */ |
| 300 | do { |
| 301 | *log_ptr = '0' + fac_level % 10; |
| 302 | fac_level /= 10; |
| 303 | log_ptr--; |
| 304 | } while (fac_level && log_ptr > logmsg); |
| 305 | *log_ptr = '<'; |
| 306 | |
| 307 | /* 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] | 308 | sent = sendto(*plogfd, log_ptr, dataptr + data_len - log_ptr, |
| 309 | MSG_DONTWAIT | MSG_NOSIGNAL, &logsrv->u.addr, logsrv_addrlen(logsrv)); |
| 310 | if (sent < 0) { |
| 311 | Alert("sendto logger #%d failed: %s (errno=%d)\n", |
| 312 | nblogger, strerror(errno), errno); |
| 313 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | |
| 317 | |
| 318 | /* |
| 319 | * send a log for the session when we have enough info about it |
| 320 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 321 | void tcp_sess_log(struct session *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 322 | { |
Cyril Bonté | acd7d63 | 2010-11-01 19:26:02 +0100 | [diff] [blame] | 323 | char pn[INET6_ADDRSTRLEN]; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 324 | struct proxy *fe = s->fe; |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 325 | struct proxy *be = s->be; |
| 326 | struct proxy *prx_log; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 327 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 328 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 329 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 330 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 331 | /* if we don't want to log normal traffic, return now */ |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 332 | 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] | 333 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 334 | return; |
| 335 | |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 336 | if (s->si[0].addr.c.from.ss_family == AF_INET) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 337 | inet_ntop(AF_INET, |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 338 | (const void *)&((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_addr, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 339 | pn, sizeof(pn)); |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 340 | else if (s->si[0].addr.c.from.ss_family == AF_INET6) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 341 | inet_ntop(AF_INET6, |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 342 | (const void *)&((struct sockaddr_in6 *)(&s->si[0].addr.c.from))->sin6_addr, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 343 | pn, sizeof(pn)); |
| 344 | |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 345 | get_localtime(s->logs.tv_accept.tv_sec, &tm); |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 346 | |
Willy Tarreau | e7ded1f | 2009-08-09 10:11:45 +0200 | [diff] [blame] | 347 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 348 | return; |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 349 | |
Willy Tarreau | e7ded1f | 2009-08-09 10:11:45 +0200 | [diff] [blame] | 350 | prx_log = fe; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 351 | tolog = fe->to_log; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 352 | |
| 353 | if (!(tolog & LW_SVID)) |
| 354 | svid = "-"; |
| 355 | else switch (s->req->cons->target.type) { |
| 356 | case TARG_TYPE_SERVER: |
| 357 | svid = s->req->cons->target.ptr.s->id; |
| 358 | break; |
| 359 | case TARG_TYPE_APPLET: |
| 360 | svid = s->req->cons->target.ptr.a->name; |
| 361 | break; |
| 362 | default: |
| 363 | svid = "<NOSRV>"; |
| 364 | break; |
| 365 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 366 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 367 | level = LOG_INFO; |
| 368 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 369 | level = LOG_ERR; |
| 370 | |
| 371 | 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] | 372 | " %s %s/%s %ld/%ld/%s%ld %s%lld" |
| 373 | " %c%c %d/%d/%d/%d/%s%u %ld/%ld\n", |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 374 | s->si[0].addr.c.from.ss_family == AF_UNIX ? "unix" : pn, |
| 375 | s->si[0].addr.c.from.ss_family == AF_UNIX ? s->listener->luid : (ntohs((s->si[0].addr.c.from.ss_family == AF_INET) ? |
| 376 | ((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_port : |
| 377 | ((struct sockaddr_in6 *)&s->si[0].addr.c.from)->sin6_port)), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 378 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 379 | 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] | 380 | fe->id, be->id, svid, |
| 381 | (s->logs.t_queue >= 0) ? s->logs.t_queue : -1, |
| 382 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 383 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 384 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 385 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 386 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 387 | 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] | 388 | (s->flags & SN_REDISP)?"+":"", |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 389 | (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] | 390 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 391 | |
| 392 | s->logs.logwait = 0; |
| 393 | } |
| 394 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 395 | |
| 396 | /* |
| 397 | * Local variables: |
| 398 | * c-indent-level: 8 |
| 399 | * c-basic-offset: 8 |
| 400 | * End: |
| 401 | */ |