Search code examples
phpawkmetrics

extract rate as a percent from access log file


I am trying to calculate the Error rate % from a PHP application for PHP metrics. I read the access.log file and got help outputting the total HTTP 200, 300, 400, 500 errors but I want to graph the error rate based on the 400 and 500 errors:

awk '{print $9}' access.log | sort | uniq -c | sort -rn | awk '{total += $1; print} END {print total, "Total"}'

above gives me break down count and total of all HTTP errors. Obviously, 200 is not an error. To get the error rate, I would have to divide total / 400+500 error count. Whats the best way to search for 400 and 500 errors and divide by the total to get % rate..


Solution

  • awk '{ tot[$9]+=1 } END { for (i in tot) { totl+=tot[i]} print ((tot[500]+tot[400])/totl)*100"%" }' access.log
    

    Create an array tot with the error code as the index and increment the counter every time the error is encountered. At the end, loop through the tot array an add to a running total (totl). Use this totl figure and the array figures for error 400 and 500 to perform the math required.