Как получить & ldquo; количество запросов в секунду & rdquo; для Apache в Linux?

21
задан Daniel Silveira 6 December 2008 в 00:11
поделиться

5 ответов

В в реальном времени, или можно ли использовать mod_status?

И по-видимому, существует версия вершина для апача ...

16
ответ дан Gunny 6 December 2008 в 10:11
поделиться

Я думаю, что mod_status может это сделать ...

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

Вы также можете использовать zenoss для сбора данных из mod_status с помощью плагина apache сообщества.

http://www.zenoss.com/

3
ответ дан benlumley 6 December 2008 в 00:11
поделиться

Мне не понравилось ни одно из найденных решений, поэтому я написал свое собственное.

  • mod_status не совсем точен. Он основан на том, как долго работает сервер, что в нашем случае обычно составляет месяцы. То, что я ищу, это всплески трафика.
  • В приведенном выше сценарии оболочки используется оператор 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([110],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([110],8,4), monthint, substr([110],1,2), substr([110], 13,2), substr([110], 16,2), substr([110], 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
3
ответ дан Jon Daniel 6 December 2008 в 00:11
поделиться

Можно использовать 'туалет-l' на журнале доступа для получения количества строк (который примерно соответствует количеству запросов...), Делают это каждую минуту и вычитают последнее значение для получения дельты...

5
ответ дан dicroce 6 December 2008 в 10:11
поделиться

Вот короткий сценарий bash, который я составил для выборки частоты запросов (на основе предложения dicroce использования wc -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 $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
25
ответ дан 29 November 2019 в 06:25
поделиться
Другие вопросы по тегам:

Похожие вопросы: