Advertisement
yugene

Parse sadf (sar) data to csv

Mar 20th, 2012
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. #!/usr/bin/python
  2. import getopt, sys, commands, re
  3.  
  4.  
  5. def uniq(inlist):
  6.     # finding of unique items
  7.     uniques = []
  8.     for item in inlist:
  9.         if item not in uniques:
  10.             uniques.append(item)
  11.     return uniques
  12. def usage():
  13.     help = """Use:\n
  14.     sadfParse.py --s=';' --f=',' --c='sadf -p  -- -P 1 -s 09:00:00 -e 10:30:00'\n
  15.     --c=\'sadf -p -- sar option\' sadf command with sar parameters, -p is required\n
  16.     --s=\'separator\' for setting separator character, default is \',\'\n
  17.     --f=\'division\' for setting division character, default is \'.\'\n
  18.     Don\'t use the same separator and division character\n"""
  19.     print help
  20.     sys.exit()
  21. if __name__ == "__main__":
  22.     try:
  23.         # Get option fron command line
  24.         opts, args = getopt.getopt(sys.argv[1:], 'h', ['c=', 's=', 'f='])
  25.     except getopt.GetoptError, err:
  26.             # print help information and exit:
  27.             print str(err) # will print something like "option -a not recognized"
  28.             usage()
  29.             sys.exit(2)
  30.     # set default parameters
  31.     command = 'sadf -p -- -A -s 00:00:00 -e 01:00:00'
  32.     separator = ','
  33.     division = ''
  34.     # parse options
  35.     for o, v in opts:
  36.         if o == '-h':
  37.             usage()
  38.         elif o == '--c':
  39.             command = v
  40.         elif o == '--s':
  41.             separator = v
  42.         elif o == '--f':
  43.             division = v
  44.     # execute sadf command and get timestamps and metrics
  45.     sadfData = commands.getoutput(command)
  46.     timestamps = (uniq(re.findall('\d{10}',sadfData)))
  47.     metrics = (uniq(re.findall('\d{10}\t(.*\t.*)\t.*',sadfData)))
  48.     # build  strings with metrics for each timestamp
  49.     csv = 'timestamp'
  50.     for index in metrics:
  51.         csv += separator + index
  52.     csv += '\n'
  53.     for t in timestamps:
  54.         row = ''
  55.         for m in metrics:
  56.             row += separator + ''.join(re.findall(t + '\t' + m + '\t(.*)', sadfData))
  57.         csv += t + row + '\n'
  58.     if division != '':
  59.         csv = csv.replace(".",division)
  60.     print csv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement