Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import getopt, sys, commands, re
- def uniq(inlist):
- # finding of unique items
- uniques = []
- for item in inlist:
- if item not in uniques:
- uniques.append(item)
- return uniques
- def usage():
- help = """Use:\n
- sadfParse.py --s=';' --f=',' --c='sadf -p -- -P 1 -s 09:00:00 -e 10:30:00'\n
- --c=\'sadf -p -- sar option\' sadf command with sar parameters, -p is required\n
- --s=\'separator\' for setting separator character, default is \',\'\n
- --f=\'division\' for setting division character, default is \'.\'\n
- Don\'t use the same separator and division character\n"""
- print help
- sys.exit()
- if __name__ == "__main__":
- try:
- # Get option fron command line
- opts, args = getopt.getopt(sys.argv[1:], 'h', ['c=', 's=', 'f='])
- except getopt.GetoptError, err:
- # print help information and exit:
- print str(err) # will print something like "option -a not recognized"
- usage()
- sys.exit(2)
- # set default parameters
- command = 'sadf -p -- -A -s 00:00:00 -e 01:00:00'
- separator = ','
- division = ''
- # parse options
- for o, v in opts:
- if o == '-h':
- usage()
- elif o == '--c':
- command = v
- elif o == '--s':
- separator = v
- elif o == '--f':
- division = v
- # execute sadf command and get timestamps and metrics
- sadfData = commands.getoutput(command)
- timestamps = (uniq(re.findall('\d{10}',sadfData)))
- metrics = (uniq(re.findall('\d{10}\t(.*\t.*)\t.*',sadfData)))
- # build strings with metrics for each timestamp
- csv = 'timestamp'
- for index in metrics:
- csv += separator + index
- csv += '\n'
- for t in timestamps:
- row = ''
- for m in metrics:
- row += separator + ''.join(re.findall(t + '\t' + m + '\t(.*)', sadfData))
- csv += t + row + '\n'
- if division != '':
- csv = csv.replace(".",division)
- print csv
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement