Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 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 Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 13 | #include <fcntl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 14 | #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 Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 24 | #include <common/config.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 25 | #include <common/standard.h> |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 26 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | |
| 28 | #include <types/backend.h> |
| 29 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | #include <types/log.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | #include <types/session.h> |
| 32 | |
| 33 | |
| 34 | const 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 | |
| 44 | const char *log_levels[NB_LOG_LEVELS] = { |
| 45 | "emerg", "alert", "crit", "err", |
| 46 | "warning", "notice", "info", "debug" |
| 47 | }; |
| 48 | |
| 49 | const char *monthname[12] = { |
| 50 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 51 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 52 | }; |
| 53 | |
| 54 | const char sess_term_cond[8] = "-cCsSPRI"; /* normal, CliTo, CliErr, SrvTo, SrvErr, PxErr, Resource, Internal */ |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 55 | const char sess_fin_state[8] = "-RCHDLQT"; /* cliRequest, srvConnect, srvHeader, Data, Last, Queue, Tarpit */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * Displays the message on stderr with the date and pid. Overrides the quiet |
| 59 | * mode during startup. |
| 60 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 61 | void Alert(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 62 | { |
| 63 | va_list argp; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 64 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 65 | |
| 66 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
| 67 | va_start(argp, fmt); |
| 68 | |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 69 | get_localtime(now.tv_sec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 70 | fprintf(stderr, "[ALERT] %03d/%02d%02d%02d (%d) : ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 71 | tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid()); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 72 | 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 Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 82 | void Warning(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 83 | { |
| 84 | va_list argp; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 85 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 86 | |
| 87 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 88 | va_start(argp, fmt); |
| 89 | |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 90 | get_localtime(now.tv_sec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 91 | fprintf(stderr, "[WARNING] %03d/%02d%02d%02d (%d) : ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 92 | tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, (int)getpid()); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 93 | 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 Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 102 | void qfprintf(FILE *out, const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 103 | { |
| 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 | */ |
| 117 | int 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 | */ |
| 132 | int 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 144 | /* |
| 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 Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 150 | void send_log(struct proxy *p, int level, const char *message, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 151 | { |
| 152 | static int logfd = -1; /* syslog UDP socket */ |
| 153 | static long tvsec = -1; /* to force the string to be initialized */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 154 | 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 Tarreau | 3d08953 | 2007-09-17 10:56:13 +0200 | [diff] [blame] | 167 | /* we don't want to receive anything on this socket */ |
| 168 | setsockopt(logfd, SOL_SOCKET, SO_RCVBUF, &zero, sizeof(zero)); |
Willy Tarreau | c8f24f8 | 2007-11-30 18:38:35 +0100 | [diff] [blame] | 169 | /* need for AIX which does not know about MSG_DONTWAIT */ |
| 170 | if (!MSG_DONTWAIT) |
| 171 | fcntl(logfd, F_SETFL, O_NONBLOCK); |
Willy Tarreau | 3d08953 | 2007-09-17 10:56:13 +0200 | [diff] [blame] | 172 | shutdown(logfd, SHUT_RD); /* does nothing under Linux, maybe needed for others */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | if (level < 0 || progname == NULL || message == NULL) |
| 176 | return; |
| 177 | |
Willy Tarreau | 2b35c95 | 2006-10-15 15:25:48 +0200 | [diff] [blame] | 178 | if (now.tv_sec != tvsec || dataptr == NULL) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 179 | /* this string is rebuild only once a second */ |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 180 | struct tm tm; |
| 181 | |
Willy Tarreau | 2b35c95 | 2006-10-15 15:25:48 +0200 | [diff] [blame] | 182 | tvsec = now.tv_sec; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 183 | get_localtime(tvsec, &tm); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 184 | |
| 185 | hdr_len = snprintf(logmsg, sizeof(logmsg), |
| 186 | "<<<<>%s %2d %02d:%02d:%02d %s[%d]: ", |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 187 | monthname[tm.tm_mon], |
| 188 | tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 189 | 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 Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 201 | /* |
| 202 | * FIXME: we take a huge performance hit here. We might have to replace |
| 203 | * vsnprintf() for a hard-coded log writer. |
| 204 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 205 | 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 Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 276 | void tcp_sess_log(struct session *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 277 | { |
| 278 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 279 | struct proxy *fe = s->fe; |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 280 | struct proxy *be = s->be; |
| 281 | struct proxy *prx_log; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 282 | int tolog; |
| 283 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 284 | struct tm tm; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 285 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 286 | 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 Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 295 | get_localtime(s->logs.tv_accept.tv_sec, &tm); |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 296 | |
| 297 | if (fe->logfac1 >= 0) |
| 298 | prx_log = fe; |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 299 | /* |
| 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 Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 307 | else |
| 308 | prx_log = NULL; /* global */ |
| 309 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 310 | /* 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 313 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 314 | 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 Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 321 | 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 Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 323 | 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 332 | |
| 333 | s->logs.logwait = 0; |
| 334 | } |
| 335 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 336 | |
| 337 | /* |
| 338 | * Local variables: |
| 339 | * c-indent-level: 8 |
| 340 | * c-basic-offset: 8 |
| 341 | * End: |
| 342 | */ |