#!/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() sysstat_check = os.popen("if ! rpm -q sysstat > /dev/null ; then yum install -y sysstat ; fi ", 'r').read() 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()