Class: ServerData

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

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h, p = 22) ⇒ ServerData

Create ServerData object

Parameters:

  • h (String)

    hostname

  • p (Integer) (defaults to: 22)

    port number


38
39
40
41
42
43
44
45
# 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

Instance Attribute Details

#dns_record_typeString (readonly)

Returns dns record type, “A” or “CNAME”.

Returns:

  • (String)

    dns record type, “A” or “CNAME”


49
50
51
# File 'lib/get_server_data.rb', line 49

def dns_record_type
  @dns_record_type
end

#hostnameString (readonly)

Returns hostname.

Returns:

  • (String)

    hostname


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

def hostname
  @hostname
end

#ipString (readonly)

Returns IP address.

Returns:

  • (String)

    IP address


57
58
59
# File 'lib/get_server_data.rb', line 57

def ip
  @ip
end

#ping_statusBoolean (readonly)

Returns result of ping test.

Returns:

  • (Boolean)

    result of ping test


65
66
67
# File 'lib/get_server_data.rb', line 65

def ping_status
  @ping_status
end

#portInteger (readonly)

Returns port number.

Returns:

  • (Integer)

    port number


61
62
63
# File 'lib/get_server_data.rb', line 61

def port
  @port
end

#port_statusBoolean (readonly)

Returns result of TCP port test.

Returns:

  • (Boolean)

    result of TCP port test


69
70
71
# File 'lib/get_server_data.rb', line 69

def port_status
  @port_status
end

Class Method Details

.to_hostname(input) ⇒ String

Convert IP address to hostname

Parameters:

  • input (String)

    IP address

Returns:

  • (String)

    hostname from DNS


75
76
77
78
79
80
81
82
83
84
85
# 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

Instance Method Details

#get_dns_typeString

Lookup DNS record type

Returns:

  • (String)

    record type, “A” or “CNAME”


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# 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_ipString

Lookup server IP from DNS

Returns:

  • (String)

    IP address of the server


110
111
112
113
114
115
116
117
# 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) ⇒ Boolean

Check TCP port connectivity

Parameters:

  • timeout (Integer) (defaults to: 1)

    TCP timeout in seconds

Returns:

  • (Boolean)

    success for failure of TCP connection


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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) ⇒ Boolean

Check ICMP connectivity

Parameters:

  • timeout (Integer) (defaults to: 1)

    ICMP timeout in seconds

Returns:

  • (Boolean)

    success or failure of ICMP test


148
149
150
151
152
153
154
155
156
# 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