#!/usr/bin/env python

#
# Plot the apache error activity versus the nebulous activity
#

import sys
import os
import gzip
import parse_apache_log

def getFilenamesByPrefix(directoryName, prefix):
    """
    Get a list of file names in directory starting by prefix
    """
    filenames = []
    for entry in os.listdir(directoryName):
        fullpath = directoryName + os.sep + entry
        if entry.startswith(prefix):
            if not os.path.isdir(fullpath):
                filenames.append(fullpath)
    return filenames

def processAccessFile(filename):
    """
    Process a Nebulous apache access_log file (that can be gzipped or not)
    """
    isGZipFile = False
    try:
        inFile = gzip.open(filename)
        inFile.read(1)
        inFile.close()
        isGZipFile = True
    except IOError:
        pass
    if isGZipFile:
        inFile = gzip.open(filename)
    else:
        inFile = open(filename)
    sys.stderr.write('File %s is %sa gzip file\n' % (filename, 
                                                     '' if isGZipFile else 'not '))
    counts = parse_apache_log.process(inFile)
    #parse_apache_log.show_results(counts, sys.stdout)
    return counts

def processErrorFile(filename):
    """
    Process a Nebulous apache error_log file (that can be gzipped or not)
    """
    isGZipFile = False
    try:
        inFile = gzip.open(filename)
        inFile.read(1)
        inFile.close()
        isGZipFile = True
    except IOError:
        pass
    if isGZipFile:
        inFile = gzip.open(filename)
    else:
        inFile = open(filename)
    sys.stderr.write('File %s is %sa gzip file\n' % (filename, 
                                                   '' if isGZipFile else 'not '))
    counts = parse_apache_log.processErrorLog(inFile)
    #parse_apache_log.show_errors(counts, sys.stdout)
    return counts

if __name__ == '__main__':
    """
    Usage: python sys.argv[0] <directory name containing all access_log and error_log files>

       Application log is written to /dev/stderr
       Results are written to /dev/stdout
    """
    # All logs are apache2 logs and can be found in one directory
    # which happens to be the first script argument
    logDirectoryName = sys.argv[1]

    sys.stderr.write('### Building access log filenames list\n')
    accessFilenames = getFilenamesByPrefix(logDirectoryName, 
                                           'access_log')
    sys.stderr.write('  %d access_log files\n' % (len(accessFilenames)))
    for accessFilename in accessFilenames:
        sys.stderr.write('\tFilename: %s\n' % accessFilename)
    sys.stderr.write('### Building error log filenames list\n')
    errorFilenames = getFilenamesByPrefix(logDirectoryName, 
                                          'error_log')
    sys.stderr.write('  %d error_log files\n' % (len(errorFilenames)))
    for errorFilename in errorFilenames:
        sys.stderr.write('\tFilename: %s\n' % errorFilename)

    accessCounts = dict()
    for accessFilename in accessFilenames:
        accessCounts.update(processAccessFile(accessFilename))
    errorCounts = dict()
    for errorFilename in errorFilenames:
        errorCounts.update(processErrorFile(errorFilename))

    # Now show results
    keys = set()
    keys.update(accessCounts.keys())
    keys.update(errorCounts.keys())
    for key in keys:
        try:
            nebulousAccessCount = 0
            for index in range(5):
                nebulousAccessCount += accessCounts[key][index]
            createCount = accessCounts[key][0]
            findInsCount = accessCounts[key][1]
            deleteCount = accessCounts[key][2]
            failStatCount = accessCounts[key][3]
            statCount = accessCounts[key][4]
            otherCount = accessCounts[key][5]
            unknownCount = accessCounts[key][6]
        except KeyError:
            nebulousAccessCount = -1
            createCount = -1
            findInsCount = -1
            deleteCount = -1
            failStatCount = -1
            statCount = -1
            otherCount = -1
            unknownCount = -1
        try:
            errorCount = errorCounts[key]
        except KeyError:
            errorCount = -1
        sys.stdout.write('%s %d %d %d %d %d %d %d %d %d\n' % (key, nebulousAccessCount, errorCount,
                                                              createCount, findInsCount, deleteCount,
                                                              failStatCount, statCount, otherCount, 
                                                              unknownCount))
