Skip to content

Commit

Permalink
Refactor and simplify metrics collection
Browse files Browse the repository at this point in the history
  • Loading branch information
jssjr committed Jun 10, 2013
1 parent 1c91c47 commit 53443a3
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions varnish-graphite.py
Expand Up @@ -31,11 +31,11 @@ def connect(self):

def send_metrics(self, metrics):
for stat in metrics:
if len(self.sendbuf) + len(stat) > self.buffer_size:
if len(self.sendbuf) + len("{}.{}".format(self.prefix, stat)) > self.buffer_size:
print("Sending {} bytes to {}".format(len(self.sendbuf), "{}:{}".format(self.host, self.port)))
self.sock.send(self.sendbuf)
self.sendbuf = ''
self.sendbuf += "{}\n".format(stat)
self.sendbuf += "{}.{}\n".format(self.prefix, stat)

def disconnect(self):
self.sock.close()
Expand All @@ -46,42 +46,35 @@ def parse_varnishstat():
return json.loads(subprocess.check_output(['varnishstat', '-1', '-j']))


def format_stat(title, metric, timestamp, prefix = None):
if prefix:
name = string.join([prefix, title],".")
else:
name = title

return "{} {} {}".format(name, metric, timestamp)


def report_status(prefix):
def collect_metrics():
stats = parse_varnishstat()
ts = int(time.time())

status = []
fmt = lambda x, y, z: "{} {} {}".format(x, stats[y]['value'], ts)

# Cache
status.append(format(format_stat("cache.hit", stats['cache_hit']['value'], ts, prefix)))
status.append(format(format_stat("cache.hitpass", stats['cache_hitpass']['value'], ts, prefix)))
status.append(format(format_stat("cache.miss", stats['cache_miss']['value'], ts, prefix)))
status.append(fmt('cache.hit', 'cache_hit', ts))
status.append(fmt('cache.hitpass', 'cache_hitpass', ts))
status.append(fmt('cache.miss', 'cache_miss', ts))

# Origin
status.append(format(format_stat("backend.conn", stats['backend_conn']['value'], ts, prefix)))
status.append(format(format_stat("backend.unhealthy", stats['backend_unhealthy']['value'], ts, prefix)))
status.append(format(format_stat("backend.busy", stats['backend_busy']['value'], ts, prefix)))
status.append(format(format_stat("backend.fail", stats['backend_fail']['value'], ts, prefix)))
status.append(format(format_stat("backend.reuse", stats['backend_reuse']['value'], ts, prefix)))
status.append(format(format_stat("backend.toolate", stats['backend_toolate']['value'], ts, prefix)))
status.append(format(format_stat("backend.recycle", stats['backend_recycle']['value'], ts, prefix)))
status.append(format(format_stat("backend.retry", stats['backend_retry']['value'], ts, prefix)))
status.append(format(format_stat("backend.req", stats['backend_req']['value'], ts, prefix)))
status.append(fmt('backend.conn', 'backend_conn', ts))
status.append(fmt('backend.unhealthy', 'backend_unhealthy', ts))
status.append(fmt('backend.busy', 'backend_busy', ts))
status.append(fmt('backend.fail', 'backend_fail', ts))
status.append(fmt('backend.reuse', 'backend_reuse', ts))
status.append(fmt('backend.toolate', 'backend_toolate', ts))
status.append(fmt('backend.recycle', 'backend_recycle', ts))
status.append(fmt('backend.retry', 'backend_retry', ts))
status.append(fmt('backend.req', 'backend_req', ts))

# Client
status.append(format(format_stat("client.conn", stats['client_conn']['value'], ts, prefix)))
status.append(format(format_stat("client.drop", stats['client_drop']['value'], ts, prefix)))
status.append(format(format_stat("client.req", stats['client_req']['value'], ts, prefix)))
status.append(format(format_stat("client.hdrbytes", stats['s_hdrbytes']['value'], ts, prefix)))
status.append(format(format_stat("client.bodybytes", stats['s_bodybytes']['value'], ts, prefix)))
status.append(fmt('client.conn', 'client_conn', ts))
status.append(fmt('client.drop', 'client_drop', ts))
status.append(fmt('client.req', 'client_req', ts))
status.append(fmt('client.hdrbytes', 's_hdrbytes', ts))
status.append(fmt('client.bodybytes', 's_bodybytes', ts))

return status

Expand All @@ -101,7 +94,7 @@ def main():

try:
while True:
c.send_metrics(report_status(args.prefix))
c.send_metrics(collect_metrics())
time.sleep(10)
except KeyboardInterrupt:
c.disconnect();
Expand Down

0 comments on commit 53443a3

Please sign in to comment.