Enable logging in Haproxy.

HAProxy Log Setup


  1. HAProxy wants to log.  
  2. First HAProxy does not log directly into a file due to performance reason. So we need to handle  that with syslog server. But Haproxy also requires a syslog to listen on UDP port ( which in default syslog/rsyslog installation is not enabled ).  Basically log enable format in haproxy cfg is 

“log <address> <facility> [max level [min level]]”

Note:-  Adds a global syslog server. Up to two global servers can be defined. They will receive logs for startups and exits, as well as all logs from proxies configured        with "log global”.
  <address> can be one of:
       - An IPv4 address optionally followed by a colon and a UDP port. If no port is specified, 514 is used by default (the standard syslog port).
       - A filesystem path to a UNIX domain socket, keeping in mind considerations for chroot (be sure the path is accessible inside the chroot) and uid/gid (be sure the path is appropriately writeable).
  <facility> must be one of the 24 standard syslog facilities :
         kern   user   mail   daemon auth   syslog lpr    news
          uucp   cron   auth2  ftp    ntp    audit  alert  cron2
          local0 local1 local2 local3 local4 local5 local6 local7
  An optional level can be specified to filter outgoing messages. Eight levels are known :
         emerg  alert  crit   err    warning notice info  debug

Example:-
   log global 
   log 127.0.0.1:514(default port) local0 notice         # only send important events
   log 127.0.0.1:514(default port) local0 notice notice  # same but limit output level

Note:- For performance & maintenance reasons HAProxy doesn't log directly to files. Instead it wants to log against a syslog server. This is a separate Linux daemon that most servers are equiped with already, but HAProxy requires it to listen on UDP port 514 (Default Port), and usually that's not enabled.


HAProxy Log Enable for Centos/RHEL machine:- 

  1. Create rsyslog configuration file 
             vim  /etc/rsyslog/haproxy.conf
           add these lines   #Basically this  enable all type of logs .
         
         # Enable UDP port 514 to listen to incoming log messages from haproxy
         $ModLoad imudp
         $UDPServerRun 514
         $template Haproxy,"%msg%\n"
          local0.crit -/var/log/haproxy.log;Haproxy #For Critical log only
          #local0.=info -/var/log/haproxy/haproxy.log;Haproxy  # this line enable log everything.
          local0.notice -/var/log/haproxy/admin.log;Haproxy
         # don't log anywhere else
         local0.* ~

     Restart rsyslog service.

#For Logrotation.
  Add these lines in /etc/logrotaion.d/haproxy

2. /var/log/haproxy.log {3
                          daily 
                          rotate 10 
                          missingok 
                          notifempty 
                          compress 
                          sharedscripts 
                          postrotate 
                                   /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true 
                                  /bin/kill -HUP `cat /var/run/rsyslogd.pid 2> /dev/null` 2> /dev/null || true 
                          endscript
                         }

     Reference:- 


Note:- 
  1. /bin/kill -HUP ‘any process name /or pid ‘ so basically HUP is a signal, usually sent to a program to request that it restarts and re-reads all its configuration in the process. (HANGUP SIGNAL)
  2. Now shared scripts , basically a  prescript and postscript scripts are run for each log which is rotated, meaning that a single script may be run multiple times for log file entries which match multiple files (such as the /var/log/haproxy*.log example)  If sharedscript is specified, the scripts are only run once, no matter how many logs match the  wildcarded pattern. However, if none of the logs in the pattern option overrides the nosharedscripts option and implies  create option.

Observation :- 

  1. If rsyslog server is down than it will not effect on haproxy But Log will not be written. 
  2. if not want to reload rsyslog config than use this for log rotation
          var/log/haproxy.log   
                        {  
                            missingok
                            daily
                            rotate 10
                            copytruncate
                            compress
                            dateext
                            dateformat %Y%m%d%s
                      }



Comments

Popular posts from this blog

Take AWS ec2 volume instance snapshot

Edit Distance Problem