Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kubicek committed Mar 9, 2024
1 parent ceea02d commit a761ce5
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 50 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ gem "rake", "~> 13.0"
gem "standard", "~> 1.3"

gem "dnsruby", github: "kubicek/dnsruby", branch: "patch-1"

2 changes: 2 additions & 0 deletions bin/xmon
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ end
opts[:definition].each do |file|
Xmon.load(file).check
end

Xmon::Results.print
21 changes: 3 additions & 18 deletions lib/xmon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 9 additions & 18 deletions lib/xmon/descriptions.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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])
}
Expand All @@ -83,10 +80,7 @@ class DomainDescription < Description

def initialize(name, *)
@name = name
end

def friendly_name
@name
@dsl_id = name
end

def rdap(&)
Expand All @@ -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(*, **, &)
Expand Down
28 changes: 28 additions & 0 deletions lib/xmon/results.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 3 additions & 9 deletions lib/xmon/reverse_dns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion lib/xmon/ssh.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions lib/xmon/ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lib/xmon/tcp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit a761ce5

Please sign in to comment.