How to get “requests per second” for Apache in Linux?

前端 未结 9 940
情书的邮戳
情书的邮戳 2021-01-30 17:53

In Windows for ASP, you can get it perfmon, but...

How to get \"requests per second\" for Apache in Linux?

9条回答
  •  忘了有多久
    2021-01-30 18:58

    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.

    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.

    # 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
    

提交回复
热议问题