Skip to content

Instantly share code, notes, and snippets.

@lsjostro
Last active August 31, 2021 14:44
Show Gist options
  • Save lsjostro/6281054 to your computer and use it in GitHub Desktop.
Save lsjostro/6281054 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys
import time
import socket
import urllib2
def main(haproxy_host, statsd_host):
s = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)
try:
r = urllib2.urlopen("http://%s:8080/haproxy?stats;csv" % haproxy_host).read()
except IOError, e:
raise Exception("Connection failed: %s" % e)
# Clean CSV
csv_stats = r.replace("# ", "").rstrip(",\n").replace(",\n", "\n").split("\n")
header = csv_stats[0].split(",")
hostname = socket.gethostname().split(".")[0]
for line in csv_stats[1:]:
stats = dict(zip(header, line.split(",")))
for name in ("scur", "smax", "ereq", "econ", "rate", "stot"):
#print "haproxy.%s.%s.%s.%s:%s|g" % (hostname, stats['pxname'], stats['svname'], name, stats[name])
s.sendto("haproxy.%s.%s.%s.%s:%s|g" % (hostname, stats['pxname'], stats['svname'], name, stats[name]), (statsd_host, 8125))
if __name__ == "__main__":
if len(sys.argv) < 3:
print "haproxy_statsd.py <haproxy_host> <statsd_host>"
sys.exit(1)
while True:
main(sys.argv[1], sys.argv[2])
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment