11#!/usr/bin/python
22# -*- coding: utf-8 -*-
33# Apache Regex portion original credits to: https://leancrew.com/all-this/2013/07/parsing-my-apache-logs/
4+ ## https://gitlab.com/mikeramsey/access-log-parser
5+ ## How to use.
6+ # 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.
7+ # For todays hits
8+ # ./access-logparser.py
9+ #
10+ # For yesterdays aka 1 Days ago
11+ # ./access-logparser.py 1
12+ #
13+ ##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;
14+
415
516__author__ = "Michael Ramsey"
6- __version__ = "0.1.0 "
17+ __version__ = "0.1.1 "
718__license__ = "GPL-3.0"
819
920import os
1425from datetime import date , timedelta
1526from datetime import datetime
1627from os .path import join , isfile
28+ import getpass
29+ import glob
30+
1731
32+ # import pathlib
1833
1934# print('version is', sys.version)
2035
@@ -24,10 +39,11 @@ def main():
2439 # filename = sys.argv[2]
2540 # filenametest = "/home/example.com.access_log"
2641 # username = 'server'
27- username = str (sys .argv [1 ])
42+ username = getpass .getuser ()
43+ # print(username)
2844 # Define the day of interest in the Apache common log format. Default if not specified
2945 try :
30- daysago = int (sys .argv [2 ])
46+ daysago = int (sys .argv [1 ])
3147 # daysago = 0
3248 except :
3349 daysago = 0
@@ -46,7 +62,7 @@ def main():
4662 # Current Dcpumon file
4763 dcpumon_current_log = "/var/log/dcpumon/" + datetime_dcpumon # /var/log/dcpumon/2019/Feb/15
4864 acesslog_sed = "-ssl_log"
49- if username == 'server ' :
65+ if username == 'root ' :
5066 domlogs_path = '/usr/local/apache/domlogs/'
5167 else :
5268 user_homedir = "/home/" + username
@@ -56,9 +72,9 @@ def main():
5672 elif os .path .isfile ('/usr/bin/cyberpanel' ) | os .path .isfile (os .getcwd () + '/cyberpanel' ):
5773 controlpanel = 'CyberPanel'
5874 acesslog_sed = ".access_log"
59- if username == 'server ' :
75+ if username == 'root ' :
6076 # Needs updated to glob all /home/*/logs/
61- domlogs_path = '/home/username/Desktop/domlogs'
77+ domlogs_path2 = glob . glob ( '/home/*/logs/' )
6278 else :
6379 # Get users homedir path
6480 user_homedir = os .path .expanduser ("~" + username )
@@ -70,16 +86,24 @@ def main():
7086 # Define Output file
7187 stats_output = open (os .getcwd () + '/stats.txt' , "w" )
7288
73- # Define log path directory
74- path = domlogs_path
89+ if username == 'root' and controlpanel == 'CyberPanel' :
90+ # Needs updated to glob all /home/*/logs/
91+ path = '/home/*/logs/*'
92+ domlogs_path = glob .glob ("/home/*/logs/" )
93+ print ('Root CyberPanel Detected' )
94+ # Get list of dir contents
95+ # logs_path_contents = glob.glob("/home/*/logs/*.access_log", recursive=True)
7596
76- # path = "/home/username/Desktop/domlogs"
97+ # Get list of files only from this directory
98+ logs = glob .glob ("/home/*/logs/*.access_log" )
7799
78- # Get list of dir contents
79- logs_path_contents = os .listdir (path )
80-
81- # Get list of files only from this directory
82- logs = filter (lambda f : isfile (join (path , f )), logs_path_contents )
100+ else :
101+ # Define log path directory
102+ path = domlogs_path
103+ # Get list of dir contents
104+ logs_path_contents = os .listdir (path )
105+ # Get list of files only from this directory
106+ logs = filter (lambda f : isfile (join (path , f )), logs_path_contents )
83107
84108 # Regex for the Apache common log format.
85109 parts = [ # host %h :ip/hostname of the client 172.68.142.138
@@ -330,15 +354,15 @@ def keyfunction(k):
330354
331355 print ('Accesslog path used: ' + path )
332356 # print(dcpumon_current_log)
333-
357+ print ( '============================================' )
334358 d = post_request_dict
335359 # Using dictionary comprehension to find list
336360 # keys having value in 0 will be removed from results
337361 delete = [key for key in d if d [key ] == 0 ]
338362
339363 # delete the key
340364 for key in delete : del d [key ]
341-
365+ print ( ' ' )
342366 print ('''Top POST requests for %s''' % the_day .strftime ('%b %d, %Y' ))
343367 print (' ' )
344368 # sort by dictionary by the values and print top 10 {key, value} pairs
0 commit comments