Class: DnsOne::ReqLog::DB

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

Constant Summary collapse

DB_FNAME =
"stat.db"
META_STAT_ON =
false
META_STAT_FILE =
'/tmp/dnsone_sql_prof.log'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(conf = {}) ⇒ DB

Returns a new instance of DB.



7
8
9
10
11
12
13
14
# File 'lib/dns_one/req_log/db.rb', line 7

def initialize conf = {}
    @conf = conf
 
    # Setup logger and current working dir
    DnsOne.new if @conf[:from_outside]
    
    ensure_db
end

Class Method Details



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dns_one/req_log/db.rb', line 55

def self.print
    stat = new(from_outside: true, readonly: true)
    %w(rcode req_resource cache).each do |key|
        puts "--- #{key} ---"
        stat.get_counts(key.to_sym).each_pair do |k, v|
            _k = case key
            when 'rcode'
                stat.rcodes[k]
            when 'req_resource'
                stat.request_resources[k]
            when 'cache'
                k == 0 ? :miss : :hit
            end
            puts "#{_k || k}\t#{v}"
        end
    end
end

Instance Method Details

#get_counts(counter, from = nil) ⇒ Object

select rcode, count(*) from responses where time > strftime(‘%s’, ‘now’) - 300 group by rcode



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dns_one/req_log/db.rb', line 32

def get_counts counter, from = nil
    validate_counter counter
    validate_from from

    from ||= (Time.now - 5 * 60).to_i

    s = <<-SQL
        select #{counter}, count(*) 
        from responses 
        where time > #{from}
        group by #{counter}
    SQL

    counts = {}

    rsql(s).each do |row|
        _counter, count = row
        counts[_counter] = count
    end

    counts
end

#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
# File 'lib/dns_one/req_log/db.rb', line 16

def on_response ip_address, domain_name, res_class, rcode, resp_log, from_cache
	Global.logger.debug "saving stat (user: #{ `id -un #{Process.uid}`.strip })"
    rsql(
        "INSERT INTO responses (time, rcode, req_resource, cache) VALUES (?, ?, ?, ?)", 
        [
            Time.now.to_i, 
            Resolv::DNS::RCode.const_get(rcode), 
            res_class::TypeValue, 
            (from_cache ? 1 : 0)
        ]
    )
rescue => e
    Global.logger.error e.desc
end

#rcodesObject



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dns_one/req_log/db.rb', line 73

def rcodes
    unless defined? @@rcodes
        @@rcodes = Hash[ 
            Resolv::DNS::RCode.constants.map{|c| 
                [
                    Resolv::DNS::RCode.const_get(c), 
                    Util.const_underscore(c)
                ] 
            } 
        ]
    end
    @@rcodes
end

#request_resourcesObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/dns_one/req_log/db.rb', line 87

def request_resources
    unless defined? @@request_resources
        @@request_resources = {}
        %w(A AAAA ANY CNAME HINFO MINFO MX NS PTR SOA TXT WKS).each do |res|
            val = Object.const_get("Resolv::DNS::Resource::IN::#{res}")::TypeValue
            @@request_resources[val] = res.downcase
        end
    end
    @@request_resources
end