Class: DnsOne::ReqLog::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/dns_one/req_log/account.rb

Instance Method Summary collapse

Constructor Details

#initializeAccount

Returns a new instance of Account.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/dns_one/req_log/account.rb', line 3

def initialize
    @conf = Global.conf       

    @stat = {}
    @stat_mutex = Mutex.new
    
    @last_stat = nil
    @last_stat_mutex = Mutex.new

    Thread.new { open_socket }
    Thread.new { reap }
end

Instance Method Details

#on_response(ip_address, domain_name, res_class, rcode, resp_log, from_cache) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dns_one/req_log/account.rb', line 16

def on_response ip_address, domain_name, res_class, rcode, resp_log, from_cache
    @stat_mutex.synchronize {
        @stat[:requests] ||= 0
        @stat[:requests] += 1
        
        @stat[:cache] ||= 0
        @stat[:cache] += 1 if from_cache
        
        rcode_uc = Util.const_underscore rcode
        @stat[:rcode] ||= {}
        @stat[:rcode][rcode_uc] ||= 0
        @stat[:rcode][rcode_uc] += 1

        req_resource = Util.last_mod(res_class).downcase
        @stat[:req_resource] ||= {}
        @stat[:req_resource][req_resource] ||= 0
        @stat[:req_resource][req_resource] += 1        
    }
rescue => e
    Global.logger.error e.desc
end

#open_socketObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/dns_one/req_log/account.rb', line 70

def open_socket
    sleep 1 # wait for UID change before create the socket file
    Socket.unix_server_loop(Global.conf.log_req_socket_file) do |sock, addr|
        Thread.new do
            write_socket sock
        end
    end
rescue => e
    Global.logger.error e.desc
end

#reapObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dns_one/req_log/account.rb', line 48

def reap
    loop do
        sleep (300 - Time.now.to_f % 300)
        stat = nil
        @stat_mutex.synchronize { 
            stat = @stat.deep_dup 
            reset @stat
        }
        update_last_stat stat
    rescue => e
        Global.logger.error e.desc
        sleep 10
    end
end

#reset(hash) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/dns_one/req_log/account.rb', line 81

def reset hash
    hash.each_key do |k|
        if hash[k].is_a? Hash
            reset hash[k]
        else
            hash[k] = 0
        end
    end
end

#update_last_stat(stat) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/dns_one/req_log/account.rb', line 38

def update_last_stat stat
    if !@allow_update_last_stat
        @allow_update_last_stat = true
        return
    end
    @last_stat_mutex.synchronize {
        @last_stat = stat
    }
end

#write_socket(sock) ⇒ Object



63
64
65
66
67
68
# File 'lib/dns_one/req_log/account.rb', line 63

def write_socket sock
    last_stat = @last_stat_mutex.synchronize{ @last_stat.deep_dup }
    sock.puts last_stat.to_json
rescue => e
    Global.logger.error e.desc
end