Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Very simple munin plugin to graph nginx HTTP error codes
#!/usr/bin/python
# ALL FAME GOES HERE : https://gist.github.com/mipearson/1146151
#
# 1/ Place where you want (like /usr/share/munin/plugins/nginx_status_codes.py )
# 2/ Link in /etc/munin/plugins/ :
# ln -s /usr/share/munin/plugins/nginx_status_codes.py /etc/munin/plugins/nginx_errors
# chmod +x /etc/munin/plugins/nginx_errors
# 3/ Test with
# munin-run nginx_errors
# 4/ If nescessary : add these lines in your munin conf
# [nginx*]
# user root
# env.access_log /var/log/nginx/blogs.access.log
#
# THanks to mipearson, basti122303
import re, sys, os
CODES = {
'400': 'Bad Request',
'401': 'Unauthorized',
'403': 'Forbidden',
'404': 'Not Found',
'405': 'Method Not Allowed',
'406': 'Not Acceptable',
'408': 'Request Timeout',
'499': 'Client Connection Terminated',
'500': 'Internal Server Error',
'502': 'Bad Gateway',
'503': 'Service Unavailable',
'504': 'Gateway timeout',
'Other': 'Other responses'
}
ACCESS_LOG = "/var/log/nginx/access.log"
if 'access_log' in os.environ:
ACCESS_LOG = os.environ.get('access_log')
if len(sys.argv) > 1 and sys.argv[1] == 'config':
print "graph_title nginx Error Codes"
print "graph_vlabel responses per minute"
print "graph_category nginx"
print "graph_period minute"
print "graph_info Non-200 response codes per minute"
for code in CODES:
print "stat%s.label %s %s" % (code, code, CODES[code])
print "stat%s.type DERIVE" % (code)
print "stat%s.min 0" % (code)
else:
results = {k:0 for k in CODES}
with open(ACCESS_LOG, 'r') as f:
for line in f.readlines():
code = re.search(" (\d\d\d) ", line)
if code:
code = code.group(0).strip()
if code in CODES:
results[code] += 1
elif int(code) >= 400:
results['Other'] += 1
for k in results:
print "stat%s.value %s" % (k, results[k])
@basti122303

This comment has been minimized.

Copy link

@basti122303 basti122303 commented Feb 2, 2016

Hello,
i have found a "bug".
The problem is this http://munin-monitoring.org/wiki/notes_on_datasource_names i think.
The first value of the code is internal replaced by _. For example code 400 and 500 is both _00.
https://gist.github.com/mipearson/1146151 is also wrong.
So please edit your code.
(Sorry I cant upload a patch file here ...)

print "stat%s.label %s %s" % (code, code, CODES[code])
print "stat%s.type DERIVE" % (code)
print "stat%s.min 0" % (code)
print "stat%s.value %s" % (k, results[k])

@bactisme

This comment has been minimized.

Copy link
Owner Author

@bactisme bactisme commented Apr 26, 2016

@basti122303 Yeah, sorry, I corrected the bug on my servers but not here :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment