blob: d08ad16d6e4bf4b6a248656ed11f9bff35a36432 [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 Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020027#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
29#include <types/backend.h>
30#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020032#include <types/session.h>
33
Robert Tsai81ae1952007-12-05 10:47:29 +010034#ifndef MSG_NOSIGNAL
35#define MSG_NOSIGNAL (0)
36#endif /* !MSG_NOSIGNAL */
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
38const char *log_facilities[NB_LOG_FACILITIES] = {
39 "kern", "user", "mail", "daemon",
40 "auth", "syslog", "lpr", "news",
41 "uucp", "cron", "auth2", "ftp",
42 "ntp", "audit", "alert", "cron2",
43 "local0", "local1", "local2", "local3",
44 "local4", "local5", "local6", "local7"
45};
46
47
48const char *log_levels[NB_LOG_LEVELS] = {
49 "emerg", "alert", "crit", "err",
50 "warning", "notice", "info", "debug"
51};
52
53const char *monthname[12] = {
54 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
55 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
56};
57
58const char sess_term_cond[8] = "-cCsSPRI"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal */
Willy Tarreaub8750a82006-09-03 09:56:00 +020059const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020060
61/*
62 * Displays the message on stderr with the date and pid. Overrides the quiet
63 * mode during startup.
64 */
Willy Tarreaub17916e2006-10-15 15:17:57 +020065void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +020066{
67 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +020068 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +020069
70 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
71 va_start(argp, fmt);
72
Willy Tarreaub7f694f2008-06-22 17:18:02 +020073 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +020074 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +020075 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +020076 vfprintf(stderr, fmt, argp);
77 fflush(stderr);
78 va_end(argp);
79 }
80}
81
82
83/*
84 * Displays the message on stderr with the date and pid.
85 */
Willy Tarreaub17916e2006-10-15 15:17:57 +020086void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +020087{
88 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +020089 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +020090
91 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
92 va_start(argp, fmt);
93
Willy Tarreaub7f694f2008-06-22 17:18:02 +020094 get_localtime(date.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +020095 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +020096 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +020097 vfprintf(stderr, fmt, argp);
98 fflush(stderr);
99 va_end(argp);
100 }
101}
102
103/*
104 * Displays the message on <out> only if quiet mode is not set.
105 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200106void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200107{
108 va_list argp;
109
110 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
111 va_start(argp, fmt);
112 vfprintf(out, fmt, argp);
113 fflush(out);
114 va_end(argp);
115 }
116}
117
118/*
119 * returns log level for <lev> or -1 if not found.
120 */
121int get_log_level(const char *lev)
122{
123 int level;
124
125 level = NB_LOG_LEVELS - 1;
126 while (level >= 0 && strcmp(log_levels[level], lev))
127 level--;
128
129 return level;
130}
131
132
133/*
134 * returns log facility for <fac> or -1 if not found.
135 */
136int get_log_facility(const char *fac)
137{
138 int facility;
139
140 facility = NB_LOG_FACILITIES - 1;
141 while (facility >= 0 && strcmp(log_facilities[facility], fac))
142 facility--;
143
144 return facility;
145}
146
Robert Tsai81ae1952007-12-05 10:47:29 +0100147/*
148 * Return the length of the address endpoint, suitable for use with sendto().
149 */
Willy Tarreau019767b2007-12-05 11:11:55 +0100150static inline int logsrv_addrlen(const struct logsrv *logsrv)
Robert Tsai81ae1952007-12-05 10:47:29 +0100151{
Robert Tsai81ae1952007-12-05 10:47:29 +0100152 switch (logsrv->u.addr.sa_family) {
153 case AF_UNIX:
154 return sizeof(logsrv->u.un);
155 case AF_INET:
156 return sizeof(logsrv->u.in);
157 default:
158 break;
159 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100160 return -1;
161}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200162
Willy Tarreaubaaee002006-06-26 02:48:02 +0200163/*
164 * This function sends a syslog message to both log servers of a proxy,
165 * or to global log servers if the proxy is NULL.
166 * It also tries not to waste too much time computing the message header.
167 * It doesn't care about errors nor does it report them.
168 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200169void send_log(struct proxy *p, int level, const char *message, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170{
Robert Tsai81ae1952007-12-05 10:47:29 +0100171 static int logfdunix = -1; /* syslog to AF_UNIX socket */
172 static int logfdinet = -1; /* syslog to AF_INET socket */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200173 static long tvsec = -1; /* to force the string to be initialized */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200174 va_list argp;
175 static char logmsg[MAX_SYSLOG_LEN];
176 static char *dataptr = NULL;
177 int fac_level;
178 int hdr_len, data_len;
Robert Tsai81ae1952007-12-05 10:47:29 +0100179 struct logsrv *logsrvs[2];
Willy Tarreaubaaee002006-06-26 02:48:02 +0200180 int facilities[2], loglevel[2];
Robert Tsai81ae1952007-12-05 10:47:29 +0100181 int nblogger;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200182 int nbloggers = 0;
183 char *log_ptr;
184
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185 if (level < 0 || progname == NULL || message == NULL)
186 return;
187
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200188 if (unlikely(date.tv_sec != tvsec || dataptr == NULL)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200190 struct tm tm;
191
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200192 tvsec = date.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200193 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200194
195 hdr_len = snprintf(logmsg, sizeof(logmsg),
196 "<<<<>%s %2d %02d:%02d:%02d %s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200197 monthname[tm.tm_mon],
198 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200199 progname, pid);
200 /* WARNING: depending upon implementations, snprintf may return
201 * either -1 or the number of bytes that would be needed to store
202 * the total message. In both cases, we must adjust it.
203 */
204 if (hdr_len < 0 || hdr_len > sizeof(logmsg))
205 hdr_len = sizeof(logmsg);
206
207 dataptr = logmsg + hdr_len;
208 }
209
210 va_start(argp, message);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100211 /*
212 * FIXME: we take a huge performance hit here. We might have to replace
213 * vsnprintf() for a hard-coded log writer.
214 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200215 data_len = vsnprintf(dataptr, logmsg + sizeof(logmsg) - dataptr, message, argp);
216 if (data_len < 0 || data_len > (logmsg + sizeof(logmsg) - dataptr))
217 data_len = logmsg + sizeof(logmsg) - dataptr;
218 va_end(argp);
219 dataptr[data_len - 1] = '\n'; /* force a break on ultra-long lines */
220
221 if (p == NULL) {
222 if (global.logfac1 >= 0) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100223 logsrvs[nbloggers] = &global.logsrv1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200224 facilities[nbloggers] = global.logfac1;
225 loglevel[nbloggers] = global.loglev1;
226 nbloggers++;
227 }
228 if (global.logfac2 >= 0) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100229 logsrvs[nbloggers] = &global.logsrv2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200230 facilities[nbloggers] = global.logfac2;
231 loglevel[nbloggers] = global.loglev2;
232 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;
239 nbloggers++;
240 }
241 if (p->logfac2 >= 0) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100242 logsrvs[nbloggers] = &p->logsrv2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200243 facilities[nbloggers] = p->logfac2;
244 loglevel[nbloggers] = p->loglev2;
245 nbloggers++;
246 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100247 }
248
249 /* Lazily set up syslog sockets for protocol families of configured
250 * syslog servers. */
251 for (nblogger = 0; nblogger < nbloggers; nblogger++) {
252 const struct logsrv *logsrv = logsrvs[nblogger];
253 int proto, *plogfd;
254 if (logsrv->u.addr.sa_family == AF_UNIX) {
255 proto = 0;
256 plogfd = &logfdunix;
257 } else {
258 /* sa_family == AF_INET */
259 proto = IPPROTO_UDP;
260 plogfd = &logfdinet;
261 }
262 if (*plogfd >= 0) {
263 /* socket already created. */
264 continue;
265 }
266 if ((*plogfd = socket(logsrv->u.addr.sa_family, SOCK_DGRAM,
267 proto)) < 0) {
268 Alert("socket for logger #%d failed: %s (errno=%d)\n",
269 nblogger + 1, strerror(errno), errno);
270 return;
271 }
272 /* we don't want to receive anything on this socket */
273 setsockopt(*plogfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
274 /* does nothing under Linux, maybe needed for others */
275 shutdown(*plogfd, SHUT_RD);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200276 }
277
Robert Tsai81ae1952007-12-05 10:47:29 +0100278 /* Send log messages to syslog server. */
279 for (nblogger = 0; nblogger < nbloggers; nblogger++) {
280 const struct logsrv *logsrv = logsrvs[nblogger];
281 int *plogfd = logsrv->u.addr.sa_family == AF_UNIX ?
282 &logfdunix : &logfdinet;
283 int sent;
284
Willy Tarreaubaaee002006-06-26 02:48:02 +0200285 /* we can filter the level of the messages that are sent to each logger */
Robert Tsai81ae1952007-12-05 10:47:29 +0100286 if (level > loglevel[nblogger])
Willy Tarreaubaaee002006-06-26 02:48:02 +0200287 continue;
288
289 /* For each target, we may have a different facility.
290 * We can also have a different log level for each message.
291 * This induces variations in the message header length.
292 * Since we don't want to recompute it each time, nor copy it every
293 * time, we only change the facility in the pre-computed header,
294 * and we change the pointer to the header accordingly.
295 */
Robert Tsai81ae1952007-12-05 10:47:29 +0100296 fac_level = (facilities[nblogger] << 3) + level;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200297 log_ptr = logmsg + 3; /* last digit of the log level */
298 do {
299 *log_ptr = '0' + fac_level % 10;
300 fac_level /= 10;
301 log_ptr--;
302 } while (fac_level && log_ptr > logmsg);
303 *log_ptr = '<';
304
305 /* the total syslog message now starts at logptr, for dataptr+data_len-logptr */
Robert Tsai81ae1952007-12-05 10:47:29 +0100306 sent = sendto(*plogfd, log_ptr, dataptr + data_len - log_ptr,
307 MSG_DONTWAIT | MSG_NOSIGNAL, &logsrv->u.addr, logsrv_addrlen(logsrv));
308 if (sent < 0) {
309 Alert("sendto logger #%d failed: %s (errno=%d)\n",
310 nblogger, strerror(errno), errno);
311 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200312 }
313}
314
315
316/*
317 * send a log for the session when we have enough info about it
318 */
Willy Tarreau42250582007-04-01 01:30:43 +0200319void tcp_sess_log(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200320{
321 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
Willy Tarreau73de9892006-11-30 11:40:23 +0100322 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100323 struct proxy *be = s->be;
324 struct proxy *prx_log;
Willy Tarreau42250582007-04-01 01:30:43 +0200325 int tolog;
326 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200327 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328
Willy Tarreaubaaee002006-06-26 02:48:02 +0200329 if (s->cli_addr.ss_family == AF_INET)
330 inet_ntop(AF_INET,
331 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
332 pn, sizeof(pn));
333 else
334 inet_ntop(AF_INET6,
335 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
336 pn, sizeof(pn));
337
Willy Tarreaufe944602007-10-25 10:34:16 +0200338 get_localtime(s->logs.tv_accept.tv_sec, &tm);
Willy Tarreauddb358d2006-12-17 22:55:52 +0100339
340 if (fe->logfac1 >= 0)
341 prx_log = fe;
Willy Tarreau97de6242006-12-27 17:18:38 +0100342 /*
343 * FIXME: should we fall back to the backend if the frontend did not
344 * define any log ? It seems like we should not permit such complex
345 * setups because they would induce a debugging nightmare for the
346 * admin.
347 */
348 // else if (be->logfac1 >= 0)
349 // prx_log = be;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100350 else
351 prx_log = NULL; /* global */
352
Willy Tarreau42250582007-04-01 01:30:43 +0200353 /* FIXME: let's limit ourselves to frontend logging for now. */
354 tolog = fe->to_log;
355 svid = (tolog & LW_SVID) ? (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "-";
Willy Tarreaubaaee002006-06-26 02:48:02 +0200356
Willy Tarreau42250582007-04-01 01:30:43 +0200357 send_log(prx_log, LOG_INFO, "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
358 " %s %s/%s %d/%d/%s%d %s%lld"
Krzysztof Piotr Oledzki25b501a2008-01-06 16:36:16 +0100359 " %c%c %d/%d/%d/%d/%s%u %d/%d\n",
Willy Tarreau42250582007-04-01 01:30:43 +0200360 pn,
361 (s->cli_addr.ss_family == AF_INET) ?
362 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
363 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200364 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
365 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.tv_accept.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200366 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 Tarreau8b3977f2008-01-18 11:16:32 +0100370 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out,
Willy Tarreau42250582007-04-01 01:30:43 +0200371 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 Oledzki25b501a2008-01-06 16:36:16 +0100374 (s->flags & SN_REDISP)?"+":"",
375 (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries,
Willy Tarreau42250582007-04-01 01:30:43 +0200376 s->logs.srv_queue_size, s->logs.prx_queue_size);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200377
378 s->logs.logwait = 0;
379}
380
Willy Tarreaubaaee002006-06-26 02:48:02 +0200381
382/*
383 * Local variables:
384 * c-indent-level: 8
385 * c-basic-offset: 8
386 * End:
387 */