Skip to content
This repository was archived by the owner on Sep 29, 2020. It is now read-only.

Commit d39c239

Browse files
committed
Merge pull request voxpupuli#287 from deric/exec
support definition of multiple exec commands
2 parents 125b28c + c660407 commit d39c239

File tree

7 files changed

+197
-37
lines changed

7 files changed

+197
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ class { 'collectd::plugin::entropy':
332332
####Class: `collectd::plugin::exec`
333333

334334
```puppet
335-
collectd::plugin::exec {
335+
collectd::plugin::exec::cmd {
336336
'dummy':
337337
user => nobody,
338338
group => nogroup,

manifests/plugin/exec.pp

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
11
# See http://collectd.org/documentation/manpages/collectd.conf.5.shtml#plugin_exec
2-
define collectd::plugin::exec (
3-
$user,
4-
$group,
5-
$exec = [],
6-
$notification_exec = [],
7-
$ensure = present,
8-
$order = '10',
2+
class collectd::plugin::exec (
3+
$commands = {},
4+
$interval = undef,
5+
$ensure = present,
6+
$globals = false,
97
) {
108
include collectd::params
119

12-
validate_array($exec)
13-
validate_array($notification_exec)
10+
validate_hash($commands)
11+
validate_bool($globals)
1412

15-
$conf_dir = $collectd::params::plugin_conf_dir
13+
collectd::plugin {'exec':
14+
ensure => $ensure,
15+
globals => $globals,
16+
interval => $interval,
17+
}
18+
19+
# should be loaded after global plugin configuration
20+
$exec_conf = "${collectd::params::plugin_conf_dir}/exec-config.conf"
21+
22+
concat{ $exec_conf:
23+
ensure => $ensure,
24+
mode => '0640',
25+
owner => 'root',
26+
group => $collectd::params::root_group,
27+
notify => Service['collectd'],
28+
ensure_newline => true,
29+
}
1630

17-
# This is deprecated file naming ensuring old style file removed, and should be removed in next major relese
18-
file { "${name}.load-deprecated":
19-
ensure => absent,
20-
path => "${conf_dir}/${name}.conf",
31+
concat::fragment{'collectd_plugin_exec_conf_header':
32+
order => '00',
33+
content => '<Plugin exec>',
34+
target => $exec_conf,
2135
}
22-
# End deprecation
2336

24-
file {
25-
"${name}.load":
26-
ensure => $ensure,
27-
path => "${conf_dir}/${order}-${name}.conf",
28-
owner => 'root',
29-
group => $collectd::params::root_group,
30-
mode => '0644',
31-
content => template('collectd/exec.conf.erb'),
32-
notify => Service['collectd'],
37+
concat::fragment{'collectd_plugin_exec_conf_footer':
38+
order => '99',
39+
content => '</Plugin>',
40+
target => $exec_conf,
3341
}
3442

35-
}
43+
create_resources(collectd::plugin::exec::cmd, $commands)
44+
}

manifests/plugin/exec/cmd.pp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
define collectd::plugin::exec::cmd (
2+
$user,
3+
$group,
4+
$exec = [],
5+
$notification_exec = [],
6+
$ensure = present,
7+
) {
8+
include collectd::params
9+
include collectd::plugin::exec
10+
11+
validate_array($exec)
12+
validate_array($notification_exec)
13+
14+
$conf_dir = $collectd::params::plugin_conf_dir
15+
16+
# This is deprecated file naming ensuring old style file removed, and should be removed in next major relese
17+
file { "${name}.load-deprecated":
18+
ensure => absent,
19+
path => "${conf_dir}/${name}.conf",
20+
}
21+
# End deprecation
22+
23+
concat::fragment{"collectd_plugin_exec_conf_${title}":
24+
ensure => $ensure,
25+
order => '50', # somewhere between header and footer
26+
target => $collectd::plugin::exec::exec_conf,
27+
content => template('collectd/plugin/exec/cmd.conf.erb'),
28+
}
29+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
require 'spec_helper'
2+
3+
describe 'collectd::plugin::exec', :type => :class do
4+
5+
let :facts do
6+
{
7+
:osfamily => 'Debian',
8+
:concat_basedir => tmpfilename('collectd-exec'),
9+
:id => 'root',
10+
:kernel => 'Linux',
11+
:path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
12+
:collectd_version => '5.0'
13+
}
14+
end
15+
16+
context 'single command' do
17+
let :params do
18+
{
19+
:commands => { 'hello' =>
20+
{'user' => 'nobody', 'group' => 'users', 'exec' => ['/bin/echo', 'hello world']}
21+
},
22+
}
23+
end
24+
25+
it 'Will create /etc/collectd.d/conf.d/exec-config.conf' do
26+
should contain_concat__fragment('collectd_plugin_exec_conf_header').with({
27+
:content => /<Plugin exec>/,
28+
:target => '/etc/collectd/conf.d/exec-config.conf',
29+
:order => '00'
30+
})
31+
end
32+
33+
it 'Will create /etc/collectd.d/conf.d/exec-config' do
34+
should contain_concat__fragment('collectd_plugin_exec_conf_footer').with({
35+
:content => /<\/Plugin>/,
36+
:target => '/etc/collectd/conf.d/exec-config.conf',
37+
:order => '99'
38+
})
39+
end
40+
41+
it 'includes exec statement' do
42+
should contain_concat__fragment('collectd_plugin_exec_conf_hello').with({
43+
:content => /Exec \"nobody:users\" \"\/bin\/echo\" \"hello world\"/,
44+
:target => '/etc/collectd/conf.d/exec-config.conf',
45+
})
46+
end
47+
end
48+
49+
context 'multiple commands' do
50+
let :params do
51+
{
52+
:commands => {
53+
'hello' => { 'user' => 'nobody', 'group' => 'users',
54+
'exec' => ['/bin/echo', 'hello world']
55+
},
56+
'my_date' => { 'user' => 'nobody', 'group' => 'users',
57+
'exec' => ['/bin/date']
58+
}
59+
},
60+
}
61+
end
62+
63+
it 'includes echo statement' do
64+
should contain_concat__fragment('collectd_plugin_exec_conf_hello').with({
65+
:content => /Exec \"nobody:users\" \"\/bin\/echo\" \"hello world\"/,
66+
:target => '/etc/collectd/conf.d/exec-config.conf',
67+
})
68+
end
69+
70+
it 'includes date statement' do
71+
should contain_concat__fragment('collectd_plugin_exec_conf_my_date').with({
72+
:content => /Exec \"nobody:users\" \"\/bin\/date\"/,
73+
:target => '/etc/collectd/conf.d/exec-config.conf',
74+
})
75+
end
76+
end
77+
78+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'spec_helper'
2+
3+
describe 'collectd::plugin::exec::cmd', :type => :define do
4+
let :facts do
5+
{
6+
:osfamily => 'Debian',
7+
:id => 'root',
8+
:concat_basedir => tmpfilename('collectd-exec'),
9+
:path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
10+
}
11+
end
12+
13+
context 'define a command' do
14+
let(:title) { 'whoami' }
15+
let :params do
16+
{
17+
:user => 'www-data',
18+
:group => 'users',
19+
:exec => ['whoami', '--help']
20+
}
21+
end
22+
23+
it 'executes whoami command' do
24+
should contain_concat__fragment('collectd_plugin_exec_conf_whoami').with({
25+
:content => /Exec "www-data:users" "whoami" "--help"/,
26+
:target => '/etc/collectd/conf.d/exec-config.conf',
27+
})
28+
end
29+
end
30+
31+
context 'define a notification' do
32+
let(:title) { 'whoami' }
33+
let :params do
34+
{
35+
:user => 'www-data',
36+
:group => 'users',
37+
:notification_exec => ['whoami', '--help']
38+
}
39+
end
40+
41+
it 'executes whoami command' do
42+
should contain_concat__fragment('collectd_plugin_exec_conf_whoami').with({
43+
:content => /NotificationExec "www-data:users" "whoami" "--help"/,
44+
:target => '/etc/collectd/conf.d/exec-config.conf',
45+
})
46+
end
47+
end
48+
49+
end

templates/exec.conf.erb

Lines changed: 0 additions & 11 deletions
This file was deleted.

templates/plugin/exec/cmd.conf.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<%- unless @exec.empty? -%>
2+
Exec "<%= @user %>:<%= @group %>" "<%= @exec.join('" "') %>"
3+
<%- end -%>
4+
<%- unless @notification_exec.empty? -%>
5+
NotificationExec "<%= @user %>:<%= @group %>" "<%= @notification_exec.join('" "') %>"
6+
<%- end -%>

0 commit comments

Comments
 (0)