1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-26 02:48:28 +00:00

Merge various improvements for "nginx_error" plugin

This commit is contained in:
Lars Kruse 2018-01-20 01:52:19 +01:00
commit 35b8cdd641

View file

@ -7,21 +7,27 @@
nginx error - Munin plugin to monitor nginx error rates (http status codes per minute). nginx error - Munin plugin to monitor nginx error rates (http status codes per minute).
=head1 APPLICABLE SYSTEMS =head1 APPLICABLE SYSTEMS
Any Linux host, running nginx, with bash version > 4.0 Any host running nginx, with bash version > 4.0
=head1 CONFIGURATION =head1 CONFIGURATION
This shows the default configuration of this plugin. You can override This shows the default configuration of this plugin. You can override
the log file path and the logpattern. 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] [nginx_error]
group adm
env.logpath /var/log/nginx env.logpath /var/log/nginx
env.logpattern a.*.log env.logpattern a.*.log
Nginx must also be configured to log accesses in "combined" log format (default) Nginx must also be configured to log accesses in "combined" log format (default)
=head1 USAGE =head1 USAGE
Link this plugin to /etc/munin/plugins/ and restart the munin-node. Link this plugin to /etc/munin/plugins/ and restart the munin-node.
@ -33,51 +39,68 @@ will parse the log file /var/log/nginx/a.mydomaincom.log
You can change 'env.logpattern' using asterisk ('*') to match your logs filenames. You can change 'env.logpattern' using asterisk ('*') to match your logs filenames.
'env.logpattern' is ignored for a non-symlink configuration.
=head1 INTERPRETATION =head1 INTERPRETATION
The plugin shows nginx http "error" status rates by parsing access log. The plugin shows nginx http "error" status rates by parsing access log.
=head1 MAGIC MARKERS =head1 MAGIC MARKERS
#%# family=auto #%# family=auto
#%# capabilities=autoconf #%# capabilities=autoconf
=head1 BUGS =head1 BUGS
None known. None known.
=head1 VERSION =head1 VERSION
$Id:$ 1.1 - 2018/01/20
* add 'dirty config' capability support
* fix shell style issues reported by shellcheck
* improve readability of symlink configuration code
1.0 - 2017/02/21
=head1 AUTHOR =head1 AUTHOR
vovansystems@gmail.com, 2013 vovansystems@gmail.com, 2013
=head1 LICENSE =head1 LICENSE
GPLv3 GPLv3
=cut =cut
if [ -z $logpath ]; then
logpath='/var/log/nginx'
fi
name=`basename $0` set -eu
domain=${name/nginx_error/}
if [[ $domain != '_' && ${#domain} -ne 0 ]]; then # default environment variable values
domain=${domain:1} logpath=${logpath:-/var/log/nginx}
if [ -z $logpattern ]; then
logpattern='a.*.log'
fi # derive the name of the log file from a potential symlink-configured virtual host
logpattern=${logpattern/\*/$domain} 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 else
logpattern='access.log' log_filename='access.log'
fi fi
log="$logpath/$logpattern" log="$logpath/$log_filename"
# declaring an array with http status codes, we are interested in # declaring an array with http status codes, we are interested in
declare -A http_codes declare -A http_codes
@ -94,44 +117,53 @@ http_codes[500]='Internal Server Error'
http_codes[502]='Bad Gateway' http_codes[502]='Bad Gateway'
http_codes[503]='Service Unavailable' http_codes[503]='Service Unavailable'
do_ () { # Fetch
declare -A line_counts
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]};
done <<< "$values"
fi
for k in ${!http_codes[@]}; do # parse error counts from log file
echo "error$k.value ${line_counts[$k]:-0}" do_fetch () {
local count status_code
declare -A line_counts
while read -r count status_code; do
line_counts[$status_code]=$count
done <<< "$(awk '{print $9}' "$log" | sort | uniq -c)"
for status_code in "${!http_codes[@]}"; do
echo "error${status_code}.value ${line_counts[$status_code]:-0}"
done done
exit 0
} }
do_config () { do_config () {
echo "graph_title $logpattern - Nginx errors per minute" local status_code
echo 'graph_vlabel pages with http error codes / ${graph_period}' 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_category webserver"
echo "graph_period minute" 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 k in ${!http_codes[@]}; do for status_code in "${!http_codes[@]}"; do
echo "error$k.type DERIVE" echo "error${status_code}.type DERIVE"
echo "error$k.min 0" echo "error${status_code}.min 0"
echo "error$k.label $k ${http_codes[$k]}" echo "error${status_code}.label $status_code ${http_codes[$status_code]}"
done done
exit 0
} }
do_autoconf () { do_autoconf () {
echo yes echo yes
exit 0
} }
case $1 in
config|autoconf|'') case ${1:-} in
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 esac
exit $? exit $?