From 6c101d5c4bcb9b354dba3a9ccd3f12ff1db67c37 Mon Sep 17 00:00:00 2001 From: Bernd Zeimetz Date: Thu, 18 Jun 2015 18:46:56 +0200 Subject: [PATCH 1/5] Use concat to define the process plugin config. Adding a parser function to keep it compatible with the old way to define process matches. --- .../collectd_convert_processmatch.rb | 32 ++++++++++++++++ manifests/plugin/processes.pp | 38 ++++++++++++++++++- manifests/plugin/processes/process.pp | 16 ++++++++ manifests/plugin/processes/processmatch.pp | 17 +++++++++ templates/plugin/processes.conf.erb | 22 ----------- 5 files changed, 101 insertions(+), 24 deletions(-) create mode 100644 lib/puppet/parser/functions/collectd_convert_processmatch.rb create mode 100644 manifests/plugin/processes/process.pp create mode 100644 manifests/plugin/processes/processmatch.pp delete mode 100644 templates/plugin/processes.conf.erb diff --git a/lib/puppet/parser/functions/collectd_convert_processmatch.rb b/lib/puppet/parser/functions/collectd_convert_processmatch.rb new file mode 100644 index 000000000..76dda7e3b --- /dev/null +++ b/lib/puppet/parser/functions/collectd_convert_processmatch.rb @@ -0,0 +1,32 @@ +module Puppet::Parser::Functions + + newfunction(:collectd_convert_processmatch, :type => :rvalue, :arity => 1, :doc => <<-ENDDOC + Converts the array from the old style to define process matches in the + processes plugin into a create_resources compatible hash which + can be used with the new style define. + + Example: + [ { 'name' => 'foo', 'regex' => '.*' } , { 'name' => 'bar', 'regex' => '[0-9]+' } ] + will be converted to + { 'foo' => { 'regex' => '.*' } , 'bar' => { 'regex' => '[0-9]+' } } + ENDDOC + ) do |args| + if args.size != 1 then + raise(Puppet::ParseError, "convert_process_match_array(): Needs exactly one argument") + end + + parray = args[0] + unless parray.is_a?(Array) + raise(Puppet::ParseError, "convert_process_match_array(): Needs an array as argument") + end + + phash = Hash.new + + parray.each do |p| + phash[p['name']] = { 'regex' => p['regex'] } + end + + return phash + end +end + diff --git a/manifests/plugin/processes.pp b/manifests/plugin/processes.pp index 113d6e69a..712ed3055 100644 --- a/manifests/plugin/processes.pp +++ b/manifests/plugin/processes.pp @@ -10,7 +10,41 @@ collectd::plugin {'processes': ensure => $ensure, - content => template('collectd/plugin/processes.conf.erb'), - interval => $interval, } + + concat{"${collectd::params::plugin_conf_dir}/processes-config.conf": + ensure => $ensure, + mode => '0640', + owner => 'root', + group => $collectd::params::root_group, + notify => Service['collectd'], + ensure_newline => true, + } + concat::fragment{'collectd_plugin_processes_conf_header': + ensure => $ensure, + order => '00', + content => '', + target => "${collectd::params::plugin_conf_dir}/processes-config.conf", + } + concat::fragment{'collectd_plugin_processes_conf_footer': + ensure => $ensure, + order => '99', + content => '', + target => "${collectd::params::plugin_conf_dir}/processes-config.conf", + } + + + if $processes { + collectd::plugin::processes::process { $processes : } + } + if $process_matches { + $process_matches_resources = collectd_convert_processmatch($process_matches) + $defaults = { 'ensure' => $ensure } + create_resources( + collectd::plugin::processes::processmatch, + $process_matches_resources, + $defaults + ) + } + } diff --git a/manifests/plugin/processes/process.pp b/manifests/plugin/processes/process.pp new file mode 100644 index 000000000..f2cac9035 --- /dev/null +++ b/manifests/plugin/processes/process.pp @@ -0,0 +1,16 @@ +define collectd::plugin::processes::process ( + $process = $name, + $ensure = 'present' +){ + + include collectd::plugin::processes + include collectd::params + + concat::fragment{"collectd_plugin_processes_conf_process_${process}": + ensure => $ensure, + order => '50', + content => "Process \"${process}\"\n", + target => "${collectd::params::plugin_conf_dir}/processes-config.conf" + } + +} diff --git a/manifests/plugin/processes/processmatch.pp b/manifests/plugin/processes/processmatch.pp new file mode 100644 index 000000000..d524091ea --- /dev/null +++ b/manifests/plugin/processes/processmatch.pp @@ -0,0 +1,17 @@ +define collectd::plugin::processes::processmatch ( + $regex, + $ensure = 'present', + $matchname = $name +){ + + include collectd::plugin::processes + include collectd::params + + concat::fragment{"collectd_plugin_processes_conf_processmatch_${matchname}": + ensure => $ensure, + order => '51', + content => "ProcessMatch \"${matchname}\" \"${regex}\"\n", + target => "${collectd::params::plugin_conf_dir}/processes-config.conf" + } + +} diff --git a/templates/plugin/processes.conf.erb b/templates/plugin/processes.conf.erb deleted file mode 100644 index b25c80470..000000000 --- a/templates/plugin/processes.conf.erb +++ /dev/null @@ -1,22 +0,0 @@ -<% if @processes or @process_matches -%> - -<% -if @processes - @processes.each do |process| --%> - Process "<%= process %>" -<% - end -end --%> -<% -if @process_matches - @process_matches.each do |match| --%> - ProcessMatch "<%= match['name'] %>" "<%= match['regex'] %>" -<% - end -end --%> - -<% end -%> From e3a221c402590e2dbe55451f0983f7730151aaa9 Mon Sep 17 00:00:00 2001 From: Bernd Zeimetz Date: Fri, 19 Jun 2015 10:20:56 +0200 Subject: [PATCH 2/5] Pass order/interval; Fix spec tests. --- manifests/plugin/processes.pp | 3 + .../classes/collectd_plugin_processes_spec.rb | 59 ++++++++----------- 2 files changed, 26 insertions(+), 36 deletions(-) diff --git a/manifests/plugin/processes.pp b/manifests/plugin/processes.pp index 712ed3055..168a4fef5 100644 --- a/manifests/plugin/processes.pp +++ b/manifests/plugin/processes.pp @@ -1,6 +1,7 @@ # See http://collectd.org/documentation/manpages/collectd.conf.5.shtml#plugin_processes class collectd::plugin::processes ( $ensure = present, + $order = 10, $interval = undef, $processes = undef, $process_matches = undef, @@ -10,6 +11,8 @@ collectd::plugin {'processes': ensure => $ensure, + order => $order, + interval => $interval, } concat{"${collectd::params::plugin_conf_dir}/processes-config.conf": diff --git a/spec/classes/collectd_plugin_processes_spec.rb b/spec/classes/collectd_plugin_processes_spec.rb index 74ffff7cf..4ec2b1c68 100644 --- a/spec/classes/collectd_plugin_processes_spec.rb +++ b/spec/classes/collectd_plugin_processes_spec.rb @@ -4,47 +4,34 @@ let :facts do {:osfamily => 'RedHat'} end + context ':ensure => present' do + context ':ensure => present and default parameters' do - context ':ensure => present, default params' do - it 'Will create /etc/collectd.d/10-processes.conf' do - should contain_file('processes.load').with({ - :ensure => 'present', - :path => '/etc/collectd.d/10-processes.conf', - :content => //, - }) - end - end + it 'Will create /etc/collectd/conf.d/10-processes.conf to load the plugin' do + should contain_file('processes.load').with({ + :ensure => 'present', + :path => '/etc/collectd/conf.d/10-processes.conf', + :content => /LoadPlugin processes/, + }) + end - context ':ensure => present, specific params' do - let :params do - { :processes => [ 'process1'], - :process_matches => [ - { 'name' => 'process-all', - 'regex' => 'process[0-9]' } - ], - } - end + it 'Will create /etc/collectd.d/conf.d/processes-config.conf' do + should contain_concat__fragment('collectd_plugin_processes_conf_header').with({ + :content => //, + :target => '/etc/collectd/conf.d/processes-config.conf', + :order => '00' + }) + end - it 'Will create /etc/collectd.d/10-processes.conf' do - should contain_file('processes.load').with({ - :ensure => 'present', - :path => '/etc/collectd.d/10-processes.conf', - :content => /\n\s*Process "process1"\n\s*ProcessMatch "process-all" "process\[0-9\]"\n<\/Plugin>/, - }) + it 'Will create /etc/collectd.d/conf.d/processes-config.conf' do + should contain_concat__fragment('collectd_plugin_processes_conf_footer').with({ + :content => /<\/Plugin>/, + :target => '/etc/collectd/conf.d/processes-config.conf', + :order => '99' + }) + end end - end - context ':ensure => absent' do - let :params do - {:ensure => 'absent'} - end - - it 'Will not create /etc/collectd.d/10-processes.conf' do - should contain_file('processes.load').with({ - :ensure => 'absent', - :path => '/etc/collectd.d/10-processes.conf', - }) - end end end From 2ed9e84114f6140d61b5678142708230604895e9 Mon Sep 17 00:00:00 2001 From: Bernd Zeimetz Date: Fri, 19 Jun 2015 10:36:08 +0200 Subject: [PATCH 3/5] Use Debian as osfamily instead of RedHat. --- spec/classes/collectd_plugin_processes_spec.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/spec/classes/collectd_plugin_processes_spec.rb b/spec/classes/collectd_plugin_processes_spec.rb index 4ec2b1c68..31fc8ecbf 100644 --- a/spec/classes/collectd_plugin_processes_spec.rb +++ b/spec/classes/collectd_plugin_processes_spec.rb @@ -2,8 +2,16 @@ describe 'collectd::plugin::processes', :type => :class do let :facts do - {:osfamily => 'RedHat'} + { + :osfamily => 'Debian', + :concat_basedir => tmpfilename('collectd-processes'), + :id => 'root', + :kernel => 'Linux', + :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', + :collectd_version => '5.0' + } end + context ':ensure => present' do context ':ensure => present and default parameters' do @@ -17,7 +25,7 @@ it 'Will create /etc/collectd.d/conf.d/processes-config.conf' do should contain_concat__fragment('collectd_plugin_processes_conf_header').with({ - :content => //, + :content => //, :target => '/etc/collectd/conf.d/processes-config.conf', :order => '00' }) From 211b3d586a6cb4d35181952f34278796714671bf Mon Sep 17 00:00:00 2001 From: Bernd Zeimetz Date: Sat, 27 Jun 2015 09:41:44 +0200 Subject: [PATCH 4/5] Add missing include collectd::params. Thanks @txaj --- manifests/plugin/processes.pp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/manifests/plugin/processes.pp b/manifests/plugin/processes.pp index 168a4fef5..9241f2bb6 100644 --- a/manifests/plugin/processes.pp +++ b/manifests/plugin/processes.pp @@ -9,6 +9,8 @@ if $processes { validate_array($processes) } if $process_matches { validate_array($process_matches) } + include collectd::params + collectd::plugin {'processes': ensure => $ensure, order => $order, From 05967216bc09bb8ce5ccef98b89a098c9eaf5e1f Mon Sep 17 00:00:00 2001 From: Bernd Zeimetz Date: Sun, 28 Jun 2015 09:29:49 +0200 Subject: [PATCH 5/5] Update documentation. --- README.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.md b/README.md index 7bb37bdb4..310e437ca 100644 --- a/README.md +++ b/README.md @@ -648,6 +648,8 @@ class { 'collectd::plugin::postgresql': ####Class: `collectd::plugin::processes` +You can either specify processes / process matches at once: + ```puppet class { 'collectd::plugin::processes': processes => ['process1', 'process2'], @@ -657,6 +659,17 @@ class { 'collectd::plugin::processes': } ``` +Or define single processes / process matches: +```puppet +collectd::plugin::processes::process { 'collectd' : } +``` + +```puppet +collectd::plugin::processes::processmatch { 'elasticsearch' : + regex => '.*java.*org.elasticsearch.bootstrap.Elasticsearch' +} +``` + ####Class: `collectd::plugin::python` * `modulepaths` is an array of paths where will be Collectd looking for Python modules, Puppet will ensure that each of specified directories exists and it is owned by `root` (and `chmod 0750`). If you don't specify any `modulepaths` a default value for given distribution will be used.