HaProxy statistics

Templates, scripts for templates, scripts and requests for templates.

Moderators: Developers, Moderators

BWare
Posts: 33
Joined: Thu Jun 12, 2003 4:57 am
Location: Amsterdam
Contact:

HaProxy statistics

Post by BWare »

I know there is a different method available for monitoring haproxy statistics, but it involves perl as an snmp subagent, which is unusable in my environment, so I ended up in writing my own solution...

First of all I use the snmp 'pass' method to execute a shellscript in snmpd.conf:

Code: Select all

pass .1.3.6.1.4.1.2021.83 /bin/bash /path/to/scripts/haproxy.sh
The script looks like this and requires 'socat' to obtain the statistics via the haproxy socket:

Code: Select all

#!/bin/bash                       
###########                       
#                                 
function getData() {              
        let OBJTYPE=$1                  ; # OID.1.1.1.y
        let OBJTYPE--                                  
        let OBJID=$2                    ; # OID.1.1.X.y

        OLDIFS=$IFS
        IFS=","    
        OBJECT=( $( echo "${HAINFO[${OBJID}]}" ) )
        IFS=$OLDIFS                               

        RAWVALUE="`echo ${OBJECT[$OBJTYPE]}`"
        case "$OBJTYPE" in                   
                33)                          
                        RETVAL="integer\n"   
                        RETVAL="$RETVAL${OBJID}\n"
                        EXITVAL=0                 
                        ;;                        
                17)                               
                        RETVAL="string\n"         
                        RETVAL="$RETVAL${RAWVALUE:-0}\n"
                        EXITVAL=0                       
                        ;;                              
                9)                                      
                        RETVAL="counter\n"              
                        RETVAL="$RETVAL${RAWVALUE:-0}\n"
                        EXITVAL=0                       
                        ;;                              
                8)                                      
                        RETVAL="counter\n"              
                        RETVAL="$RETVAL${RAWVALUE:-0}\n"
                        EXITVAL=0                       
                        ;;                              
                1)                                      
                        RETVAL="string\n"               
                        RETVAL="$RETVAL${RAWVALUE:-0}\n"
                        EXITVAL=0                       
                        ;;                              
                0)                                      
                        RETVAL="string\n"               
                        RETVAL="$RETVAL${RAWVALUE:-0}\n"
                        EXITVAL=0                       
                        ;;                              
                *)                                      
                        RETVAL="integer\n"              
                        RETVAL="$RETVAL${RAWVALUE:-0}\n"
                        EXITVAL=0                       
                        ;;                              
        esac                                            
}                                                       

