如何在Linux中获取Apache的“每秒请求数”?

时间:2008-12-05 23:56:33

标签: linux apache monitoring

在Windows for ASP中,你可以得到它perfmon,但是......

如何在Linux中获取“每秒请求数”

9 个答案:

答案 0 :(得分:25)

这是一个简短的bash脚本,用于对请求率进行采样(基于日志文件中使用wc -l的{​​{3}})。

#!/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 $0` -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]*\).*/\1/'`
while true
do
 newCount=`wc -l $logFile | sed 's/\([0-9]*\).*/\1/'`
 diff=$(( newCount - lastCount ))
 rate=$(echo "$diff / $frequency" |bc -l)
 echo $rate
 lastCount=$newCount
 sleep $frequency
done

答案 1 :(得分:16)

实时,或者您可以使用mod_status吗?

显然,有top for apache的版本......

答案 2 :(得分:9)

总而言之,您可以使用mod_statusapachetop

或者,您可以使用Adam Franco和Jon Daniel的漂亮脚本来实时观看。

如果您想查看一个临时日期和小时,可以发出以下命令:

grep "29/Oct/2014:12" /var/log/apache2/example.com.log | cut -d[ -f2 | cut -d] -f1 | awk -F: '{print $2":"$3}' | sort -nk1 -nk2 | uniq -c | awk '{ if ($1 > 10) print $0}'

替换为您感兴趣的日期和小时,并使用日志文件的正确路径名。替换。

它将打印出类似的内容:

1913 12:47
 226 12:48
 554 12:49
 918 12:50

有一篇很好的文章here,其中有更多选项可以使用awk,cut和uniq命令组合来快速获取此类数据。

答案 3 :(得分:5)

您可以在访问日志中使用'wc -l'来获取行数(大致对应于请求数...)。每分钟执行一次并减去最后一个值以获取delta ...

答案 4 :(得分:3)

我认为mod_status可以做到......

http://httpd.apache.org/docs/2.0/mod/mod_status.html

您还可以使用zenoss使用社区apache插件从mod_status收集数据。

http://www.zenoss.com/

答案 5 :(得分:3)

脚本显示不一致的数字。 -f参数会影响输出很多!一读也不准确。

我最终使用了:

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

找到here

答案 6 :(得分:3)

我不喜欢我找到的任何解决方案,所以我自己写了。

  • mod_status不够准确。它取决于服务器启动的时间,在我们的例子中通常是几个月。我正在寻找的是交通高峰。
  • 上面的shell脚本使用了一个sleep()语句,这个语句并不理想,因为实际检索数据需要x秒。

因此,此解决方案在access_log 15000请求之前采用特定行,并使用记录的时间与当前时间进行比较。

# 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 $5}' `
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($5... bit
        LASTTIME=`tail -15000 /var/log/httpd/access_log | head -1 | awk '{printf("%s\n",substr($5,2,20));}' `
        APACHE_RPS=`echo $LASTTIME | gawk -vREQUESTS=15000 ' {
                # convert apache datestring into time format accepted by mktime();
                monthstr = substr($0,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($0,8,4), monthint, substr($0,1,2), substr($0, 13,2), substr($0, 16,2), substr($0, 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

答案 7 :(得分:2)

我编写了一组Perl脚本,显示过去1分钟,5分钟和15分钟的平均请求数(如上)。它位于https://gist.github.com/1040144

答案 8 :(得分:1)

mod_status是一个!如果你打电话给:

的http:// {IP} /服务器状态刷新= 1&安培;自动刷新=真

然后它每2秒自动刷新一次,这样你就可以看到一个恒定的实时视图: - )