Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. #!/usr/bin/python
  2. import sys
  3. import os
  4. from collections import Counter
  5. from datetime import datetime
  6.  
  7. ##### start arg part
  8. import argparse
  9. parser = argparse.ArgumentParser(description='This does things to an Apache log file')
  10. parser.add_argument('filename', help='is your filename correct?') # required=True is implied here
  11. my_arg = parser.parse_args()
  12. ##### end arg part
  13.  
  14. ##### initialize stuff
  15. ip_list = [] # for the list of IPs
  16. ms_list = list() # for the list of load times
  17. cnt = int() # initialize counting iterations at 0...
  18. http_code = list() # initialize list
  19. ms_sum = long(0)
  20. ##### initialize stuff
  21.  
  22. with open(my_arg.filename) as fp:
  23.     my_lines = fp.readline() # reads first line
  24.     print("First date and time: %s" % my_lines.strip().split()[3].lstrip('[')) # this is the first date and time
  25.     while my_lines: # continue reading until EOF...
  26.         awk_like = my_lines.strip().split() # whitespace delimited list per line
  27.         ip_list.append(awk_like[0]) # add field 0 to the list of visitors... - make this into a dict
  28.         #ms_sum += long(awk_like[-1]) # this doesn't correct the problem at all
  29.         if awk_like[-1].isdigit():
  30.             ms_list.append(float(awk_like[-1])) # add field -1 to list of load times - make this a set
  31.     if awk_like[9].startswith(('4','5'))
  32.             http_code.append(awk_like[9])
  33.         cnt += 1 # counting the iterations - not the best approach but it works
  34.         my_lines = fp.readline() # point to the next line...
  35.         if my_lines == "": # check for last line in a file - checking for EOF actually here
  36.             fp.seek(-2, os.SEEK_END)
  37.             while fp.read(1) != b"\n":
  38.                 fp.seek(-2, os.SEEK_CUR)
  39.             last = fp.readline()
  40.             print("Last date and time: %s" % last.strip().split()[3].lstrip('[')) # this is the first date and time
  41.  
  42. print(all(isinstance(x, float) for x in ms_list)) # this looks like it's printing all floats...wtf??!?
  43. print("Number of lines parsed: %s" % cnt) # print number of lines parsed - works I guess?
  44. print("Most common visitor: %s" % Counter(ip_list).most_common(1)[0][0]) # find most common in list
  45. print("Total length of load list: %s" % len(ms_list))
  46. #print("ms_sum is", ms_sum) # nada
  47. avg_load_time = (sum(ms_list)-len(ms_list))/len(ms_list) # getting the average load time
  48. print("Average load time: %sms" % avg_load_time) # printing the average load time
  49. max_load_time = max(ms_list)-1 # this is sloppy - shouldn't have to subtact 1
  50. min_load_time = min(ms_list)-1 # this is sloppy - shouldn't have to subtract 1
  51. total_load_time = sum(ms_list)-len(ms_list) # this is sloppy - can't figure out why sum is wrong (everything is a float!)
  52. print("Min load time: %sms" % min_load_time)
  53. print("Max load time: %sms" % max_load_time) # this doesn't work correctly - it's already an int though
  54. print("Total load time: %sms" % total_load_time)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement