#!/usr/local/bin/python3 # coding: utf-8 # ==================================================== # Author: chang - EMail:changbo@hmg100.com # Last modified: 2017-6-2 # Filename: logcheck.py # Description: real time analysis nginx log,base sys, Counter # blog:http://www.cnblogs.com/changbo # ==================================================== from collections import Counter import json import os import sys import threading logfile = sys.argv[1] # logfile = 'hmgaccess.log' logname = logfile[:-4] txtname = '%s.txt' % logname def concurrent(filepath): timelist = [] with open(filepath) as f: line = f.readlines() for i in line: timetmp = i.split(" ") if len(timetmp) > 2: timelist.append((((timetmp[3]).split("/"))[2])[5:]) count = dict(Counter(timelist)) # output = sys.stdout jsObj = json.dumps(count) with open('concurrent.txt', 'a+') as f: # sys.stdout = f f.write(jsObj) # sys.stdout = output print("success!please check file concurrent.txt!!") with open('concurrent.txt') as f2: line = f2.readlines() newline = eval(line[0]) linetmp = dict(zip(newline.values(), newline.keys())) newline = sorted(linetmp.items(), key=lambda d: d[0], reverse=True) print(type(newline)) for line in newline: key, value = line print(key, value) with open(txtname, 'a+') as f3: f3.write('%s %s ' % (value, key)) os.remove('concurrent.txt') if __name__ == '__main__': t1 = threading.Thread(target=concurrent(logfile)) t1.start()
END!