Hosts Up - dhoelzer/DAD GitHub Wiki

Bear in mind that the creation of jobs should be reserved for the most trusted individuals who will be touching the log management system. With that in mind, the ability to create arbitrary scripts within the interface is quite powerful, as illustrated below. Here we have an example script that can be used to check to see if a host is alive not just by pinging it but by checking a variety of ports, making it possible to check on systems that otherwise might not respond!

def up?(host)
  return false if host.nil?
    # Wait for 4 seconds for a response.  Previously used 1 second but would occasionally
    # create false alarms when network conditions were poor.
  results = `ping -q -c 1 -W 4 #{host}`  # Linux uses "-W" for timeout.
  return true if $?.exitstatus == 0
  results = `nc -w 1 #{host} 80`  # Try a TCP connection to port 80
  return true if $?.exitstatus == 0
  results = `nc -w 1 #{host} 443`  # Try a TCP connection to port 443
  return true if $?.exitstatus == 0
  results = `nc -w 1 #{host} 25`  # Try a TCP connection to port 25
  return true if $?.exitstatus == 0
  results = `nc -w 1 #{host} 22`  # Try a TCP connection to port 22
  return true if $?.exitstatus == 0
  return false
end

hosts = System.where(:monitor => true).pluck(:name)
hosts.each do |host|
  Alert.hostUnreachable(System.find_by_name(host)) unless up?(host)
end