Uptime via stats without scraping html


#1

Is there anyway to programmatically obtain the uptime metric from haproxy statistics via http without having to scrape the html version of the output? It would be nice to have this information in csv or some other format, but it seems the only way this is exposed is via the /haproxy?stats api. For bonus, it would be nice to see this in unix time rather than having to convert the human readable string.

Thanks.


#2

I took a quick look at the Telegraf input for HAProxy and it looks like you can add ;csv to the end of the stats URL and get the results back in some machine-readable format. Maybe give that a try?

EDIT: Ah, it looks like you can also get at it through the socket interface: https://www.datadoghq.com/blog/how-to-collect-haproxy-metrics/


#3

So the socket isn’t http and is only available if you are local to the host. Further, the csv output does not contain the uptime info. Only the /haproxy?stats api does; not /haproxy?stats;csv.


#4

I think you have three options:

  1. Parse the HTML
  2. Submit a patch to HAProxy to include this metric in the CSV
  3. Write a little service to retrieve the uptime from the socket interface and provide it via HTTP

#5

Note that the stats socket can also listen to a TCP address if you need. And if you want to filter accesses to it, you can run a TCP instance with source IP based ACLs which forwards to the local socket.


#6

Excellent, I think forwarding to a local socket behind a tcp listener will do what I need. However, I’m having trouble getting this to work. I’m trying something like the following:

global
  stats socket /var/run/haproxy.sock
listen stats-listener
  bind 10.1.1.1:9101
  mode tcp
  server stats-socket unix@/var/run/haproxy.sock

But not able to get a reply to “show info”, for example. What am I doing wrong?