Skip to content

Commit 8661f48

Browse files
aepodtruthbk
authored andcommitted
Added Tcp_check agent (#286)
* Added tcp_check - This allows for the use of http://docs.datadoghq.com/integrations/tcpcheck/ * Fixed typo in pp file * Fixed issue with bad quotes in comment and variable name issue * Fixed issue with spec test * more spec file fixes * spec test updated to handle tags better
1 parent 3ddb8b4 commit 8661f48

3 files changed

Lines changed: 256 additions & 0 deletions

File tree

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
# Class: datadog_agent::integrations::tcp_check
2+
#
3+
# This class will install the necessary config to hook the tcp_check in the agent
4+
#
5+
# Parameters:
6+
# name
7+
# (Required) - Name of the service.
8+
# This will be included as a tag: instance:<name>.
9+
#
10+
# host
11+
# (Required) - Host to be checked.
12+
# This will be included as a tag: url:<host>:<port>.
13+
#
14+
# port
15+
# (Required) - Port to be checked.
16+
# This will be included as a tag: url:<host>:<port>.
17+
#
18+
# timeout
19+
# (Optional) - Timeout for the check. Defaults to 10 seconds.
20+
#
21+
# threshold
22+
# (Optional) - Used in conjunction with window. An alert will
23+
# trigger if the check fails <threshold> times in <window> attempts.
24+
#
25+
# window
26+
# (Optional) - Refer to threshold.
27+
#
28+
# collect_response_time
29+
# (Optional) - Defaults to false. If this is not set to true, no
30+
# response time metric will be collected. If it is set to true, the
31+
# metric returned is network.tcp.response_time.
32+
#
33+
# skip_event
34+
# The (optional) skip_event parameter will instruct the check to not
35+
# create any event to avoid duplicates with a server side service check.
36+
# This default to False.
37+
#
38+
# tags
39+
# The (optional) tags to add to the check instance.
40+
#
41+
# Sample Usage:
42+
#
43+
# Add a class for each check instance:
44+
#
45+
# class { 'datadog_agent::integrations::tcp_check':
46+
# name => 'localhost-ftp',
47+
# host => 'ftp.example.com',
48+
# port => '21',
49+
# }
50+
#
51+
# class { 'datadog_agent::integrations::tcp_check':
52+
# name => 'localhost-ssh',
53+
# host => '127.0.0.1',
54+
# port => '22',
55+
# threshold => 1,
56+
# window => 1,
57+
# tags => ['production', 'ssh access'],
58+
# }
59+
#
60+
# class { 'datadog_agent::integrations::tcp_check':
61+
# name => 'localhost-web-response',
62+
# host => '127.0.0.1',
63+
# port => '80',
64+
# timeout => '8',
65+
# threshold => 1,
66+
# window => 1,
67+
# collect_response_time => 1,
68+
# skip_event => 1,
69+
# tags => ['production', 'webserver response time'],
70+
# }
71+
#
72+
# Add multiple instances in one class declaration:
73+
#
74+
# class { 'datadog_agent::integrations::tcp_check':
75+
# instances => [{
76+
# 'name' => 'www.example.com-http',
77+
# 'host' => 'www.example.com',
78+
# 'port' => '80',
79+
# },
80+
# {
81+
# 'name' => 'www.example.com-https',
82+
# 'host' => 'www.example.com',
83+
# 'port' => '443',
84+
# }]
85+
# }
86+
87+
88+
class datadog_agent::integrations::tcp_check (
89+
$check_name = undef,
90+
$host = undef,
91+
$port = undef,
92+
$timeout = 10,
93+
$threshold = undef,
94+
$window = undef,
95+
$collect_response_time = undef,
96+
$skip_event = undef,
97+
$tags = [],
98+
$instances = undef,
99+
) inherits datadog_agent::params {
100+
include datadog_agent
101+
102+
if !$instances and $host {
103+
$_instances = [{
104+
'check_name' => $check_name,
105+
'host' => $host,
106+
'port' => $port,
107+
'timeout' => $timeout,
108+
'threshold' => $threshold,
109+
'window' => $window,
110+
'collect_response_time' => $collect_response_time,
111+
'skip_event' => $skip_event,
112+
'tags' => $tags,
113+
}]
114+
} elsif !$instances{
115+
$_instances = []
116+
} else {
117+
$_instances = $instances
118+
}
119+
120+
file { "${datadog_agent::params::conf_dir}/tcp_check.yaml":
121+
ensure => file,
122+
owner => $datadog_agent::params::dd_user,
123+
group => $datadog_agent::params::dd_group,
124+
mode => '0600',
125+
content => template('datadog_agent/agent-conf.d/tcp_check.yaml.erb'),
126+
require => Package[$datadog_agent::params::package_name],
127+
notify => Service[$datadog_agent::params::service_name]
128+
}
129+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
require 'spec_helper'
2+
3+
describe 'datadog_agent::integrations::tcp_check' do
4+
let(:facts) {{
5+
operatingsystem: 'Ubuntu',
6+
}}
7+
let(:conf_dir) { '/etc/dd-agent/conf.d' }
8+
let(:dd_user) { 'dd-agent' }
9+
let(:dd_group) { 'root' }
10+
let(:dd_package) { 'datadog-agent' }
11+
let(:dd_service) { 'datadog-agent' }
12+
let(:conf_file) { "#{conf_dir}/tcp_check.yaml" }
13+
14+
it { should compile.with_all_deps }
15+
it { should contain_file(conf_file).with(
16+
owner: dd_user,
17+
group: dd_group,
18+
mode: '0600',
19+
)}
20+
it { should contain_file(conf_file).that_requires("Package[#{dd_package}]") }
21+
it { should contain_file(conf_file).that_notifies("Service[#{dd_service}]") }
22+
23+
context 'with default parameters' do
24+
it { should contain_file(conf_file).without_content(%r{name: }) }
25+
it { should contain_file(conf_file).without_content(%r{host: }) }
26+
it { should contain_file(conf_file).without_content(%r{port: }) }
27+
it { should contain_file(conf_file).without_content(%r{timeout: 1}) }
28+
it { should contain_file(conf_file).without_content(%{threshold: }) }
29+
it { should contain_file(conf_file).without_content(%r{window: }) }
30+
it { should contain_file(conf_file).without_content(%r{collect_response_time: }) }
31+
it { should contain_file(conf_file).without_content(%r{skip_event: }) }
32+
it { should contain_file(conf_file).without_content(%r{tags: }) }
33+
end
34+
35+
context 'with parameters set' do
36+
let(:params) {{
37+
check_name: 'foo.bar.baz',
38+
host: 'foo.bar.baz',
39+
port: '80',
40+
timeout: 123,
41+
threshold: 456,
42+
window: 789,
43+
collect_response_time: true,
44+
skip_event: true,
45+
}}
46+
47+
it { should contain_file(conf_file).with_content(%r{name: foo.bar.baz}) }
48+
it { should contain_file(conf_file).with_content(%r{host: foo.bar.baz}) }
49+
it { should contain_file(conf_file).with_content(%r{port: 80}) }
50+
it { should contain_file(conf_file).with_content(%r{timeout: 123}) }
51+
it { should contain_file(conf_file).with_content(%r{threshold: 456}) }
52+
it { should contain_file(conf_file).with_content(%r{window: 789}) }
53+
it { should contain_file(conf_file).with_content(%r{collect_response_time: true}) }
54+
it { should contain_file(conf_file).with_content(%r{skip_event: true}) }
55+
end
56+
57+
context 'with tags parameter array' do
58+
let(:params) {{
59+
check_name: 'foo.bar.baz',
60+
host: 'foo.bar.baz',
61+
port: '80',
62+
tags: [ 'foo', 'bar', 'baz' ],
63+
}}
64+
it { should contain_file(conf_file).with_content(/tags:\s+- foo\s+- bar\s+- baz\s*?[^-]/m) }
65+
end
66+
67+
context 'with tags parameter empty values' do
68+
context 'mixed in with other tags' do
69+
let(:params) {{
70+
check_name: 'foo.bar.baz',
71+
host: 'foo.bar.baz',
72+
port: '80',
73+
tags: [ 'foo', '', 'baz' ]
74+
}}
75+
76+
it { should contain_file(conf_file).with_content(/tags:\s+- foo\s+- baz\s*?[^-]/m) }
77+
end
78+
79+
context 'single element array of an empty string' do
80+
let(:params) {{
81+
tags: [''],
82+
}}
83+
84+
skip("undefined behavior")
85+
end
86+
87+
context 'single value empty string' do
88+
let(:params) {{
89+
tags: '',
90+
}}
91+
92+
skip("doubly undefined behavior")
93+
end
94+
end
95+
96+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
init_config:
2+
3+
instances:
4+
<%- (Array(@_instances)).each do |instance| -%>
5+
- name: <%= instance['check_name'] %>
6+
host: <%= instance['host'] %>
7+
port: <%= instance['port'] %>
8+
<% if instance['timeout'] -%>
9+
timeout: <%= instance['timeout'] %>
10+
<% end -%>
11+
<% if instance['threshold'] -%>
12+
threshold: <%= instance['threshold'] %>
13+
<% end -%>
14+
<% if instance['window'] -%>
15+
window: <%= instance['window'] %>
16+
<% end -%>
17+
<% if instance['collect_response_time'] -%>
18+
collect_response_time: <%= instance['collect_response_time'] %>
19+
<% end -%>
20+
<% if instance['skip_event'] -%>
21+
skip_event: <%= instance['skip_event'] %>
22+
<% end -%>
23+
<% if instance['tags'] and ! instance['tags'].empty? -%>
24+
tags:
25+
<%- Array(instance['tags']).each do |tag| -%>
26+
<%- if tag != '' -%>
27+
- <%= tag %>
28+
<%- end -%>
29+
<%- end -%>
30+
<% end -%>
31+
<% end -%>

0 commit comments

Comments
 (0)