Class: PassiveDNS::PDNSToolStateDB

Inherits:
PDNSToolState show all
Defined in:
lib/pdns/pdns.rb

Instance Attribute Summary collapse

Attributes inherited from PDNSToolState

#debug

Instance Method Summary collapse

Methods inherited from PDNSToolState

#to_gdf, #to_graphml, #to_graphviz, #to_json, #to_s, #to_xml, #to_yaml

Constructor Details

#initialize(sqlitedb = nil) ⇒ PDNSToolStateDB

Returns a new instance of PDNSToolStateDB.



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/pdns/pdns.rb', line 194

def initialize(sqlitedb=nil)
	puts "PDNSToolState  initialize  #{sqlitedb}" if @debug
	@level = 0
	@sqlitedb = sqlitedb
	raise "Cannot use this class without a database file" unless @sqlitedb
	unless File.exists?(@sqlitedb)
		newdb = true
	end
	@sqlitedbh = SQLite3::Database.new(@sqlitedb)
	if newdb
		create_tables
	end
	res = @sqlitedbh.execute("select min(level) from queue where state = 'pending'")
	if res
		res.each do |row|
			@level = row[0].to_i
			puts "changed @level = #{@level}" if @debug
		end
	end
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



193
194
195
# File 'lib/pdns/pdns.rb', line 193

def level
  @level
end

Instance Method Details

#add_query(query, state, level = @level+1) ⇒ Object



241
242
243
244
245
246
247
248
249
# File 'lib/pdns/pdns.rb', line 241

def add_query(query,state,level=@level+1)
	return if get_state(query)
	curtime = Time.now().to_f
	begin
		puts "add_query(#{query},#{state},level=#{level})" if @debug
		@sqlitedbh.execute("insert into queue values ('#{query}','#{state}',#{level},#{curtime})")
	rescue
	end
end

#add_result(res) ⇒ Object



232
233
234
235
236
237
238
239
# File 'lib/pdns/pdns.rb', line 232

def add_result(res)
	puts "adding result: #{res.to_s}" if @debug
	curtime = Time.now().to_f
	@sqlitedbh.execute("insert into results values ('#{res.query}','#{res.answer}','#{res.rrtype}','#{res.ttl}','#{res.firstseen}','#{res.lastseen}',#{curtime})")

	add_query(res.answer,'pending')
	add_query(res.query,'pending')
end

#create_tablesObject



215
216
217
218
219
220
221
222
223
# File 'lib/pdns/pdns.rb', line 215

def create_tables
	puts "creating tables" if @debug
	@sqlitedbh.execute("create table results (query, answer, rrtype, ttl, firstseen, lastseen, ts REAL)")
	@sqlitedbh.execute("create table queue (query, state, level INTEGER, ts REAL)")
	@sqlitedbh.execute("create index residx on results (ts)")
	@sqlitedbh.execute("create unique index queue_unique on queue (query)")
	@sqlitedbh.execute("create index queue_level_idx on queue (level)")
	@sqlitedbh.execute("create index queue_state_idx on queue (state)")
end

#each_query(max_level = 20) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/pdns/pdns.rb', line 265

def each_query(max_level=20)
	puts "each_query max_level=#{max_level} curlevel=#{@level}" if @debug
	rows = @sqlitedbh.execute("select query, state, level from queue where state = 'failed' or state = 'pending' order by level limit 1")
	if rows
		rows.each do |row|
			query,state,level = row
			puts "  #{query},#{state},#{level}" if @debug
			if level < max_level
				update_query(query,'queried')
				yield query
			end
		end
	end
end

#get_state(query) ⇒ Object



255
256
257
258
259
260
261
262
263
# File 'lib/pdns/pdns.rb', line 255

def get_state(query)
	rows = @sqlitedbh.execute("select state from queue where query = '#{query}'")
	if rows
		rows.each do |row|
			return row[0]
		end
	end
	false
end

#next_resultObject



225
226
227
228
229
230
# File 'lib/pdns/pdns.rb', line 225

def next_result
	rows = @sqlitedbh.execute("select query, answer, rrtype, ttl, firstseen, lastseen from results order by ts")
	rows.each do |row|
		yield PDNSResult.new(*row)
	end
end

#update_query(query, state) ⇒ Object



251
252
253
# File 'lib/pdns/pdns.rb', line 251

def update_query(query,state)
	@sqlitedbh.execute("update queue set state = '#{state}' where query = '#{query}'")
end