Created
August 14, 2013 11:35
-
-
Save 0atman/6230261 to your computer and use it in GitHub Desktop.
errors-by-date.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import datetime | |
| from collections import Counter | |
| import json | |
| import argparse | |
| parser = argparse.ArgumentParser( | |
| description='Count Tomcat errors and group by date.' | |
| ) | |
| parser.add_argument( | |
| 'path', | |
| metavar='path', | |
| type=str, | |
| help='the path to the log files' | |
| ) | |
| args = parser.parse_args() | |
| dates = [] | |
| for root,dirs,files in os.walk(args.path): | |
| print len(files), "files in", args.path | |
| for file in files: | |
| f=open(os.path.join(root,file), 'r') | |
| for line in f.readlines(): | |
| if "ERROR" in line: | |
| if "[" not in line.split(',')[0]: | |
| date = line.split(',')[0] | |
| else: | |
| date = line.split(',')[0].split('[')[1] | |
| day = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S').date() | |
| dates.append(day.strftime('%Y-%m-%d')) | |
| f.close() | |
| date_counter = Counter(dates) | |
| print json.dumps(date_counter, indent=2) | |
| print reduce(lambda x, y: x + y, date_counter.values(), 0) , "errors" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment