Skip to content

Instantly share code, notes, and snippets.

@jcsalterego
Created December 19, 2009 16:01
Show Gist options
  • Select an option

  • Save jcsalterego/260131 to your computer and use it in GitHub Desktop.

Select an option

Save jcsalterego/260131 to your computer and use it in GitHub Desktop.
Munin script for Abnormal Return Codes in haproxy logs
#!/usr/bin/env ruby
#
# Munin script for Abnormal Return Codes in haproxy logs
#
require 'time'
LOG_FILE="/var/log/haproxy.log"
TAIL_LINES = 1500
THRESHOLD_MINUTES = 5
# haproxy log field position constants
STATUS_FIELD = 10
TIME_FIELDS = 0..2
def config
puts "graph_category haproxy"
puts "graph_title Haproxy Abnormal Return Codes"
puts "graph_vlabel Instances"
puts "err_total.label 5xx Server Errors"
puts "redir_total.label 3xx Redirects"
0
end
def main
matches = []
threshold = Time.now - THRESHOLD_MINUTES * 60
lines = %x(tail -n #{TAIL_LINES} #{LOG_FILE}).split("\n")
hits = { 300 => 0, 500 => 0 }
lines.each do |line|
fields = line.strip.split
timestamp = Time.parse(fields[TIME_FIELDS].join(" "))
status_code = fields[STATUS_FIELD].to_i
if timestamp > threshold
if status_code >= 300 and status_code < 400
hits[300] += 1
elsif status_code >= 500
hits[500] += 1
end
end
end
puts "err_total.value #{hits[500]}"
puts "redir_total.value #{hits[300]}"
0
end
if $0 == __FILE__
if ARGV.include?('config')
exit config
else
exit main
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment