Skip to content
This repository has been archived by the owner on Mar 6, 2020. It is now read-only.

add auto-detection of client name and address #37

Merged
merged 4 commits into from
Jul 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions lib/sensu/settings/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ def initialize
self.class.create_category_methods
end

# 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.
#
# @return [Hash] default client settings
def client_defaults
{
:name => system_hostname,
:address => system_address
}
end

# Default settings.
#
# @return [Hash] settings.
Expand All @@ -47,6 +64,9 @@ def default_settings
CATEGORIES.each do |category|
default[category] = {}
end
if ["client", "rspec"].include?(sensu_service_name)
default[:client] = client_defaults
end
default
end

Expand Down Expand Up @@ -253,20 +273,19 @@ def load_redis_env
end

# Load Sensu client settings from the environment. This method
# loads client settings from several variables if
# `SENSU_CLIENT_NAME` is set: `SENSU_CLIENT_NAME`,
# `SENSU_CLIENT_ADDRESS`, and `SENSU_CLIENT_SUBSCRIPTIONS`. The
# Sensu client address defaults to the current system hostname
# and subscriptions defaults to an empty array.
# loads client settings from several variables:
# `SENSU_CLIENT_NAME`, `SENSU_CLIENT_ADDRESS`, and
# `SENSU_CLIENT_SUBSCRIPTIONS`.
#
# The client subscriptions defaults to an empty array.
def load_client_env
if ENV["SENSU_CLIENT_NAME"]
@settings[:client] ||= {}
@settings[:client][:name] = ENV["SENSU_CLIENT_NAME"]
@settings[:client][:address] = ENV.fetch("SENSU_CLIENT_ADDRESS", system_hostname)
@settings[:client][:subscriptions] = ENV.fetch("SENSU_CLIENT_SUBSCRIPTIONS", "").split(",")
@settings[:client][:name] = ENV["SENSU_CLIENT_NAME"] if ENV["SENSU_CLIENT_NAME"]
@settings[:client][:address] = ENV["SENSU_CLIENT_ADDRESS"] if ENV["SENSU_CLIENT_ADDRESS"]
@settings[:client][:subscriptions] = ENV.fetch("SENSU_CLIENT_SUBSCRIPTIONS", "").split(",")
if ENV.keys.any? {|k| k =~ /^SENSU_CLIENT/}
warning("using sensu client environment variables", :client => @settings[:client])
@indifferent_access = false
end
@indifferent_access = false
end

# Load Sensu API settings from the environment. This method sets
Expand Down Expand Up @@ -376,6 +395,17 @@ def system_hostname
Socket.gethostname rescue "unknown"
end

# Retrieve the system IP address. If a valid non-loopback
# IPv4 address cannot be found and an error is thrown,
# "unknown" will be returned.
#
# @return [String] system ip address
def system_address
Socket.ip_address_list.find { |address|
address.ipv4? && !address.ipv4_loopback?
}.ip_address rescue "unknown"
end

# Record a warning.
#
# @param message [String] warning message.
Expand Down
9 changes: 9 additions & 0 deletions spec/loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
expect(failures.size).to eq(0)
end

it "can load Sensu client settings with auto-detected defaults" do
@loader.load_env
expect(@loader.warnings.size).to eq(0)
expect(@loader.default_settings[:client][:name]).to be_kind_of(String)
expect(@loader.default_settings[:client][:address]).to be_kind_of(String)
end

it "can load Sensu transport settings from the environment" do
ENV["SENSU_TRANSPORT_NAME"] = "redis"
@loader.load_env
Expand Down Expand Up @@ -77,6 +84,8 @@
expect(client[:address]).to eq("127.0.0.1")
expect(client[:subscriptions]).to eq(["foo", "bar", "baz"])
ENV["SENSU_CLIENT_NAME"] = nil
ENV["SENSU_CLIENT_ADDRESS"] = nil
ENV["SENSU_CLIENT_SUBSCRIPTIONS"] = nil
end

it "can load Sensu API settings from the environment" do
Expand Down