Class: UV::Ping

Inherits:
Object
  • Object
show all
Defined in:
lib/uv-rays/ping.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, count: 1, interval: 1, timeout: 5) ⇒ Ping

Returns a new instance of Ping.



11
12
13
14
15
# File 'lib/uv-rays/ping.rb', line 11

def initialize(host, count: 1, interval: 1, timeout: 5)
    @host = host
    @count = count
    @timeout = timeout
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



17
18
19
# File 'lib/uv-rays/ping.rb', line 17

def count
  @count
end

#durationObject (readonly)

Returns the value of attribute duration.



17
18
19
# File 'lib/uv-rays/ping.rb', line 17

def duration
  @duration
end

#exceptionObject (readonly)

Returns the value of attribute exception.



17
18
19
# File 'lib/uv-rays/ping.rb', line 17

def exception
  @exception
end

#hostObject (readonly)

Returns the value of attribute host.



17
18
19
# File 'lib/uv-rays/ping.rb', line 17

def host
  @host
end

#ipObject (readonly)

Returns the value of attribute ip.



17
18
19
# File 'lib/uv-rays/ping.rb', line 17

def ip
  @ip
end

#pingableObject (readonly)

Returns the value of attribute pingable.



17
18
19
# File 'lib/uv-rays/ping.rb', line 17

def pingable
  @pingable
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



17
18
19
# File 'lib/uv-rays/ping.rb', line 17

def timeout
  @timeout
end

#warningObject (readonly)

Returns the value of attribute warning.



17
18
19
# File 'lib/uv-rays/ping.rb', line 17

def warning
  @warning
end

Instance Method Details

#pingObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/uv-rays/ping.rb', line 19

def ping
    @ip = if IPAddress.valid?(@host)
        @host
    else
        nslookup(@host)
    end

    if @ip.nil?
        @pingable = false
        @exception = 'DNS lookup failed for both IPv4 and IPv6'
        return false
    end

    ipaddr = IPAddr.new @ip
    if ipaddr.ipv4?
        ping4(@ip, @count, @timeout)
    else
        ping6(@ip, @count, @timeout)
    end
end