module ElbPing::CLI

Setup and initialization for running as a CLI app happens here. Specifically, on load it will read in environment variables and set up default values for the app.

Constants

OPTIONS

Set up default options

PARSER

Build parser for command line options

Public Class Methods

main() click to toggle source

Main entry point of the program. Specifically, this method will:

  • Parse and validate command line arguments

  • Use the ‘ElbPing::Resolver` to discover ELB nodes

  • Use the ‘ElbPing::HttpPinger` to ping ELB nodes

  • Track the statistics for these pings

  • And, finally, use call upon ‘ElbPing::Display` methods to output to stdout

# File lib/elbping/cli.rb, line 59
def self.main
  # Catch ctrl-c
  run = true
  trap("SIGINT") {
    run = false
  }

  ##
  # Parse and validate command line arguments
  PARSER.parse!(ARGV) rescue usage

  if ARGV.size < 1
    usage
  end

  unless ARGV[0] =~ URI::regexp
    ElbPing::Display.error "ELB URI does not seem valid"
    usage
  end

  elb_uri_s = ARGV[0]
  elb_uri = URI.parse(elb_uri_s)

  ##
  # Discover ELB nodes
  #
  # TODO: Perhaps some retry logic
  begin
    nodes = ElbPing::Resolver.find_elb_nodes elb_uri.host
  rescue StandardError => e
    ElbPing::Display.error "Unable to query DNS for #{elb_uri.host}"
    ElbPing::Display.debug e
    exit(false)
  end

  if nodes.size < 1
    ElbPing::Display.error "Could not find any ELB nodes, no pings sent"
    exit(false)
  end

  ##
  # Set up summary objects for stats tracking of latency and loss
  stats = ElbPing::Stats.new
  nodes.each { |node| stats.add_node node }

  ##
  # Run the main loop of the program
  iteration = 0
  while run && (OPTIONS[:count] < 1 || iteration < OPTIONS[:count])

    sleep OPTIONS[:wait] if iteration > 0

    ##
    # Ping each node while tracking requests, responses, and latencies
    nodes.map { |node|
      break if not run

      status = ElbPing::HttpPinger.ping_node(node,
        elb_uri.host,
        elb_uri.port,
        (elb_uri.path == "") ? "/" : elb_uri.path,
        (elb_uri.scheme == 'https'),
        OPTIONS[:verb_len], OPTIONS[:timeout])

      # Display the response from the ping
      ElbPing::Display.response status

      # Register stats
      stats.register status
    }
    iteration += 1
  end
  # Display the stats summary
  ElbPing::Display.summary stats
end
usage() click to toggle source

Displays usage

# File lib/elbping/cli.rb, line 46
def self.usage
  ElbPing::Display.out PARSER.help
  exit(false)
end