Skip to content

Instantly share code, notes, and snippets.

@FiXato
Created September 20, 2012 22:12
Show Gist options
  • Save FiXato/3758665 to your computer and use it in GitHub Desktop.
Save FiXato/3758665 to your computer and use it in GitHub Desktop.
UnrealIRCd TKL Stats
#!/usr/bin/env ruby
# encoding: utf-8
require 'yaml'
THRESHOLD = 5
lines = `grep -E '[A-Z]:[lL]ine added for ' ~/UnrealIRCd/logs/tkl.log`.split("\n");nil
ban_types = []
results = lines.map do |line|
if line.match(/\[([^\]]+)\] - (.+) added for (\S+)@(\S+) on (.+) \(from (\S+)( to expire at (.+))?: (.+)\)/)
puts "1- #{line}" if $2 == '' || $2 == ' '
ban_types << $2
next {:log_date => $1, :ban_type => $2, :ident => $3, :host => $4, :ban_date => $5, :server => $6, :ban_expiration_date => $8, :ban_reason => $9}
elsif line.match(/\[([^\]]+)\] - (Permanent Global Q:[Ll].+) added for (\S+) on (.+) \(from (\S+): (.+)\)/)
puts "2- #{line}" if $2 == '' || $2 == ' '
ban_types << $2
next {:log_date => $1, :ban_type => $2, :host => $3, :ban_date => $4, :server => $5, :ban_reason => $6}
else
next line
end
line
end;nil
unmatched_lines = results.select{|line|line.kind_of?(String)}
puts "#{unmatched_lines.size} results aren't matched" if(unmatched_lines.size == 0)
unmatched = []
results_by_type = Hash.new{|h,k|h[k] = []}
results.each do |result|
unmatched << result if result[:ban_type].nil?
results_by_type[result[:ban_type]] << result
end;nil
puts "#{unmatched.size} results aren't matched" if(unmatched.size == 0)
results_by_type_and_host = {}
results_by_type.each do |ban_type, results_for_ban_type|
results_by_type_and_host[ban_type] = {:size => results_for_ban_type.size, :results_per_host => Hash.new{|h,k|h[k] = []}}
results_for_ban_type.each do |result|
results_by_type_and_host[ban_type][:results_per_host][result[:host]] << result
end
end;nil
results_by_type_and_host.each do |ban_type, results_hash|
under_threshold = []
puts "#{results_hash[:size]} #{ban_type}:"
results_hash[:results_per_host].sort_by{|hostname,bans_for_host|bans_for_host.size}.reverse.each do |hostname, bans_for_host|
if bans_for_host.size < THRESHOLD
under_threshold += bans_for_host
next
end
puts " #{hostname} has #{bans_for_host.size} matches"
end
puts " #{under_threshold.size} bans under threshold of #{THRESHOLD} matches."
puts '-'*80
end;nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment