Haproxy

HAProxy logs to a syslog facility local0, via a socket connection. By default, your syslog configuration probably doesn’t accept socket connections, and doesn’t have a local0 facility, so you have no HAProxy log. If you want it, configure syslog to accept TCP connections by adding -r to syslogd parameters.

RedHat/CentOS: edit the value of SYSLOGD_OPTIONS in /etc/sysconfig/syslog
SYSLOGD_OPTIONS="-m 0 -r"
 
Debian/Ubuntu: edit the value of SYSLOGD in /etc/default/syslogd
SYSLOGD="-r"
 
Then set up syslog facility local0 and direct it to file /var/log/haproxy.log by adding this line to /etc/syslog.conf:
 
local0.* /var/log/haproxy.log
 
Restart syslog service:
 
service syslog restart

Haproxy配置文件

haproxy + mysql配置文件

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
 
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    log         127.0.0.1 local6
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     20000
    ulimit-n    65535
    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
    option      redispatch
    option      contstats
    retries     3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          70s
    timeout server          70s
    timeout check           10s
 
 
#---------------------------------------------------------------------
# statistics setting
#---------------------------------------------------------------------
listen admin_stats
    bind  *:1080
    log   127.0.0.1 local6 err
    stats enable
    stats refresh 1m
    stats uri /admin?stats
    stats realm MOBCON\ Haproxy
    stats auth admin:admin
    stats hide-version
    stats show-legends
    stats admin if TRUE
 
 
#---------------------------------------------------------------------
# health check setting
#---------------------------------------------------------------------
listen site_status
    bind 0.0.0.0:1081
    mode http
    monitor-uri          /site_status
    acl                          site_dead nbsrv(tomcatcluster) lt 2
    monitor                      fail if site_dead
 
 
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend http-in
    bind                         *:80
    log                          global
    option                       httplog
    option                       logasap
    option                       dontlognull
    option                       forwardfor
    errorfile            503            /etc/haproxy/errfile/sorry.http
    errorfile            504            /etc/haproxy/errfile/sorry.http
 
    acl              uri_manager path_beg /manager /config /loader /logmon
    acl              inside_ip src 10.0.0.0/8 192.168.0.0/16 221.246.250.70
    block if ! inside_ip uri_manager
 
    rsprep ^Server:\ (.*)$ Server:\ Mobcon\ Server if ! inside_ip
 
    rspadd FrontServer:\ <%= hostname %> if inside_ip
 
    default_backend  tomcatcluster
 
    # log the name of the virtual server
    capture request  header Host len 2048
 
    # log the name of the https connection server
    capture request  header X-MOBCON-SECURE len 2048
 
    # log the name of the  user-agent
    capture request  header User-Agent len 2048
 
    # log the amount of data uploaded during a POST
    capture request  header Content-Length len 2048
 
    # log the beginning of the referrer
    capture request  header Referer len 2048
 
    # log the beginning of the Cookie
    capture request  header Cookie len 2048
 
    # log the URL location during a redirection
    capture response header Content-Type len 2048
 
    # logging the content-length is useful with "option logasap"
    capture response header Content-Length len 2048
 
#---------------------------------------------------------------------
# static backend for serving up sorry page and such
#---------------------------------------------------------------------
#backend static
#       balance     roundrobin
 
#---------------------------------------------------------------------
# round robin balancing between the tomcatcluster backends
#---------------------------------------------------------------------
backend tomcatcluster
    balance     leastconn
        option  httpchk HEAD /monitor/localhost/ HTTP/1.1\r\nHost:\ m2.mobcon.jp\r\nUser-Agent:\ Mozilla\/5.0\ (iPhone;\ U;\ CPU\ iPhone\ OS\ 2_0_1\ like\ Mac\ OS\ X;\ ja-jp)\
AppleWebKit\/525.18.1\ (KHTML,\ like Gecko)\ Version\/3.1.1\ Mobile\/5B108\ Safari\/525.20\r\nConnection: Close\r\n\r\n
        <%if ! frontnode.empty? -%>
        <% frontnode.keys.sort.each do |nodename| -%>
        server  <%= nodename -%> <%= frontnode[nodename] -%> check inter 2000 rise 2 fall 3<% end -%>
        <% end -%>
 
        <%if ! backnode.empty? -%>
        <% backnode.keys.sort.each do |nodename| -%>
        server  <%= nodename -%> <%= backnode[nodename] -%> check backup inter 2000 rise 2 fall 3<% end -%>
        <% end -%>

