class ServerData

Models server data (hostname, port, IP address, etc.)

Attributes

dns_record_type[R]

@!attribute [r] dns_record_type @return [String] dns record type, “A” or “CNAME”

hostname[R]

@!attribute [r] hostname @return [String] hostname

ip[R]

@!attribute [r] ip @return [String] IP address

ping_status[R]

@!attribute [r] ping_status @return [Boolean] result of ping test

port[R]

@!attribute [r] port @return [Integer] port number

port_status[R]

@!attribute [r] port_status @return [Boolean] result of TCP port test

Public Class Methods

new(h, p = 22) click to toggle source

Create ServerData object

@param h [String] hostname @param p [Integer] port number

# File lib/get_server_data.rb, line 38
def initialize(h, p = 22)
  @dns_record_type = nil
  @hostname        = h
  @ip              = nil
  @port            = p
  @ping_status     = nil
  @port_status     = nil
end
to_hostname(input) click to toggle source

Convert IP address to hostname

@param input [String] IP address @return [String] hostname from DNS

# File lib/get_server_data.rb, line 75
def self.to_hostname(input)
  if input =~ /^\d+\.\d+\.\d+.\d+$/
    begin
      return Resolv.getname(input)
    rescue Resolv::ResolvError
      return input
    end
  else
    return input
  end
end

Public Instance Methods

get_dns_type() click to toggle source

Lookup DNS record type

@return [String] record type, “A” or “CNAME”

# File lib/get_server_data.rb, line 122
def get_dns_type
  resolver = Resolv::DNS.new
  begin
    resolver.getresource(@hostname, Resolv::DNS::Resource::IN::CNAME)
  rescue Resolv::ResolvError
    # do nothing
  else
    @dns_record_type = 'CNAME'
  end

  if @dns_record_type.nil?
    begin
      resolver.getresource(@hostname, Resolv::DNS::Resource::IN::A)
    rescue Resolv::ResolvError
      @dns_record_type = ""
    else
      @dns_record_type = 'A'
    end
  end
  @dns_record_type
end
get_ip() click to toggle source

Lookup server IP from DNS

@return [String] IP address of the server

# File lib/get_server_data.rb, line 110
def get_ip
  begin
    @ip = Resolv.getaddress(@hostname)
  rescue Resolv::ResolvError
    @ip = ""
  end
  @ip
end
get_port_status(timeout = 1) click to toggle source

Check TCP port connectivity

@param timeout [Integer] TCP timeout in seconds @return [Boolean] success for failure of TCP connection

# File lib/get_server_data.rb, line 91
def get_port_status(timeout = 1)
  begin
    Timeout::timeout(timeout) do
      begin
        s = TCPSocket.new(@hostname, @port)
        s.close
        return @port_status = true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH, SocketError
        return @port_status = false
      end
    end
  rescue Timeout::Error
  end
  @port_status = false
end
ping(timeout = 1) click to toggle source

Check ICMP connectivity

@param timeout [Integer] ICMP timeout in seconds @return [Boolean] success or failure of ICMP test

# File lib/get_server_data.rb, line 148
def ping(timeout = 1)
  ping = Net::Ping::External.new(@hostname, 7, timeout)
  if ping.ping.nil?
    @ping_status = false
  else
    @ping_status = ping.ping
  end
  @ping_status
end