Class: Munin2Graphite::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/munin2graphite/config.rb

Defined Under Namespace

Classes: ConfigFileNotFoundException, MalformedConfigFileException, NotConfiguredException, RequiredFieldMissingException

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject

Returns the value of attribute config.



23
24
25
# File 'lib/munin2graphite/config.rb', line 23

def config
  @config
end

Class Method Details

.check_configObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/munin2graphite/config.rb', line 57

def check_config
  fields={:carbon => [:hostname,:port],:graphite => [:endpoint,:prefix,:user,:password],:scheduler => [:metrics_period,:graphs_period]}
  fields.each do |k,v|
    v.each do |inner_field|
      field = "#{k}_#{inner_field}"
      if !@config.params[field] 
        workers.each do |worker|
          raise RequiredFieldMissingException.new("Error, required field not found in config ':#{field}' for worker #{worker}") unless @config.params[worker][field]
        end
        
        raise RequiredFieldMissingException.new("Error, required field not found in config ':#{field}'") if workers.empty?
      end
    end
  end
end

.config_file=(config_file) ⇒ Object



104
105
106
# File 'lib/munin2graphite/config.rb', line 104

def config_file=(config_file)
  @config_file = config_file
end

.config_for_class(klass) ⇒ Object

Returns the config for a given class



15
16
17
# File 'lib/munin2graphite/config.rb', line 15

def config_for_class(klass)
  return @config[klass.to_s.decamelize.to_sym]
end

.config_for_worker(worker) ⇒ Object

This method will return a config class but for a given worker, so everything will be the same as in the original class but the config changes made in this worker



27
28
29
30
31
32
33
# File 'lib/munin2graphite/config.rb', line 27

def config_for_worker(worker)
  return self if worker == "global"
  cloned = self.clone
  cloned.config = @config.clone
  cloned.config.params = @config.params.merge(@config.params[worker])
  return cloned
end

.configured?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/munin2graphite/config.rb', line 40

def configured?
  return @config_file != nil
end

.deconfigure!Object



35
36
37
38
# File 'lib/munin2graphite/config.rb', line 35

def deconfigure!
  @config = nil
  @config_file = nil
end

.logObject



92
93
94
95
96
97
98
99
100
101
102
# File 'lib/munin2graphite/config.rb', line 92

def log
  shift_age  = self["log_shift_age"] ? self["log_shift_age"].to_i : 1
  shift_size = self["log_shift_size"].to_i || 100000
  @log ||= if self["log"] == "STDOUT"
             Logger.new(STDOUT, shift_age, shift_size)
           else
             Logger.new(self["log"], shift_age, shift_size)
           end
  @log.level = self["log_level"] == "DEBUG" ? Logger::DEBUG : Logger::INFO
  @log
end

.method_missing(method_name, *args) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/munin2graphite/config.rb', line 73

def method_missing(method_name,*args)
  if !@config
    if @config_file
      parse_and_check!
    end
    raise NotConfiguredException.new("Not Configured") unless @config
  end
    
  if method_name == :"[]"
    if @config.params.has_key?(args.first.to_s)
      return @config.params[args.first.to_s]
    end
  end
  if @config.params.respond_to?method_name
    return @config.params.send(method_name,*args)
  end
  super
end

.parse_and_check!Object



113
114
115
116
# File 'lib/munin2graphite/config.rb', line 113

def parse_and_check!
  parse_config
  check_config
end

.parse_configObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/munin2graphite/config.rb', line 44

def parse_config
  begin
    @config = ParseConfig.new(@config_file)
  rescue Errno::ENOENT
    raise ConfigFileNotFoundException.new("Error, trying to open the config file #{@config_file}")
  rescue ArgumentError => exception
    raise MalformedConfigFileException.new("Malformed config file '#{@config_file}' #{exception.message}")
  rescue
    raise "Unknown error when trying to open config file '#{@config_file}'"
  end
  @config
end

.workersObject



19
20
21
# File 'lib/munin2graphite/config.rb', line 19

def workers
  return @config.groups + @config.params['workers'].to_a
end