Skip to content

Instantly share code, notes, and snippets.

@denzuko
Forked from dgoodwin/ansible-log-analyzer.py
Created September 30, 2019 00:45
Show Gist options
  • Save denzuko/985cd84f2a5f1ea8c407c682f94a7e8f to your computer and use it in GitHub Desktop.
Save denzuko/985cd84f2a5f1ea8c407c682f94a7e8f to your computer and use it in GitHub Desktop.
Ansible Log Scraper
#!/usr/bin/env python
#
# Usage: ansible-log-analyzer.py [LOGFILE]
import re
import sys
from datetime import datetime, timedelta
TASK_RE = re.compile(r'(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3}) p=\d+ u=[\w-]* \|\s*TASK: \[(.*)\]')
first_task_time = None
last_task_name = None
last_task_start = None
# Map task name to a dict of 'iterations' (an int) and 'runtime' (a timedelta):
results = {}
with open(sys.argv[1]) as f:
for line in f:
match = TASK_RE.match(line)
if match:
task_name = match.groups()[1]
task_start = datetime.strptime(match.groups()[0], '%Y-%m-%d %H:%M:%S,%f')
if task_name not in results:
results[task_name] = {
"iterations": 0,
"runtime": timedelta(),
}
if first_task_time is None:
first_task_time = task_start
else:
# First adjust the interval and runtime for previous task
delta = task_start - last_task_start
results[last_task_name]["iterations"] = results[last_task_name]["iterations"] + 1
results[last_task_name]["runtime"] = results[last_task_name]["runtime"] + delta
last_task_name = task_name
last_task_start = task_start
def left_pad(i):
if i < 10:
return "0%s" % i
return "%s" % i
# Sort the results into a list of tuples, descending order based on total runtime:
tuples = [(k, v['iterations'], v['runtime']) for k, v in results.iteritems()]
sorted_tuples = sorted(tuples, key=lambda x: x[2])
for task, iterations, runtime in sorted_tuples:
print task
print " iterations: %s" % iterations
#hours, remainder = divmod(runtime.seconds, 3600)
minutes, seconds = divmod(runtime.seconds, 60)
print " total time: %s:%s" % (left_pad(minutes), left_pad(seconds))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment