Class: Sensu::CLI

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

Class Method Summary collapse

Class Method Details

.read(arguments = ARGV) ⇒ Hash

Parse CLI arguments using Ruby stdlib ‘optparse`. This method provides Sensu with process options (eg. log file), and can provide users with information, such as the Sensu version.



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sensu/cli.rb', line 12

def self.read(arguments=ARGV)
  options = {}
  if File.exist?("/etc/sensu/config.json")
    options[:config_file] = "/etc/sensu/config.json"
  end
  if Dir.exist?("/etc/sensu/conf.d")
    options[:config_dirs] = ["/etc/sensu/conf.d"]
  end
  optparse = OptionParser.new do |opts|
    opts.on("-h", "--help", "Display this message") do
      puts opts
      exit
    end
    opts.on("-V", "--version", "Display version") do
      puts VERSION
      exit
    end
    opts.on("-c", "--config FILE", "Sensu JSON config FILE. Default: /etc/sensu/config.json (if exists)") do |file|
      options[:config_file] = file
    end
    opts.on("-d", "--config_dir DIR[,DIR]", "DIR or comma-delimited DIR list for Sensu JSON config files. Default: /etc/sensu/conf.d (if exists)") do |dir|
      options[:config_dirs] = dir.split(",")
    end
    opts.on("--validate_config", "Validate the compiled configuration and exit") do
      options[:validate_config] = true
    end
    opts.on("-P", "--print_config", "Print the compiled configuration and exit") do
      options[:print_config] = true
    end
    opts.on("-e", "--extension_dir DIR", "DIR for Sensu extensions") do |dir|
      options[:extension_dir] = dir
    end
    opts.on("-l", "--log FILE", "Log to a given FILE. Default: STDOUT") do |file|
      options[:log_file] = file
    end
    opts.on("-L", "--log_level LEVEL", "Log severity LEVEL") do |level|
      log_level = level.to_s.downcase.to_sym
      unless Logger::LEVELS.include?(log_level)
        puts "Unknown log level: #{level}"
        exit 1
      end
      options[:log_level] = log_level
    end
    opts.on("-v", "--verbose", "Enable verbose logging") do
      options[:log_level] = :debug
    end
    opts.on("-b", "--background", "Fork into the background") do
      options[:daemonize] = true
    end
    opts.on("-p", "--pid_file FILE", "Write the PID to a given FILE") do |file|
      options[:pid_file] = file
    end
  end
  optparse.parse!(arguments)
  options
end