Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New hostname_whitelist_regex configuration variable #355

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 14 additions & 2 deletions lib/puppet/reports/datadog_reports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
raise(Puppet::ParseError, "Invalid hostname_extraction_regex #{HOSTNAME_REGEX}")
end

unless config[:hostname_whitelist_regex].nil?
begin
HOSTNAME_WHITELIST_REGEX = Regexp.new config[:hostname_whitelist_regex]
rescue
raise(Puppet::ParseError, "Invalid hostname_whitelist_regex #{config[:hostname_whitelist_regex]}")
end
end

desc <<-DESC
Send notification of metrics to Datadog
DESC
Expand Down Expand Up @@ -53,6 +61,11 @@ def process
end
end

if !HOSTNAME_WHITELIST_REGEX.nil? && !@msg_host.match(HOSTNAME_WHITELIST_REGEX)
Puppet.info "Not reporting #{@msg_host} to Datadog (does not match white list)"
return
end

event_title = ''
alert_type = ''
event_priority = 'low'
Expand Down Expand Up @@ -106,7 +119,7 @@ def process
event_data << "\n@@@\n"
end

Puppet.debug "Sending metrics for #{@msg_host} to Datadog"
Puppet.info "Sending run info for #{@msg_host} to Datadog"
@dog = Dogapi::Client.new(API_KEY)
@dog.batch_metrics do
self.metrics.each { |metric,data|
Expand All @@ -118,7 +131,6 @@ def process
}
end

Puppet.debug "Sending events for #{@msg_host} to Datadog"
@dog.emit_event(Dogapi::Event.new(event_data,
:msg_title => event_title,
:event_type => 'config_management.run',
Expand Down
5 changes: 5 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
# Instead of reporting the puppet nodename, use this regex to extract the named
# 'hostname' captured group to report the run in Datadog.
# ex.: '^(?<hostname>.*\.datadoghq\.com)(\.i-\w{8}\..*)?$'
# $hostname_whitelist_regex
# Completely optional.
# Only report the host to Datadog if its name matches this regular expression.
# $log_to_syslog
# Set value of 'log_to_syslog' variable. Default is true -> yes as in dd-agent.
# Valid values here are: true or false.
Expand Down Expand Up @@ -198,6 +201,7 @@
$service_enable = true,
$manage_repo = true,
$hostname_extraction_regex = nil,
$hostname_whitelist_regex = nil,
$dogstatsd_port = 8125,
$statsd_forward_host = '',
$statsd_forward_port = '',
Expand Down Expand Up @@ -430,6 +434,7 @@
puppetmaster_user => $puppetmaster_user,
dogapi_version => $datadog_agent::params::dogapi_version,
hostname_extraction_regex => $hostname_extraction_regex,
hostname_whitelist_regex => $hostname_whitelist_regex,
}
}

Expand Down
3 changes: 2 additions & 1 deletion manifests/reports.pp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
$puppet_gem_provider,
$puppetmaster_user,
$dogapi_version,
$hostname_extraction_regex = nil
$hostname_extraction_regex = nil,
$hostname_whitelist_regex = nil
) {

include datadog_agent::params
Expand Down
2 changes: 2 additions & 0 deletions spec/classes/datadog_agent_reports_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{
api_key: 'notanapikey',
hostname_extraction_regex: nil,
hostname_whitelist_regex: nil,
puppetmaster_user: 'puppet',
dogapi_version: 'installed',
puppet_gem_provider: 'gem'
Expand Down Expand Up @@ -59,6 +60,7 @@
{
api_key: 'notanapikey',
hostname_extraction_regex: nil,
hostname_whitelist_regex: nil,
puppetmaster_user: 'puppet',
dogapi_version: '1.2.2',
puppet_gem_provider: 'gem'
Expand Down
3 changes: 3 additions & 0 deletions templates/datadog.yaml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
<% if !@hostname_extraction_regex.nil? -%>
:hostname_extraction_regex: '<%= @hostname_extraction_regex %>'
<% end -%>
<% if !@hostname_whitelist_regex.nil? -%>
:hostname_whitelist_regex: '<%= @hostname_whitelist_regex %>'
<% end -%>