如何在 Linux 中获取 Apache 的“每秒请求数”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/345546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get "requests per second" for Apache in Linux?
提问by Daniel Silveira
In Windows for ASP, you can get it perfmon, but...
在 Windows for ASP 中,您可以获得 perfmon,但是...
How to get "requests per second"for Apache in Linux?
如何在 Linux 中获取 Apache 的“每秒请求数”?
采纳答案by Gunny
In realtime, or can you use mod_status?
实时,还是可以使用mod_status?
And apparently, there is a version of top for apache...
回答by benlumley
I think mod_status can do it ...
我认为 mod_status 可以做到...
http://httpd.apache.org/docs/2.0/mod/mod_status.html
http://httpd.apache.org/docs/2.0/mod/mod_status.html
You can also use zenoss to collect data from mod_status using the community apache plugin.
您还可以使用 zenoss 使用社区 apache 插件从 mod_status 收集数据。
回答by dicroce
You can use 'wc -l' on the access log to get the number of lines (which roughly corresponds to the number of requests...) Do that every minute and subtract the last value to get the delta...
您可以在访问日志上使用“wc -l”来获取行数(大致对应于请求数...)每分钟执行一次并减去最后一个值以获得增量...
回答by Adam Franco
Here is a short bash script I made up to sample the request rate (based on dicroce's suggestionof using wc -l
on the log file).
这是我编写的一个简短的 bash 脚本,用于对请求率进行采样(基于dicrocewc -l
在日志文件上使用的建议)。
#!/bin/sh
##############################################################################
# This script will monitor the number of lines in a log file to determine the
# number of requests per second.
#
# Example usage:
# reqs-per-sec -f 15 -i /var/www/http/access.log
#
# Author: Adam Franco
# Date: 2009-12-11
# License: http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
##############################################################################
usage="Usage: `basename while true; do tail -n0 -f access.log>/tmp/tmp.log & sleep 2; kill $! ; wc -l /tmp/tmp.log | cut -c-2; done 2>/dev/null
` -f <frequency in seconds, min 1, default 60> -l <log file>"
# Set up options
while getopts ":l:f:" options; do
case $options in
l ) logFile=$OPTARG;;
f ) frequency=$OPTARG;;
\? ) echo -e $usage
exit 1;;
* ) echo -e $usage
exit 1;;
esac
done
# Test for logFile
if [ ! -n "$logFile" ]
then
echo -e $usage
exit 1
fi
# Test for frequency
if [ ! -n "$frequency" ]
then
frequency=60
fi
# Test that frequency is an integer
if [ $frequency -eq $frequency 2> /dev/null ]
then
:
else
echo -e $usage
exit 3
fi
# Test that frequency is an integer
if [ $frequency -lt 1 ]
then
echo -e $usage
exit 3
fi
if [ ! -e "$logFile" ]
then
echo "$logFile does not exist."
echo
echo -e $usage
exit 2
fi
lastCount=`wc -l $logFile | sed 's/\([0-9]*\).*//'`
while true
do
newCount=`wc -l $logFile | sed 's/\([0-9]*\).*//'`
diff=$(( newCount - lastCount ))
rate=$(echo "$diff / $frequency" |bc -l)
echo $rate
lastCount=$newCount
sleep $frequency
done
回答by Daniel S. Sterling
I wrote a set of Perl scripts that show the average requests-per-second for the past 1, 5 and 15 minutes (like top). It's at https://gist.github.com/1040144.
我编写了一组 Perl 脚本,显示过去 1、5 和 15 分钟(如 top)的平均每秒请求数。它位于https://gist.github.com/1040144。
回答by xztraz
Script shows inconsistent numbers. -f
parameter affects output a lot! and first reading is not accurate either.
脚本显示不一致的数字。-f
参数对输出影响很大!而且初读也不准确。
I ended up using:
我最终使用了:
# This check is needed because if the logs have just rolled over, then we need a minimum
# amount of data to report on.
# You will probably need to adjust the 3500000 - this is roughly the file size when the
# log file hits 15000 requests.
FILESIZE=`ls -l /var/log/httpd/access_log | awk '{print }' `
if [ $FILESIZE -le 3500000 ]
then
# not enough data - log file has rolled over
echo "APACHE_RPS|0"
else
# Based on 15000 requests. Depending on the location of the date field in
# your apache log file you may need to adjust the ...substr(... bit
LASTTIME=`tail -15000 /var/log/httpd/access_log | head -1 | awk '{printf("%s\n",substr(,2,20));}' `
APACHE_RPS=`echo $LASTTIME | gawk -vREQUESTS=15000 ' {
# convert apache datestring into time format accepted by mktime();
monthstr = substr(grep "29/Oct/2014:12" /var/log/apache2/example.com.log | cut -d[ -f2 | cut -d] -f1 | awk -F: '{print ":"}' | sort -nk1 -nk2 | uniq -c | awk '{ if ( > 10) print 1913 12:47
226 12:48
554 12:49
918 12:50
}'
,4,3);
if(monthstr == "Jan"){ monthint = "01"; }
if(monthstr == "Feb"){ monthint = "02"; }
if(monthstr == "Mar"){ monthint = "03"; }
if(monthstr == "Apr"){ monthint = "04"; }
if(monthstr == "May"){ monthint = "05"; }
if(monthstr == "Jun"){ monthint = "06"; }
if(monthstr == "Jul"){ monthint = "07"; }
if(monthstr == "Aug"){ monthint = "08"; }
if(monthstr == "Sep"){ monthint = "09"; }
if(monthstr == "Oct"){ monthint = "10"; }
if(monthstr == "Nov"){ monthint = "11"; }
if(monthstr == "Dec"){ monthint = "12"; }
mktimeformat=sprintf("%s %s %s %s %s %s [DST]\n", substr(##代码##,8,4), monthint, substr(##代码##,1,2), substr(##代码##, 13,2), substr(##代码##, 16,2), substr(##代码##, 19,2) );
# calculate difference
difference = systime() - mktime(mktimeformat);
# printf("%s - %s = %s\n",systime(), mktime(mktimeformat), difference);
printf("%s\n",REQUESTS/difference);
} ' `
echo "APACHE_RPS|${APACHE_RPS}"
fi
Found here.
在这里找到。
回答by psf
mod_status is the one! if you call it with:
mod_status 就是其中之一!如果你用以下方式调用它:
http://{ip}/server-status?refresh=1&auto-refresh=true
http://{ip}/server-status?refresh=1&auto-refresh=true
Then it auto refreshes every 2 seconds so you can see a constant realtime view :-)
然后它每 2 秒自动刷新一次,以便您可以看到恒定的实时视图 :-)
回答by Jon Daniel
I didn't like any of the solutions I found, so I wrote my own.
我不喜欢我找到的任何解决方案,所以我自己写了一个。
- mod_status isn't really accurate enough. It's based on how long the server is up, which in our case is normally months. What I'm looking for is traffic spikes.
- the shell script above uses a sleep() statement which isn't ideal as it takes x seconds to actually retrieve the data.
- mod_status 不够准确。它基于服务器运行的时间,在我们的例子中通常是几个月。我正在寻找的是流量高峰。
- 上面的 shell 脚本使用 sleep() 语句,这并不理想,因为实际检索数据需要 x 秒。
So this solution takes a particular line in the access_log 15000 requests ago, and uses the time recorded to compare with the current time.
所以这个解决方案在access_log 15000个请求之前取了一个特定的行,并使用记录的时间与当前时间进行比较。
##代码##回答by Wtower
To sum up, you can use mod_statusand apachetop.
综上所述,您可以使用mod_status和apachetop。
Alternatively, you can use Adam Franco's and Jon Daniel's nice scripts to have a live look.
或者,您可以使用 Adam Franco 和 Jon Daniel 的精彩脚本进行现场查看。
If you would like to have a look at a partiular date and hour, you can issue this little command:
如果您想查看特定日期和时间,可以发出以下小命令:
##代码##Replace with the date and hour you are interested and also with the proper pathfilename of the log file.
替换为您感兴趣的日期和时间以及日志文件的正确路径文件名。
It will print out something like:
它会打印出类似的东西:
##代码##There is a nice article herewith more options on using a combination of awk, cut and uniq commands to get quick stats of the kind.
这里有一篇不错的文章,其中提供了更多关于使用 awk、cut 和 uniq 命令组合来获取此类快速统计信息的选项。