blob: 542dd0dc4c967135e8d79a514dbccc73c10547df [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
13#include <stdarg.h>
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <syslog.h>
18#include <time.h>
19#include <unistd.h>
20
21#include <sys/time.h>
22
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020023#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020024#include <common/standard.h>
Willy Tarreaufb278672006-10-15 15:38:50 +020025#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
27#include <types/backend.h>
28#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029#include <types/log.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030#include <types/session.h>
31
32
33const char *log_facilities[NB_LOG_FACILITIES] = {
34 "kern", "user", "mail", "daemon",
35 "auth", "syslog", "lpr", "news",
36 "uucp", "cron", "auth2", "ftp",
37 "ntp", "audit", "alert", "cron2",
38 "local0", "local1", "local2", "local3",
39 "local4", "local5", "local6", "local7"
40};
41
42
43const char *log_levels[NB_LOG_LEVELS] = {
44 "emerg", "alert", "crit", "err",
45 "warning", "notice", "info", "debug"
46};
47
48const char *monthname[12] = {
49 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
50 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
51};
52
53const char sess_term_cond[8] = "-cCsSPRI"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal */
Willy Tarreaub8750a82006-09-03 09:56:00 +020054const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */
Willy Tarreaubaaee002006-06-26 02:48:02 +020055
56/*
57 * Displays the message on stderr with the date and pid. Overrides the quiet
58 * mode during startup.
59 */
Willy Tarreaub17916e2006-10-15 15:17:57 +020060void Alert(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +020061{
62 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +020063 struct tm *tm;
64
65 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
66 va_start(argp, fmt);
67
Willy Tarreaubf736132006-10-15 22:54:47 +020068 tm = localtime((time_t *)&now.tv_sec);
Willy Tarreaubaaee002006-06-26 02:48:02 +020069 fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ",
70 tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)getpid());
71 vfprintf(stderr, fmt, argp);
72 fflush(stderr);
73 va_end(argp);
74 }
75}
76
77
78/*
79 * Displays the message on stderr with the date and pid.
80 */
Willy Tarreaub17916e2006-10-15 15:17:57 +020081void Warning(const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +020082{
83 va_list argp;
Willy Tarreaubaaee002006-06-26 02:48:02 +020084 struct tm *tm;
85
86 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
87 va_start(argp, fmt);
88
Willy Tarreaubf736132006-10-15 22:54:47 +020089 tm = localtime((time_t *)&now.tv_sec);
Willy Tarreaubaaee002006-06-26 02:48:02 +020090 fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ",
91 tm->tm_yday, tm->tm_hour, tm->tm_min, tm->tm_sec, (int)getpid());
92 vfprintf(stderr, fmt, argp);
93 fflush(stderr);
94 va_end(argp);
95 }
96}
97
98/*
99 * Displays the message on <out> only if quiet mode is not set.
100 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200101void qfprintf(FILE *out, const char *fmt, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102{
103 va_list argp;
104
105 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) {
106 va_start(argp, fmt);
107 vfprintf(out, fmt, argp);
108 fflush(out);
109 va_end(argp);
110 }
111}
112
113/*
114 * returns log level for <lev> or -1 if not found.
115 */
116int get_log_level(const char *lev)
117{
118 int level;
119
120 level = NB_LOG_LEVELS - 1;
121 while (level >= 0 && strcmp(log_levels[level], lev))
122 level--;
123
124 return level;
125}
126
127
128/*
129 * returns log facility for <fac> or -1 if not found.
130 */
131int get_log_facility(const char *fac)
132{
133 int facility;
134
135 facility = NB_LOG_FACILITIES - 1;
136 while (facility >= 0 && strcmp(log_facilities[facility], fac))
137 facility--;
138
139 return facility;
140}
141
142
Willy Tarreaubaaee002006-06-26 02:48:02 +0200143/*
144 * This function sends a syslog message to both log servers of a proxy,
145 * or to global log servers if the proxy is NULL.
146 * It also tries not to waste too much time computing the message header.
147 * It doesn't care about errors nor does it report them.
148 */
Willy Tarreaub17916e2006-10-15 15:17:57 +0200149void send_log(struct proxy *p, int level, const char *message, ...)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200150{
151 static int logfd = -1; /* syslog UDP socket */
152 static long tvsec = -1; /* to force the string to be initialized */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200153 va_list argp;
154 static char logmsg[MAX_SYSLOG_LEN];
155 static char *dataptr = NULL;
156 int fac_level;
157 int hdr_len, data_len;
158 struct sockaddr_in *sa[2];
159 int facilities[2], loglevel[2];
160 int nbloggers = 0;
161 char *log_ptr;
162
163 if (logfd < 0) {
164 if ((logfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
165 return;
Willy Tarreau3d089532007-09-17 10:56:13 +0200166 /* we don't want to receive anything on this socket */
167 setsockopt(logfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero));
168 shutdown(logfd, SHUT_RD); /* does nothing under Linux, maybe needed for others */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200169 }
170
171 if (level < 0 || progname == NULL || message == NULL)
172 return;
173
Willy Tarreau2b35c952006-10-15 15:25:48 +0200174 if (now.tv_sec != tvsec || dataptr == NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175 /* this string is rebuild only once a second */
Willy Tarreaubf736132006-10-15 22:54:47 +0200176 struct tm *tm = localtime((time_t *)&now.tv_sec);
Willy Tarreau2b35c952006-10-15 15:25:48 +0200177 tvsec = now.tv_sec;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200178
179 hdr_len = snprintf(logmsg, sizeof(logmsg),
180 "<<<<>%s %2d %02d:%02d:%02d %s[%d]: ",
181 monthname[tm->tm_mon],
182 tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
183 progname, pid);
184 /* WARNING: depending upon implementations, snprintf may return
185 * either -1 or the number of bytes that would be needed to store
186 * the total message. In both cases, we must adjust it.
187 */
188 if (hdr_len < 0 || hdr_len > sizeof(logmsg))
189 hdr_len = sizeof(logmsg);
190
191 dataptr = logmsg + hdr_len;
192 }
193
194 va_start(argp, message);
Willy Tarreau8d5d7f22007-01-21 19:16:41 +0100195 /*
196 * FIXME: we take a huge performance hit here. We might have to replace
197 * vsnprintf() for a hard-coded log writer.
198 */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200199 data_len = vsnprintf(dataptr, logmsg + sizeof(logmsg) - dataptr, message, argp);
200 if (data_len < 0 || data_len > (logmsg + sizeof(logmsg) - dataptr))
201 data_len = logmsg + sizeof(logmsg) - dataptr;
202 va_end(argp);
203 dataptr[data_len - 1] = '\n'; /* force a break on ultra-long lines */
204
205 if (p == NULL) {
206 if (global.logfac1 >= 0) {
207 sa[nbloggers] = &global.logsrv1;
208 facilities[nbloggers] = global.logfac1;
209 loglevel[nbloggers] = global.loglev1;
210 nbloggers++;
211 }
212 if (global.logfac2 >= 0) {
213 sa[nbloggers] = &global.logsrv2;
214 facilities[nbloggers] = global.logfac2;
215 loglevel[nbloggers] = global.loglev2;
216 nbloggers++;
217 }
218 } else {
219 if (p->logfac1 >= 0) {
220 sa[nbloggers] = &p->logsrv1;
221 facilities[nbloggers] = p->logfac1;
222 loglevel[nbloggers] = p->loglev1;
223 nbloggers++;
224 }
225 if (p->logfac2 >= 0) {
226 sa[nbloggers] = &p->logsrv2;
227 facilities[nbloggers] = p->logfac2;
228 loglevel[nbloggers] = p->loglev2;
229 nbloggers++;
230 }
231 }
232
233 while (nbloggers-- > 0) {
234 /* we can filter the level of the messages that are sent to each logger */
235 if (level > loglevel[nbloggers])
236 continue;
237
238 /* For each target, we may have a different facility.
239 * We can also have a different log level for each message.
240 * This induces variations in the message header length.
241 * Since we don't want to recompute it each time, nor copy it every
242 * time, we only change the facility in the pre-computed header,
243 * and we change the pointer to the header accordingly.
244 */
245 fac_level = (facilities[nbloggers] << 3) + level;
246 log_ptr = logmsg + 3; /* last digit of the log level */
247 do {
248 *log_ptr = '0' + fac_level % 10;
249 fac_level /= 10;
250 log_ptr--;
251 } while (fac_level && log_ptr > logmsg);
252 *log_ptr = '<';
253
254 /* the total syslog message now starts at logptr, for dataptr+data_len-logptr */
255
256#ifndef MSG_NOSIGNAL
257 sendto(logfd, log_ptr, dataptr + data_len - log_ptr, MSG_DONTWAIT,
258 (struct sockaddr *)sa[nbloggers], sizeof(**sa));
259#else
260 sendto(logfd, log_ptr, dataptr + data_len - log_ptr, MSG_DONTWAIT | MSG_NOSIGNAL,
261 (struct sockaddr *)sa[nbloggers], sizeof(**sa));
262#endif
263 }
264}
265
266
267/*
268 * send a log for the session when we have enough info about it
269 */
Willy Tarreau42250582007-04-01 01:30:43 +0200270void tcp_sess_log(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200271{
272 char pn[INET6_ADDRSTRLEN + strlen(":65535")];
Willy Tarreau73de9892006-11-30 11:40:23 +0100273 struct proxy *fe = s->fe;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100274 struct proxy *be = s->be;
275 struct proxy *prx_log;
Willy Tarreau42250582007-04-01 01:30:43 +0200276 int tolog;
277 char *svid;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200278 struct tm *tm;
279
Willy Tarreaubaaee002006-06-26 02:48:02 +0200280 if (s->cli_addr.ss_family == AF_INET)
281 inet_ntop(AF_INET,
282 (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr,
283 pn, sizeof(pn));
284 else
285 inet_ntop(AF_INET6,
286 (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr,
287 pn, sizeof(pn));
288
Willy Tarreaubf736132006-10-15 22:54:47 +0200289 tm = localtime((time_t *)&s->logs.tv_accept.tv_sec);
Willy Tarreauddb358d2006-12-17 22:55:52 +0100290
291 if (fe->logfac1 >= 0)
292 prx_log = fe;
Willy Tarreau97de6242006-12-27 17:18:38 +0100293 /*
294 * FIXME: should we fall back to the backend if the frontend did not
295 * define any log ? It seems like we should not permit such complex
296 * setups because they would induce a debugging nightmare for the
297 * admin.
298 */
299 // else if (be->logfac1 >= 0)
300 // prx_log = be;
Willy Tarreauddb358d2006-12-17 22:55:52 +0100301 else
302 prx_log = NULL; /* global */
303
Willy Tarreau42250582007-04-01 01:30:43 +0200304 /* FIXME: let's limit ourselves to frontend logging for now. */
305 tolog = fe->to_log;
306 svid = (tolog & LW_SVID) ? (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "-";
Willy Tarreaubaaee002006-06-26 02:48:02 +0200307
Willy Tarreau42250582007-04-01 01:30:43 +0200308 send_log(prx_log, LOG_INFO, "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]"
309 " %s %s/%s %d/%d/%s%d %s%lld"
310 " %c%c %d/%d/%d/%d %d/%d\n",
311 pn,
312 (s->cli_addr.ss_family == AF_INET) ?
313 ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) :
314 ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port),
315 tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900,
316 tm->tm_hour, tm->tm_min, tm->tm_sec, s->logs.tv_accept.tv_usec/1000,
317 fe->id, be->id, svid,
318 (s->logs.t_queue >= 0) ? s->logs.t_queue : -1,
319 (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1,
320 (tolog & LW_BYTES) ? "" : "+", s->logs.t_close,
321 (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in,
322 sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT],
323 sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT],
324 actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0,
325 s->logs.srv_queue_size, s->logs.prx_queue_size);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326
327 s->logs.logwait = 0;
328}
329
Willy Tarreaubaaee002006-06-26 02:48:02 +0200330
331/*
332 * Local variables:
333 * c-indent-level: 8
334 * c-basic-offset: 8
335 * End:
336 */