Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Try out utilization grapher.

  • Loading branch information...
commit 571196d44435dde45c896e641a41bfaa97f3a0b1 1 parent b57b4d8
Neil Williams spladug authored

Showing 2 changed files with 104 additions and 0 deletions. Show diff stats Hide diff stats

  1. +34 0 alerts.py
  2. +70 0 utilization.py
34 alerts.py
@@ -6,12 +6,14 @@
6 6 import os
7 7 import sys
8 8 import time
  9 +import socket
9 10
10 11 import wessex
11 12
12 13 __all__ = ["harold", "config"]
13 14
14 15 harold = None
  16 +graphite = None
15 17 config = None
16 18
17 19 def init(config_path='production.ini'):
@@ -19,6 +21,8 @@ def init(config_path='production.ini'):
19 21 config = load_config(path=config_path)
20 22 if config.has_section('logging'):
21 23 configure_logging(config)
  24 + if config.has_section('graphite'):
  25 + configure_graphite(config)
22 26 harold = get_harold(config)
23 27
24 28 def load_config(path='production.ini'):
@@ -74,3 +78,33 @@ def configure_logging(config):
74 78 logger.setLevel(_get_logging_level(config))
75 79 logger.addHandler(ch)
76 80 return logger
  81 +
  82 +def _parse_addr(addr):
  83 + host, port_str = addr.split(':', 1)
  84 + return host, int(port_str)
  85 +
  86 +
  87 +def configure_graphite(config):
  88 + global graphite
  89 +
  90 + class Graphite(object):
  91 + def __init__(self, address):
  92 + self.address = address
  93 +
  94 + def _send_message(self, msg):
  95 + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  96 + sock.connect(self.address)
  97 + sock.send(msg + '\n')
  98 + sock.close()
  99 +
  100 + def send_values(self, items):
  101 + messages = []
  102 + timestamp = str(time.time())
  103 + for key, value in items.iteritems():
  104 + messages.append(" ".join((key, str(value), timestamp)))
  105 + if messages:
  106 + self._send_message(messages.join("\n"))
  107 +
  108 + address_text = config.get('graphite', 'address')
  109 + address = _parse_addr(address_text)
  110 + graphite = Graphite(address)
70 utilization.py
... ... @@ -0,0 +1,70 @@
  1 +import csv
  2 +import urllib2
  3 +import collections
  4 +
  5 +import alerts
  6 +
  7 +
  8 +def fetch_session_counts(haproxy_stats_urls):
  9 + current = collections.Counter()
  10 + limit = collections.Counter()
  11 +
  12 + for url in haproxy_stats_urls:
  13 + csv_data = urllib2.urlopen(url, timeout=3)
  14 + reader = csv.reader(csv_data)
  15 +
  16 + for i, row in enumerate(reader):
  17 + if i == 0: continue
  18 +
  19 + proxy_name, service_name, s_cur, s_lim, status = row[0], row[1], row[4], row[6], row[17]
  20 +
  21 + if service_name in ("FRONTEND", "BACKEND"):
  22 + continue
  23 +
  24 + if status != "UP":
  25 + continue
  26 +
  27 + current[proxy_name] += int(s_cur)
  28 + limit[proxy_name] += int(s_lim)
  29 +
  30 + ret = []
  31 + for pool, limit in limit.most_common():
  32 + ret.append((pool, current[pool], limit))
  33 + return ret
  34 +
  35 +
  36 +def notify_graphite(usage):
  37 + values = {}
  38 + for pool, cur, limit in usage:
  39 + values["stats.utilization.%s.current" % pool] = cur
  40 + values["stats.utilization.%s.capacity" % pool] = limit
  41 + alerts.graphite.send_values(values)
  42 +
  43 +
  44 +def pretty_print(usage):
  45 + print "%20s%20s%10s" % ("", "sessions", "")
  46 + print "%20s%10s%10s%10s" % ("pool", "cur", "max", "% util")
  47 + print "-" * 50
  48 + for pool, cur, limit in usage:
  49 + print "%20s%10d%10d%10.2f" % (pool, cur, limit, float(cur) / limit * 100.0)
  50 +
  51 +
  52 +def main():
  53 + alerts.init()
  54 +
  55 + haproxy_urls = [value for key, value in
  56 + alerts.config.items("haproxy")
  57 + if key.startswith("url")]
  58 +
  59 + while True:
  60 + try:
  61 + usage_by_pool = fetch_session_counts(haproxy_urls)
  62 + except urllib2.urlerror:
  63 + pass
  64 + else:
  65 + notify_graphite(usage_by_pool)
  66 + pretty_print(usage_by_pool)
  67 +
  68 +
  69 +if __name__ == "__main__":
  70 + main()

0 comments on commit 571196d

Please sign in to comment.
Something went wrong with that request. Please try again.