Class: Netmidget

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Netmidget

Returns a new instance of Netmidget.



10
11
12
13
14
15
16
# File 'lib/netmidget.rb', line 10

def initialize(opts)
  @hostname = opts[:hostname] || `/bin/hostname`.chomp
  @site = opts[:site]
  @ip = opts[:ip]
  @device_name = opts[:device_name]
  @log = opts[:log] || nil
end

Instance Attribute Details

#device_nameObject

Returns the value of attribute device_name.



4
5
6
# File 'lib/netmidget.rb', line 4

def device_name
  @device_name
end

#hostnameObject

Returns the value of attribute hostname.



4
5
6
# File 'lib/netmidget.rb', line 4

def hostname
  @hostname
end

#ipObject

Returns the value of attribute ip.



4
5
6
# File 'lib/netmidget.rb', line 4

def ip
  @ip
end

#logObject

Returns the value of attribute log.



4
5
6
# File 'lib/netmidget.rb', line 4

def log
  @log
end

#siteObject

Returns the value of attribute site.



4
5
6
# File 'lib/netmidget.rb', line 4

def site
  @site
end

Instance Method Details

#device_strObject



18
19
20
# File 'lib/netmidget.rb', line 18

def device_str
  "#{site}::#{device_name}::#{ip}"
end

#pingObject



22
23
24
# File 'lib/netmidget.rb', line 22

def ping
  Net::Ping::External.new(ip).ping?
end

#ping?Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
# File 'lib/netmidget.rb', line 26

def ping?
  if not resp = ping
    warn_str = "#{device_str} was unresponsive...trying again"
    log ? log.warn(warn_str) : puts(Time.now.to_s + ": " + warn_str)
    resp = ping
  end
  resp
end

#ping_output_str_failObject



59
60
61
62
63
# File 'lib/netmidget.rb', line 59

def ping_output_str_fail
  str = ping_output_str_prefix + "Fail"
  log ? log.error(str) : puts(str = (stdout_prefix + str))
  str
end

#ping_output_str_prefixObject



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

def ping_output_str_prefix
  "Pinging #{device_str}: "
end

#ping_output_str_successObject



53
54
55
56
57
# File 'lib/netmidget.rb', line 53

def ping_output_str_success
  str = ping_output_str_prefix + "Success"
  log ? log.info(str) : puts(str = (stdout_prefix + str))
  str
end

#stdout_prefixObject



45
46
47
# File 'lib/netmidget.rb', line 45

def stdout_prefix
  "#{Time.now.to_s}: #{hostname} "
end

#tracerouteObject



35
36
37
# File 'lib/netmidget.rb', line 35

def traceroute
  `/usr/sbin/traceroute -n -m 20 -w 2 #{ip} 2>&1`
end

#traceroute_output_strObject



39
40
41
42
43
# File 'lib/netmidget.rb', line 39

def traceroute_output_str
  str = traceroute
  log ? log.error(str) : puts(str = (stdout_prefix + str))
  str
end