Module: Panoptimon

Defined in:
lib/panoptimon/util.rb,
lib/panoptimon.rb,
lib/panoptimon/http.rb,
lib/panoptimon/monitor.rb,
lib/panoptimon/version.rb,
lib/panoptimon/collector.rb

Overview

Copyright © 2012 Sourcefire, Inc.

Defined Under Namespace

Modules: CollectorSink, Logger, Util Classes: Collector, HTTP, Metric, Monitor, Panoptimon

Constant Summary collapse

VERSION =
"0.4.4"

Class Method Summary collapse

Class Method Details

.load_options(args) ⇒ Object



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
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
133
134
135
136
137
138
139
140
# File 'lib/panoptimon.rb', line 20

def self.load_options (args)
  defaults = {
    :daemonize      => true,
    :config_dir     => '/etc/panoptimon/',
    :config_file    => '%/panoptimon.json',
    :collectors_dir => '%/collectors',
    :plugins_dir    => '%/plugins',
    :collector_interval => 60,
    :collector_timeout  => 120,
  }

  options = ->() {
    o = {}
    OptionParser.new do |opts|

      opts.on('-C', '--config-dir DIR',
        "Config directory (#{defaults[:config_dir]})"
      ) { |v| o[:config_dir] = v }

      opts.on('-c', '--config-file FILENAME',
        "Alternative configuration file ",
        "(#{defaults[:config_file]})"
      ) { |v| o[:config_file] = v.nil? ? '' : v }

      opts.on('-D', '--[no-]foreground',
        "Don't daemonize (#{not defaults[:daemonize]})"
      ) { |v| o[:daemonize] = ! v }

      ['collectors', 'plugins'].each { |x|
        k = "#{x}_dir".to_sym
        opts.on("--#{x}-dir DIR",
          "#{x.capitalize} directory (#{defaults[k]})"
        ) { |v| o[k] = v }
      }

      [:collectors, :plugins].each { |x|
        opts.on('--list-'+x.to_s, "list all #{x} found"
        ) { (o[:lists] ||= []).push(x) }
      }

      opts.on('-o', '--configure X=Y',
        'Set configuration values'
      ) { |x|
        (k,v) = x.split(/=/, 2)
        (o[:configure] ||= {})[k.to_sym] = v
      }

      opts.on('--show WHAT',
        %q{Show/validate settings for:}, %q{  'config' / collector:foo / plugin:foo}
      ) { |x| (k,v) = x.split(/:/, 2)
        o[:show] = {k.to_sym => v||true}
      }

      opts.on('--plugin-test FILE',
        'Load and test plugin(s).'
      ) { |x| (o[:plugin_test] ||= []).push(x) }

      opts.on('-d', '--debug', "Enable debugging."
      ) { |v| o[:debug] = v }

      opts.on('--verbose', "Enable verbose output"
      ) { |v| o[:verbose] = v }

      opts.on('-v', '--version', "Print version"
      ) { 
        puts "panoptimon version #{Panoptimon::VERSION}"
        o[:quit] = true
        opts.terminate
      }

      opts.on('--help-defaults', 'Show default config values'
      ) {
        puts JSON.pretty_generate(defaults)
        o[:quit] = true
        opts.terminate
      }

      opts.on("-h", "--help", "Show this message"
      ) {
        puts opts
        o[:quit] = true
        opts.terminate
      }

    end.parse!(args)

    return o
  }.call

  return false if options[:quit]

  render = ->(d, x) { # x with '%/' can be relative to dir d
    f = "#{x}"; f.sub!(/^%\//, '').nil? ? f : File.join(d, f)
  }

  # default config file is relative to config dir
  cfile = render.call(
    options[:config_dir]  || defaults[:config_dir],
    options[:config_file] || defaults[:config_file])

  config = defaults.merge(
    options[:config_file] == '' ? {} :
      JSON.parse(File.read(cfile), {:symbolize_names => true})
  ).merge(options);

  config[:config_file] = cfile # for diagnostics

  (config.delete(:configure) || {}).each { | k,v| config[k] = v }

  [:collectors_dir, :plugins_dir].each { |d|
    config[d] = render.call(config[:config_dir], config[d])
  }

  # make all paths absolute
  [:config_file, :config_dir, :collectors_dir, :plugins_dir].each { |d|
    config[d] = File.expand_path(config[d]) unless config[d] == ''
  }

  return OpenStruct.new(config).freeze

end