通过日志工作分析日志时,非json日志分析起来比较麻烦。通过以下的配置,可以让生成的日志为json。
log-format {"haproxy_clientIP":"%ci","haproxy_clientPort":"%cp","haproxy_dateTime":"%t","haproxy_frontendNameTransport":"%ft","haproxy_backend":"%b","haproxy_serverName":"%s","haproxy_Tw":"%Tw","haproxy_Tc":"%Tc","haproxy_Tt":"%Tt","haproxy_bytesRead":"%B","haproxy_terminationState":"%ts","haproxy_actconn":%ac,"haproxy_FrontendCurrentConn":%fc,"haproxy_backendCurrentConn":%bc,"haproxy_serverConcurrentConn":%sc,"haproxy_retries":%rc,"haproxy_srvQueue":%sq,"haproxy_backendQueue":%bq,"haproxy_backendSourceIP":"%bi","haproxy_backendSourcePort":"%bp","http_request":%r,"request_headers":"%hrl","response_headers":"%hsl","status_code":"%ST"}
配置实例:
| # Global settings |
| #--------------------------------------------------------------------- |
| global |
| log 127.0.0.1 local2 |
| log-send-hostname |
| chroot /var/lib/haproxy |
| pidfile /var/run/haproxy.pid |
| maxconn 30000 |
| user haproxy |
| group haproxy |
| daemon |
| # turn on stats unix socket |
| stats socket /var/lib/haproxy/stats |
| #--------------------------------------------------------------------- |
| # common defaults that all the 'listen' and 'backend' sections will |
| # use if not designated in their block |
| #--------------------------------------------------------------------- |
| defaults |
| mode http |
| log global |
| option httplog |
| option dontlognull |
| option http-server-close |
| option forwardfor except 127.0.0.0/8 |
| option redispatch |
| retries 3 |
| timeout http-request 10s |
| timeout queue 1m |
| timeout connect 10s |
| timeout client 1m |
| timeout server 1m |
| timeout http-keep-alive 10s |
| timeout check 10s |
| maxconn 50000 |
| #--------------------------------------------------------------------- |
| #--------------------------------------------------------------------- |
| listen myapp1 :22222 |
| mode tcp |
| option tcplog |
| log-format {"haproxy_clientIP":"%ci","haproxy_clientPort":"%cp","haproxy_dateTime":"%t","haproxy_frontendNameTransport":"%ft","haproxy_backend":"%b","haproxy_serverName":"%s","haproxy_Tw":"%Tw","haproxy_Tc":"%Tc","haproxy_Tt":"%Tt","haproxy_bytesRead":"%B","haproxy_terminationState":"%ts","haproxy_actconn":%ac,"haproxy_FrontendCurrentConn":%fc,"haproxy_backendCurrentConn":%bc,"haproxy_serverConcurrentConn":%sc,"haproxy_retries":%rc,"haproxy_srvQueue":%sq,"haproxy_backendQueue":%bq,"haproxy_backendSourceIP":"%bi","haproxy_backendSourcePort":"%bp"} |
| option logasap |
| balance leastconn |
| server server1 xx.xx.xx.xx:22222 |
| server server2 xx.xx.xx.xx:22222 |
| server server3 xx.xx.xx.xx:22222 |
| listen myapp2 :22201 |
| mode tcp |
| option tcplog |
| log-format {"haproxy_clientIP":"%ci","haproxy_clientPort":"%cp","haproxy_dateTime":"%t","haproxy_frontendNameTransport":"%ft","haproxy_backend":"%b","haproxy_serverName":"%s","haproxy_Tw":"%Tw","haproxy_Tc":"%Tc","haproxy_Tt":"%Tt","haproxy_bytesRead":"%B","haproxy_terminationState":"%ts","haproxy_actconn":%ac,"haproxy_FrontendCurrentConn":%fc,"haproxy_backendCurrentConn":%bc,"haproxy_serverConcurrentConn":%sc,"haproxy_retries":%rc,"haproxy_srvQueue":%sq,"haproxy_backendQueue":%bq,"haproxy_backendSourceIP":"%bi","haproxy_backendSourcePort":"%bp"} |
| option logasap |
| balance leastconn |
| server server1 xx.xx.xx.xx:22201 |
| server server2 xx.xx.xx.xx:22201 |
| server server3 xx.xx.xx.xx:22201 |
| # Interface Web |
| listen stats *:80 |
| stats enable |
| stats uri / |
| stats refresh 5s |
| stats hide-version |
来自 <https://github.com/jothoma1/graylog-contentpack-haproxy/blob/master/sample-haproxy.cfg>