haproxy log parse

Apr  6 15:35:48 localhost haproxy[20695]: 202.24.194.65:49503 [06/Apr/2012:15:35:48.704] http-in tomcatcluster/geki95105:tomcat1 0/0/0/219/+219 200 +317 - - ---- 2/2/2/1/0 0/0 {demo2.mobcon.jp||Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7||http://demo2.mobcon.jp/nissen/x3.nissen.co.jp/cate104/index.htm|X_MOBCON_UID=yagpfvvv33ouj0f0h} {image/png|9377} "GET /nissen/x3.nissen.co.jp/img/2012sum/nv_01_off.png HTTP/1.0"

python reg haproxy

haproxy_re = (
r'(?P<date>\w+\s+\d+\s+\d+:\d+:\d+) '
r'(?P<host>\w+) '
r'haproxy\[(?P<pid>\d+)\]: '
r'(?P<client_ip>(\d{1,3}\.){3}\d{1,3}):(?P<client_port>\d{1,5}) '
r'\[(?P<accept_date>.*)\] '
r'(?P<frontend_name>[\w-]+) '
r'(?P<backend_name>\w+)/(?P<backendnode>[\w:]+) '
r'(?P<Tq>(-1|\d+))/(?P<Tw>(-1|\d+))/(?P<Tc>(-1|\d+))/(?P<Tr>(-1|\d+))/(?P<Tt>\+?\d+) '
r'(?P<status_code>\d{3}) (?P<bytes_read>\+?\d+) '
r'(?P<captured_request_cookie>\S+) (?P<captured_response_cookie>\S+) '
r'(?P<termination_state>[\w-]{4}) (?P<actconn>\d+)/(?P<feconn>\d+)/'
r'(?P<beconn>\d+)/(?P<srv_conn>\d+)/(?P<retries>\d+) '
r'(?P<server_queue>\d+)/(?P<listener_queue>\d+) '
r'(\{(?P<domain>[\w\.-]+)\|(?P<ssl>[\w-]+)?\|(?P<user_agent>.*?)\|(?P<req_content>\d+)?\|(?P<referer>.*?)\|(?P<cookie>.*?)\} )?'
r'(\{(?P<rep_content_type>.*?)\|(?P<rep_content_length>\d+)\} )?'
r'("(?P<method>\w+) /(?P<serviceid>.*?)/(?P<siteid>.*?)/(?P<path>.*?) (?P<protocol>.*?)")'
)
haproxy_re = re.compile(haproxy_re)

haproxyログ切断問題

経緯: haproxy自分はログ出力機能が付いていないため、外部ログ管理ツールを利用しないといけない。

今回の構成変更でhaproxyのログがrsyslogに転送し、ローカールに保存しています。

問題 単行ログの長さより、haproxyやrsyslogに途中切断した場合があります。

通常rsyslogの単行長さは2048です。 haproxyは1024ですが、URIの長さも1024の制限があります。

特にURIが長い場合は、問題が出てる

解決方法:

  • rsyslogの設定ファイル/修正

defaultが2Kです

vi /etc/rsyslog.conf

$MaxMessageSize 16k
  • haproxyのソースコードを修正し、再作成します。

buffer、uri関数定義は下記の様な変更する

vi /root/haproxy-1.4.20/include/common/defaults.h
#define BUFSIZE         16384
#define REQURI_LEN      32768
#define CAPTURE_LEN     32768

ログ長さ制限修正

vi /root/haproxy-1.4.20/include/types/log.h
#define MAX_SYSLOG_LEN          32768

haproxy-1.4.20のフォルダに戻って、haproxyをmakeする

 make TARGET=linux26\

新しいhaproxyコマンドを配布すると完了

/var/lib/openshift/bccd8eac1968476490eaee9ced33c7bf/app-root/runtime/repo/php/data/pages/haproxy.txt · 最后更改: 2012/09/07 09:21 (外部编辑)
到顶部
CC Attribution-Noncommercial-Share Alike 3.0 Unported
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0