#!/usr/local/bin/ruby
# Copyright (C) 2003  HIROSHIMA, Naoki  <naokih at iron-horse dot org>

def get_tai(tai)
  s = 0
  ns = 0
  tai.each_byte{|c|
    u = c.chr.hex
    s = s << 4
    s = s + (ns >> 28)
    s = s & 0xffffffff
    ns = ns & 0xfffffff
    ns = ns << 4
    ns = ns + u
  }
  s
end

now = Time.now.to_i

total = 0
count = 0
sent = 0
cached = 0

file = File.open('/service/dnscache/log/main/current')
while line = file.gets
  # @400000003f58ffe023c7eb74 d2ada506:08fe:9e26 + 0001 www.iron-horse.org
  info = line.split
  tai = get_tai(info[0])
  if (((now - 300) < tai) && (tai < now)) 
    if info[1] == 'stats' then
      total = info[2].to_i;
    elsif info[1] == 'sent' then
      count += 1
      sent += info[3].to_i
    elsif info[1] == 'rr' then
      cached += 1
    end
  end
end
file.close

if ARGV[0] == 't'
  print total, "\n", total, "\n\n\n"
elsif ARGV[0] == 'n'
  print count, "\n", count, "\n\n\n"
elsif ARGV[0] == 's'
  print sent, "\n", sent, "\n\n\n"
elsif ARGV[0] == 'c'
  print cached, "\n", cached, "\n\n\n"
end
