File tree 1 file changed +26
-0
lines changed
1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
1
+ Monitor Apache / Nginx Log File
2
+ Count the number of hits in a Apache/Nginx
3
+ This small script will count the number of hits in a Apache/Nginx log file.
4
+ How it works
5
+ This script can easily be adapted to any other log file.
6
+
7
+ The script starts with making an empty dictionary for storing the IP addresses andcount how many times they exist.
8
+
9
+ Then we open the file (in this example the Nginx access.log file) and read the
10
+ content line by line.
11
+
12
+ The for loop go through the file and splits the strings to get the IP address.
13
+
14
+ The len() function is used to ensure the length of IP address.
15
+
16
+ If the IP already exists , increase by 1.
17
+ ips = {}
18
+
19
+ fh = open("/var/log/nginx/access.log", "r").readlines()
20
+ for line in fh:
21
+ ip = line.split(" ")[0]
22
+ if 6 < len(ip) <=15:
23
+ ips[ip] = ips.get(ip, 0) + 1
24
+ print ips
25
+ Test it out
26
+ If you now browse to your website, and run the python script, you should see your IP address + the counts.
You can’t perform that action at this time.
0 commit comments