diff --git a/Gemfile b/Gemfile index 344af03..3be55fb 100644 --- a/Gemfile +++ b/Gemfile @@ -10,4 +10,3 @@ gem "rake", "~> 13.0" gem "standard", "~> 1.3" gem "dnsruby", github: "kubicek/dnsruby", branch: "patch-1" - diff --git a/bin/xmon b/bin/xmon index 009053b..e748d49 100755 --- a/bin/xmon +++ b/bin/xmon @@ -28,3 +28,5 @@ end opts[:definition].each do |file| Xmon.load(file).check end + +Xmon::Results.print diff --git a/lib/xmon.rb b/lib/xmon.rb index 095edd5..c60b6d0 100644 --- a/lib/xmon.rb +++ b/lib/xmon.rb @@ -14,35 +14,20 @@ require_relative "xmon/reverse_dns" require_relative "xmon/whois" require_relative "xmon/ssh" +require_relative "xmon/results" module Xmon class Error < StandardError; end - class Results - def self.add(result) - @results ||= [] - @results << result - end - - def self.get - @results.flatten(1).select { |a| a.is_a?(Array) } - end - end - def self.print_results(results) # puts "Results: #{results.inspect}".colorize(:blue) results.each do |res| res.each do |r| if r.is_a?(Array) - friendly_name = if r[1].respond_to?(:parent) - [r[1].class.to_s.split("::").last, r[1].parent.friendly_name].join("/") - else - r[1].class.to_s - end if r[0] == :ok - puts "OK [#{friendly_name}]: #{r[2]}".colorize(:green) + puts "OK [#{r[1]}]: #{r[2]}".colorize(:green) else - puts "FAIL: [#{friendly_name}]: #{r[2]} != #{r[3]}".colorize(:red) + puts "FAIL: [#{r[1]}]: #{r[2]} != #{r[3]}".colorize(:red) end end end diff --git a/lib/xmon/descriptions.rb b/lib/xmon/descriptions.rb index 7c644ce..ccb5553 100644 --- a/lib/xmon/descriptions.rb +++ b/lib/xmon/descriptions.rb @@ -1,18 +1,17 @@ module Xmon class Description - attr_reader :parent, :results + attr_reader :parent, :dsl_id + + def path + [@parent ? @parent.path : nil, self.class.to_s.split("::").last, dsl_id].compact.join("/") + end def friendly_name - c = self.class.to_s.split("::").last - if @parent - [@parent.friendly_name, c].join("/") - else - c - end + [path, dsl_id].join("/") end def compare(name, a, b) - info = [friendly_name, name].join("/") + info = [path, name].join("/") if a == b [:ok, info, a].compact else @@ -67,11 +66,9 @@ def status(status) # standard:disable Style/TrivialAccessors end def check - @results = [] (@descriptions || []).each { |d| puts "#{d.class} #{@name}".colorize(:yellow) res = d.check - @results << res Xmon::Results.add(res) Xmon.print_results([res]) } @@ -83,10 +80,7 @@ class DomainDescription < Description def initialize(name, *) @name = name - end - - def friendly_name - @name + @dsl_id = name end def rdap(&) @@ -109,10 +103,7 @@ class IPv4Description < Description attr_accessor :address def initialize(address, *) @address = address - end - - def friendly_name - @address + @dsl_id = address end def ptr(*, **, &) diff --git a/lib/xmon/results.rb b/lib/xmon/results.rb new file mode 100644 index 0000000..7697a81 --- /dev/null +++ b/lib/xmon/results.rb @@ -0,0 +1,28 @@ +module Xmon + class Results + def self.add(result) + @results ||= [] + @results << result + end + + def self.get + @results.flatten(1).select { |a| a.is_a?(Array) } + end + + def self.print + res = get + resg = res.group_by(&:first) + puts "Results" + puts "-------" + puts "Total: #{res.count}" + puts "OK: #{resg[:ok].count}".colorize(:green) if resg[:ok] + if resg[:fail] + puts "FAIL: #{resg[:fail].count}".colorize(:red) + puts "-------" + resg[:fail].each do |r| + puts "FAIL: #{r[1]} #{r[2]} != #{r[3]}".colorize(:red) + end + end + end + end +end diff --git a/lib/xmon/reverse_dns.rb b/lib/xmon/reverse_dns.rb index 9030982..bd7531a 100644 --- a/lib/xmon/reverse_dns.rb +++ b/lib/xmon/reverse_dns.rb @@ -3,18 +3,12 @@ module Xmon class ReverseDNS < Description def initialize(parent, value = nil) - if parent - puts "NEW REVERSE DNS #{value} #{parent.address}" - @parent = parent - @address = parent.address - @hostname = value - else - puts "NEW REVERSE DNS #{value}" - end + @parent = parent + @address = parent.address + @hostname = value end def fetch(record, type = "A") - puts "fetching #{record} #{type}" Dnsruby::Resolver.new.query(record, type).answer.map { |a| a.rdata.to_s }.join end diff --git a/lib/xmon/ssh.rb b/lib/xmon/ssh.rb index dd9c0c3..6e067f3 100644 --- a/lib/xmon/ssh.rb +++ b/lib/xmon/ssh.rb @@ -25,7 +25,6 @@ def fetch(host, port = 22) end def check - puts "checking SSH for #{@address} #{@host} #{@port}" current = fetch(@address, @port) r = [] r << compare(:version, @version, current[:version]) if @version diff --git a/lib/xmon/ssl.rb b/lib/xmon/ssl.rb index cab5888..d025d8f 100644 --- a/lib/xmon/ssl.rb +++ b/lib/xmon/ssl.rb @@ -3,12 +3,16 @@ class SSL < TCP def initialize(parent, *args, **kwargs) @parent = parent @address = parent.address - @host = kwargs[:host] - @path = kwargs[:path] || "/" + @dsl_id = @port define_attributes([:host, :status_code, :server, :cert_sn, :location]) super end + def dsl_id + # [@port, @host, @path].join(":") + [@port, @host].join(":") + end + def fetch(host, name = nil, port = 443, path = "/") ctx = OpenSSL::SSL::SSLContext.new sock = TCPSocket.new(host, port) @@ -37,7 +41,6 @@ def fetch(host, name = nil, port = 443, path = "/") end def check - puts "checking SSL for #{@address} #{@host} #{@port} #{@path}" current = fetch(@address, @host, @port, @path) r = [] r << compare(:status_code, @status_code, current[:status_code]) if @status_code diff --git a/lib/xmon/tcp.rb b/lib/xmon/tcp.rb index d48e459..1034784 100644 --- a/lib/xmon/tcp.rb +++ b/lib/xmon/tcp.rb @@ -8,6 +8,7 @@ def initialize(parent, *args, **kwargs) @address = parent.address @port = args[0] @protocol = kwargs[:protocol] + @dsl_id = @port end def key(type, value) @@ -39,6 +40,7 @@ def fetch(host, ports, protocol = :tcp) end end end + File.delete("nmap.xml") if File.exist?("nmap.xml") @out.flatten end