From 60f9eecc0db5c6053a8baba9b593ed8d6daa7a26 Mon Sep 17 00:00:00 2001 From: Jean Rouge Date: Mon, 28 Sep 2015 15:24:30 -0700 Subject: [PATCH 1/3] Adding a datadog::consul recipe to monitor Consul --- recipes/consul.rb | 33 +++++++++++++++++++++++++++++++ templates/default/consul.yaml.erb | 16 +++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 recipes/consul.rb create mode 100644 templates/default/consul.yaml.erb diff --git a/recipes/consul.rb b/recipes/consul.rb new file mode 100644 index 00000000..e73453f2 --- /dev/null +++ b/recipes/consul.rb @@ -0,0 +1,33 @@ +include_recipe 'datadog::dd-agent' + +# Monitor consul +# +# Assuming you have a consul instance on a given server, you will need to set +# up the following attributes at some point in your Chef run, in either +# a role or another cookbook. +# +# Note that the Agent only supports monitoring one Consul instance +# +# A few explanatory words: +# - `catalog_checks`: Whether to perform checks against the Consul service Catalog +# - `new_leader_checks`: Whether to enable new leader checks from this agent +# Note: if this is set on multiple agents in the same cluster +# you will receive one event per leader change per agent +# - `service_whitelist`: Services to restrict catalog querying to +# The default settings query up to 50 services. So if you have more than +# this many in your Consul service catalog, you will want to fill in the +# whitelist +# +# node['datadog']['consul']['instances'] = [ +# { +# 'url' => 'http://localhost:8500', +# 'new_leader_checks' => false, +# 'catalog_checks' => false, +# 'service_whitelist' => [], +# 'tags' => [node.chef_environment] +# } +# ] + +datadog_monitor 'consul' do + instances node['datadog']['consul']['instances'] +end diff --git a/templates/default/consul.yaml.erb b/templates/default/consul.yaml.erb new file mode 100644 index 00000000..4d369ee1 --- /dev/null +++ b/templates/default/consul.yaml.erb @@ -0,0 +1,16 @@ +instances: +<% @instances.each do |i| -%> + - url: <%= i['url'] %> + <% unless i['new_leader_checks'].nil? %> + new_leader_checks: <%= i['new_leader_checks'] %> + <% end %> + <% unless i['catalog_checks'].nil? %> + catalog_checks: <%= i['catalog_checks'] %> + <% end %> + <% if i['service_whitelist'].nil? %> + service_whitelist: <%= i['service_whitelist'] %> + <% end %> +<% end -%> + +# Nothing to configure here +init_config: From b23a02098bc1552e07347d719cdae301f5b1b2b1 Mon Sep 17 00:00:00 2001 From: Darron Froese Date: Tue, 29 Sep 2015 14:56:18 -0600 Subject: [PATCH 2/3] Fix service_whitelist and add tags. --- templates/default/consul.yaml.erb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/templates/default/consul.yaml.erb b/templates/default/consul.yaml.erb index 4d369ee1..3bfb784e 100644 --- a/templates/default/consul.yaml.erb +++ b/templates/default/consul.yaml.erb @@ -7,9 +7,15 @@ instances: <% unless i['catalog_checks'].nil? %> catalog_checks: <%= i['catalog_checks'] %> <% end %> - <% if i['service_whitelist'].nil? %> + <% unless i['service_whitelist'].nil? %> service_whitelist: <%= i['service_whitelist'] %> <% end %> + <% if i.key?('tags') -%> + tags: + <% i['tags'].each do |t| -%> + - <%= t %> + <% end -%> + <% end -%> <% end -%> # Nothing to configure here From 520f540be9b656647406889a925d9bd536bf434f Mon Sep 17 00:00:00 2001 From: Darron Froese Date: Tue, 29 Sep 2015 14:56:38 -0600 Subject: [PATCH 3/3] Add Testkitchen test. --- .kitchen.yml | 14 ++++++++ .../datadog_consul/serverspec/Gemfile | 1 + .../datadog_consul/serverspec/consul_spec.rb | 36 +++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 120000 test/integration/datadog_consul/serverspec/Gemfile create mode 100644 test/integration/datadog_consul/serverspec/consul_spec.rb diff --git a/.kitchen.yml b/.kitchen.yml index a4efa261..0552d9ca 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -98,6 +98,20 @@ suites: - dskDevice - ifName +- name: datadog_consul + run_list: + - recipe[datadog::consul] + attributes: + datadog: + <<: *DATADOG + consul: + instances: + - url: http://localhost:8500 + new_leader_checks: false + catalog_checks: false + service_whitelist: consul + tags: ['_default'] + - name: datadog_cassandra run_list: - recipe[datadog::cassandra] diff --git a/test/integration/datadog_consul/serverspec/Gemfile b/test/integration/datadog_consul/serverspec/Gemfile new file mode 120000 index 00000000..74f9789f --- /dev/null +++ b/test/integration/datadog_consul/serverspec/Gemfile @@ -0,0 +1 @@ +../../helpers/serverspec/Gemfile \ No newline at end of file diff --git a/test/integration/datadog_consul/serverspec/consul_spec.rb b/test/integration/datadog_consul/serverspec/consul_spec.rb new file mode 100644 index 00000000..dc4bb9a4 --- /dev/null +++ b/test/integration/datadog_consul/serverspec/consul_spec.rb @@ -0,0 +1,36 @@ +# Encoding: utf-8 +require 'json_spec' +require 'serverspec' +require 'yaml' + +set :backend, :exec +set :path, '/sbin:/usr/local/sbin:$PATH' + +AGENT_CONFIG = '/etc/dd-agent/conf.d/consul.yaml' + +describe service('datadog-agent') do + it { should be_running } +end + +describe file(AGENT_CONFIG) do + it { should be_a_file } + + it 'is valid yaml matching input values' do + generated = YAML.load_file(AGENT_CONFIG) + + expected = { + 'instances' => [ + { + 'url' => 'http://localhost:8500', + 'new_leader_checks' => false, + 'catalog_checks' => false, + 'service_whitelist' => 'consul', + 'tags' => ['_default'] + } + ], + 'init_config' => nil + } + + expect(generated.to_json).to be_json_eql expected.to_json + end +end