Class: NetuitiveRubyApi::ConfigManager

Inherits:
Object
  • Object
show all
Defined in:
lib/netuitive_ruby_api/config_manager.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/netuitive_ruby_api/config_manager.rb', line 6

def data
  @data
end

.event_cache_enabledObject (readonly)

Returns the value of attribute event_cache_enabled.



10
11
12
# File 'lib/netuitive_ruby_api/config_manager.rb', line 10

def event_cache_enabled
  @event_cache_enabled
end

.event_cache_intervalObject (readonly)

Returns the value of attribute event_cache_interval.



12
13
14
# File 'lib/netuitive_ruby_api/config_manager.rb', line 12

def event_cache_interval
  @event_cache_interval
end

.event_cache_sizeObject (readonly)

Returns the value of attribute event_cache_size.



11
12
13
# File 'lib/netuitive_ruby_api/config_manager.rb', line 11

def event_cache_size
  @event_cache_size
end

.netuitivedAddrObject (readonly)

Returns the value of attribute netuitivedAddr.



4
5
6
# File 'lib/netuitive_ruby_api/config_manager.rb', line 4

def netuitivedAddr
  @netuitivedAddr
end

.netuitivedPortObject (readonly)

Returns the value of attribute netuitivedPort.



5
6
7
# File 'lib/netuitive_ruby_api/config_manager.rb', line 5

def netuitivedPort
  @netuitivedPort
end

.sample_cache_enabledObject (readonly)

Returns the value of attribute sample_cache_enabled.



7
8
9
# File 'lib/netuitive_ruby_api/config_manager.rb', line 7

def sample_cache_enabled
  @sample_cache_enabled
end

.sample_cache_intervalObject (readonly)

Returns the value of attribute sample_cache_interval.



9
10
11
# File 'lib/netuitive_ruby_api/config_manager.rb', line 9

def sample_cache_interval
  @sample_cache_interval
end

.sample_cache_sizeObject (readonly)

Returns the value of attribute sample_cache_size.



8
9
10
# File 'lib/netuitive_ruby_api/config_manager.rb', line 8

def sample_cache_size
  @sample_cache_size
end

Class Method Details

.boolean_property(name, var) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/netuitive_ruby_api/config_manager.rb', line 21

def boolean_property(name, var)
  prop = ENV[var].nil? ? nil : ENV[var].dup
  if prop.nil? || (prop == '')
    prop = data[name]
  else
    prop.strip!
    prop = prop.casecmp('true').zero?
  end
  prop
end

.float_property(name, var) ⇒ Object



32
33
34
# File 'lib/netuitive_ruby_api/config_manager.rb', line 32

def float_property(name, var)
  property(name, var).to_f
end

.int_property(name, var) ⇒ Object



36
37
38
# File 'lib/netuitive_ruby_api/config_manager.rb', line 36

def int_property(name, var)
  property(name, var).to_i
end

.load_configObject



52
53
54
55
# File 'lib/netuitive_ruby_api/config_manager.rb', line 52

def load_config
  gem_root = File.expand_path('../../..', __FILE__)
  @data = YAML.load_file "#{gem_root}/config/agent.yml"
end

.property(name, var, default = nil) ⇒ Object



14
15
16
17
18
19
# File 'lib/netuitive_ruby_api/config_manager.rb', line 14

def property(name, var, default = nil)
  prop = ENV[var]
  prop = data[name] if prop.nil? || (prop == '')
  return prop unless prop.nil? || (prop == '')
  default
end

.read_configObject



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
# File 'lib/netuitive_ruby_api/config_manager.rb', line 57

def read_config
  @sample_cache_enabled = boolean_property('sampleCacheEnabled', 'NETUITIVE_RUBY_SAMPLE_CACHE_ENABLED')
  @sample_cache_size = int_property('sampleCacheSize', 'NETUITIVE_RUBY_SAMPLE_CACHE_SIZE')
  @sample_cache_interval = int_property('sampleCacheInterval', 'NETUITIVE_RUBY_SAMPLE_CACHE_INTERVAL')
  @event_cache_enabled = boolean_property('eventCacheEnabled', 'NETUITIVE_RUBY_EVENT_CACHE_ENABLED')
  @event_cache_size = int_property('eventCacheSize', 'NETUITIVE_RUBY_SAMPLE_CACHE_SIZE')
  @event_cache_interval = int_property('eventCacheInterval', 'NETUITIVE_RUBY_SAMPLE_CACHE_INTERVAL')
  @netuitivedAddr = property('netuitivedAddr', 'NETUITIVE_RUBY_NETUITIVED_ADDR')
  @netuitivedPort = property('netuitivedPort', 'NETUITIVE_RUBY_NETUITIVED_PORT')
  debugLevelString = property('debugLevel', 'NETUITIVE_RUBY_DEBUG_LEVEL')
  NetuitiveRubyApi::NetuitiveLogger.log.level = if debugLevelString == 'error'
                                                  Logger::ERROR
                                                elsif debugLevelString == 'info'
                                                  Logger::INFO
                                                elsif debugLevelString == 'debug'
                                                  Logger::DEBUG
                                                else
                                                  Logger::ERROR
                                                end
  NetuitiveRubyApi::NetuitiveLogger.log.info "netuitived port: #{@netuitivedPort}"
  NetuitiveRubyApi::NetuitiveLogger.log.info "netuitived addr: #{@netuitivedAddr}"
  NetuitiveRubyApi::NetuitiveLogger.log.debug "read config file. Results:
    netuitivedAddr: #{@netuitivedAddr}
    netuitivedPort: #{@netuitivedPort}
    debugLevel: #{debugLevelString}
    sample_cache_enabled: #{@sample_cache_enabled}
    sample_cache_size: #{@sample_cache_size}
    sample_cache_interval: #{@sample_cache_interval}
    event_cache_enabled: #{@event_cache_enabled}
    event_cache_size: #{@event_cache_size}
    event_cache_interval: #{@event_cache_interval}"
end

.string_list_property(name, var) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/netuitive_ruby_api/config_manager.rb', line 40

def string_list_property(name, var)
  list = []
  prop = ENV[var].nil? ? nil : ENV[var].dup
  if prop.nil? || (prop == '')
    list = data[name] if !data[name].nil? && data[name].is_a?(Array)
  else
    list = prop.split(',')
  end
  list.each(&:strip!) unless list.empty?
  list
end