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/env ruby
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',
'Other' => 'Other responses'
}
if ARGV[0] == 'config'; then
puts "graph_title nginx Error Codes"
puts "graph_vlabel responses per minute"
puts "graph_category nginx"
puts "graph_period minute"
puts "graph_info Non-200 response codes per minute"
CODES.each do |code, desc|
puts "#{code}.label #{code} #{desc}"
puts "#{code}.type DERIVE"
puts "#{code}.min 0"
end
else
results = Hash[*CODES.keys.map { |k| [k, 0]}.flatten]
File.open("/var/log/nginx/access.log").readlines.each do |line|
if line =~ /" (\d\d\d)/
code = $1
if CODES.keys.include?(code)
results[code] += 1
elsif code.to_i >= 400
results['Other'] += 1
end
end
end
results.each do |k,v|
puts "#{k}.value #{v}"
end
end
@mipearson

This comment has been minimized.

Copy link
Owner Author

@mipearson mipearson commented Aug 15, 2011

Suggestion: fork this gist and add all the codes, add better log parsing (awk print $9 is unreliable)

@mipearson

This comment has been minimized.

Copy link
Owner Author

@mipearson mipearson commented Aug 16, 2011

Improved: all ruby, deals with unrecognized codes, spits out zero when no result is found.

@hcarvalhoalves

This comment has been minimized.

Copy link

@hcarvalhoalves hcarvalhoalves commented May 1, 2012

Does this ever worked? I get Permission denied - /var/log/nginx/access.log (Errno::EACCES), regardless of how I set the file permissions (even with 644 it fails!).

@mipearson

This comment has been minimized.

Copy link
Owner Author

@mipearson mipearson commented May 1, 2012

@hcarvalhoalves, check the permissions on /var/log/nginx first. It's probable that the log file is readable but the directory isn't.

@basti12203

This comment has been minimized.

Copy link

@basti12203 basti12203 commented Oct 22, 2012

I have installed ruby create a file "/usr/share/munin/plugins/nginx_errors" with the above code.
Do "ln -s /usr/share/munin/plugins/nginx_errors /etc/munin/plugins/nginx_errors".
Edit "/etc/munin/plugin-conf.d/munin-node" add
[nginx_*]
user munin
command ruby %c
and restart the munin-node. But there are no Data collected (-nan).

Please can someone help?

@benzheren

This comment has been minimized.

Copy link

@benzheren benzheren commented May 16, 2013

For people who are still wondering how to fix the problem of @basti12203, you need to put smth like this in your /etc/munin/plugin-conf.d/munin-node

[nginx*]
user deployer
group adm
env.url http://localhost/nginx_status
@bactisme

This comment has been minimized.

Copy link

@bactisme bactisme commented Jan 22, 2016

Because I don't have ruby on my server, I wrote a python version : https://gist.github.com/bactisme/40148ca85cc82f3cf26f

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