blob: 20f9bb4ff9ac6770c158ce895ae6712a107cd114 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General logging functions.
3 *
4 * Copyright 2000-2006 Willy Tarreau <w@1wt.eu>
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 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>
21
22#include <sys/time.h>
23
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020024#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020026#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
28#include <types/backend.h>
29#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <types/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/session.h>
32
33
34const 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
44const char *log_levels[NB_LOG_LEVELS] = {
45 "emerg", "alert", "crit", "err",
46 "warning", "notice", "info", "debug"
47};
48
49const char *monthname[12] = {
50 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
51 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
52};
53
54const char sess_term_cond[8] = "-cCsSPRI"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal */
Willy Tarreaub8750a82006-09-03 09:56:00 +020055const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020056
57/*
58 * Displays the message on stderr with the date and pid. Overrides the quiet
59 * mode during startup.
60 */
Willy Tarreaub17916e2006-10-15 15:17:57 +020061void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +020062{
63 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +020064 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +020065
66 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
67 va_start(argp, fmt);
68
Willy Tarreaufe944602007-10-25 10:34:16 +020069 get_localtime(now.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +020070 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +020071 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +020072 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 Tarreaub17916e2006-10-15 15:17:57 +020082void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +020083{
84 va_list argp;
Willy Tarreaufe944602007-10-25 10:34:16 +020085 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +020086
87 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
88 va_start(argp, fmt);
89
Willy Tarreaufe944602007-10-25 10:34:16 +020090 get_localtime(now.tv_sec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +020091 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
Willy Tarreaufe944602007-10-25 10:34:16 +020092 tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid());
Willy Tarreaubaaee002006-06-26 02:48:02 +020093 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 Tarreaub17916e2006-10-15 15:17:57 +0200102void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200103{
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 */
117int 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 */
132int 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
143
Willy Tarreaubaaee002006-06-26 02:48:02 +0200144/*
145 * 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{
152 static int logfd = -1; /* syslog UDP socket */
153 static long tvsec = -1; /* to force the string to be initialized */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200154 va_list argp;
155 static char logmsg[MAX_SYSLOG_LEN];
156 static char *dataptr = NULL;
157 int fac_level;
158 int hdr_len, data_len;
159 struct sockaddr_in *sa[2];
160 int facilities[2], loglevel[2];
161 int nbloggers = 0;
162 char *log_ptr;
163
164 if (logfd < 0) {
165 if ((logfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
166 return;
Willy Tarreau3d089532007-09-17 10:56:13 +0200167 /* we don't want to receive anything on this socket */
168 setsockopt(logfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100169 /* need for AIX which does not know about MSG_DONTWAIT */
170 if (!MSG_DONTWAIT)
171 fcntl(logfd, F_SETFL, O_NONBLOCK);
Willy Tarreau3d089532007-09-17 10:56:13 +0200172 shutdown(logfd, SHUT_RD); /* does nothing under Linux, maybe needed for others */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200173 }
174
175 if (level < 0 || progname == NULL || message == NULL)
176 return;
177
Willy Tarreau2b35c952006-10-15 15:25:48 +0200178 if (now.tv_sec != tvsec || dataptr == NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200179 /* this string is rebuild only once a second */
Willy Tarreaufe944602007-10-25 10:34:16 +0200180 struct tm tm;
181
Willy Tarreau2b35c952006-10-15 15:25:48 +0200182 tvsec = now.tv_sec;
Willy Tarreaufe944602007-10-25 10:34:16 +0200183 get_localtime(tvsec, &tm);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200184
185 hdr_len = snprintf(logmsg, sizeof(logmsg),
186 "<<<<>%s %2d %02d:%02d:%02d %s[%d]: ",
Willy Tarreaufe944602007-10-25 10:34:16 +0200187 monthname[tm.tm_mon],
188 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreaubaaee002006-06-26 02:48:02 +0200189 progname, pid);
190 /* WARNING: depending upon implementations, snprintf may return
191 * either -1 or the number of bytes that would be needed to store
192 * the total message. In both cases, we must adjust it.
193 */
194 if (hdr_len < 0 || hdr_len > sizeof(logmsg))
195 hdr_len = sizeof(logmsg);
196
197 dataptr = logmsg + hdr_len;
198 }
199
200 va_start(argp, message);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100201 /*
202 * FIXME: we take a huge performance hit here. We might have to replace
203 * vsnprintf() for a hard-coded log writer.
204 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200205 data_len = vsnprintf(dataptr, logmsg + sizeof(logmsg) - dataptr, message, argp);
206 if (data_len < 0 || data_len > (logmsg + sizeof(logmsg) - dataptr))
207 data_len = logmsg + sizeof(logmsg) - dataptr;
208 va_end(argp);
209 dataptr[data_len - 1] = '\n'; /* force a break on ultra-long lines */
210
211 if (p == NULL) {
212 if (global.logfac1 >= 0) {
213 sa[nbloggers] = &global.logsrv1;
214 facilities[nbloggers] = global.logfac1;
215 loglevel[nbloggers] = global.loglev1;
216 nbloggers++;
217 }
218 if (global.logfac2 >= 0) {
219 sa[nbloggers] = &global.logsrv2;
220 facilities[nbloggers] = global.logfac2;
221 loglevel[nbloggers] = global.loglev2;
222 nbloggers++;
223 }
224 } else {
225 if (p->logfac1 >= 0) {
226 sa[nbloggers] = &p->logsrv1;
227 facilities[nbloggers] = p->logfac1;
228 loglevel[nbloggers] = p->loglev1;
229 nbloggers++;
230 }
231 if (p->logfac2 >= 0) {
232 sa[nbloggers] = &p->logsrv2;
233 facilities[nbloggers] = p->logfac2;
234 loglevel[nbloggers] = p->loglev2;
235 nbloggers++;
236 }
237 }
238
239 while (nbloggers-- > 0) {
240 /* we can filter the level of the messages that are sent to each logger */
241 if (level > loglevel[nbloggers])
242 continue;
243
244 /* For each target, we may have a different facility.
245 * We can also have a different log level for each message.
246 * This induces variations in the message header length.
247 * Since we don't want to recompute it each time, nor copy it every
248 * time, we only change the facility in the pre-computed header,
249 * and we change the pointer to the header accordingly.
250 */
251 fac_level = (facilities[nbloggers] << 3) + level;
252 log_ptr = logmsg + 3; /* last digit of the log level */
253 do {
254 *log_ptr = '0' + fac_level % 10;
255 fac_level /= 10;
256 log_ptr--;
257 } while (fac_level && log_ptr > logmsg);
258 *log_ptr = '<';
259
260 /* the total syslog message now starts at logptr, for dataptr+data_len-logptr */
261
262#ifndef MSG_NOSIGNAL
263 sendto(logfd, log_ptr, dataptr + data_len - log_ptr, MSG_DONTWAIT,
264 (struct sockaddr *)sa[nbloggers], sizeof(**sa));
265#else
266 sendto(logfd, log_ptr, dataptr + data_len - log_ptr, MSG_DONTWAIT | MSG_NOSIGNAL,
267 (struct sockaddr *)sa[nbloggers], sizeof(**sa));
268#endif
269 }
270}
271
272
273/*
274 * send a log for the session when we have enough info about it
275 */
Willy Tarreau42250582007-04-01 01:30:43 +0200276void tcp_sess_log(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200277{
278 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
Willy Tarreau73de9892006-11-30 11:40:23 +0100279 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100280 struct proxy *be = s->be;
281 struct proxy *prx_log;
Willy Tarreau42250582007-04-01 01:30:43 +0200282 int tolog;
283 char *svid;
Willy Tarreaufe944602007-10-25 10:34:16 +0200284 struct tm tm;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200285
Willy Tarreaubaaee002006-06-26 02:48:02 +0200286 if (s->cli_addr.ss_family == AF_INET)
287 inet_ntop(AF_INET,
288 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
289 pn, sizeof(pn));
290 else
291 inet_ntop(AF_INET6,
292 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
293 pn, sizeof(pn));
294
Willy Tarreaufe944602007-10-25 10:34:16 +0200295 get_localtime(s->logs.tv_accept.tv_sec, &tm);
Willy Tarreauddb358d2006-12-17 22:55:52 +0100296
297 if (fe->logfac1 >= 0)
298 prx_log = fe;
Willy Tarreau97de6242006-12-27 17:18:38 +0100299 /*
300 * FIXME: should we fall back to the backend if the frontend did not
301 * define any log ? It seems like we should not permit such complex
302 * setups because they would induce a debugging nightmare for the
303 * admin.
304 */
305 // else if (be->logfac1 >= 0)
306 // prx_log = be;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100307 else
308 prx_log = NULL; /* global */
309
Willy Tarreau42250582007-04-01 01:30:43 +0200310 /* FIXME: let's limit ourselves to frontend logging for now. */
311 tolog = fe->to_log;
312 svid = (tolog & LW_SVID) ? (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "-";
Willy Tarreaubaaee002006-06-26 02:48:02 +0200313
Willy Tarreau42250582007-04-01 01:30:43 +0200314 send_log(prx_log, LOG_INFO, "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
315 " %s %s/%s %d/%d/%s%d %s%lld"
316 " %c%c %d/%d/%d/%d %d/%d\n",
317 pn,
318 (s->cli_addr.ss_family == AF_INET) ?
319 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
320 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
Willy Tarreaufe944602007-10-25 10:34:16 +0200321 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
322 tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.tv_accept.tv_usec/1000,
Willy Tarreau42250582007-04-01 01:30:43 +0200323 fe->id, be->id, svid,
324 (s->logs.t_queue >= 0) ? s->logs.t_queue : -1,
325 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
326 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
327 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in,
328 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
329 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
330 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
331 s->logs.srv_queue_size, s->logs.prx_queue_size);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200332
333 s->logs.logwait = 0;
334}
335
Willy Tarreaubaaee002006-06-26 02:48:02 +0200336
337/*
338 * Local variables:
339 * c-indent-level: 8
340 * c-basic-offset: 8
341 * End:
342 */