]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] log: add support for passing the forwarded hostname
authorJoe Williams <joe@joetify.com>
Wed, 29 Dec 2010 16:05:48 +0000 (17:05 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 29 Dec 2010 16:05:48 +0000 (17:05 +0100)
Haproxy does not include the hostname rather the IP of the machine in
the syslog headers it sends. Unfortunately this means that for each log
line rsyslog does a reverse dns on the client IP and in the case of
non-routable IPs one gets the public hostname not the internal one.

While this is valid according to RFC3164 as one might imagine this is
troublsome if you have some machines with public IPs, internal IPs, no
reverse DNS entries, etc and you want a standardized hostname based log
directory structure. The rfc says the preferred value is the hostname.

This patch adds a global "log-send-hostname" statement which accepts an
optional string to force the host name. If unset, the local host name
is used.

doc/configuration.txt
include/types/global.h
src/cfgparse.c
src/haproxy.c
src/log.c

index 6f30014eb794c6cc7fa7350c384210b71423809e..f20b8b2ea09e3821ac25abab8bc812b8d58c3554 100644 (file)
@@ -435,6 +435,7 @@ The following keywords are supported in the "global" section :
    - gid
    - group
    - log
+   - log-send-hostname
    - nbproc
    - pidfile
    - uid
@@ -527,6 +528,13 @@ log <address> <facility> [max level [min level]]
 
          emerg  alert  crit   err    warning notice info  debug
 
+log-send-hostname [<string>]
+  Sets the hostname field in the syslog header. If optional "string" parameter
+  is set the header is set to the string contents, otherwise uses the hostname
+  of the system. Generally used if one is not relaying logs through an
+  intermediate syslog server or for simply customizing the hostname printed in
+  the logs.
+
 nbproc <number>
   Creates <number> processes when going daemon. This requires the "daemon"
   mode. By default, only one process is created, which is the recommended mode
index 906a52cc9a15ca23521468117f7c5c6279090cfd..faf9bb28d060071e1fcb59b1c26e3ec44fd3f3da 100644 (file)
@@ -80,6 +80,7 @@ struct global {
        int loglev1, loglev2;
        int minlvl1, minlvl2;
        struct logsrv logsrv1, logsrv2;
+       char *log_send_hostname;   /* set hostname in syslog header */
        struct {
                int maxpollevents; /* max number of poll events at once */
                int maxaccept;     /* max number of consecutive accept() */
index 9d3e2fc405cfa6c9fb1f766b4db11cd50537ae1b..ddfbe42536884acecbc7f8faacad1b1ce80fc0c5 100644 (file)
@@ -966,6 +966,28 @@ int cfg_parse_global(const char *file, int linenum, char **args, int kwm)
                        err_code |= ERR_ALERT | ERR_FATAL;
                }
        }
+       else if (!strcmp(args[0], "log-send-hostname")) { /* set the hostname in syslog header */
+               char *name;
+               int len;
+
+               if (global.log_send_hostname != NULL) {
+                       Alert("parsing [%s:%d] : '%s' already specified. Continuing.\n", file, linenum, args[0]);
+                       err_code |= ERR_ALERT;
+                       goto out;
+               }
+
+               if (*(args[1]))
+                       name = args[1];
+               else
+                       name = hostname;
+
+               len = strlen(name);
+
+               /* We'll add a space after the name to respect the log format */
+               free(global.log_send_hostname);
+               global.log_send_hostname = malloc(len + 2);
+               snprintf(global.log_send_hostname, len + 2, "%s ", name);
+       }
        else if (!strcmp(args[0], "spread-checks")) {  /* random time between checks (0-50) */
                if (global.spread_checks != 0) {
                        Alert("parsing [%s:%d]: spread-checks already specified. Continuing.\n", file, linenum);
index c5aa3ccc5aed845d6dfc07090a7999e68a392187..9ba08201a811052a0e4ef38afb01afdd233b5b0f 100644 (file)
@@ -867,6 +867,7 @@ void deinit(void)
 
        protocol_unbind_all();
 
+       free(global.log_send_hostname); global.log_send_hostname = NULL;
        free(global.chroot);  global.chroot = NULL;
        free(global.pidfile); global.pidfile = NULL;
        free(global.node);    global.node = NULL;
index d1387b5f7c995d9496f58b314eda6fec8f04b274..d52727b6f6fdaba07a333b12ccd34a1ee15e0eed 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -189,9 +189,10 @@ void send_log(struct proxy *p, int level, const char *message, ...)
                get_localtime(tvsec, &tm);
 
                hdr_len = snprintf(logmsg, sizeof(logmsg),
-                                  "<<<<>%s %2d %02d:%02d:%02d %s[%d]: ",
+                                  "<<<<>%s %2d %02d:%02d:%02d %s%s[%d]: ",
                                   monthname[tm.tm_mon],
                                   tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
+                                  global.log_send_hostname ? global.log_send_hostname : "",
                                   progname, pid);
                /* WARNING: depending upon implementations, snprintf may return
                 * either -1 or the number of bytes that would be needed to store