#!/usr/bin/python # -*- coding: utf-8 -*- # Apache Regex portion original credits to: https://leancrew.com/all-this/2013/07/parsing-my-apache-logs/ ## https://gitlab.com/mikeramsey/access-log-parser ## How to use. # Run the script from your account via manual or curl method. It autodetects the current user and defaults to the todays date if not argument for how many days ago it provided. # For todays hits # ./access-logparser.py # # For yesterdays aka 1 Days ago # ./access-logparser.py 1 # ##python <(curl -s https://gitlab.com/mikeramsey/access-log-parser/-/raw/master/access-logparser.py || wget -qO - https://gitlab.com/mikeramsey/access-log-parser/-/raw/master/access-logparser.py) 1; __author__ = "Michael Ramsey" __version__ = "0.1.2" __license__ = "GPL-3.0" import os import re import sys import time from collections import Counter from datetime import date, timedelta from datetime import datetime from os.path import join, isfile import getpass import glob # import pathlib # print('version is', sys.version) def main(): script = sys.argv[0] # filename = sys.argv[2] # filenametest = "/home/example.com.access_log" # username = 'server' username = getpass.getuser() # print(username) # Define the day of interest in the Apache common log format. Default if not specified try: daysago = int(sys.argv[1]) # daysago = 0 except: daysago = 0 the_day = date.today() - timedelta(daysago) apache_day = the_day.strftime('[%d/%b/%Y:') dcpumon_day = the_day.strftime('%Y/%b/%d') # Set variables to empty controlpanel = '' domlogs_path = '' try: if os.path.isfile('/usr/local/cpanel/cpanel') | os.path.isfile(os.getcwd() + '/cpanel'): controlpanel = 'Cpanel' datetime_dcpumon = date.today().strftime('%Y/%b/%d') # 2020/Feb/10 # Current Dcpumon file dcpumon_current_log = "/var/log/dcpumon/" + datetime_dcpumon # /var/log/dcpumon/2019/Feb/15 acesslog_sed = "-ssl_log" if username == 'root': domlogs_path = '/usr/local/apache/domlogs/' else: user_homedir = "/home/" + username user_accesslogs = "/home/" + username + "/logs/" domlogs_path = "/usr/local/apache/domlogs/" + username elif os.path.isfile('/usr/bin/cyberpanel') | os.path.isfile(os.getcwd() + '/cyberpanel'): controlpanel = 'CyberPanel' acesslog_sed = ".access_log" if username == 'root': # Needs updated to glob all /home/*/logs/ domlogs_path2 = glob.glob('/home/*/logs/') else: # Get users homedir path user_homedir = os.path.expanduser("~" + username) domlogs_path = user_homedir + "/logs/" except: controlpanel = 'Control Panel not found' # Define Output file stats_output = open(os.getcwd() + '/stats.txt', "w") if username == 'root' and controlpanel == 'CyberPanel': # Needs updated to glob all /home/*/logs/ path = '/home/*/logs/*' domlogs_path = glob.glob("/home/*/logs/") print('Root CyberPanel Detected') # Get list of dir contents # logs_path_contents = glob.glob("/home/*/logs/*.access_log", recursive=True) # Get list of files only from this directory logs = glob.glob("/home/*/logs/*.access_log") else: # Define log path directory path = domlogs_path # Get list of dir contents logs_path_contents = os.listdir(path) # Get list of files only from this directory logs = filter(lambda f: isfile(join(path, f)), logs_path_contents) # Regex for the Apache common log format. parts = [ # host %h :ip/hostname of the client 172.68.142.138 # indent %l (unused) :client identity via client's identd configuration - # user %u :HTTP authenticated user ID - # time %t :timestamp [09/Mar/2019:00:38:03 -0600] # request "%r" :request method of request, resource requested, & protocol "POST /wp-login.php HTTP/1.1" # status %>s :Apache status code 404 # size %b (careful,can be'-'):size of request in bytes, excluding headers 3767 # referrer "%{Referer}i" :Referer "https://www.google.com/" # user agent "%{User-agent}i":User-Agent "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0" r'(?P\S+)', r'\S+', r'(?P\S+)', r'\[(?P