blob: f90ec90f6e1b0a6ce35b5000a9d86fb09c48b191 [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
55const char sess_term_cond[8] = "-cCsSPRI"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal */
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/*
145 * Return the length of the address endpoint, suitable for use with sendto().
146 */
Willy Tarreau019767b2007-12-05 11:11:55 +0100147static inline int logsrv_addrlen(const struct logsrv *logsrv)
Robert Tsai81ae1952007-12-05 10:47:29 +0100148{
Robert Tsai81ae1952007-12-05 10:47:29 +0100149 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 Tsai81ae1952007-12-05 10:47:29 +0100157 return -1;
158}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200159
Willy Tarreaubaaee002006-06-26 02:48:02 +0200160/*
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 Tarreaub17916e2006-10-15 15:17:57 +0200166void send_log(struct proxy *p, int level, const char *message, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200167{
Robert Tsai81ae1952007-12-05 10:47:29 +0100168 static int logfdunix = -1; /* syslog to AF_UNIX socket */
169 static int logfdinet = -1; /* syslog to AF_INET socket */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170 static long tvsec = -1; /* to force the string to be initialized */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200171 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 Tsai81ae1952007-12-05 10:47:29 +0100176 struct logsrv *logsrvs[2];
Willy Tarreauf7edefa2009-05-10 17:20:05 +0200177 int facilities[2], loglevel[2], minlvl[2];
Robert Tsai81ae1952007-12-05 10:47:29 +0100178 int nblogger;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200179 int nbloggers = 0;
180 char *log_ptr;
181
Kevinm48936af2010-12-22 16:08:21 +0000182 if (level < 0 || message == NULL)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183 return;
184
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200185 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200186 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200187 struct tm tm;
188
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200189 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200190 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200191
192 hdr_len = snprintf(logmsg, sizeof(logmsg),
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100193 "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200194 monthname[tm.tm_mon],
195 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Joe Williamsdf5b38f2010-12-29 17:05:48 +0100196 global.log_send_hostname ? global.log_send_hostname : "",
Kevinm48936af2010-12-22 16:08:21 +0000197 global.log_tag, pid);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200198 /* 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 Tarreau8d5d7f22007-01-21 19:16:41 +0100209 /*
210 * FIXME: we take a huge performance hit here. We might have to replace
211 * vsnprintf() for a hard-coded log writer.
212 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200213 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 Tsai81ae1952007-12-05 10:47:29 +0100221 logsrvs[nbloggers] = &global.logsrv1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200222 facilities[nbloggers] = global.logfac1;
223 loglevel[nbloggers] = global.loglev1;
Willy Tarreauf7edefa2009-05-10 17:20:05 +0200224 minlvl[nbloggers] = global.minlvl1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200225 nbloggers++;
226 }
227 if (global.logfac2 >= 0) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100228 logsrvs[nbloggers] = &global.logsrv2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200229 facilities[nbloggers] = global.logfac2;
230 loglevel[nbloggers] = global.loglev2;
Willy Tarreauf7edefa2009-05-10 17:20:05 +0200231 minlvl[nbloggers] = global.minlvl2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200232 nbloggers++;
233 }
234 } else {
235 if (p->logfac1 >= 0) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100236 logsrvs[nbloggers] = &p->logsrv1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200237 facilities[nbloggers] = p->logfac1;
238 loglevel[nbloggers] = p->loglev1;
Willy Tarreauf7edefa2009-05-10 17:20:05 +0200239 minlvl[nbloggers] = p->minlvl1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240 nbloggers++;
241 }
242 if (p->logfac2 >= 0) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100243 logsrvs[nbloggers] = &p->logsrv2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200244 facilities[nbloggers] = p->logfac2;
245 loglevel[nbloggers] = p->loglev2;
Willy Tarreauf7edefa2009-05-10 17:20:05 +0200246 minlvl[nbloggers] = p->minlvl2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200247 nbloggers++;
248 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100249 }
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 Tarreaubaaee002006-06-26 02:48:02 +0200278 }
279
Robert Tsai81ae1952007-12-05 10:47:29 +0100280 /* 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 Tarreaubaaee002006-06-26 02:48:02 +0200287 /* we can filter the level of the messages that are sent to each logger */
Robert Tsai81ae1952007-12-05 10:47:29 +0100288 if (level > loglevel[nblogger])
Willy Tarreaubaaee002006-06-26 02:48:02 +0200289 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 Tarreauf7edefa2009-05-10 17:20:05 +0200298 fac_level = (facilities[nblogger] << 3) + MAX(level, minlvl[nblogger]);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299 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 Tsai81ae1952007-12-05 10:47:29 +0100308 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 Tarreaubaaee002006-06-26 02:48:02 +0200314 }
315}
316
317
318/*
319 * send a log for the session when we have enough info about it
320 */
Willy Tarreau42250582007-04-01 01:30:43 +0200321void tcp_sess_log(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200322{
Cyril Bontéacd7d632010-11-01 19:26:02 +0100323 char pn[INET6_ADDRSTRLEN];
Willy Tarreau73de9892006-11-30 11:40:23 +0100324 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100325 struct proxy *be = s->be;
326 struct proxy *prx_log;
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200327 int tolog, level, err;
Willy Tarreau42250582007-04-01 01:30:43 +0200328 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200329 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200330
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200331 /* if we don't want to log normal traffic, return now */
Willy Tarreauee28de02010-06-01 09:51:00 +0200332 err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || (s->req->cons->conn_retries != be->conn_retries);
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200333 if (!err && (fe->options2 & PR_O2_NOLOGNORM))
334 return;
335
Willy Tarreau957c0a52011-03-03 17:42:23 +0100336 if (s->si[0].addr.c.from.ss_family == AF_INET)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200337 inet_ntop(AF_INET,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100338 (const void *)&((struct sockaddr_in *)&s->si[0].addr.c.from)->sin_addr,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339 pn, sizeof(pn));
Willy Tarreau957c0a52011-03-03 17:42:23 +0100340 else if (s->si[0].addr.c.from.ss_family == AF_INET6)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200341 inet_ntop(AF_INET6,
Willy Tarreau957c0a52011-03-03 17:42:23 +0100342 (const void *)&((struct sockaddr_in6 *)(&s->si[0].addr.c.from))->sin6_addr,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200343 pn, sizeof(pn));
344
Willy Tarreaufe944602007-10-25 10:34:16 +0200345 get_localtime(s->logs.tv_accept.tv_sec, &tm);
Willy Tarreauddb358d2006-12-17 22:55:52 +0100346
Willy Tarreaue7ded1f2009-08-09 10:11:45 +0200347 if (fe->logfac1 < 0 && fe->logfac2 < 0)
348 return;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100349
Willy Tarreaue7ded1f2009-08-09 10:11:45 +0200350 prx_log = fe;
Willy Tarreau42250582007-04-01 01:30:43 +0200351 tolog = fe->to_log;
Willy Tarreau71904a42011-02-13 14:30:26 +0100352
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 Tarreaubaaee002006-06-26 02:48:02 +0200366
Willy Tarreauc9bd0cc2009-05-10 11:57:02 +0200367 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 Tarreau1772ece2009-04-03 14:49:12 +0200372 " %s %s/%s %ld/%ld/%s%ld %s%lld"
373 " %c%c %d/%d/%d/%d/%s%u %ld/%ld\n",
Willy Tarreau957c0a52011-03-03 17:42:23 +0100374 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 Tarreaufe944602007-10-25 10:34:16 +0200378 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
Willy Tarreau1772ece2009-04-03 14:49:12 +0200379 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.tv_accept.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200380 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 Tarreau8b3977f2008-01-18 11:16:32 +0100384 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200385 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
386 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
Willy Tarreau827aee92011-03-10 16:55:02 +0100387 actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0,
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100388 (s->flags & SN_REDISP)?"+":"",
Willy Tarreauee28de02010-06-01 09:51:00 +0200389 (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200390 s->logs.srv_queue_size, s->logs.prx_queue_size);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200391
392 s->logs.logwait = 0;
393}
394
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395
396/*
397 * Local variables:
398 * c-indent-level: 8
399 * c-basic-offset: 8
400 * End:
401 */