blob: 02b6b20eb924ed84d009073a4b8afaef5be707e5 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
Willy Tarreauc8f24f82007-11-30 18:38:35 +010013#include <fcntl.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020014#include <stdarg.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <syslog.h>
19#include <time.h>
20#include <unistd.h>
Robert Tsai81ae1952007-12-05 10:47:29 +010021#include <errno.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022
23#include <sys/time.h>
24
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020025#include <common/config.h>
Willy Tarreaud6d06902009-08-19 11:22:33 +020026#include <common/compat.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020027#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020028#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <types/global.h>
Willy Tarreauec6c5df2008-07-15 00:22:45 +020031
32#include <proto/log.h>
Willy Tarreau827aee92011-03-10 16:55:02 +010033#include <proto/stream_interface.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034
Willy Tarreaubaaee002006-06-26 02:48:02 +020035const 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
45const char *log_levels[NB_LOG_LEVELS] = {
46 "emerg", "alert", "crit", "err",
47 "warning", "notice", "info", "debug"
48};
49
50const char *monthname[12] = {
51 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
52 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
53};
54
Simon Horman752dc4a2011-06-21 14:34:59 +090055const char sess_term_cond[9] = "-cCsSPRID"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal, Down */
Willy Tarreaub8750a82006-09-03 09:56:00 +020056const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020057
58/*
59 * Displays the message on stderr with the date and pid. Overrides the quiet
60 * mode during startup.
61 */
Willy Tarreaub17916e2006-10-15 15:17:57 +020062void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +020063{
64 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +020065 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +020066
67 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
68 va_start(argp, fmt);
69
Willy Tarreaub7f694f2008-06-22 17:18:02 +020070 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +020071 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +020072 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +020073 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 Tarreaub17916e2006-10-15 15:17:57 +020083void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +020084{
85 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +020086 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +020087
88 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
89 va_start(argp, fmt);
90
Willy Tarreaub7f694f2008-06-22 17:18:02 +020091 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +020092 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +020093 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +020094 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 Tarreaub17916e2006-10-15 15:17:57 +0200103void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104{
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 */
118int 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 */
133int 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 Tsai81ae1952007-12-05 10:47:29 +0100144/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200145 * 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 Tarreaub17916e2006-10-15 15:17:57 +0200150void send_log(struct proxy *p, int level, const char *message, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151{
Robert Tsai81ae1952007-12-05 10:47:29 +0100152 static int logfdunix = -1; /* syslog to AF_UNIX socket */
153 static int logfdinet = -1; /* syslog to AF_INET socket */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200154 static long tvsec = -1; /* to force the string to be initialized */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200155 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 Tsai81ae1952007-12-05 10:47:29 +0100160 struct logsrv *logsrvs[2];
Willy Tarreauf7edefa2009-05-10 17:20:05 +0200161 int facilities[2], loglevel[2], minlvl[2];
Robert Tsai81ae1952007-12-05 10:47:29 +0100162 int nblogger;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163 int nbloggers = 0;
164 char *log_ptr;
165
Kevinm48936af2010-12-22 16:08:21 +0000166 if (level < 0 || message == NULL)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200167 return;
168
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200169 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200171 struct tm tm;
172
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200173 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200174 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175
176 hdr_len = snprintf(logmsg, sizeof(logmsg),
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100177 "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200178 monthname[tm.tm_mon],
179 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100180 global.log_send_hostname ? global.log_send_hostname : "",
Kevinm48936af2010-12-22 16:08:21 +0000181 global.log_tag, pid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182 /* 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 Tarreau8d5d7f22007-01-21 19:16:41 +0100193 /*
194 * FIXME: we take a huge performance hit here. We might have to replace
195 * vsnprintf() for a hard-coded log writer.
196 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200197 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 Tsai81ae1952007-12-05 10:47:29 +0100205 logsrvs[nbloggers] = &global.logsrv1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200206 facilities[nbloggers] = global.logfac1;
207 loglevel[nbloggers] = global.loglev1;
Willy Tarreauf7edefa2009-05-10 17:20:05 +0200208 minlvl[nbloggers] = global.minlvl1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200209 nbloggers++;
210 }
211 if (global.logfac2 >= 0) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100212 logsrvs[nbloggers] = &global.logsrv2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200213 facilities[nbloggers] = global.logfac2;
214 loglevel[nbloggers] = global.loglev2;
Willy Tarreauf7edefa2009-05-10 17:20:05 +0200215 minlvl[nbloggers] = global.minlvl2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200216 nbloggers++;
217 }
218 } else {
219 if (p->logfac1 >= 0) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100220 logsrvs[nbloggers] = &p->logsrv1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200221 facilities[nbloggers] = p->logfac1;
222 loglevel[nbloggers] = p->loglev1;
Willy Tarreauf7edefa2009-05-10 17:20:05 +0200223 minlvl[nbloggers] = p->minlvl1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200224 nbloggers++;
225 }
226 if (p->logfac2 >= 0) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100227 logsrvs[nbloggers] = &p->logsrv2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200228 facilities[nbloggers] = p->logfac2;
229 loglevel[nbloggers] = p->loglev2;
Willy Tarreauf7edefa2009-05-10 17:20:05 +0200230 minlvl[nbloggers] = p->minlvl2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231 nbloggers++;
232 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100233 }
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 Colombier11bcb6c2011-03-24 12:23:00 +0100240 if (logsrv->addr.ss_family == AF_UNIX) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100241 proto = 0;
242 plogfd = &logfdunix;
243 } else {
Robert Tsai81ae1952007-12-05 10:47:29 +0100244 proto = IPPROTO_UDP;
245 plogfd = &logfdinet;
246 }
247 if (*plogfd >= 0) {
248 /* socket already created. */
249 continue;
250 }
David du Colombier11bcb6c2011-03-24 12:23:00 +0100251 if ((*plogfd = socket(logsrv->addr.ss_family, SOCK_DGRAM,
Robert Tsai81ae1952007-12-05 10:47:29 +0100252 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 Tarreaubaaee002006-06-26 02:48:02 +0200261 }
262
Robert Tsai81ae1952007-12-05 10:47:29 +0100263 /* Send log messages to syslog server. */
264 for (nblogger = 0; nblogger < nbloggers; nblogger++) {
265 const struct logsrv *logsrv = logsrvs[nblogger];
David du Colombier11bcb6c2011-03-24 12:23:00 +0100266 int *plogfd = logsrv->addr.ss_family == AF_UNIX ?
Robert Tsai81ae1952007-12-05 10:47:29 +0100267 &logfdunix : &logfdinet;
268 int sent;
269
Willy Tarreaubaaee002006-06-26 02:48:02 +0200270 /* we can filter the level of the messages that are sent to each logger */
Robert Tsai81ae1952007-12-05 10:47:29 +0100271 if (level > loglevel[nblogger])
Willy Tarreaubaaee002006-06-26 02:48:02 +0200272 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 Tarreauf7edefa2009-05-10 17:20:05 +0200281 fac_level = (facilities[nblogger] << 3) + MAX(level, minlvl[nblogger]);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200282 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 Tsai81ae1952007-12-05 10:47:29 +0100291 sent = sendto(*plogfd, log_ptr, dataptr + data_len - log_ptr,
Willy Tarreau1b4b7ce2011-04-05 16:56:50 +0200292 MSG_DONTWAIT | MSG_NOSIGNAL,
293 (struct sockaddr *)&logsrv->addr, get_addr_len(&logsrv->addr));
Robert Tsai81ae1952007-12-05 10:47:29 +0100294 if (sent < 0) {
295 Alert("sendto logger #%d failed: %s (errno=%d)\n",
296 nblogger, strerror(errno), errno);
297 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200298 }
299}
300
301
302/*
303 * send a log for the session when we have enough info about it
304 */
Willy Tarreau42250582007-04-01 01:30:43 +0200305void tcp_sess_log(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200306{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100307 char pn[INET6_ADDRSTRLEN];
Willy Tarreau73de9892006-11-30 11:40:23 +0100308 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100309 struct proxy *be = s->be;
310 struct proxy *prx_log;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200311 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200312 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200313 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200314
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200315 /* if we don't want to log normal traffic, return now */
Willy Tarreauee28de02010-06-01 09:51:00 +0200316 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || (s->req->cons->conn_retries != be->conn_retries);
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200317 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
318 return;
319
Willy Tarreau957c0a52011-03-03 17:42:23 +0100320 if (s->si[0].addr.c.from.ss_family == AF_INET)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200321 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100322 (const void *)&((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_addr,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200323 pn, sizeof(pn));
Willy Tarreau957c0a52011-03-03 17:42:23 +0100324 else if (s->si[0].addr.c.from.ss_family == AF_INET6)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100326 (const void *)&((struct sockaddr_in6 *)(&s->si[0].addr.c.from))->sin6_addr,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327 pn, sizeof(pn));
328
Willy Tarreaufe944602007-10-25 10:34:16 +0200329 get_localtime(s->logs.tv_accept.tv_sec, &tm);
Willy Tarreauddb358d2006-12-17 22:55:52 +0100330
Willy Tarreaue7ded1f2009-08-09 10:11:45 +0200331 if (fe->logfac1 < 0 && fe->logfac2 < 0)
332 return;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100333
Willy Tarreaue7ded1f2009-08-09 10:11:45 +0200334 prx_log = fe;
Willy Tarreau42250582007-04-01 01:30:43 +0200335 tolog = fe->to_log;
Willy Tarreau71904a42011-02-13 14:30:26 +0100336
337 if (!(tolog & LW_SVID))
338 svid = "-";
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200339 else switch (s->target.type) {
Willy Tarreau71904a42011-02-13 14:30:26 +0100340 case TARG_TYPE_SERVER:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200341 svid = s->target.ptr.s->id;
Willy Tarreau71904a42011-02-13 14:30:26 +0100342 break;
343 case TARG_TYPE_APPLET:
Willy Tarreau7b7a8e92011-03-27 19:53:06 +0200344 svid = s->target.ptr.a->name;
Willy Tarreau71904a42011-02-13 14:30:26 +0100345 break;
346 default:
347 svid = "<NOSRV>";
348 break;
349 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200350
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200351 level = LOG_INFO;
352 if (err && (fe->options2 & PR_O2_LOGERRORS))
353 level = LOG_ERR;
354
355 send_log(prx_log, level, "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
Willy Tarreau1772ece2009-04-03 14:49:12 +0200356 " %s %s/%s %ld/%ld/%s%ld %s%lld"
357 " %c%c %d/%d/%d/%d/%s%u %ld/%ld\n",
Willy Tarreau957c0a52011-03-03 17:42:23 +0100358 s->si[0].addr.c.from.ss_family == AF_UNIX ? "unix" : pn,
359 s->si[0].addr.c.from.ss_family == AF_UNIX ? s->listener->luid : (ntohs((s->si[0].addr.c.from.ss_family == AF_INET) ?
360 ((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_port :
361 ((struct sockaddr_in6 *)&s->si[0].addr.c.from)->sin6_port)),
Willy Tarreaufe944602007-10-25 10:34:16 +0200362 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +0200363 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.tv_accept.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200364 fe->id, be->id, svid,
365 (s->logs.t_queue >= 0) ? s->logs.t_queue : -1,
366 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
367 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
Willy Tarreau8b3977f2008-01-18 11:16:32 +0100368 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200369 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
370 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
Willy Tarreau827aee92011-03-10 16:55:02 +0100371 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100372 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +0200373 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200374 s->logs.srv_queue_size, s->logs.prx_queue_size);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200375
376 s->logs.logwait = 0;
377}
378
Willy Tarreaubaaee002006-06-26 02:48:02 +0200379
380/*
381 * Local variables:
382 * c-indent-level: 8
383 * c-basic-offset: 8
384 * End:
385 */