#!/usr/bin/python

'''
    File name: log_quicklook.py
    Prepared by: MCL
    Date created: 1/6/2017
    Date last modified: 5/6/2017
    Python Version: 2.3+

    This script extracts quicklook access information from the access_log[.X]
    generated by Apache 2.0 through identifying the key phrases using grep
    and the Python re module (regular expressions) if necessary.

    (1) GET /DataProd/quicklook/
    (2) http://telescope.livjm.ac.uk/DataProd/quicklook/

    An example of Apache 2.0 NCSA extended/combined log format:
    "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""

    %h: 80.229.2.174
    %l: -
    %u: NSO_Priority_3
    %t: [20/May/2017:18:00:18 +0100]
    %r: "GET /DataProd/quicklook/NSO_Priority_3/20170519/
        h_e_20170519_15_1_1_9.head HTTP/1.1"
    %s: 200
    %b: 16966
    {Referer}: "http://telescope.livjm.ac.uk/DataProd/quicklook/
               NSO_Priority_3/20170519/"
    {User-Agent}: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5)
                   AppleWebKit/537.36 (KHTML, like Gecko)
                   Chrome/58.0.3029.110 Safari/537.36"

'''


import os
import re
import time
import subprocess
from datetime import timedelta
from datetime import datetime


def get_creation_time(path_to_file):
    """
    Get the time of file creation/last modification.
    (minimum requirement python 2.3)
    """
    try:
        ctime = float(os.path.getctime(path_to_file))
    except AttributeError:
        # Can't get creation time, returning last modified time
        ctime = float(os.path.getmtime(path_to_file))
    return ctime


def remove_rows_before_given_time(text, regex, ftime):
    """
    Remove lines in the chuck of log that was added before the given time.
    (minimum requirement python 2.3)
    """
    text = text.splitlines()
    new_text = ''
    # populate logs into the dictionary
    for i, line in enumerate(text):
        items = re.match(regex, line).groups()
        t = items[3]
        # timezone adjustment
        t_diff = timedelta(hours=int(t[-5:-2]), minutes=int(t[-2:]))
        # this line can be replaced by strptime in python 2.4+
        item_time = datetime(
            *(time.strptime(t[:-6], "%d/%b/%Y:%H:%M:%S")[0:6])
            ) + t_diff
        item_unix_time = time.mktime(item_time.timetuple())
        if item_unix_time > ftime:
            new_text += line + '\n'
    return new_text


logs_path = "/var/log/httpd/"
# logs_path = "/home/eng/log_mcl_test/"
output_path = "/home/eng/log_quicklook/"
output_filename = "log_quicklook.txt"

# regular expression to split the above text
regex_access_log = '([(\d\.)]+) (.*) (.*) \[(.*?)\] "(.*?)" (\d+) (.*) ' +\
                   '"(.*?)" "(.*?)"'

# key 'phrase' to search in the apache log
search_key = "\"GET /DataProd/quicklook/.*.livjm.ac.uk/DataProd/quicklook/\""

# Create output folder and file if not exist
if not os.path.isdir(output_path):
    subprocess.call('mkdir ' + output_path, shell=True)


if not os.path.isfile(output_path + output_filename):
    subprocess.call('touch ' + output_path + output_filename, shell=True)


for i in range(20):

    filename = 'access_log'
    if i != 0:
        filename += '.' + str(i)

    log_path = logs_path + filename
    if not os.path.isfile(log_path):
        break

    # if running the script for the first time, set file_ctime to 0
    file_ctime = get_creation_time(log_path)
    file_ctime = 0

    # grep lines with the search key and pipe directly to memory
    lines = subprocess.Popen("grep " + search_key + " " + logs_path +
                             filename, shell=True,
                             stdout=subprocess.PIPE).communicate()[0]
    lines_reduced = remove_rows_before_given_time(lines, regex_access_log,
                                                  file_ctime)

    # write to file
    f = open(output_path + output_filename, 'a')
    f.write(lines_reduced)
    f.close()
