Class: Rubicante::Host

Inherits:
Object
  • Object
show all
Includes:
OsFunctions
Defined in:
lib/rubicante/host.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from OsFunctions

#is_windows?

Constructor Details

#initialize(name) ⇒ Host

Returns a new instance of Host.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubicante/host.rb', line 19

def initialize(name)
  @name     = name
  @ports    = []
  @services = []
  @types    = []
  @websites = []

  @log = Logging.logger[self]

  # Prepare Website logger
  @appender = Logging.logger['rubicante']
  Logging.logger['Rubicante::Website'].add_appenders(Logging.appenders.stdout) if not @appender.nil?
  Logging.logger['Rubicante::Website'].level = @log.level
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/rubicante/host.rb', line 16

def name
  @name
end

#portsObject

Returns the value of attribute ports.



17
18
19
# File 'lib/rubicante/host.rb', line 17

def ports
  @ports
end

#servicesObject

Returns the value of attribute services.



17
18
19
# File 'lib/rubicante/host.rb', line 17

def services
  @services
end

#typesObject

Returns the value of attribute types.



17
18
19
# File 'lib/rubicante/host.rb', line 17

def types
  @types
end

#websitesObject

Returns the value of attribute websites.



17
18
19
# File 'lib/rubicante/host.rb', line 17

def websites
  @websites
end

Instance Method Details

#check_port(port) ⇒ Object

Check if the specified port is active by connecting to it



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rubicante/host.rb', line 60

def check_port(port)
  port_output = "#{@name}:#{port}"

  @log.debug "Checking port #{port_output}..."

  begin
    test = TCPSocket.open(@name, port)
    @log.debug "Port #{port_output} looks good"
    return true # if we get here, the socket opened
  rescue
    @log.debug "Port #{port_output} raised an exception when opening"
    return false  # if we get here, there are problems with the port
  end
end

#check_portsObject

Iterates through all the ports in the Host and runs check_port(port) against them.

Yields: port, is_alive

Each registered port is yielded along with the boolean result from check_port(port).

Example:

host = Rubicante::Host.new("test-host")
host.port(80)
host.port(443)

host.check_ports do |port, response|
  puts "Port #{port} is UP" if response
  puts "Port #{port} is DOWN" if not response
end


93
94
95
96
97
# File 'lib/rubicante/host.rb', line 93

def check_ports
  @ports.each do |port|
    yield port, check_port(port)
  end
end

#check_servicesObject



176
177
178
179
180
# File 'lib/rubicante/host.rb', line 176

def check_services
  @services.each do |service|
    yield service, is_running?(service)
  end
end

#check_websitesObject

Iterates through all the websites in the Host and runs wrong? against them looking for problems. If a problem is found, a Hash of the URL and the HTTP Status code is yielded.

Yields: a Hash

{
  :url    => String,  # the URL of the current website in the block
  :code   => String   # the HTTP Status code of the check (i.e., 404, 500, etc.)
}

Example

host = Rubicante::Host.new("test-host")
host.website('www.exmaple.com')
host.website('www.rubicante.com')
host.website('www.openbsd.org')

host.check_websites do |result|
  puts "Website #{result[:url]} failed with code #{result[:code]}!"
end


120
121
122
123
124
125
# File 'lib/rubicante/host.rb', line 120

def check_websites
  @log.debug "Checking websites registered to host '#{@name}'"
  @websites.each do |website|
    yield website.wrong?
  end
end

#get_wmiObject



156
157
158
# File 'lib/rubicante/host.rb', line 156

def get_wmi
  WIN32OLE.connect("winmgmts://#{@name}")
end

#is_running?(service) ⇒ Boolean

Checks the host to see if the specified service is running

Returns:

  • (Boolean)


161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rubicante/host.rb', line 161

def is_running?(service)
  query = "SELECT Name, State FROM Win32_Service WHERE Name='#{service}' AND State='Running'"

  result = false
  service_label = "#{@name}->#{service}"

  @log.debug "Checking services #{service_label}..."
  get_wmi.ExecQuery(query).each do |result|
    @log.debug "#{service_label}.State == #{result.State}"
    result = true if result.State = 'Running'
  end

  return result
end

#pingObject



34
35
36
37
# File 'lib/rubicante/host.rb', line 34

def ping
  @log.debug "Performing TCP echo ping on host '#{@name}'"
  Ping.pingecho @name
end

#port(port_number) ⇒ Object



39
40
41
42
# File 'lib/rubicante/host.rb', line 39

def port(port_number)
  @ports << port_number
  self
end

#service(service_name) ⇒ Object



44
45
46
47
# File 'lib/rubicante/host.rb', line 44

def service(service_name)
  @services << service_name
  self
end

#type(type_name) ⇒ Object



49
50
51
52
# File 'lib/rubicante/host.rb', line 49

def type(type_name)
  @types << type_name
  self
end

#website(website_url) ⇒ Object



54
55
56
57
# File 'lib/rubicante/host.rb', line 54

def website(website_url)
  @websites << Website.new(website_url)
  self
end

#wrong?Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/rubicante/host.rb', line 127

def wrong?
  @log.debug "Determing what is wrong with host '#{@name}'"
  result = HostError.new(@name)
  result.ping = self.ping

  # If the host is alive, continue testing
  if result.ping
    check_ports do |port, response|
      result.bad_ports << port if not response
    end

    check_websites do |website_error|
      result.add(website_error)
    end

    if is_windows?
      check_services do |service, response|
        result.bad_services << service if not response
      end
    end
  end

  return result
end