#!/usr/bin/ruby

unless ARGV.length == 1
  puts "Usage: script <syslog file>"
  exit
end

def get_log_lines(filename)
  tally = Hash.new(0)
  total = 0

  # doesn't check the file can be opened
  File.open(filename) do | log |
    log.each do | log_line |
      total += 1
      daemon = log_line.split[4].sub(/\[\d+\]/, '')
      tally[daemon] +=1
    end
  end
  puts "Total Lines in #{filename}: #{total}"
  return total, tally
end

total, daemon_tally = get_log_lines(ARGV[0])

daemon_tally.invert.sort{|a,b| b<=>a}.each do | key, value |
  pct = key.to_f / total.to_f * 100.0
  puts "#{value.ljust(20)} #{key.to_s.rjust(6)} (#{pct.round}%)"
end

