Class: GRI::Main

Inherits:
Object show all
Defined in:
lib/gri/main.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMain

Returns a new instance of Main.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gri/main.rb', line 20

def initialize
  @options = {}
  optparser = optparse @options
  optparser.parse!
  config_path = options[:config_path] || GRI::Config::DEFAULT_PATH
  @config = GRI::Config.init config_path
  @options.each {|k, v| @config.setvar k.to_s, v}
  root_dir = @config['root-dir'] ||= Config::ROOT_PATH

  log_dir = @config['log-dir'] || root_dir + '/log'
  begin
    Dir.mkdir log_dir unless File.exist? log_dir
    Log.init "#{log_dir}/#{optparser.program_name}.log",
      :log_level=>@config['log-level']
  rescue SystemCallError
  end

  plugin_dirs = @config.getvar('plugin-dir') || [root_dir + '/plugin']
  GRI::Plugin.load_plugins plugin_dirs, @config

  @config['tra-dir'] ||= root_dir + '/tra'
  @config['gra-dir'] ||= root_dir + '/gra'
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



18
19
20
# File 'lib/gri/main.rb', line 18

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/gri/main.rb', line 18

def options
  @options
end

Instance Method Details

#optparse(opts) ⇒ Object



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
# File 'lib/gri/main.rb', line 83

def optparse opts
  op = OptionParser.new
  op.on('--debug [FLAGS]', String) {|arg| STDOUT.sync = true
    a = (arg || '').split(/,/)
    $debug = Hash[*(a.zip([true]*a.size).flatten)]
    opts['log-level'] = 'debug'}

  op.on('--add-writer=WRITER') {|arg| (opts['writers'] ||= []).push arg}
  op.on('-O OPT_STR') {|arg| (opts['O'] ||= []).push arg}
  op.on('--collector') {opts['collector'] = true}
  op.on('--config-path=PATH') {|arg| opts[:config_path] = arg}
  op.on('--duration=SEC', Integer) {|arg| opts['duration'] = arg}
  op.on('--fake-snmp=FILE') {|arg| opts['fake_snmp'] = arg}
  op.on('--gritab-path=PATH') {|arg| opts['gritab-path'] = arg}
  op.on('--host-pat=PAT', '-h') {|arg| (opts['host-pat'] ||= []).push arg}
  op.on('--interval=SEC', Integer) {|arg| opts['interval'] = arg}
  op.on('--log-level=LEVEL') {|arg| opts['log-level'] = arg}
  op.on('--nop') {opts['nop'] = true}
  op.on('--para') {opts['para'] = true}
  op.on('-p', '--plugin-dir=DIR') {|arg|
    (opts['plugin-dir'] ||= []).push arg}
  op.on('--rrdupdater', '--updater') {opts['updater'] = true}
  op.on('--single') {opts['para'] = false}
  op.on('--tra=URL') {|arg| opts['tra'] = arg}
  op.on('--reset-writers') {opts['reset-writers'] = true}

  op.on('-c COMMUNITY') {|arg| opts['community'] = arg}
  op.on('-v VER') {|arg| opts['version'] = arg}
  op
end

#runObject



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
# File 'lib/gri/main.rb', line 44

def run
  @config['para'] = true unless config.has_key? 'para'
  if @config['walker']
    app = AppWalker.new @config
    writer = Writer.create 'stdout', @config.to_h
    app.writers.push writer
  elsif @config['collector']
    # collector
    app = AppCollector.new @config
    writer = Writer.create 'ldb', @config.to_h
    app.writers.push writer
  else
    # minigri
    app = AppCollector.new @config
    wopts = @config.to_h.update :merge_p=>true
    writer = Writer.create 'rrd', wopts
    app.writers.push writer
  end
  app.writers.clear if @options['reset-writers']
  if @options['writers']
    for w in @options['writers']
      writer = Writer.create w, @config.to_h
      app.writers.push writer if writer
    end
  end
  app.run
  if app.metrics[:nometrics].zero? and !$debug
    hostname = Socket.gethostname rescue 'unknown'
    hostname = hostname.split(/\./).first
    t = Time.now.to_i
    interval = @config['interval'] || 300
    records = app.metrics.map {|k, v|
      {'_interval'=>interval, '_host'=>"GRIMETRICS-#{hostname}",
        '_time'=>t, '_key'=>"num_#{k}", 'num'=>v}}
    writer = Writer.create 'ldb', @config.to_h
    writer.write records
  end
end