Class: ServerData
- Inherits:
-
Object
- Object
- ServerData
- Defined in:
- lib/get_server_data.rb
Overview
Models server data (hostname, port, IP address, etc.)
Instance Attribute Summary collapse
-
#dns_record_type ⇒ String
readonly
Dns record type, “A” or “CNAME”.
-
#hostname ⇒ String
readonly
Hostname.
-
#ip ⇒ String
readonly
IP address.
-
#ping_status ⇒ Boolean
readonly
Result of ping test.
-
#port ⇒ Integer
readonly
Port number.
-
#port_status ⇒ Boolean
readonly
Result of TCP port test.
Class Method Summary collapse
-
.to_hostname(input) ⇒ String
Convert IP address to hostname.
Instance Method Summary collapse
-
#get_dns_type ⇒ String
Lookup DNS record type.
-
#get_ip ⇒ String
Lookup server IP from DNS.
-
#get_port_status(timeout = 1) ⇒ Boolean
Check TCP port connectivity.
-
#initialize(h, p = 22) ⇒ ServerData
constructor
Create ServerData object.
-
#ping(timeout = 1) ⇒ Boolean
Check ICMP connectivity.
Constructor Details
#initialize(h, p = 22) ⇒ ServerData
Create ServerData object
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_type ⇒ String (readonly)
Returns 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 |
#hostname ⇒ String (readonly)
Returns hostname.
53 54 55 |
# File 'lib/get_server_data.rb', line 53 def hostname @hostname end |
#ip ⇒ String (readonly)
Returns IP address.
57 58 59 |
# File 'lib/get_server_data.rb', line 57 def ip @ip end |
#ping_status ⇒ Boolean (readonly)
Returns result of ping test.
65 66 67 |
# File 'lib/get_server_data.rb', line 65 def ping_status @ping_status end |
#port ⇒ Integer (readonly)
Returns port number.
61 62 63 |
# File 'lib/get_server_data.rb', line 61 def port @port end |
#port_status ⇒ Boolean (readonly)
Returns 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
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_type ⇒ String
Lookup DNS record type
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_ip ⇒ String
Lookup server IP from DNS
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
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
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 |