Skip to content

Instantly share code, notes, and snippets.

@colinmollenhour
Last active September 10, 2016 15:30
Show Gist options
  • Save colinmollenhour/7feceabb66faf9e1769c to your computer and use it in GitHub Desktop.
Save colinmollenhour/7feceabb66faf9e1769c to your computer and use it in GitHub Desktop.
Munin Plugin - nginx error_log
#!/bin/bash
: <<=cut
=head1 NAME
nginx error log - Munin plugin to monitor nginx error_log rates (debug, info, notice, warn, error, crit, alert, emerg).
=head1 APPLICABLE SYSTEMS
Any Linux 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.
[nginx_error_log]
env.logpath /var/log/nginx
env.logpattern *error.log
=head1 USAGE
Link this plugin to /etc/munin/plugins/ and restart the munin-node.
This plugin also can be used like wildcard-plugin for monitoring particular virtual host log.
E.g.
ln -s /usr/share/munin/plugins/nginx_error_log /etc/munin/plugins/nginx_error_log_mydomaincom
will parse the log file /var/log/nginx/mydomaincom_error.log
You can change 'env.logpattern' using asterisk ('*') to match your logs filenames.
=head1 INTERPRETATION
The plugin shows nginx error_log rates by severity by parsing error logs.
=head1 MAGIC MARKERS
#%# family=auto
#%# capabilities=autoconf
=head1 BUGS
None known.
=head1 VERSION
$Id:$
=head1 AUTHOR
[email protected], 2014
Adapted from script by [email protected], 2013
https://github.com/munin-monitoring/contrib/blob/master/plugins/nginx/nginx_error
=head1 LICENSE
GPLv3
=cut
if [ -z $logpath ]; then
logpath='/var/log/nginx'
fi
if [ -z $logpattern ]; then
name=`basename $0`
domain=${name/nginx_error_log/}
if [[ $domain != '_' && ${#domain} -ne 0 ]]; then
domain=${domain:1}
if [ -z $logpattern ]; then
logpattern='*_error.log'
fi
logpattern=${logpattern/\*/$domain}
else
logpattern='error.log'
fi
fi
log="$logpath/$logpattern"
# declaring an array with http status codes, we are interested in
declare -A levels
levels[debug]='Debug'
levels[info]='Info'
levels[notice]='Notice'
levels[warn]='Warning'
levels[error]='Error'
levels[crit]='Critical'
levels[alert]='Alert'
levels[emerg]='Emergency'
do_ () { # Fetch
for k in ${!levels[@]}; do
value=`awk -v k="[$k]" '{if ($3 == k) { i += 1 }} END { print i }' $log` # this is much better
echo "$k.value ${value:-0}"
done
exit 0
}
do_config () {
echo "graph_title nginx error logs per minute - $logpattern"
echo 'graph_vlabel log messages / ${graph_period}'
echo "graph_category nginx"
echo "graph_period minute"
echo "graph_info This graph shows nginx error log amount per minute"
for k in ${!levels[@]}; do
echo "$k.type DERIVE"
echo "$k.min 0"
echo "$k.label $k"
done
exit 0
}
do_autoconf () {
echo yes
exit 0
}
case $1 in
config|autoconf|'')
eval do_$1
esac
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment