Skip to content

Commit

Permalink
add option in datadog_agent to extract hostnames
Browse files Browse the repository at this point in the history
Fixes #160.

This option is a string regex which can be enabled and used to extract
the hostname from puppet hostname strings with a capture group.
It is useful for people that have specific puppet hostnames and that
would like to sanitize them before reporting those in Datadog.
  • Loading branch information
LeoCavaille committed Mar 30, 2016
1 parent 1e679ce commit 15f8ef9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/puppet/reports/datadog_reports.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
config = YAML.load_file(configfile)
API_KEY = config[:datadog_api_key]

# if need be initialize the regex
HOSTNAME_EXTRACTION_REGEX = config[:hostname_extraction_regex]
begin
HOSTNAME_EXTRACTION_REGEX = Regexp.new HOSTNAME_EXTRACTION_REGEX unless HOSTNAME_EXTRACTION_REGEX.nil?
rescue
raise(Puppet::ParseError, "Invalid hostname_extraction_regex #{HOSTNAME_EXTRACTION_REGEX}")
end

desc <<-DESC
Send notification of metrics to Datadog
DESC
Expand All @@ -38,6 +46,12 @@ def pluralize(number, noun)
def process
@summary = self.summary
@msg_host = self.host
unless HOSTNAME_EXTRACTION_REGEX.nil?
m = @msg_host.match(HOSTNAME_EXTRACTION_REGEX)
unless m[:hostname].nil?
@msg_host = m[:hostname]
end
end

event_title = ''
alert_type = ''
Expand Down
7 changes: 7 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
# $log_level
# Set value of 'log_level' variable. Default is 'info' as in dd-agent.
# Valid values here are: critical, debug, error, fatal, info, warn and warning.
# $hostname_extraction_regex
# Completely optional.
# 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}\..*)?$'
# $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 @@ -103,6 +108,7 @@
$log_to_syslog = true,
$service_ensure = 'running',
$service_enable = true,
$hostname_extraction_regex = nil,
$use_mount = false,
$dogstatsd_port = 8125,
$statsd_forward_host = '',
Expand Down Expand Up @@ -203,6 +209,7 @@
class { 'datadog_agent::reports':
api_key => $api_key,
puppetmaster_user => $puppetmaster_user,
hostname_extraction_regex => $hostname_extraction_regex,
}
}

Expand Down
3 changes: 2 additions & 1 deletion manifests/reports.pp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
#
class datadog_agent::reports(
$api_key,
$puppetmaster_user
$puppetmaster_user,
$hostname_extraction_regex
) {

include datadog_agent::params
Expand Down
3 changes: 3 additions & 0 deletions templates/datadog.yaml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
#
---
:datadog_api_key: '<%= @api_key %>'
<% if @hostname_extraction_regex.nil? -%>
:hostname_extraction_regex: '<%= @hostname_extraction_regex %>'
<% end -%>

0 comments on commit 15f8ef9

Please sign in to comment.