#!/usr/bin/python from __future__ import print_function import socket import time import StringIO class GraphitePush: dont_send = False prefix = 'ffpb.nodes.' target_host = None target_port = 2003 whitelist = None #[ '24a43cf85efa', '24a43cf85edb', '24a43cd94f69', '24a43ca367f0', '24a43ca36807', '24a43cd221d5' ] def __init__(self, host, port=2003): self.target_host = host self.target_port = port def __str__(self): return 'Graphite at [{0}]:{1} (prefix=\'{2}\', whitelist={3})'.format( self.target_host, self.target_port, self.prefix, self.whitelist) def push(self, data, ts=None): if ts is None: ts = time.time() ts = int(ts) output = StringIO.StringIO() whitelist = [ x for x in self.whitelist ] if not self.whitelist is None and len(self.whitelist) > 0 else None for nodeid in data: if (not whitelist is None) and (not nodeid in whitelist): #print("Skipping node {0} as it is not in the configured whitelist.".format(nodeid)) continue nodeinfo = data[nodeid] for item in ['uptime']: if item in nodeinfo: print(self.prefix, nodeid, '.', item, ' ', nodeinfo[item], ' ', ts, sep='', file=output) traffic = nodeinfo['statistics']['traffic'] if 'statistics' in nodeinfo and 'traffic' in nodeinfo['statistics'] else None if not traffic is None: for item in ['rxbytes', 'txbytes']: print(self.prefix, nodeid, '.', item, ' ', traffic[item], ' ', ts, sep='', file=output) all_output = output.getvalue() if not self.dont_send: s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) s.connect((self.target_host, self.target_port)) s.sendall(all_output) s.shutdown(socket.SHUT_WR) s.close() output.close() return all_output