Home >Operation and Maintenance >Linux Operation and Maintenance >How to write a shell script to count appche site IP visits in Linux

How to write a shell script to count appche site IP visits in Linux

WBOY
WBOYforward
2023-05-12 23:28:101813browse

It is often necessary to count apache site visits based on IP address, the most basic script.

Arrange in descending order according to IP visits:

Copy the code The code is as follows:

# !/bin/bash
#script_name: access_count
acc_log=/usr/local/apache2/logs/access_log
/bin/awk '{print $1}' $acc_log | sort | uniq -c | sort -nr

Execution effect:

Copy code The code is as follows:

[root@zabbix ~]# sh access_count
94989 192.168.100.34
38863 192.168.200.92
23658 192.168.1.71
16720 192.168.100.80
13688 192.168.200.34
1618 192.168.100.104
1251 192.168.1.202
1195 192.168.100.30
1058 192.168.1.203
934 192.168.1.208
792 127.0.0.1
773 192.168.5.126
189 192.168.68

i of the top three IP address:

Copy code The code is as follows:

#!/bin/bash

#script_name:access_count
acc_log=/usr/local/apache2/logs/access_log
/bin/ awk '{print $1}' $acc_log | sort | uniq -c | sort -nr | head -n 3

Execution effect:

Copy code The code is as follows:

[root@zabbix ~]# sh access_count

94989 192.168.100.34
38863 192.168.200.92
23658 192.168.1.71

apache site access error statistics:

Copy code The code is as follows:

#!/bin/bash

#script_name:error_count
err_log=/usr/local/apache2/logs/error_log
cat $err_log | grep -e "^\[" | awk '{print $6}' | sort | uniq -c |sort -nr

Execution effect:

Copy code The code is as follows:

[root@zabbix ~]# sh error_count

701 [core:notice]
30 [mpm_event:notice]
12 [core:warn]
1 [:error]

The above is the detailed content of How to write a shell script to count appche site IP visits in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete