From f4c192469fbdd038e7f7aca890cfa8e467eba189 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 20 Jan 2018 00:54:20 +0100 Subject: [PATCH 1/7] nginx_error: introduce real version number --- plugins/nginx/nginx_error | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/nginx/nginx_error b/plugins/nginx/nginx_error index 34e47b82..e3385158 100755 --- a/plugins/nginx/nginx_error +++ b/plugins/nginx/nginx_error @@ -48,7 +48,7 @@ None known. =head1 VERSION -$Id:$ +1.0 - 2017/02/21 =head1 AUTHOR From 808a21d4fb6194ac0c60731a636f82aeb5538361 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 20 Jan 2018 01:00:12 +0100 Subject: [PATCH 2/7] nginx_error: improve readability of symlink configuration code --- plugins/nginx/nginx_error | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/plugins/nginx/nginx_error b/plugins/nginx/nginx_error index e3385158..7b42a143 100755 --- a/plugins/nginx/nginx_error +++ b/plugins/nginx/nginx_error @@ -48,8 +48,12 @@ None known. =head1 VERSION +1.1 - 2018/01/20 + * improve readability of symlink configuration code + 1.0 - 2017/02/21 + =head1 AUTHOR vovansystems@gmail.com, 2013 @@ -60,24 +64,25 @@ GPLv3 =cut -if [ -z $logpath ]; then - logpath='/var/log/nginx' -fi -name=`basename $0` +# default environment variable values +logpath=${logpath:-/var/log/nginx} -domain=${name/nginx_error/} -if [[ $domain != '_' && ${#domain} -ne 0 ]]; then - domain=${domain:1} - if [ -z $logpattern ]; then - logpattern='a.*.log' - fi - logpattern=${logpattern/\*/$domain} + +# derive the name of the log file from a potential symlink-configured virtual host +script_name=$(basename "$0") +plugin_suffix=${script_name#nginx_error} +if [ -n "${plugin_suffix#_}" ]; then + # a domain was given via symlink configuration: adjust the logpattern + domain=${plugin_suffix#_} + # default logpattern for symlink configuration mode + logpattern=${logpattern:-a.*.log} + log_filename=${logpattern/\*/$domain} else - logpattern='access.log' + log_filename='access.log' fi -log="$logpath/$logpattern" +log="$logpath/$log_filename" # declaring an array with http status codes, we are interested in declare -A http_codes @@ -111,7 +116,7 @@ do_ () { # Fetch } do_config () { - echo "graph_title $logpattern - Nginx errors per minute" + echo "graph_title $(basename "$log") - Nginx errors per minute" echo 'graph_vlabel pages with http error codes / ${graph_period}' echo "graph_category webserver" echo "graph_period minute" From 8a68200a6787dc85c8639654ad4855b178061384 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 20 Jan 2018 01:02:19 +0100 Subject: [PATCH 3/7] nginx_error: fix style issues reported by shellcheck --- plugins/nginx/nginx_error | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/plugins/nginx/nginx_error b/plugins/nginx/nginx_error index 7b42a143..1d6244cd 100755 --- a/plugins/nginx/nginx_error +++ b/plugins/nginx/nginx_error @@ -49,6 +49,7 @@ None known. =head1 VERSION 1.1 - 2018/01/20 + * fix shell style issues reported by shellcheck * improve readability of symlink configuration code 1.0 - 2017/02/21 @@ -99,29 +100,34 @@ http_codes[500]='Internal Server Error' http_codes[502]='Bad Gateway' http_codes[503]='Service Unavailable' -do_ () { # Fetch + +# parse error counts from log file +do_ () { + local k values declare -A line_counts - values=`awk '{print $9}' $log | sort | uniq -c` + values=$(awk '{print $9}' "$log" | sort | uniq -c) if [ -n "$values" ]; then while read -r line; do - read -a tmp <<< "$line"; - line_counts[${tmp[1]}]=${tmp[0]}; + read -r -a tmp <<< "$line" + line_counts[${tmp[1]}]=${tmp[0]} done <<< "$values" fi - for k in ${!http_codes[@]}; do + for k in "${!http_codes[@]}"; do echo "error$k.value ${line_counts[$k]:-0}" done exit 0 } + do_config () { + local k echo "graph_title $(basename "$log") - Nginx errors per minute" - echo 'graph_vlabel pages with http error codes / ${graph_period}' + echo "graph_vlabel pages with http error codes / \${graph_period}" echo "graph_category webserver" echo "graph_period minute" echo "graph_info This graph shows nginx error amount per minute" - for k in ${!http_codes[@]}; do + for k in "${!http_codes[@]}"; do echo "error$k.type DERIVE" echo "error$k.min 0" echo "error$k.label $k ${http_codes[$k]}" @@ -129,14 +135,16 @@ do_config () { exit 0 } + do_autoconf () { echo yes exit 0 } + case $1 in config|autoconf|'') - eval do_$1 + eval "do_$1" esac exit $? From 0e864944d76b19278d82a602aca850c0d91ac51a Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 20 Jan 2018 01:25:14 +0100 Subject: [PATCH 4/7] nginx_error: improve readability of array accesses --- plugins/nginx/nginx_error | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/plugins/nginx/nginx_error b/plugins/nginx/nginx_error index 1d6244cd..667aef2c 100755 --- a/plugins/nginx/nginx_error +++ b/plugins/nginx/nginx_error @@ -103,34 +103,30 @@ http_codes[503]='Service Unavailable' # parse error counts from log file do_ () { - local k values + local count status_code declare -A line_counts - values=$(awk '{print $9}' "$log" | sort | uniq -c) - if [ -n "$values" ]; then - while read -r line; do - read -r -a tmp <<< "$line" - line_counts[${tmp[1]}]=${tmp[0]} - done <<< "$values" - fi + while read -r count status_code; do + line_counts[$status_code]=$count + done <<< "$(awk '{print $9}' "$log" | sort | uniq -c)" - for k in "${!http_codes[@]}"; do - echo "error$k.value ${line_counts[$k]:-0}" + for status_code in "${!http_codes[@]}"; do + echo "error${status_code}.value ${line_counts[$status_code]:-0}" done exit 0 } do_config () { - local k + local status_code echo "graph_title $(basename "$log") - Nginx errors per minute" echo "graph_vlabel pages with http error codes / \${graph_period}" echo "graph_category webserver" echo "graph_period minute" echo "graph_info This graph shows nginx error amount per minute" - for k in "${!http_codes[@]}"; do - echo "error$k.type DERIVE" - echo "error$k.min 0" - echo "error$k.label $k ${http_codes[$k]}" + for status_code in "${!http_codes[@]}"; do + echo "error${status_code}.type DERIVE" + echo "error${status_code}.min 0" + echo "error${status_code}.label $status_code ${http_codes[$status_code]}" done exit 0 } From 7ed71b441d4debfa427c2860d32e921b78df4727 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 20 Jan 2018 01:36:03 +0100 Subject: [PATCH 5/7] nginx_error: add support for 'dirty config' capability --- plugins/nginx/nginx_error | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/plugins/nginx/nginx_error b/plugins/nginx/nginx_error index 667aef2c..4b22081e 100755 --- a/plugins/nginx/nginx_error +++ b/plugins/nginx/nginx_error @@ -49,6 +49,7 @@ None known. =head1 VERSION 1.1 - 2018/01/20 + * add 'dirty config' capability support * fix shell style issues reported by shellcheck * improve readability of symlink configuration code @@ -102,7 +103,7 @@ http_codes[503]='Service Unavailable' # parse error counts from log file -do_ () { +do_fetch () { local count status_code declare -A line_counts while read -r count status_code; do @@ -112,7 +113,6 @@ do_ () { for status_code in "${!http_codes[@]}"; do echo "error${status_code}.value ${line_counts[$status_code]:-0}" done - exit 0 } @@ -128,19 +128,26 @@ do_config () { echo "error${status_code}.min 0" echo "error${status_code}.label $status_code ${http_codes[$status_code]}" done - exit 0 } do_autoconf () { echo yes - exit 0 } case $1 in - config|autoconf|'') - eval "do_$1" + config) + do_config + # support "dirty config" capability + if [ "${MUNIN_CAP_DIRTYCONFIG:-}" = "1" ]; then do_fetch; fi + ;; + autoconf) + do_autoconf + ;; + '') + do_fetch + ;; esac exit $? From 24ea5d29e9ed8127e44c3f226c82d17ef2dc9e04 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 20 Jan 2018 01:45:26 +0100 Subject: [PATCH 6/7] nginx_error: improve documentation --- plugins/nginx/nginx_error | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/plugins/nginx/nginx_error b/plugins/nginx/nginx_error index 4b22081e..73f77b07 100755 --- a/plugins/nginx/nginx_error +++ b/plugins/nginx/nginx_error @@ -7,21 +7,27 @@ nginx error - Munin plugin to monitor nginx error rates (http status codes per minute). + =head1 APPLICABLE SYSTEMS -Any Linux host, running nginx, with bash version > 4.0 +Any host running nginx, with bash version > 4.0 + =head1 CONFIGURATION This shows the default configuration of this plugin. You can override the log file path and the logpattern. +Additionally you may want to adjust 'group' (or 'user') based on the +permissions required for reading the log file. [nginx_error] + group adm env.logpath /var/log/nginx env.logpattern a.*.log Nginx must also be configured to log accesses in "combined" log format (default) + =head1 USAGE Link this plugin to /etc/munin/plugins/ and restart the munin-node. @@ -33,19 +39,25 @@ will parse the log file /var/log/nginx/a.mydomaincom.log You can change 'env.logpattern' using asterisk ('*') to match your logs filenames. +'env.logpattern' is ignored for a non-symlink configuration. + + =head1 INTERPRETATION The plugin shows nginx http "error" status rates by parsing access log. + =head1 MAGIC MARKERS #%# family=auto #%# capabilities=autoconf + =head1 BUGS None known. + =head1 VERSION 1.1 - 2018/01/20 @@ -60,6 +72,7 @@ None known. vovansystems@gmail.com, 2013 + =head1 LICENSE GPLv3 @@ -122,7 +135,7 @@ do_config () { echo "graph_vlabel pages with http error codes / \${graph_period}" echo "graph_category webserver" echo "graph_period minute" - echo "graph_info This graph shows nginx error amount per minute" + echo "graph_info This graph shows nginx error rate per minute" for status_code in "${!http_codes[@]}"; do echo "error${status_code}.type DERIVE" echo "error${status_code}.min 0" From a4882b1912652777202a0679c2640f4f6b327c28 Mon Sep 17 00:00:00 2001 From: Lars Kruse Date: Sat, 20 Jan 2018 01:49:15 +0100 Subject: [PATCH 7/7] nginx_error: enable 'set -eu' flags for shell --- plugins/nginx/nginx_error | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugins/nginx/nginx_error b/plugins/nginx/nginx_error index 73f77b07..9cb3d7df 100755 --- a/plugins/nginx/nginx_error +++ b/plugins/nginx/nginx_error @@ -80,6 +80,9 @@ GPLv3 =cut +set -eu + + # default environment variable values logpath=${logpath:-/var/log/nginx} @@ -149,7 +152,7 @@ do_autoconf () { } -case $1 in +case ${1:-} in config) do_config # support "dirty config" capability