Class: CheckTCPMemory::CLI

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

Class Method Summary collapse

Class Method Details

.parse(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/check_tcp_memory/cli.rb', line 7

def self.parse(args)
  options = OpenStruct.new
  options[:warn_percent] = 0.5
  options[:crit_percent] = 0.6

  opt_parser = OptionParser.new do |opts|
    opts.banner = "Usage: #{$0} -w <warn percent> -c <critical percent>"
    opts.on('-w', '--warn-percent PERCENT', 'Warning when percentage of total TCP memory is over this threashold. Default: 50%') do |ret|
      if ret.to_f > 100 || ret.to_f <= 0
        puts "Warning percent out of range! must be between 1-100"
        exit 1
      end
      options[:warn_percent] = ret.to_f / 100.0
    end

    opts.on('-c', '--crit-percent PERCENT', 'Critical when percentage of total TCP memory is over this threashold. Default: 60%') do |ret|
      if ret.to_f > 100 || ret.to_f <= 0
        puts "Critical percent out of range! must be between 1-100"
        exit 1
      end
      options[:crit_percent] = ret.to_f / 100.0
    end

    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end

    opts.on_tail("--version", "Show version") do
      puts CheckTCPMemory::VERSION
      exit
    end
  end

  opt_parser.parse!(args)

  return options
end