dns_one 0.4.29 → 0.4.30

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8ad077556ec78e89b5d4f8a88827dc77bf8397e8
4
- data.tar.gz: 8b7916cd71ad40672f8fca7a5baf41b13129c2a8
3
+ metadata.gz: 5bd90da7f26c043c1061527a8d6c317b323a1d63
4
+ data.tar.gz: 594f84fea313f5092620752731e463bb5b40562c
5
5
  SHA512:
6
- metadata.gz: cc2559f45f107f09ffcf56579890d057104ca176e8c27e9737a793ef754ac2b0a732dd1a84fe324e4b6d8e663c6d5e32426961b631fe92db24233acf9e81fb9a
7
- data.tar.gz: 51fe6c09207f7026008300afade6e95675ea93b79d7d2b7506eb1bdba85eee7ad82372ef4edc5b7a82ecb14507a4326c62920a86a8118bf8aba51042a5a14879
6
+ metadata.gz: aafba801a40a381693c2fb347668dae193323084ce98af5fc6e424f65ba2a50d37e9f8cba27174970a765ec0d67e4ecdb07a6ecbf3c799f2b7287219cd2c82b3
7
+ data.tar.gz: d1f868608bd2267eb2451aee3af246f56f2b958a7d15cbe7773eaf2a6bf412892ce621033cd23560daf554c13b99fda7889d30b646b761ccead708287e964f69
@@ -1,8 +1,9 @@
1
- require "thor"
2
1
 
2
+ require "thor"
3
+ require 'sqlite3'
3
4
  require "dns_one"
4
5
  require "dns_one/setup"
5
- require 'sqlite3'
6
+ require "dns_one/stat"
6
7
 
7
8
  class DnsOne::CLI < Thor
8
9
 
@@ -0,0 +1,142 @@
1
+ module DnsOne; class Stat
2
+
3
+ def initialize conf = {}
4
+ @conf = conf
5
+ ensure_db
6
+ end
7
+
8
+ def save rcode, req_resource, cache
9
+ @db.execute(
10
+ "INSERT INTO responses (time, rcode, req_resource, cache) VALUES (?, ?, ?, ?)",
11
+ [
12
+ Time.now.to_i,
13
+ Resolv::DNS::RCode.const_get(rcode),
14
+ req_resource::TypeValue,
15
+ (cache ? 1 : 0)
16
+ ]
17
+ )
18
+ rescue => e
19
+ Log.e e
20
+ end
21
+
22
+ def get_counts counter, from = nil
23
+ validate_counter counter
24
+ validate_from from
25
+
26
+ from ||= (Time.now - 5 * 60).to_i
27
+
28
+ s = <<-SQL
29
+ select #{counter}, count(*)
30
+ from responses
31
+ where time > #{from}
32
+ group by #{counter}
33
+ SQL
34
+
35
+ counts = {}
36
+
37
+ @db.execute(s) do |row|
38
+ _counter, count = row
39
+ counts[_counter] = count
40
+ end
41
+
42
+ counts
43
+ end
44
+
45
+ private
46
+
47
+ def validate_counter counter
48
+ unless [:rcode, :req_resource, :cache].include? counter
49
+ raise "invalid arg #{counter}"
50
+ end
51
+ end
52
+
53
+ def validate_from from
54
+ case from
55
+ when nil, Integer
56
+ when String
57
+ unless from =~ /^\d+$/
58
+ raise "invalid arg #{from}"
59
+ end
60
+ else
61
+ raise "invalid arg class #{from.class}"
62
+ end
63
+ end
64
+
65
+ def ensure_db
66
+ new_db = !File.exists?(db_file)
67
+ @db = SQLite3::Database.new db_file
68
+ create_tables if new_db
69
+ end
70
+
71
+ def create_tables
72
+ sqls = []
73
+
74
+ sqls << <<-SQL
75
+ create table responses (
76
+ time int,
77
+ rcode int,
78
+ req_resource int,
79
+ cache int
80
+ );
81
+ SQL
82
+
83
+ sqls << <<-SQL
84
+ CREATE INDEX responses_time ON responses (time);
85
+ SQL
86
+
87
+ sqls << <<-SQL
88
+ CREATE INDEX responses_rcode ON responses (rcode);
89
+ SQL
90
+
91
+ sqls << <<-SQL
92
+ CREATE INDEX responses_req_resource ON responses (req_resource);
93
+ SQL
94
+
95
+ sqls << <<-SQL
96
+ CREATE INDEX responses_cache ON responses (cache);
97
+ SQL
98
+
99
+ sqls.each{|sql| @db.execute sql }
100
+ end
101
+
102
+
103
+ def db_file
104
+ @conf[:db_file]
105
+ end
106
+
107
+ def request_resources
108
+ unless defined? @@request_resources
109
+ @@request_resources = {}
110
+ %w(A AAAA ANY CNAME HINFO MINFO MX NS PTR SOA TXT WKS).each do |res|
111
+ val = Object.const_get("Resolv::DNS::Resource::IN::#{res}")::TypeValue
112
+ @@request_resources[val] = res.downcase
113
+ end
114
+ end
115
+ @@request_resources
116
+ end
117
+
118
+ def const_underscore name
119
+ name = name.to_s.dup
120
+ name.gsub!('::', '/')
121
+ name.gsub!(/([A-Z\d]+)([A-Z][a-z])/,'\1_\2')
122
+ name.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
123
+ name.tr!("-", "_")
124
+ name.downcase!
125
+ name
126
+ end
127
+
128
+ def rcodes
129
+ unless @@rcodes
130
+ @@rcodes = Hash[
131
+ Resolv::DNS::RCode.constants.map{|c|
132
+ [
133
+ Resolv::DNS::RCode.const_get(c),
134
+ const_underscore(c)
135
+ ]
136
+ }
137
+ ]
138
+ end
139
+ @@rcodes
140
+ end
141
+ end; end
142
+
@@ -1,3 +1,3 @@
1
1
  module DnsOne
2
- VERSION = "0.4.29"
2
+ VERSION = "0.4.30"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dns_one
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.29
4
+ version: 0.4.30
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Lobato
@@ -171,6 +171,7 @@ files:
171
171
  - lib/dns_one/log.rb
172
172
  - lib/dns_one/server.rb
173
173
  - lib/dns_one/setup.rb
174
+ - lib/dns_one/stat.rb
174
175
  - lib/dns_one/util.rb
175
176
  - lib/dns_one/version.rb
176
177
  - lib/dns_one/zone_search.rb