Skip to content

Instantly share code, notes, and snippets.

@mago0
Created March 3, 2017 23:08
Show Gist options
  • Save mago0/c85965a53ebadfe521c1b5750202b88c to your computer and use it in GitHub Desktop.
Save mago0/c85965a53ebadfe521c1b5750202b88c to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'http_log_parser'
require 'timeout'
parser = HttpLogParser.new
UPDATE_INTERVAL = 10 #Sec
ALERT_INTERVAL = 120
ALERT_THRESHOLD = 10 #RPS
TOPX = 5
def print_stats ( time, sections, hps, total_hits, alerts )
system "clear"
p '### HTTP Stats ###'
p "Current Time: #{time}"
p 'Top Page Hits:'
sections.each do |x|
p "Page: #{x[0]}: #{x[1]}"
end
p "Hits Per Second: #{hps}"
p "Total Hits: #{total_hits}"
p 'Alerts:'
alerts.each do |alert|
p alert
end
sleep 1
end
hits = 0
alert_hits = 0
total_hits = 0
sections = {}
alerts = []
t1 = Time.now
sleep 1
loop do
begin
Timeout::timeout(0.2) {
parsed_data = parser.parse_line($stdin.gets)
/(?<section>(http:\/\/\w+\.?\w*)?(\/[\w0-9]*))/ =~ parsed_data[:request]
sections["#{section}"] ||= 0
sections["#{section}"] += 1
hits += 1
}
rescue Timeout::Error
end
t2 = Time.now
if ( ( t2 - t1 ).to_i % UPDATE_INTERVAL == 0 )
sections = sections.sort_by{|k,v| v}[0..TOPX]
alert_hits += hits
total_hits += hits
hps = hits / 10
if ( ( t2 - t1 ).to_i % ALERT_INTERVAL == 0) &&
alert_hits / ALERT_INTERVAL >= ALERT_THRESHOLD
alerts << "High traffic generated an alert - hits = #{alert_hps}, triggered at #{t2}"
end
print_stats( t2, sections, hps, total_hits, alerts )
hits = 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment