Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 86 lines (61 sloc) 2.196 kb
31101e0 Neil Williams Add a script to send HAProxy utilization data to graphite.
spladug authored
1 #!/usr/bin/python
2
77c3072 Neil Williams utilization: Use division from the future for cleanliness.
spladug authored
3 from __future__ import division
4
31101e0 Neil Williams Add a script to send HAProxy utilization data to graphite.
spladug authored
5 import csv
6 import urllib2
7 import collections
8 import time
9
10 import alerts
11
12
13 def fetch_session_counts(haproxy_stats_urls):
14 current = collections.Counter()
15 limit = collections.Counter()
95d615b Neil Williams utilization: Report haproxy queuing by pool to Graphite.
spladug authored
16 queue = {}
31101e0 Neil Williams Add a script to send HAProxy utilization data to graphite.
spladug authored
17
18 for url in haproxy_stats_urls:
19 csv_data = urllib2.urlopen(url, timeout=3)
8805a0a Neil Williams utilization: Use csv.DictReader for clarity.
spladug authored
20 reader = csv.DictReader(csv_data)
31101e0 Neil Williams Add a script to send HAProxy utilization data to graphite.
spladug authored
21
22 for i, row in enumerate(reader):
23 if i == 0: continue
24
8805a0a Neil Williams utilization: Use csv.DictReader for clarity.
spladug authored
25 proxy_name = row["# pxname"]
26 service_name = row["svname"]
27 q_cur = row["qcur"]
28 s_cur = row["scur"]
29 s_lim = row["slim"]
30 status = row["status"]
95d615b Neil Williams utilization: Report haproxy queuing by pool to Graphite.
spladug authored
31
32 if service_name == "BACKEND":
33 queue[proxy_name] = int(q_cur)
31101e0 Neil Williams Add a script to send HAProxy utilization data to graphite.
spladug authored
34
35 if service_name in ("FRONTEND", "BACKEND"):
36 continue
37
38 if status != "UP":
39 continue
40
41 current[proxy_name] += int(s_cur)
42 limit[proxy_name] += int(s_lim)
43
95d615b Neil Williams utilization: Report haproxy queuing by pool to Graphite.
spladug authored
44 return [(pool, current[pool], capacity, queue[pool])
31101e0 Neil Williams Add a script to send HAProxy utilization data to graphite.
spladug authored
45 for pool, capacity in limit.most_common()]
46
47
48 def notify_graphite(usage):
49 values = {}
95d615b Neil Williams utilization: Report haproxy queuing by pool to Graphite.
spladug authored
50 for pool, cur, limit, queue in usage:
31101e0 Neil Williams Add a script to send HAProxy utilization data to graphite.
spladug authored
51 values["stats.utilization.%s.current" % pool] = cur
52 values["stats.utilization.%s.capacity" % pool] = limit
95d615b Neil Williams utilization: Report haproxy queuing by pool to Graphite.
spladug authored
53 values["stats.utilization.%s.queue" % pool] = queue
31101e0 Neil Williams Add a script to send HAProxy utilization data to graphite.
spladug authored
54 alerts.graphite.send_values(values)
55
56
57 def pretty_print(usage):
58 print "%20s%20s%10s" % ("", "sessions", "")
95d615b Neil Williams utilization: Report haproxy queuing by pool to Graphite.
spladug authored
59 print "%20s%10s%10s%10s%10s" % ("pool", "cur", "max", "% util", "queue")
60 print "-" * 60
61 for pool, cur, limit, queue in usage:
62 print "%20s%10d%10d%10.2f%10d" % (pool, cur, limit, cur / limit * 100.0, queue)
31101e0 Neil Williams Add a script to send HAProxy utilization data to graphite.
spladug authored
63
64
65 def main():
66 alerts.init()
67
68 haproxy_urls = [value for key, value in
69 alerts.config.items("haproxy")
70 if key.startswith("url")]
71
72 while True:
73 try:
74 usage_by_pool = fetch_session_counts(haproxy_urls)
75 except urllib2.URLError:
76 pass
77 else:
78 notify_graphite(usage_by_pool)
79 pretty_print(usage_by_pool)
80
81 time.sleep(1)
82
83
84 if __name__ == "__main__":
85 main()
Something went wrong with that request. Please try again.