Skip to content

Instantly share code, notes, and snippets.

@dgoodwin
Created July 19, 2016 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dgoodwin/36cd1fe9ab4b13359662634440af8012 to your computer and use it in GitHub Desktop.
Save dgoodwin/36cd1fe9ab4b13359662634440af8012 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))
@tkettenba
Copy link

Hello,
i like this script. Unfortunately newer ansible seem not to flag the tasks with 'TASK:', but just 'TASK'.

Can you please update the RE to:

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 \[(.*)\]')

Maybe also switch to 'print functions' by adding 'from future import print_function'

BR,
Tom

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment