Class: Sensu::Settings::Loader

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

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLoader

Returns a new instance of Loader.



23
24
25
26
27
28
29
30
# File 'lib/sensu/settings/loader.rb', line 23

def initialize
  @warnings = []
  @errors = []
  @settings = default_settings
  @indifferent_access = false
  @loaded_files = []
  self.class.create_category_methods
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



17
18
19
# File 'lib/sensu/settings/loader.rb', line 17

def errors
  @errors
end

#loaded_filesObject (readonly)

Returns the value of attribute loaded_files.



21
22
23
# File 'lib/sensu/settings/loader.rb', line 21

def loaded_files
  @loaded_files
end

#warningsObject (readonly)

Returns the value of attribute warnings.



13
14
15
# File 'lib/sensu/settings/loader.rb', line 13

def warnings
  @warnings
end

Class Method Details

.create_category_methodsObject

Create setting category accessors and methods to test the existence of definitions. Called in initialize().



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/sensu/settings/loader.rb', line 76

def self.create_category_methods
  CATEGORIES.each do |category|
    define_method(category) do
      setting_category(category)
    end
    method_name = category.to_s.chop + "_exists?"
    define_method(method_name.to_sym) do |name|
      definition_exists?(category, name)
    end
  end
end

Instance Method Details

#[](key) ⇒ Object

Retrieve the setting object corresponding to a key, acting like a Hash object.

Parameters:

  • key (String, Symbol)

Returns:

  • (Object)

    value for key.



103
104
105
# File 'lib/sensu/settings/loader.rb', line 103

def [](key)
  to_hash[key]
end

#client_defaultsHash

Auto-detected defaults for client definition

Client name defaults to system hostname. Client address defaults to first detected non-loopback ipv4 address.

Client subscriptions are intentionally omitted here as sensu-client will provide defaults using client name after final settings are loaded.

Returns:

  • (Hash)

    default client settings



42
43
44
45
46
47
# File 'lib/sensu/settings/loader.rb', line 42

def client_defaults
  {
    :name => system_hostname,
    :address => system_address
  }
end

#default_settingsHash

Default settings.

Returns:

  • (Hash)

    settings.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sensu/settings/loader.rb', line 52

def default_settings
  default = {
    :client => {},
    :sensu => {
      :spawn => {
        :limit => 12
      }
    },
    :transport => {
      :name => "rabbitmq",
      :reconnect_on_error => true
    }
  }
  CATEGORIES.each do |category|
    default[category] = {}
  end
  if ["client", "rspec"].include?(sensu_service_name)
    default[:client] = client_defaults
  end
  default
end

#load_client_overridesObject

Load Sensu client settings overrides. This method adds any overrides to the client definition. Overrides include:

  • Ensuring client subscriptions include a single subscription based on the

client name, e.g “client:i-424242”.



177
178
179
180
181
182
# File 'lib/sensu/settings/loader.rb', line 177

def load_client_overrides
  @settings[:client][:subscriptions] ||= []
  @settings[:client][:subscriptions] << "client:#{@settings[:client][:name]}"
  @settings[:client][:subscriptions].uniq!
  warning("applied sensu client overrides", :client => @settings[:client])
end

#load_directory(directory) ⇒ Object

Load settings from files in a directory. Files may be in nested directories.

Parameters:

  • directory (String)

    path.



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/sensu/settings/loader.rb', line 160

def load_directory(directory)
  warning("loading config files from directory", :directory => directory)
  path = directory.gsub(/\\(?=\S)/, "/")
  if File.readable?(path) && File.executable?(path)
    Dir.glob(File.join(path, "**{,/*/**}/*.json")).uniq.each do |file|
      load_file(file)
    end
  else
    load_error("insufficient permissions for loading", :directory => directory)
  end
end

#load_envObject

Load settings from the environment.

Loads: SENSU_TRANSPORT_NAME, RABBITMQ_URL, REDIS_URL,

SENSU_CLIENT_NAME, SENSU_CLIENT_ADDRESS
SENSU_CLIENT_SUBSCRIPTIONS, SENSU_API_PORT


112
113
114
115
116
117
118
# File 'lib/sensu/settings/loader.rb', line 112

def load_env
  load_transport_env
  load_rabbitmq_env
  load_redis_env
  load_client_env
  load_api_env
end

#load_file(file, must_exist = true) ⇒ Object

Load settings from a JSON file.

Parameters:

  • file (String)

    path.

  • must_exist (TrueClass, FalseClass) (defaults to: true)

    if the file must exist and is readable.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/sensu/settings/loader.rb', line 125

def load_file(file, must_exist=true)
  if File.file?(file) && File.readable?(file)
    begin
      warning("loading config file", :file => file)
      contents = read_config_file(file)
      config = Sensu::JSON.load(contents)
      merged = deep_merge(@settings, config)
      unless @loaded_files.empty?
        changes = deep_diff(@settings, merged)
        warning("config file applied changes", {
          :file => file,
          :changes => changes
        })
      end
      @settings = merged
      @indifferent_access = false
      @loaded_files << file
    rescue Sensu::JSON::ParseError => error
      load_error("config file must be valid json", {
        :file => file,
        :error => error.to_s
      })
    end
  elsif must_exist
    load_error("config file does not exist or is not readable", :file => file)
  else
    warning("config file does not exist or is not readable", :file => file)
    warning("ignoring config file", :file => file)
  end
end

#load_overrides!Object

Load overrides, i.e. settings which should always be present. Examples include client settings overrides which ensure a per-client subscription.



186
187
188
# File 'lib/sensu/settings/loader.rb', line 186

def load_overrides!
  load_client_overrides if ["client", "rspec"].include?(sensu_service_name)
end

#set_env!Object

Set Sensu settings related environment variables. This method sets ‘SENSU_LOADED_TEMPFILE` to a new temporary file path, a file containing the colon delimited list of loaded configuration files (using `create_loaded_tempfile!()`. The environment variable `SENSU_CONFIG_FILES` has been removed, due to the exec ARG_MAX (E2BIG) error when spawning processes after loading many configuration files (e.g. > 2000).



197
198
199
# File 'lib/sensu/settings/loader.rb', line 197

def set_env!
  ENV["SENSU_LOADED_TEMPFILE"] = create_loaded_tempfile!
end

#to_hashHash

Access settings as an indifferent hash.

Returns:

  • (Hash)

    settings.



91
92
93
94
95
96
# File 'lib/sensu/settings/loader.rb', line 91

def to_hash
  unless @indifferent_access
    indifferent_access!
  end
  @settings
end

#validateArray

Validate the loaded settings.

Returns:

  • (Array)

    validation failures.



204
205
206
207
# File 'lib/sensu/settings/loader.rb', line 204

def validate
  validator = Validator.new
  @errors += validator.run(@settings, sensu_service_name)
end