From 37a7317c1db3ef90935e3fcb49a99d9fb68bf491 Mon Sep 17 00:00:00 2001 From: Daniel Schmitz Date: Thu, 5 Feb 2015 13:44:53 +0800 Subject: [PATCH] Read file one time and sort counts into array instead of reading log file for each status code again --- plugins/nginx/nginx_error | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/plugins/nginx/nginx_error b/plugins/nginx/nginx_error index ef5efa90..9db63cff 100755 --- a/plugins/nginx/nginx_error +++ b/plugins/nginx/nginx_error @@ -95,10 +95,15 @@ http_codes[502]='Bad Gateway' http_codes[503]='Service Unavailable' do_ () { # Fetch + declare -A line_counts + values=`awk '{print $6}' $log | sort | uniq -c` + while read -r line; do + read -a tmp <<< "$line"; + line_counts[${tmp[1]}]=${tmp[0]}; + done <<< "$values" + for k in ${!http_codes[@]}; do - #value=`awk -v k=$k '{if ($9 == k) { print $9 }}' $log | wc -l` # this is very slow and ugly - value=`awk -v k=$k '{if ($9 == k) { i += 1 }} END { print i }' $log` # this is much better - echo "error$k.value ${value:-0}" + echo "error$k.value ${line_counts[$k]:-0}" done exit 0 }