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.



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

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.



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

def errors
  @errors
end

#loaded_filesObject (readonly)

Returns the value of attribute loaded_files.



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

def loaded_files
  @loaded_files
end

#warningsObject (readonly)

Returns the value of attribute warnings.



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

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().



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sensu/settings/loader.rb', line 83

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.



111
112
113
# File 'lib/sensu/settings/loader.rb', line 111

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



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

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

#default_settingsHash

Default settings.

Returns:

  • (Hash)

    settings.



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
# File 'lib/sensu/settings/loader.rb', line 53

def default_settings
  default = {
    :client => {},
    :sensu => {
      :spawn => {
        :limit => 12
      },
      :keepalives => {
        :thresholds => {
          :warning => 120,
          :critical => 180
        }
      }
    },
    :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

#hexdigestString

Create a SHA256 hex digest for the settings Hash object. The client definition scope is ignored when the current process is not a Sensu client, as it is essentially ignored and it will likely cause a sum mismatch between two Sensu service systems. This method will not recalculate the hex digest, unless the settings have been altered, determine by the values of ‘@hexdigest` and `@indifferent_access`.

Returns:

  • (String)

    SHA256 hex digest.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sensu/settings/loader.rb', line 124

def hexdigest
  if @hexdigest && @indifferent_access
    @hexdigest
  else
    hash = case sensu_service_name
    when "client", "rspec"
      to_hash
    else
      to_hash.reject do |key, value|
        key.to_s == "client"
      end
    end
    @hexdigest = Digest::SHA256.hexdigest(hash.to_s)
  end
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”.



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/sensu/settings/loader.rb', line 210

def load_client_overrides
  @settings[:client][:subscriptions] ||= []
  if @settings[:client][:subscriptions].is_a?(Array)
    @settings[:client][:subscriptions] << "client:#{@settings[:client][:name]}"
    @settings[:client][:subscriptions].uniq!
    warning("applied sensu client overrides", :client => @settings[:client])
    @indifferent_access = false
  else
    warning("unable to apply sensu client overrides", {
      :reason => "client subscriptions is not an array",
      :client => @settings[:client]
    })
  end
end

#load_directory(directory) ⇒ Object

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

Parameters:

  • directory (String)

    path.



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/sensu/settings/loader.rb', line 193

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


145
146
147
148
149
150
151
# File 'lib/sensu/settings/loader.rb', line 145

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.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/sensu/settings/loader.rb', line 158

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 = contents.empty? ? {} : 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.



227
228
229
# File 'lib/sensu/settings/loader.rb', line 227

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).



238
239
240
# File 'lib/sensu/settings/loader.rb', line 238

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

#to_hashHash

Access settings as an indifferent hash.

Returns:

  • (Hash)

    settings.



98
99
100
101
102
103
104
# File 'lib/sensu/settings/loader.rb', line 98

def to_hash
  unless @indifferent_access
    indifferent_access!
    @hexdigest = nil
  end
  @settings
end

#validateArray

Validate the loaded settings.

Returns:

  • (Array)

    validation failures.



245
246
247
248
# File 'lib/sensu/settings/loader.rb', line 245

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