Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count accesses per hour from log file entries?

I have a log-file where every line contains IP address, time of access, and the URL accessed. I want to count the accesses per hour.

Time of access data looks like this

[01/Jan/2017:14:15:45 +1000]
[01/Jan/2017:14:15:45 +1000]
[01/Jan/2017:15:16:05 +1000]
[01/Jan/2017:16:16:05 +1000] 

How can I improve it so I don't need to set up the variable and if statement for every hour?

twoPM = 0
thrPM = 0
fouPM = 0
timeStamp = line.split('[')[1].split(']')[0]
formated_timeStamp = datetime.datetime.strptime(timeStamp,'%d/%b/%Y:%H:%M:%S %z').strftime('%H')
if formated_timeStamp == '14':
    twoPM +=1
if formated_timeStamp == '15':
    thrPM +=1
if formated_timeStamp == '16':
    fouPM +=1
like image 653
Lucia Lin Avatar asked Aug 22 '17 07:08

Lucia Lin


1 Answers

  1. You can include the brackets into your strptime format description:

    datetime.datetime.strptime(line.strip(),'[%d/%b/%Y:%H:%M:%S %z]')
    
  2. You can extract the hour using the .hour attribute of any datetime.datetime object:

    timestamp = datetime.datetime.strptime(…)
    hour = timestamp.hour
    
  3. You can count the number of elements using a collections.Counter:

    from collections import Counter
    
    
    def read_logs(filename):
        with open(filename) as log_file:
             for line in log_file:
                 timestamp = datetime.datetime.strptime(
                         line.strip(),
                         '[%d/%b/%Y:%H:%M:%S %z]')
                 yield timestamp.hour
    
    
    def count_access(log_filename):
        return Counter(read_logs(log_filename))
    
    
    if __name__ == '__main__':
        print(count_access('/path/to/logs/'))
    
like image 74
301_Moved_Permanently Avatar answered Sep 30 '22 10:09

301_Moved_Permanently