Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 2 | * include/haproxy/log.h |
| 3 | * This file contains definitions of log-related functions. |
| 4 | * |
| 5 | * Copyright (C) 2000-2020 Willy Tarreau - w@1wt.eu |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 22 | #ifndef _HAPROXY_LOG_H |
| 23 | #define _HAPROXY_LOG_H |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 25 | #include <syslog.h> |
| 26 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 27 | #include <haproxy/api.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 28 | #include <haproxy/log-t.h> |
| 29 | #include <haproxy/pool-t.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 30 | #include <haproxy/proxy-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 31 | #include <haproxy/stream.h> |
Willy Tarreau | 43c538e | 2018-09-05 14:58:15 +0200 | [diff] [blame] | 32 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 33 | extern struct pool_head *pool_head_requri; |
| 34 | extern struct pool_head *pool_head_uniqueid; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 35 | |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 36 | extern const char *log_levels[]; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 37 | extern char *log_format; |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 38 | extern char default_tcp_log_format[]; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 39 | extern char default_http_log_format[]; |
| 40 | extern char clf_http_log_format[]; |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 41 | |
Dragan Dosen | 0b85ece | 2015-09-25 19:17:44 +0200 | [diff] [blame] | 42 | extern char default_rfc5424_sd_log_format[]; |
| 43 | |
Willy Tarreau | 13ef773 | 2018-11-12 07:25:28 +0100 | [diff] [blame] | 44 | extern unsigned int dropped_logs; |
| 45 | |
Emeric Brun | 12941c8 | 2020-07-07 14:19:42 +0200 | [diff] [blame] | 46 | /* lof forward proxy list */ |
| 47 | extern struct proxy *cfg_log_forward; |
| 48 | |
Christopher Faulet | f8188c6 | 2017-06-02 16:20:16 +0200 | [diff] [blame] | 49 | extern THREAD_LOCAL char *logline; |
| 50 | extern THREAD_LOCAL char *logline_rfc5424; |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 51 | |
Emeric Brun | 45c457a | 2020-07-09 23:23:34 +0200 | [diff] [blame^] | 52 | /* global syslog message counter */ |
| 53 | extern int cum_log_messages; |
| 54 | |
Emeric Brun | 54932b4 | 2020-07-07 09:43:24 +0200 | [diff] [blame] | 55 | /* syslog UDP message handler */ |
| 56 | void syslog_fd_handler(int fd); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 57 | |
Christopher Faulet | 0132d06 | 2017-07-26 15:33:35 +0200 | [diff] [blame] | 58 | /* Initialize/Deinitialize log buffers used for syslog messages */ |
| 59 | int init_log_buffers(); |
| 60 | void deinit_log_buffers(); |
| 61 | |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 62 | /* build a log line for the session and an optional stream */ |
Willy Tarreau | 43c538e | 2018-09-05 14:58:15 +0200 | [diff] [blame] | 63 | int sess_build_logline(struct session *sess, struct stream *s, char *dst, size_t maxsize, struct list *list_format); |
| 64 | |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 65 | /* |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 66 | * send a log for the stream when we have enough info about it. |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 67 | * Will not log if the frontend has no log defined. |
| 68 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 69 | void strm_log(struct stream *s); |
Willy Tarreau | 5383935 | 2018-09-05 19:51:10 +0200 | [diff] [blame] | 70 | void sess_log(struct session *sess); |
William Lallemand | bddd4fd | 2012-02-27 11:23:10 +0100 | [diff] [blame] | 71 | |
Christopher Faulet | 5c6fefc | 2019-08-11 19:40:12 +0200 | [diff] [blame] | 72 | /* send a applicative log with custom list of log servers */ |
| 73 | void app_log(struct list *logsrvs, struct buffer *tag, int level, const char *format, ...) |
| 74 | __attribute__ ((format(printf, 4, 5))); |
| 75 | |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 76 | /* |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 77 | * add to the logformat linked list |
| 78 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 79 | int add_to_logformat_list(char *start, char *end, int type, struct list *list_format, char **err); |
William Lallemand | 723b73a | 2012-02-08 16:37:49 +0100 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * Parse the log_format string and fill a linked list. |
| 83 | * Variable name are preceded by % and composed by characters [a-zA-Z0-9]* : %varname |
| 84 | * You can set arguments using { } : %{many arguments}varname |
| 85 | */ |
Thierry FOURNIER / OZON.IO | 8a4e442 | 2016-11-23 00:41:28 +0100 | [diff] [blame] | 86 | int parse_logformat_string(const char *str, struct proxy *curproxy, struct list *list_format, int options, int cap, char **err); |
Christopher Faulet | 4b0b79d | 2018-03-26 15:54:32 +0200 | [diff] [blame] | 87 | |
| 88 | /* Parse "log" keyword and update the linked list. */ |
| 89 | int parse_logsrv(char **args, struct list *logsrvs, int do_del, char **err); |
| 90 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 91 | /* |
William Lallemand | 2a4a44f | 2012-02-06 16:00:33 +0100 | [diff] [blame] | 92 | * This function adds a header to the message and sends the syslog message |
| 93 | * using a printf format string |
| 94 | */ |
| 95 | void send_log(struct proxy *p, int level, const char *format, ...) |
| 96 | __attribute__ ((format(printf, 3, 4))); |
| 97 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 98 | /* |
| 99 | * This function sends a syslog message to both log servers of a proxy, |
| 100 | * or to global log servers if the proxy is NULL. |
| 101 | * It also tries not to waste too much time computing the message header. |
| 102 | * It doesn't care about errors nor does it report them. |
| 103 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | |
Christopher Faulet | 5c6fefc | 2019-08-11 19:40:12 +0200 | [diff] [blame] | 105 | void __send_log(struct list *logsrvs, struct buffer *tag, int level, char *message, size_t size, char *sd, size_t sd_size); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 106 | |
| 107 | /* |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 108 | * returns log format for <fmt> or LOG_FORMAT_UNSPEC if not found. |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 109 | */ |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 110 | enum log_fmt get_log_format(const char *fmt); |
Dragan Dosen | 1322d09 | 2015-09-22 16:05:32 +0200 | [diff] [blame] | 111 | |
| 112 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | * returns log level for <lev> or -1 if not found. |
| 114 | */ |
| 115 | int get_log_level(const char *lev); |
| 116 | |
| 117 | /* |
| 118 | * returns log facility for <fac> or -1 if not found. |
| 119 | */ |
| 120 | int get_log_facility(const char *fac); |
| 121 | |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 122 | /* |
| 123 | * Write a string in the log string |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 124 | * Take cares of quote options |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 125 | * |
Joseph Herlant | 85b4059 | 2018-11-15 12:10:04 -0800 | [diff] [blame] | 126 | * Return the address of the \0 character, or NULL on error |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 127 | */ |
Olivier Houchard | 5462052 | 2018-09-06 18:14:09 +0200 | [diff] [blame] | 128 | char *lf_text_len(char *dst, const char *src, size_t len, size_t size, const struct logformat_node *node); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 129 | |
| 130 | /* |
Joseph Herlant | 85b4059 | 2018-11-15 12:10:04 -0800 | [diff] [blame] | 131 | * Write a IP address to the log string |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 132 | * +X option write in hexadecimal notation, most significant byte on the left |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 133 | */ |
Willy Tarreau | 26ffa85 | 2018-09-05 15:23:10 +0200 | [diff] [blame] | 134 | char *lf_ip(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 135 | |
| 136 | /* |
| 137 | * Write a port to the log |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 138 | * +X option write in hexadecimal notation, most significant byte on the left |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 139 | */ |
Willy Tarreau | 26ffa85 | 2018-09-05 15:23:10 +0200 | [diff] [blame] | 140 | char *lf_port(char *dst, const struct sockaddr *sockaddr, size_t size, const struct logformat_node *node); |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 141 | |
William Lallemand | a1cc381 | 2012-02-08 16:38:44 +0100 | [diff] [blame] | 142 | |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 143 | /* |
| 144 | * Function to handle log header building (exported for sinks) |
| 145 | */ |
Emeric Brun | 9f9b22c | 2020-07-02 16:16:59 +0200 | [diff] [blame] | 146 | char *update_log_hdr_rfc5424(const time_t time, suseconds_t frac); |
Emeric Brun | bd16381 | 2020-05-06 14:33:46 +0200 | [diff] [blame] | 147 | char *update_log_hdr(const time_t time); |
| 148 | char * get_format_pid_sep1(int format, size_t *len); |
| 149 | char * get_format_pid_sep2(int format, size_t *len); |
| 150 | |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 151 | /* |
| 152 | * Test if <idx> index numbered from 0 is in <rg> range with low and high |
| 153 | * limits of indexes numbered from 1. |
| 154 | */ |
| 155 | static inline int in_smp_log_range(struct smp_log_range *rg, unsigned int idx) |
| 156 | { |
| 157 | if (idx + 1 <= rg->high && idx + 1 >= rg->low) |
| 158 | return 1; |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Builds a log line for the stream (must be valid). |
| 164 | */ |
| 165 | static inline int build_logline(struct stream *s, char *dst, size_t maxsize, struct list *list_format) |
| 166 | { |
| 167 | return sess_build_logline(strm_sess(s), s, dst, maxsize, list_format); |
| 168 | } |
| 169 | |
Emeric Brun | 5464885 | 2020-07-06 15:54:06 +0200 | [diff] [blame] | 170 | struct ist *build_log_header(enum log_fmt format, int level, int facility, struct ist *metadata, size_t *nbelem); |
Emeric Brun | 45c457a | 2020-07-09 23:23:34 +0200 | [diff] [blame^] | 171 | |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 172 | #endif /* _HAPROXY_LOG_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 173 | |
| 174 | /* |
| 175 | * Local variables: |
| 176 | * c-indent-level: 8 |
| 177 | * c-basic-offset: 8 |
| 178 | * End: |
| 179 | */ |