X-Git-Url: http://git.onelab.eu/?p=myops.git;a=blobdiff_plain;f=web%2Fcollect%2Fsar2graphite.py;fp=web%2Fcollect%2Fsar2graphite.py;h=cb679de0f4991122f13fe9496f29b04b04cd572e;hp=0000000000000000000000000000000000000000;hb=85070b3d456667f238051af1a2f1f2a0c12300ab;hpb=607f0e13927eb18075c375fa9ba5527da4fcbb44 diff --git a/web/collect/sar2graphite.py b/web/collect/sar2graphite.py new file mode 100755 index 0000000..cb679de --- /dev/null +++ b/web/collect/sar2graphite.py @@ -0,0 +1,101 @@ +#!/usr/bin/python + +import os +import time +import sys + +if 'LANG' in os.environ: + del os.environ['LANG'] +os.environ['PATH'] = "/sbin:/bin:/usr/sbin:/usr/bin" + +def date2ts(d, fmt="%m/%d/%y %H:%M:%S"): + return int(time.mktime(time.strptime(d, fmt))) + +def subfix(h): + f = h.split('_') + sf = f[::-1] + print sf + return sf[2] + "." + +def main(): + from optparse import OptionParser + parser = OptionParser() + + parser.set_defaults(prefix="sar.", + sarargs="-q", + seconds=60, + subfix=False, + debug=False, + ghost=None, + filter=None, + ) + + parser.add_option("", "--prefix", dest="prefix", help="") + parser.add_option("", "--sarargs", dest="sarargs", help="") + parser.add_option("-s", "--seconds", dest="seconds", help="") + parser.add_option("", "--subfix", dest="subfix", action="store_true", help="") + parser.add_option("", "--debug", dest="debug", action="store_true", help="") + parser.add_option("", "--filter", dest="filter", help="") + parser.add_option("", "--ghost", dest="ghost", help="") + + (config, args) = parser.parse_args() + if len(sys.argv) == 1: + parser.print_help() + sys.exit(1) + + if config.ghost is None: + # send to current host by default. + ghost_input = os.popen("/bin/hostname", 'r') + config.ghost = ghost_input.read().strip() + + hostname_input = os.popen("/bin/hostname | sed -e 's/\./_/g' ", 'r') + sar_input = os.popen("/usr/bin/sar %s %s 1" % (config.sarargs, config.seconds), 'r') + nc_output = os.popen("/usr/bin/nc %s 2003" % config.ghost, 'w') + + hostname = hostname_input.read().strip() + hostname_input.close() + + first_date = "" + headers = [] + + if config.subfix: + sf = subfix(hostname) + else: + sf = "" + prefix = config.prefix + sf + hostname + "." + + skip_keys = [ 'CPU', 'IFACE' ] + + for line in sar_input: + fields = line.split() + + if first_date == "": + first_date = fields[3] + continue + + if headers == []: + headers = fields[1:] + continue + + if fields == []: + headers = [] + + if len(fields) > 0 and fields[0] != "Average:": + ts = date2ts("%s %s" % (first_date, fields[0]) ) + key_fix = "" + for k,v in zip(headers, fields[1:]): + if config.filter and k in config.filter: + if v not in config.filter: break + else: key_fix = v + "." + if k in skip_keys: continue + + k = k.replace("/","_") + if not config.debug: + print >>nc_output, prefix + key_fix + k, v, ts + print prefix + key_fix + k, v, ts + nc_output.flush() + + nc_output.close() + +if __name__ == "__main__": + main()