Module: ElbPing::CLI

Defined in:
lib/elbping/cli.rb

Overview

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.

Constant Summary collapse

OPTIONS =

Set up default options

{}
PARSER =

Build parser for command line options

OptionParser.new do |opts|
  opts.banner = "Usage: #{$0} [options] <elb uri>"

  opts.on("-L LENGTH", "--verb-length LENGTH", Integer,
    "Use verb LENGTH characters long (default: #{OPTIONS[:verb_len]})") do |n|
    OPTIONS[:verb_len] = n
  end
  opts.on("-W SECONDS", "--timeout SECONDS", Integer,
    "Use timeout of SECONDS for HTTP requests (default: #{OPTIONS[:timeout]})") do |n|
    OPTIONS[:timeout] = n
  end
  opts.on("-w SECONDS", "--wait SECONDS", Integer,
    "Wait SECONDS between pings (default: #{OPTIONS[:wait]})") do |n|
    OPTIONS[:wait] = n
  end
  opts.on("-c COUNT", "--count COUNT", Integer,
    "Ping each node COUNT times (default: #{OPTIONS[:count]})") do |n|
    OPTIONS[:count] = n
  end
end

Class Method Summary collapse

Class Method Details

.mainObject

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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# 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.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

.usageObject

Displays usage



46
47
48
49
# File 'lib/elbping/cli.rb', line 46

def self.usage
  ElbPing::Display.out PARSER.help
  exit(false)
end