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 | |
| 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 Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 23 | #include <common/config.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 24 | #include <common/standard.h> |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 25 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | |
| 27 | #include <types/backend.h> |
| 28 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | #include <types/log.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 30 | #include <types/session.h> |
| 31 | |
| 32 | |
| 33 | const 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 | |
| 43 | const char *log_levels[NB_LOG_LEVELS] = { |
| 44 | "emerg", "alert", "crit", "err", |
| 45 | "warning", "notice", "info", "debug" |
| 46 | }; |
| 47 | |
| 48 | const char *monthname[12] = { |
| 49 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 50 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 51 | }; |
| 52 | |
| 53 | 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] | 54 | 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] | 55 | |
| 56 | /* |
| 57 | * Displays the message on stderr with the date and pid. Overrides the quiet |
| 58 | * mode during startup. |
| 59 | */ |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 60 | void Alert(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | { |
| 62 | va_list argp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 63 | struct tm *tm; |
| 64 | |
| 65 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
| 66 | va_start(argp, fmt); |
| 67 | |
Willy Tarreau | bf73613 | 2006-10-15 22:54:47 +0200 | [diff] [blame] | 68 | tm = localtime((time_t *)&now.tv_sec); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 69 | 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 Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 81 | void Warning(const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 82 | { |
| 83 | va_list argp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 84 | struct tm *tm; |
| 85 | |
| 86 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) { |
| 87 | va_start(argp, fmt); |
| 88 | |
Willy Tarreau | bf73613 | 2006-10-15 22:54:47 +0200 | [diff] [blame] | 89 | tm = localtime((time_t *)&now.tv_sec); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 90 | 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 Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 101 | void qfprintf(FILE *out, const char *fmt, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | { |
| 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 | */ |
| 116 | int 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 | */ |
| 131 | int 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 143 | /* |
| 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 Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 149 | void send_log(struct proxy *p, int level, const char *message, ...) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 150 | { |
| 151 | static int logfd = -1; /* syslog UDP socket */ |
| 152 | static long tvsec = -1; /* to force the string to be initialized */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 153 | 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 Tarreau | 3d08953 | 2007-09-17 10:56:13 +0200 | [diff] [blame^] | 166 | /* 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | if (level < 0 || progname == NULL || message == NULL) |
| 172 | return; |
| 173 | |
Willy Tarreau | 2b35c95 | 2006-10-15 15:25:48 +0200 | [diff] [blame] | 174 | if (now.tv_sec != tvsec || dataptr == NULL) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 175 | /* this string is rebuild only once a second */ |
Willy Tarreau | bf73613 | 2006-10-15 22:54:47 +0200 | [diff] [blame] | 176 | struct tm *tm = localtime((time_t *)&now.tv_sec); |
Willy Tarreau | 2b35c95 | 2006-10-15 15:25:48 +0200 | [diff] [blame] | 177 | tvsec = now.tv_sec; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 178 | |
| 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 Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 195 | /* |
| 196 | * FIXME: we take a huge performance hit here. We might have to replace |
| 197 | * vsnprintf() for a hard-coded log writer. |
| 198 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 199 | 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 Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 270 | void tcp_sess_log(struct session *s) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 271 | { |
| 272 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 273 | struct proxy *fe = s->fe; |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 274 | struct proxy *be = s->be; |
| 275 | struct proxy *prx_log; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 276 | int tolog; |
| 277 | char *svid; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 278 | struct tm *tm; |
| 279 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 280 | 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 Tarreau | bf73613 | 2006-10-15 22:54:47 +0200 | [diff] [blame] | 289 | tm = localtime((time_t *)&s->logs.tv_accept.tv_sec); |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 290 | |
| 291 | if (fe->logfac1 >= 0) |
| 292 | prx_log = fe; |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 293 | /* |
| 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 Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 301 | else |
| 302 | prx_log = NULL; /* global */ |
| 303 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 304 | /* 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 307 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 308 | 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 Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 326 | |
| 327 | s->logs.logwait = 0; |
| 328 | } |
| 329 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 330 | |
| 331 | /* |
| 332 | * Local variables: |
| 333 | * c-indent-level: 8 |
| 334 | * c-basic-offset: 8 |
| 335 | * End: |
| 336 | */ |