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