|
| 1 | +""" |
| 2 | +Module used to: |
| 3 | +1. Collect the latencies from the provided logs, |
| 4 | +2. generates a graph for the top N latencies against time |
| 5 | +
|
| 6 | +The input file is a modified file, which contains latencies are grepped from ceph OSD log for faster run. |
| 7 | +-> grep -a dequeue_op.*latency ceph-osd.log > latencies.log |
| 8 | +( Performed to reduce run time, since lots of logging would be generated ) |
| 9 | +
|
| 10 | +NOTE : The debug levels for the OSD for the latencies to be captured, |
| 11 | + the Param: debug_osd needs to be set to 20 globally |
| 12 | +""" |
| 13 | + |
| 14 | +import re |
| 15 | +import sys |
| 16 | +from datetime import datetime |
| 17 | + |
| 18 | +import openpyxl |
| 19 | +from docopt import docopt |
| 20 | +from openpyxl.chart import LineChart, Reference |
| 21 | + |
| 22 | +doc = """ |
| 23 | +Usage: |
| 24 | + generate_latency_graph.py --latencies-log <file_name> [--num-ops <ops>] |
| 25 | +
|
| 26 | +Options: |
| 27 | + --latencies-log <name> Name of the file, where the latencies are grepped from ceph OSD log |
| 28 | + -> grep -a dequeue_op.*latency ceph-osd.log > latencies.log |
| 29 | + ----num-ops <num> Top N latencies to be collected for the graph |
| 30 | +""" |
| 31 | + |
| 32 | + |
| 33 | +class Op(object): |
| 34 | + def __init__(self, line): |
| 35 | + self.lat = float(re.search(r"latency (\d+.\\d+)", line).group(1)) |
| 36 | + self.line = line |
| 37 | + time_format = "%Y-%m-%d %H:%M:%S.%f" |
| 38 | + self.time = datetime.strptime(line[:23], time_format) |
| 39 | + |
| 40 | + |
| 41 | +def run(args): |
| 42 | + logfile = args["--latencies-log"] |
| 43 | + num_ops = int(args.get("--num-ops", 10000)) |
| 44 | + ops = [] |
| 45 | + top_ops = [] |
| 46 | + |
| 47 | + with open(logfile) as f: |
| 48 | + for line in f.readlines(): |
| 49 | + ops.append(Op(line)) |
| 50 | + ops.sort(key=lambda x: x.time) |
| 51 | + print(f"{num_ops} longest requests:") |
| 52 | + for i in range(min(num_ops, len(ops))): |
| 53 | + top_ops.append((ops[i].lat, ops[i].time, ops[i].line)) |
| 54 | + top_ops.sort(key=lambda x: x[1]) |
| 55 | + # Creating workbook and adding data to it |
| 56 | + wb = openpyxl.Workbook() |
| 57 | + sheet = wb.active |
| 58 | + text = f"Operations from Timestamp :{ops[0].time} - {ops[-1].time}" |
| 59 | + sheet.append([text]) |
| 60 | + for val in top_ops: |
| 61 | + sheet.append([val[0], val[1]]) |
| 62 | + |
| 63 | + max_row = sheet.max_row # if sheet.max_row < 100 else 100 |
| 64 | + values = Reference(sheet, min_col=1, min_row=2, max_col=1, max_row=max_row) |
| 65 | + # Create object of LineChart class |
| 66 | + chart = LineChart() |
| 67 | + chart.add_data(values) |
| 68 | + # set the title of the chart |
| 69 | + chart.title = f" Latencies {text}" |
| 70 | + chart.x_axis.title = " Time progression ----> " |
| 71 | + chart.y_axis.title = " Time in sec " |
| 72 | + sheet.add_chart(chart, "E2") |
| 73 | + |
| 74 | + # save the file |
| 75 | + fname = f"{sys.argv[1]}.xlsx" |
| 76 | + wb.save(fname) |
| 77 | + print("Done!") |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + args = docopt(doc) |
| 82 | + try: |
| 83 | + run(args) |
| 84 | + except Exception as err: |
| 85 | + print( |
| 86 | + f"Exception hit while generating graph for the latencies provided.\n error : {err} " |
| 87 | + ) |
| 88 | + exit(1) |
0 commit comments