I did something similar for graphite using bash and socat to read from
haproxy socket.

Script at http://pastebin.com/ccenegXb

Regards,
Vivek

On Tue, Oct 2, 2012 at 4:08 PM, Joel Krauska <[email protected]> wrote:

> I use something like this:
> (it has a few things that are specific to my implementation)
>
> #!/usr/bin/python
> import os
> import csv
> import urllib2
> import pprint
> import time
>
> datadir = '/home/rrdcollect/haproxy-stats/rrds'
> hostlist = ['a.network.com']
> haproxycreds = ['url','user','pass']
> key_values = ['scur', 'bin', 'bout']
> for host in hostlist:
>        print host
>
>        url = 'http://%s/%s;csv;norefresh' % (host, haproxycreds[0])
>        print url
>        pp = pprint.PrettyPrinter(indent=4)
>        passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
>        passman.add_password(None, url, haproxycreds[1], haproxycreds[2])
>        authhandler = urllib2.HTTPBasicAuthHandler(passman)
>        opener = urllib2.build_opener(authhandler)
>        urllib2.install_opener(opener)
>        f = urllib2.urlopen(url)
>
>        firstrow = True
>        e = dict()
>        for row_ind,row in enumerate(csv.reader(f)):
>               if firstrow:
>                      mapping = row
>                      firstrow = False
>               if "FRONTEND" in row or "BACKEND" in row:
>                      d = {}
>                      for ind,col_val in enumerate(row):
>                             col_name = mapping[ind]
>                             d[col_name]=col_val
>                             row_key = row[0],row_ind
>                             e[row_key]=d
>        for keys in key_values:
>               for key_tuple,row_dict in e.iteritems():
>                      row_val = row_dict.get(keys,'')
>                      if keys == "scur":
>                             pitem = "CurrentSessionRate"
>                      elif keys == "bin":
>                             pitem = "bpsIn"
>                             row_val = int(row_val)*8
>                             row_val = str(row_val)
>                      elif keys == "bout":
>                             pitem = "bpsOut"
>                             row_val = int(row_val)*8
>                             row_val = str(row_val)
>
>                      rrd = "%s/haproxyStat_%s_%s_%s.rrd" % (datadir,
> pitem, key_tuple[0], host)
>                      print rrd
>                      cmd = "rrdtool update %s N:%s" % (rrd, row_val)
>                      print cmd
>                      if not os.path.isfile(rrd):
>                             print 'RRD does not exist!'
>                             from time import time
>                             now = int(time())
>                             print now
>                             now-=300
>                             print now
>                             maxvalue =
> 10000000000000000000000000000000000000
>                             if "bps" in pitem:
>                                    cmd = 'rrdtool create %s --start %s
> --step 300 DS:data:DERIVE:600:0:%s RRA:AVERAGE:0.5:1:9000
> RRA:AVERAGE:0.5:12:4000 RRA:AVERAGE:0.5:72:5000
> RRA:AVERAGE:0.5:288:2500' % (rrd, now, maxvalue)
>                             else:
>                                    cmd = 'rrdtool create %s --start %s
> --step 300 DS:data:COUNTER:600:0:%s RRA:AVERAGE:0.5:1:9000
> RRA:AVERAGE:0.5:12:4000 RRA:AVERAGE:0.5:72:5000
> RRA:AVERAGE:0.5:288:2500' % (rrd, now, maxvalue)
>                             print cmd
>                             os.system(cmd)
>                      os.system(cmd)
>
>
>
> On Tue, Oct 2, 2012 at 11:17 AM, Saul Waizer <[email protected]> wrote:
> > Hello List,
> >
> > This is a general question, has anyone developed a method of polling
> > statistics from HAproxy to determine the current requests per second
> rate? I
> > am building a custom nagios plugin that will also work with graphite and
> > cacti, i am trying to find the best approach to gather RPS statistics.
> Any
> > suggestions?
> >
> > Thank You,
> > Saul
>
>

Reply via email to