From 32ca3296c792004d401d1fc5b26567840a74fc83 Mon Sep 17 00:00:00 2001 From: Zhengda Lu Date: Tue, 25 Feb 2025 15:14:28 -0500 Subject: [PATCH] update mongo configuration template --- manifests/integrations/mongo.pp | 27 ++++++++++++----- .../datadog_agent_integrations_mongo_spec.rb | 24 +++++++++++++++ templates/agent-conf.d/mongo.yaml.erb | 29 +++++++++++++++++++ 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/manifests/integrations/mongo.pp b/manifests/integrations/mongo.pp index ff87c3f9..85c4ba7d 100644 --- a/manifests/integrations/mongo.pp +++ b/manifests/integrations/mongo.pp @@ -5,16 +5,20 @@ # NOTE: In newer versions of the Datadog Agent, the ssl parameters will be deprecated in favor the tls variants # # Parameters: +# $hosts +# Array of hosts host (and optional port number) where the mongod instance is running +# $dbm +# Enable the Database Monitoring feature +# $database_autodiscovery +# Enable the Database Autodiscovery feature +# $reported_database_hostname +# Optional database hostname override the mongodb hostname detected by the Agent from mongodb admin command serverStatus # $additional_metrics # Optional array of additional metrics # $database # Optionally specify database to query. Defaults to 'admin' -# $host: -# The host mongo is running on. Defaults to '127.0.0.1' # $password # Optionally specify password for connection -# $port -# The port mongo is running on. Defaults to 27017 # $ssl # Optionally enable SSL for connection # $ssl_ca_certs @@ -37,6 +41,12 @@ # Optional array of tags # $username # Optionally specify username for connection +# $host: +# Deprecated use $hosts instead +# The host mongo is running on. Defaults to '127.0.0.1' +# $port +# Deprecated use $hosts instead +# The port mongo is running on. Defaults to 27017 # # Sample Usage (Older Agent Versions): # @@ -73,19 +83,20 @@ # { # 'additional_metrics' => ['top'], # 'database' => 'database_name', -# 'host' => 'localhost', +# 'hosts' => ['localhost:27017'], # 'password' => 'mongo_password', -# 'port' => '27017', # 'tls' => true, # 'tls_ca_file' => '/path/to/ca.pem', # 'tls_allow_invalid_certificates' => false, # 'tls_certificate_key_file' => '/path/to/combined.pem', # 'tags' => ['optional_tag1', 'optional_tag2'], # 'username' => 'mongo_username', +# 'dbm' => true, +# 'database_autodiscovery' => {'enabled' => true}, +# 'reported_database_hostname' => 'mymongodbhost', # }, # { -# 'host' => 'localhost', -# 'port' => '27018', +# 'hosts' => ['localhost:27017'], # 'tags' => [], # 'additional_metrics' => [], # 'collections' => [], diff --git a/spec/classes/datadog_agent_integrations_mongo_spec.rb b/spec/classes/datadog_agent_integrations_mongo_spec.rb index 99072dd0..528da0d2 100644 --- a/spec/classes/datadog_agent_integrations_mongo_spec.rb +++ b/spec/classes/datadog_agent_integrations_mongo_spec.rb @@ -27,6 +27,30 @@ it { is_expected.to contain_file(conf_file).without_content(%r{tags:}) } end + context 'with one mongo host defined in hosts' do + let(:params) do + { + servers: [ + { + 'hosts' => ['localhost:27017'], + 'username' => 'user', + 'password' => 'pass', + 'dbm' => true, + 'database_autodiscovery' => { 'enabled' => true }, + 'reported_database_hostname' => 'mongohost', + }, + ], + } + end + + it { is_expected.to contain_file(conf_file).with_content(%r{- hosts:\s+- localhost:27017}) } + it { is_expected.to contain_file(conf_file).with_content(%r{username: user}) } + it { is_expected.to contain_file(conf_file).with_content(%r{password: pass}) } + it { is_expected.to contain_file(conf_file).with_content(%r{dbm: true}) } + it { is_expected.to contain_file(conf_file).with_content(%r{database_autodiscovery:\s+enabled: true}) } + it { is_expected.to contain_file(conf_file).with_content(%r{reported_database_hostname: mongohost}) } + end + context 'with one mongo' do let(:params) do { diff --git a/templates/agent-conf.d/mongo.yaml.erb b/templates/agent-conf.d/mongo.yaml.erb index d8f843af..d1b74375 100644 --- a/templates/agent-conf.d/mongo.yaml.erb +++ b/templates/agent-conf.d/mongo.yaml.erb @@ -4,7 +4,24 @@ init_config: instances: <% @servers.each do |server| -%> + <% if !server['hosts'].nil? && server['hosts'].any? -%> + - hosts: + <%- server['hosts'].each do |host| -%> + - <%= host %> + <%- end -%> + <%- if !server['username'].nil? -%> + username: <%= server['username'] %> + <%- end -%> + <%- if !server['password'].nil? -%> + password: <%= server['password'] %> + <%- end -%> + <%- if !server['database'].nil? -%> + database: <%= server['database'] %> + <%- end -%> + <%- end -%> + <% if server['hosts'].nil? -%> - server: mongodb://<%= server['username'] %><%= ":" unless server['password'].nil? %><%= server['password'] %><%= "@" unless server['username'].nil? %><%= server['host'] %>:<%= server['port'] %>/<%= server['database'] %> + <%- end -%> <%- if !server['tags'].nil? && server['tags'].any? -%> tags: <%- server['tags'].each do |tag| -%> @@ -50,4 +67,16 @@ instances: - <%= collection %> <%- end -%> <%- end -%> + <%- if !server['dbm'].nil? -%> + dbm: <%= server['dbm'] %> + <%- end -%> + <%- if !server['database_autodiscovery'].nil? -%> + database_autodiscovery: + <%- if !server['database_autodiscovery']['enabled'].nil? -%> + enabled: <%= server['database_autodiscovery']['enabled'] %> + <%- end -%> + <%- end -%> + <%- if !server['reported_database_hostname'].nil? -%> + reported_database_hostname: <%= server['reported_database_hostname'] %> + <%- end -%> <% end -%>