Last active
April 7, 2022 04:11
-
-
Save bactisme/40148ca85cc82f3cf26f to your computer and use it in GitHub Desktop.
Very simple munin plugin to graph nginx HTTP error codes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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])