diff --git a/README.md b/README.md index 2c9b2738a..a0829349f 100644 --- a/README.md +++ b/README.md @@ -332,7 +332,7 @@ class { 'collectd::plugin::entropy': ####Class: `collectd::plugin::exec` ```puppet -collectd::plugin::exec { +collectd::plugin::exec::cmd { 'dummy': user => nobody, group => nogroup, diff --git a/manifests/plugin/exec.pp b/manifests/plugin/exec.pp index f9317db79..d3fa4eb87 100644 --- a/manifests/plugin/exec.pp +++ b/manifests/plugin/exec.pp @@ -1,35 +1,44 @@ # See http://collectd.org/documentation/manpages/collectd.conf.5.shtml#plugin_exec -define collectd::plugin::exec ( - $user, - $group, - $exec = [], - $notification_exec = [], - $ensure = present, - $order = '10', +class collectd::plugin::exec ( + $commands = {}, + $interval = undef, + $ensure = present, + $globals = false, ) { include collectd::params - validate_array($exec) - validate_array($notification_exec) + validate_hash($commands) + validate_bool($globals) - $conf_dir = $collectd::params::plugin_conf_dir + collectd::plugin {'exec': + ensure => $ensure, + globals => $globals, + interval => $interval, + } + + # should be loaded after global plugin configuration + $exec_conf = "${collectd::params::plugin_conf_dir}/exec-config.conf" + + concat{ $exec_conf: + ensure => $ensure, + mode => '0640', + owner => 'root', + group => $collectd::params::root_group, + notify => Service['collectd'], + ensure_newline => true, + } - # This is deprecated file naming ensuring old style file removed, and should be removed in next major relese - file { "${name}.load-deprecated": - ensure => absent, - path => "${conf_dir}/${name}.conf", + concat::fragment{'collectd_plugin_exec_conf_header': + order => '00', + content => '', + target => $exec_conf, } - # End deprecation - file { - "${name}.load": - ensure => $ensure, - path => "${conf_dir}/${order}-${name}.conf", - owner => 'root', - group => $collectd::params::root_group, - mode => '0644', - content => template('collectd/exec.conf.erb'), - notify => Service['collectd'], + concat::fragment{'collectd_plugin_exec_conf_footer': + order => '99', + content => '', + target => $exec_conf, } -} + create_resources(collectd::plugin::exec::cmd, $commands) +} \ No newline at end of file diff --git a/manifests/plugin/exec/cmd.pp b/manifests/plugin/exec/cmd.pp new file mode 100644 index 000000000..cd44113fe --- /dev/null +++ b/manifests/plugin/exec/cmd.pp @@ -0,0 +1,29 @@ +define collectd::plugin::exec::cmd ( + $user, + $group, + $exec = [], + $notification_exec = [], + $ensure = present, +) { + include collectd::params + include collectd::plugin::exec + + validate_array($exec) + validate_array($notification_exec) + + $conf_dir = $collectd::params::plugin_conf_dir + + # This is deprecated file naming ensuring old style file removed, and should be removed in next major relese + file { "${name}.load-deprecated": + ensure => absent, + path => "${conf_dir}/${name}.conf", + } + # End deprecation + + concat::fragment{"collectd_plugin_exec_conf_${title}": + ensure => $ensure, + order => '50', # somewhere between header and footer + target => $collectd::plugin::exec::exec_conf, + content => template('collectd/plugin/exec/cmd.conf.erb'), + } +} diff --git a/spec/classes/collectd_plugin_exec_spec.rb b/spec/classes/collectd_plugin_exec_spec.rb new file mode 100644 index 000000000..d3cf96638 --- /dev/null +++ b/spec/classes/collectd_plugin_exec_spec.rb @@ -0,0 +1,78 @@ +require 'spec_helper' + +describe 'collectd::plugin::exec', :type => :class do + + let :facts do + { + :osfamily => 'Debian', + :concat_basedir => tmpfilename('collectd-exec'), + :id => 'root', + :kernel => 'Linux', + :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', + :collectd_version => '5.0' + } + end + + context 'single command' do + let :params do + { + :commands => { 'hello' => + {'user' => 'nobody', 'group' => 'users', 'exec' => ['/bin/echo', 'hello world']} + }, + } + end + + it 'Will create /etc/collectd.d/conf.d/exec-config.conf' do + should contain_concat__fragment('collectd_plugin_exec_conf_header').with({ + :content => //, + :target => '/etc/collectd/conf.d/exec-config.conf', + :order => '00' + }) + end + + it 'Will create /etc/collectd.d/conf.d/exec-config' do + should contain_concat__fragment('collectd_plugin_exec_conf_footer').with({ + :content => /<\/Plugin>/, + :target => '/etc/collectd/conf.d/exec-config.conf', + :order => '99' + }) + end + + it 'includes exec statement' do + should contain_concat__fragment('collectd_plugin_exec_conf_hello').with({ + :content => /Exec \"nobody:users\" \"\/bin\/echo\" \"hello world\"/, + :target => '/etc/collectd/conf.d/exec-config.conf', + }) + end + end + + context 'multiple commands' do + let :params do + { + :commands => { + 'hello' => { 'user' => 'nobody', 'group' => 'users', + 'exec' => ['/bin/echo', 'hello world'] + }, + 'my_date' => { 'user' => 'nobody', 'group' => 'users', + 'exec' => ['/bin/date'] + } + }, + } + end + + it 'includes echo statement' do + should contain_concat__fragment('collectd_plugin_exec_conf_hello').with({ + :content => /Exec \"nobody:users\" \"\/bin\/echo\" \"hello world\"/, + :target => '/etc/collectd/conf.d/exec-config.conf', + }) + end + + it 'includes date statement' do + should contain_concat__fragment('collectd_plugin_exec_conf_my_date').with({ + :content => /Exec \"nobody:users\" \"\/bin\/date\"/, + :target => '/etc/collectd/conf.d/exec-config.conf', + }) + end + end + +end \ No newline at end of file diff --git a/spec/defines/collectd_plugin_exec_cmd_spec.rb b/spec/defines/collectd_plugin_exec_cmd_spec.rb new file mode 100644 index 000000000..c324f8eab --- /dev/null +++ b/spec/defines/collectd_plugin_exec_cmd_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe 'collectd::plugin::exec::cmd', :type => :define do + let :facts do + { + :osfamily => 'Debian', + :id => 'root', + :concat_basedir => tmpfilename('collectd-exec'), + :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', + } + end + + context 'define a command' do + let(:title) { 'whoami' } + let :params do + { + :user => 'www-data', + :group => 'users', + :exec => ['whoami', '--help'] + } + end + + it 'executes whoami command' do + should contain_concat__fragment('collectd_plugin_exec_conf_whoami').with({ + :content => /Exec/, + :target => '/etc/collectd/conf.d/exec-config.conf', + }) + end + end + +end \ No newline at end of file diff --git a/templates/exec.conf.erb b/templates/exec.conf.erb deleted file mode 100644 index 086d75656..000000000 --- a/templates/exec.conf.erb +++ /dev/null @@ -1,11 +0,0 @@ -# Generated by Puppet -LoadPlugin "exec" - - -<% if @exec %> - Exec "<%= @user %>:<%= @group %>" <% @exec.each do |exec| -%>"<%= exec %>"<% end -%> -<% end %> -<% if !@notification_exec.empty? %> - NotificationExec "<%= @user %>:<%= @group %>" <% @notification_exec.each do |exec| -%>"<%= exec %>"<% end -%> -<% end %> - diff --git a/templates/plugin/exec/cmd.conf.erb b/templates/plugin/exec/cmd.conf.erb new file mode 100644 index 000000000..fcd7291ab --- /dev/null +++ b/templates/plugin/exec/cmd.conf.erb @@ -0,0 +1,7 @@ +<% if @exec %> + Exec "<%= @user %>:<%= @group %>" "<%= @exec.join('" "') %>" +<% end %> +<% if !@notification_exec.empty? %> + NotificationExec "<%= @user %>:<%= @group %>" <% @notification_exec.each do |exec| -%>"<%= exec %>"<% end -%> +<% end %> +