Class: DNSTraverse::SummaryStats

Inherits:
Object
  • Object
show all
Defined in:
lib/dnstraverse/summary_stats.rb

Instance Method Summary collapse

Constructor Details

#initialize(referral) ⇒ SummaryStats

Returns a new instance of SummaryStats.



4
5
6
7
# File 'lib/dnstraverse/summary_stats.rb', line 4

def initialize(referral)
  @summary_stats = get_summary_stats(referral)
  @answer_stats = get_answer_stats(referral)
end

Instance Method Details

#answersObject

answers returns a hash summarising the answers referred to by the key :answered in the summary statistics

the key is the rdata string (e.g. 192.168.0.1) the value is a hash containing:

:prob #=> probability of getting this result
:rrs #=> array of Dnsruby::RR records

all probabilities add up to the chance of getting an answer

(e.g. get_summary_stats()[:answered][:prob] )

for example {

'192.168.0.1' => { :prob => 0.2, :rrs => [...] },
'192.168.0.2' => { :prob => 0.3, :rrs => [...] }

}



53
54
55
# File 'lib/dnstraverse/summary_stats.rb', line 53

def answers
  @answer_stats
end

#each_answerObject

each_answer takes a block and yields the probability and RR array

for example stats.each_answer do |prob, records|

puts "#{prob} = #{records.join(',')}"

end



63
64
65
66
67
# File 'lib/dnstraverse/summary_stats.rb', line 63

def each_answer
  @answer_stats.each_pair do |type, ainfo|
    yield ainfo[:prob], ainfo[:rr]
  end
end

#each_summaryObject

each_summary takes a block and yields the type and summary info

for example stats.each_summary do |type, sinfo|

puts "#{prob} = #{sinfo[:prob]}"

end



32
33
34
35
36
# File 'lib/dnstraverse/summary_stats.rb', line 32

def each_summary
  @summary_stats.each_pair do |type, sinfo|
    yield type, sinfo
  end
end

#summaryObject

summary returns a hash summarising the results

the key is the type of result, e.g.:

:exception, :noglue, :nodata, :answered, etc.

the value is a hash containing:

:prob #=> probability of getting this result

all probabilities will add up to 1

for example {

:exception => { :prob => 0.5 },
:answered => { :prob => 0.5 }

}



22
23
24
# File 'lib/dnstraverse/summary_stats.rb', line 22

def summary
  @summary_stats
end

#text(args = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dnstraverse/summary_stats.rb', line 69

def text(args = {})
  prefix = args[:prefix] || '  '
  indent = args[:indent] || "#{prefix}          "
  o = ''
  each_summary do |type, sinfo|
    if type == :answered then
      each_answer do |prob, records|
        initial = "#{prefix}#{txt_prob prob} answered with "
        rr_prefix = "\n" + (' ' * initial.length)
        o << initial
        o << records.map {|x| x.to_s.gsub(/\s+/, ' ') }.join(rr_prefix) + "\n"
      end
    end
  end
  each_summary do |type, sinfo|
    if type != :answered then
      case type
      when :nodata
        o << "#{prefix}#{txt_prob sinfo[:prob]} found no such record"
      when :referral_lame
        o << "#{prefix}#{txt_prob sinfo[:prob]} resulted in a lame referral"
      when :exception
        o << "#{prefix}#{txt_prob sinfo[:prob]} resulted in an exception"
      when :error
        o << "#{prefix}#{txt_prob sinfo[:prob]} resulted in an error"
      when :noglue
        o << "#{prefix}#{txt_prob sinfo[:prob]} found no glue"
      when :loop
        o << "#{prefix}#{txt_prob sinfo[:prob]} resulted in a loop"
      when :maxdepth
        o << "#{prefix}#{txt_prob sinfo[:prob]} exceeded maximum depth"
      when :cname_loop
        o << "#{prefix}#{txt_prob sinfo[:prob]} resulted in a CNAME loop"
      else
        o << "#{prefix}#{txt_prob sinfo[:prob]} #{type}"
      end
      o << "\n"
    end
  end
  return o
end