function getNext() {
        REQUEST=$1  

        # Always start at .1.1.1.1
        if [ "$REQUEST" == "" ]; then
                REQUEST=".1.1.1.0"   
        fi                           

        OBJECTID=`echo $REQUEST | awk 'BEGIN { FS="." } ; { print $5 }'`
        OBJECTTYPE=`echo $REQUEST | awk 'BEGIN { FS="." } ; { print $4 }'`

        if [ "$OBJECTID" == "" ]; then
                let OBJECTID=0        
        fi                            

        let OBJECTID=$OBJECTID
        let OBJECTTYPE=$OBJECTTYPE

        # Get next entry
        if [ $OBJECTID -lt ${#HAINFO[@]} ]; then
                let OBJECTID=${OBJECTID}+1      
        fi                                      

        if [ $OBJECTID -ge ${#HAINFO[@]} ]; then
                let OBJECTTYPE=${OBJECTTYPE}+1  
                let OBJECTID=1                  
        fi                                      

        if [ $OBJECTTYPE -gt ${MAXITEMS} ]; then
                exit 0                          
        fi                                      

        getData ${OBJECTTYPE} ${OBJECTID}

        RETOID="$MY_OID.1.1.${OBJECTTYPE}.${OBJECTID}\n"
}                                                       

###### Settings ######
MY_OID=".1.3.6.1.4.1.2021.83"   ;# Set in /etc/snmp/snmpd.conf

# Arguments
REQ_OID="${2#$MY_OID}"          ;# Strip MY_OID from requested OID
REQ_TYPE="$1"                   ;# n,g(GET),s(SET)                

###### MAIN ######

### Log
#logger -t snmp_vsinfo -p debug "SNMP request attempted: $0 $*"

# Obtain haproxy-stats info in HAINFO array and ItemNames in HAITEMS array
OLDIFS=$IFS                                                               
IFS=$'\n'
HAINFO=( $( echo "show stat" | socat stdio /var/run/haproxy.stat ) )
IFS=","
HAITEMS=( $( echo "${HAINFO[0]}" | grep \# | cut -d \  -f 2- ) )
IFS=$OLDIFS
let MAXITEMS=${#HAITEMS[@]}+1

# Check request
case "${REQ_TYPE}" in
        -n)
                getNext $REQ_OID
                ;;

        -s)
                ### Someone tried to set a value... log for analysis
                logger -t snmp_haproxy -p crit "SET attempted $0 $*"
                exit 0
                ;;
        -g)
                ### GetValue
                REQUEST=$REQ_OID

                OBJECTID=`echo $REQUEST | awk 'BEGIN { FS="." } ; { print $5 }'`
                OBJECTTYPE=`echo $REQUEST | awk 'BEGIN { FS="." } ; { print $4 }'`

                getData ${OBJECTTYPE} ${OBJECTID}
                RETOID="$MY_OID.1.1.${OBJECTTYPE}.${OBJECTID}\n"
                ;;
        *)
                exit 0
                ;;
esac

printf "${RETOID}${RETVAL}"
exit ${EXITVAL}
It's still a bit 'rough around the edges', but seems to do the job. I had to raise the snmp timeout for the hosts that I poll to about 2000ms in order to obtain all the data.
Attachments
cacti_data_query_snmp_-_haproxy_statistics.xml
Data query based on the shell script in this posting.
(23.12 KiB) Downloaded 974 times
--
Regardz,
BWare
spirit
Posts: 4
Joined: Tue Mar 29, 2005 4:05 am

missing /resource/snmp_queries/haproxy.xml

Post by spirit »

Hi, i'm trying you script, but it's missing the file

/resource/snmp_queries/haproxy.xml


can you post it ?

Regards,

SPiRiT
-------
BWare
Posts: 33
Joined: Thu Jun 12, 2003 4:57 am
Location: Amsterdam
Contact:

Post by BWare »

Attached file should indeed be placed in |cacti_path|/resources/snmp_queries/
Attachments
haproxy.xml
|cacti_path|/resource/snmp_queries/haproxy.xml
(34.14 KiB) Downloaded 860 times
--
Regardz,
BWare
spirit
Posts: 4
Joined: Tue Mar 29, 2005 4:05 am

Post by spirit »

thanks you very much !
gaozaoren2009
Posts: 3
Joined: Tue Dec 01, 2009 4:08 am

Post by gaozaoren2009 »

hi when i option
[root@lb01 ~]# snmpbulkwalk -c public -v2c localhost .1.3.6.1.4.1.2021.83
UCD-SNMP-MIB::ucdavis.83 = No Such Instance currently exists at this OID
[root@lb01 ~]#

but my snmpd is work good
BWare
Posts: 33
Joined: Thu Jun 12, 2003 4:57 am
Location: Amsterdam
Contact:

Post by BWare »

gaozaoren2009 wrote:hi when i option
[root@lb01 ~]# snmpbulkwalk -c public -v2c localhost .1.3.6.1.4.1.2021.83
UCD-SNMP-MIB::ucdavis.83 = No Such Instance currently exists at this OID
[root@lb01 ~]#

but my snmpd is work good
In order to solve this, give some more info, like snmpd version, snmpd.conf contents, log output of the server
--
Regardz,
BWare
gaozaoren2009
Posts: 3
Joined: Tue Dec 01, 2009 4:08 am

Post by gaozaoren2009 »

BWare wrote:
gaozaoren2009 wrote:hi when i option
[root@lb01 ~]# snmpbulkwalk -c public -v2c localhost .1.3.6.1.4.1.2021.83
UCD-SNMP-MIB::ucdavis.83 = No Such Instance currently exists at this OID
[root@lb01 ~]#

but my snmpd is work good
In order to solve this, give some more info, like snmpd version, snmpd.conf contents, log output of the server



sorry ,maybe i see what happen ,
pass .1.3.6.1.4.1.2021.83 /bin/bash /path/to/scripts/haproxy.sh

I change it to
exec .1.3.6.1.4.1.2021.83 /bin/bash /path/to/scripts/haproxy.sh

[root@lb01 ~]# snmpbulkwalk -c public -v2c localhost .1.3.6.1.4.1.2021.83.1.1.34
UCD-SNMP-MIB::ucdavis.83.1.1.34.1.1 = INTEGER: 1
UCD-SNMP-MIB::ucdavis.83.1.1.34.2.1 = STRING: "/bin/bash"
UCD-SNMP-MIB::ucdavis.83.1.1.34.3.1 = STRING: "/bin/haproxy.sh"
UCD-SNMP-MIB::ucdavis.83.1.1.34.100.1 = INTEGER: 0
UCD-SNMP-MIB::ucdavis.83.1.1.34.102.1 = INTEGER: 0
UCD-SNMP-MIB::ucdavis.83.1.1.34.103.1 = ""
[root@lb01 ~]#

is it work fine ?


RRDTool Command:

/usr/local/rrdtool-1.2.23/bin/rrdtool graph - \
--imgformat=PNG \
--start=-86400 \
--end=-300 \
--title="61.16.5.216-lb01 - |query_pxname| - Proxy Traffic" \
--base=1024 \
--height=120 \
--width=500 \
--alt-autoscale \
--upper-limit=1500 \
--lower-limit=-1500 \
--vertical-label="Requests / Bytes" \
--font TITLE:10: \
--font AXIS:8: \
--font LEGEND:8: \
--font UNIT:8: \
DEF:a="/home/web/cacti/rra/61_16_5_216-lb01_smax_1985.rrd":bin:AVERAGE \
DEF:b="/home/web/cacti/rra/61_16_5_216-lb01_smax_1985.rrd":bout:AVERAGE \
CDEF:cdefe=b,-1,* \
AREA:a#00FF00FF:"Bytes IN\n" \
GPRINT:a:LAST:"Current\:%8.2lf %s" \
GPRINT:a:AVERAGE:"Average\:%8.2lf %s" \
GPRINT:a:MAX:"Maximum\:%8.2lf %s\n" \
AREA:cdefe#0000FFFF:"Bytes OUT\n" \
GPRINT:b:LAST:"Current\:%8.2lf %s" \
GPRINT:b:AVERAGE:"Average\:%8.2lf %s" \
GPRINT:b:MAX:"Maximum\:%8.2lf %s\n"

RRDTool Says:

ERROR: opening '/home/web/cacti/rra/61_16_5_216-lb01_smax_1985.rrd': No such file or directory
BWare
Posts: 33
Joined: Thu Jun 12, 2003 4:57 am
Location: Amsterdam
Contact:

Post by BWare »

gaozaoren2009 wrote:
BWare wrote:
gaozaoren2009 wrote:hi when i option
[root@lb01 ~]# snmpbulkwalk -c public -v2c localhost .1.3.6.1.4.1.2021.83
UCD-SNMP-MIB::ucdavis.83 = No Such Instance currently exists at this OID
[root@lb01 ~]#

but my snmpd is work good
In order to solve this, give some more info, like snmpd version, snmpd.conf contents, log output of the server
sorry ,maybe i see what happen ,
pass .1.3.6.1.4.1.2021.83 /bin/bash /path/to/scripts/haproxy.sh

I change it to
exec .1.3.6.1.4.1.2021.83 /bin/bash /path/to/scripts/haproxy.sh
pass and exec behave differently, check snmpd.conf man page. Also, pass needs to be enabled during compile time of snmpd!

<snip>

The errors are expected as you're using exec instead of pass.

What version of snmpd are you using ?

Code: Select all

snmpd -v
Is the 'pass' option enabled at compile time ?

Code: Select all

snmpd -H  2>&1 | grep -i pass
--
Regardz,
BWare
gaozaoren2009
Posts: 3
Joined: Tue Dec 01, 2009 4:08 am

Post by gaozaoren2009 »

BWare wrote:
gaozaoren2009 wrote:
BWare wrote: In order to solve this, give some more info, like snmpd version, snmpd.conf contents, log output of the server
sorry ,maybe i see what happen ,
pass .1.3.6.1.4.1.2021.83 /bin/bash /path/to/scripts/haproxy.sh

I change it to
exec .1.3.6.1.4.1.2021.83 /bin/bash /path/to/scripts/haproxy.sh
pass and exec behave differently, check snmpd.conf man page. Also, pass needs to be enabled during compile time of snmpd!

<snip>

The errors are expected as you're using exec instead of pass.

What version of snmpd are you using ?

Code: Select all

snmpd -v
Is the 'pass' option enabled at compile time ?

Code: Select all

snmpd -H  2>&1 | grep -i pass


[root@lb01 ~]# snmpd -H 2>&1 | grep -i pass
createUser username (MD5|SHA) passphrase [DES [passphrase]]
smuxpeer OID-IDENTITY PASSWORD
pass miboid command
pass_persist miboid program
defPassphrase string
defAuthPassphrase string
defPrivPassphrase string
[root@lb01 ~]# snmpd -v

NET-SNMP version: 5.3.2.2
Web: http://www.net-snmp.org/
Email: net-snmp-coders@lists.sourceforge.net

[root@lb01 ~]# rpm -qa |grep snmp
net-snmp-utils-5.3.2.2-5.el5_3.2
net-snmp-libs-5.3.2.2-5.el5_3.2
net-snmp-libs-5.3.2.2-5.el5_3.2
net-snmp-perl-5.3.2.2-5.el5_3.2
net-snmp-devel-5.3.2.2-5.el5_3.2
net-snmp-devel-5.3.2.2-5.el5_3.2
net-snmp-5.3.2.2-5.el5_3.2
[root@lb01 ~]#
BWare
Posts: 33
Joined: Thu Jun 12, 2003 4:57 am
Location: Amsterdam
Contact:

Post by BWare »

From what I can tell, the snmpd was compiled with the 'pass' option enabled, so try to enable debugging (logger lines in the script) and check what happens when you snmpwalk the oid you configured in /etc/snmp/snmpd.conf
--
Regardz,
BWare
Illindar
Posts: 5
Joined: Sat Oct 27, 2007 3:29 pm

Post by Illindar »

Ran into this problem myself... turned out to be a simple thing:

Edit or add 'stats socket /var/run/haproxy.stat mode 666' to the global section of your /etc/haproxy/haproxy.cfg and restart haproxy.

Basically the snmp user doesn't have access to view the socket file by default.
Illindar
Posts: 5
Joined: Sat Oct 27, 2007 3:29 pm

Updates since 2009

Post by Illindar »

It looks like there have been some socket additions since this thread was updated. I've made the following modifications to the script and xml so that rate, rate_limit, and rate_max are all available.

in haproxy.xml change:

Code: Select all

<interface>
        <name>Get HaProxy Statistics</name>
        <oid_index>.1.3.6.1.4.1.2021.83.1.1.34</oid_index>
        <index_order>pxname:svname:qcur:qmax:scur:smax:slim:stot:bin:bout:dreq:dresp:ereq:econ:eresp:wretr:wredis:status:weight:act:bck:
chkfail:chkdown:lastchg:downtime:qlimit:pid:iid:sid:throttle:lbtot:tracked:type:idx</index_order>
        <index_order_type>numeric</index_order_type>
        <index_title_format>|chosen_order_field|</index_title_format>
        <fields>
                <idx>
                        <name>idx</name>
                        <method>walk</method>
                        <source>value</source>
                        <direction>input</direction>
                        <oid>.1.3.6.1.4.1.2021.83.1.1.34</oid>
                </idx>
to

Code: Select all

<interface>
        <name>Get HaProxy Statistics</name>
        <oid_index>.1.3.6.1.4.1.2021.83.1.1.37</oid_index>
        <index_order>pxname:svname:qcur:qmax:scur:smax:slim:stot:bin:bout:dreq:dresp:ereq:econ:eresp:wretr:wredis:status:weight:act:bck:
chkfail:chkdown:lastchg:downtime:qlimit:pid:iid:sid:throttle:lbtot:tracked:type:rate:rate_lim:rate_max:idx</index_order>
        <index_order_type>numeric</index_order_type>
        <index_title_format>|chosen_order_field|</index_title_format>
        <fields>
                <idx>
                        <name>idx</name>
                        <method>walk</method>
                        <source>value</source>
                        <direction>input</direction>
                        <oid>.1.3.6.1.4.1.2021.83.1.1.37</oid>
                </idx>
and at the bottom change

Code: Select all

                </fields>
</interface>
to

Code: Select all

                <rate>
                        <name>rate</name>
                        <method>walk</method>
                        <source>value</source>
                        <direction>output</direction>
                        <oid>.1.3.6.1.4.1.2021.83.1.1.34</oid>
                </rate> </fields> </interface>
                <rate_lim>
                        <name>rate_lim</name>
                        <method>walk</method>
                        <source>value</source>
                        <direction>output</direction>
                        <oid>.1.3.6.1.4.1.2021.83.1.1.35</oid>
                </rate_lim>
                                <rate_max>
                        <name>rate_max</name>
                        <method>walk</method>
                        <source>value</source>
                        <direction>output</direction>
                        <oid>.1.3.6.1.4.1.2021.83.1.1.36</oid>
                </rate_max>
                </fields>
</interface>
in haproxy.sh change:

Code: Select all

        case "$OBJTYPE" in
                33)
                        RETVAL="integer\n"
                        RETVAL="$RETVAL${OBJID}\n"
                        EXITVAL=0
                        ;;
to

Code: Select all

        case "$OBJTYPE" in
                36)
                        RETVAL="integer\n"
                        RETVAL="$RETVAL${OBJID}\n"
                        EXITVAL=0
                        ;;
I've also attached my graph template for tracking sessions.
Attachments
cacti_graph_template_haproxy_session_statistics.xml
(22.75 KiB) Downloaded 461 times
tiggermanh
Posts: 6
Joined: Fri May 19, 2006 12:31 pm
Location: Dayton Ohio

Re: HaProxy statistics

Post by tiggermanh »

Would someone be willing to post up a same graph of the output of this template?

I am using the alternate method of perl, and may switch if this provides more usable data.
pclaupeo
Posts: 3
Joined: Fri Oct 22, 2010 6:06 am

Re: HaProxy statistics

Post by pclaupeo »

When I have a chance, I'll fix this up and post along w/ some sample data templates and graph templates.

Email me w/ questions. Yes, it is a long way off, and cacti is difficult to understand.

Code: Select all

<interface>
        <name>Get Haproxy servers</name>
        <description>Queries a host for a list of backend servers</description>
        <oid_index>.1.3.6.1.4.1.29385.106.1.2.28.1</oid_index>
        <index_order>beDescr:beIndex</index_order>
        <index_order_type>alphabetic</index_order_type>
        <index_title_format>|chosen_order_field|</index_title_format>

        <fields>
                <beIndex>
                        <name>Index</name>
                        <method>walk</method>
                        <source>value</source>
                        <direction>input</direction>
                        <oid>.1.3.6.1.4.1.29385.106.1.2.28.1</oid>
                </beIndex>
                <beDescr>
                        <name>Description</name>
                        <method>walk</method>
                        <source>value</source>
                        <direction>input</direction>
                        <oid>.1.3.6.1.4.1.29385.106.1.2.1.1</oid>
                </beDescr>
                <beSessionsCurrent>
                        <name>Sessions Current</name>
                        <method>walk</method>
                        <source>value</source>
                        <direction>output</direction>
                        <oid>.1.3.6.1.4.1.29385.106.1.2.4.1</oid>
                </beSessionsCurrent>
                <beSessionsMax>
                        <name>Sessions Max</name>
                        <method>walk</method>
                        <source>value</source>
                        <direction>output</direction>
                        <oid>.1.3.6.1.4.1.29385.106.1.2.5.1</oid>
                </beSessionsMax>
                <beSessionsLimit>
                        <name>Sessions Limit</name>
                        <method>walk</method>
                        <source>value</source>
                        <direction>output</direction>
                        <oid>.1.3.6.1.4.1.29385.106.1.2.6.1</oid>
                </beSessionsLimit>
                <beQueueCurrent>
                        <name>Queue Current</name>
                        <method>walk</method>
                        <source>value</source>
                        <direction>output</direction>
                        <oid>.1.3.6.1.4.1.29385.106.1.2.2.1</oid>
                </beQueueCurrent>
                <beQueueMax>
                        <name>Queue Max</name>
                        <method>walk</method>
                        <source>value</source>
                        <direction>output</direction>
                        <oid>.1.3.6.1.4.1.29385.106.1.2.3.1</oid>
                </beQueueMax>
                <beSessionsRateCurrent>
                        <name>Sessions Rate Current</name>
                        <method>walk</method>
                        <source>value</source>
                        <direction>output</direction>
                        <oid>.1.3.6.1.4.1.29385.106.1.2.33.1</oid>
                </beSessionsRateCurrent>
        </fields>
</interface>
rollingsun
Posts: 2
Joined: Mon Oct 11, 2010 2:34 am

Re: HaProxy statistics

Post by rollingsun »

um ~

could you guys tell me how to use this ? like normal way ?

put the shell script into the <cacti>/scripts .
import the .xml template .
add new devices and create graphy ?

is that ok ? may be I have to try ~
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests