| 1 | #!/usr/bin/python3 | 
|---|
| 2 |  | 
|---|
| 3 | """ | 
|---|
| 4 | Python Script to convert output from trun to rmit like output | 
|---|
| 5 | """ | 
|---|
| 6 | import argparse | 
|---|
| 7 | import json | 
|---|
| 8 | import locale | 
|---|
| 9 | import os | 
|---|
| 10 | import re | 
|---|
| 11 | import sys | 
|---|
| 12 |  | 
|---|
| 13 | locale.setlocale( locale.LC_ALL, 'en_US.UTF-8' ) | 
|---|
| 14 |  | 
|---|
| 15 | parser = argparse.ArgumentParser(description='Python Script to convert output from trun to rmit like output') | 
|---|
| 16 | parser.add_argument('--out', nargs='?', type=argparse.FileType('w'), default=sys.stdout) | 
|---|
| 17 | parser.add_argument('files', nargs='+') | 
|---|
| 18 | options =  parser.parse_args() | 
|---|
| 19 |  | 
|---|
| 20 | def extract(file): | 
|---|
| 21 | raw = [] | 
|---|
| 22 | with open(file, "r") as f: | 
|---|
| 23 | for line in f: | 
|---|
| 24 | if line.startswith("Combined"): | 
|---|
| 25 | break | 
|---|
| 26 |  | 
|---|
| 27 | h = next(f) | 
|---|
| 28 | next(f) | 
|---|
| 29 |  | 
|---|
| 30 | columns = h.strip().split() | 
|---|
| 31 | if not len(columns) == 20: | 
|---|
| 32 | print("{}: Failed to split header row '{}', unexpected number of columns.".format(file, h), file=sys.stderr) | 
|---|
| 33 | raise ValueError | 
|---|
| 34 |  | 
|---|
| 35 | for line in f: | 
|---|
| 36 | line = line.strip() | 
|---|
| 37 | if not line: | 
|---|
| 38 | break | 
|---|
| 39 |  | 
|---|
| 40 | raw.append(line) | 
|---|
| 41 |  | 
|---|
| 42 | results = [] | 
|---|
| 43 | for l in raw: | 
|---|
| 44 | vals = l.split() | 
|---|
| 45 | if not len(vals) == 21: | 
|---|
| 46 | print("{}: Failed to split result row '{}', unexpected number of columns ({}).".format(file, l, len(vals)), file=sys.stderr) | 
|---|
| 47 | raise ValueError | 
|---|
| 48 | d = { | 
|---|
| 49 | 'Request Rate': float(vals[0]), | 
|---|
| 50 | 'MinReplies':   float(vals[1]), | 
|---|
| 51 | 'MeanReplies':  float(vals[2]), | 
|---|
| 52 | 'MaxReplies':   float(vals[3]), | 
|---|
| 53 | 'StdReplies':   float(vals[4]), | 
|---|
| 54 | 'Conn':         float(vals[5]), | 
|---|
| 55 | 'Resp':         float(vals[6]), | 
|---|
| 56 | 'Xfer':         float(vals[7]), | 
|---|
| 57 | 'Total':        float(vals[8]), | 
|---|
| 58 | 'Std':          float(vals[9]), | 
|---|
| 59 | 'Med':          float(vals[10]), | 
|---|
| 60 | 'Min':          float(vals[11]), | 
|---|
| 61 | 'Max':          float(vals[12]), | 
|---|
| 62 | 'Data Rate':    float(vals[13]), | 
|---|
| 63 | 'Errors':       float(vals[14]), | 
|---|
| 64 | 'Reqs':         float(vals[15]), | 
|---|
| 65 | 'ActRateC':     float(vals[16]), | 
|---|
| 66 | 'ActRate':      float(vals[17]), | 
|---|
| 67 | 'Error Rate':   float(vals[18]), | 
|---|
| 68 | 'SamplesT':     float(vals[19]), | 
|---|
| 69 | 'SamplesR':     float(vals[20]) | 
|---|
| 70 | } | 
|---|
| 71 | results.append(d) | 
|---|
| 72 |  | 
|---|
| 73 | return results | 
|---|
| 74 |  | 
|---|
| 75 |  | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 | data = [] | 
|---|
| 79 |  | 
|---|
| 80 | print(options.files) | 
|---|
| 81 |  | 
|---|
| 82 | for file in options.files: | 
|---|
| 83 | # checking if it is a file | 
|---|
| 84 | if os.path.isfile(file): | 
|---|
| 85 | filename = os.path.basename(file) | 
|---|
| 86 | match = re.search("swbsrv\.([0-9]+)gb\.(cfa|nginx)", filename) | 
|---|
| 87 | try: | 
|---|
| 88 | series = match[2] | 
|---|
| 89 | memory = match[1] | 
|---|
| 90 | except: | 
|---|
| 91 | print("Can't parse filename '{}' from File '{}'.".format(filename, file), file=sys.stderr) | 
|---|
| 92 | continue | 
|---|
| 93 |  | 
|---|
| 94 | try: | 
|---|
| 95 | results = extract(file) | 
|---|
| 96 | except OSError: | 
|---|
| 97 | print("Cannot open File '{}'.".format(file), file=sys.stderr) | 
|---|
| 98 | continue | 
|---|
| 99 | except ValueError: | 
|---|
| 100 | continue | 
|---|
| 101 |  | 
|---|
| 102 | for result in results: | 
|---|
| 103 | d = [series, memory, result] | 
|---|
| 104 | data.append(d) | 
|---|
| 105 |  | 
|---|
| 106 | else: | 
|---|
| 107 | print("File '{}' does not exist.".format(file), file=sys.stderr) | 
|---|
| 108 |  | 
|---|
| 109 | options.out.write(json.dumps(data)) | 
|---|
| 110 | options.out.flush() | 
|---|
| 111 | options.out.write("\n") | 
|---|