Skip to content

Commit

Permalink
Merge pull request redhat-openstack#278 from bzed/process_processmatc…
Browse files Browse the repository at this point in the history
…h_concat

Use concat to define the process plugin config.
  • Loading branch information
blkperl committed Jun 29, 2015
2 parents 5d4b527 + 0596721 commit 27adfd3
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 59 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,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'],
Expand All @@ -671,6 +673,17 @@ class { 'collectd::plugin::protocols':
}
```

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.
Expand Down
32 changes: 32 additions & 0 deletions lib/puppet/parser/functions/collectd_convert_processmatch.rb
Original file line number Diff line number Diff line change
@@ -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

41 changes: 40 additions & 1 deletion manifests/plugin/processes.pp
Original file line number Diff line number Diff line change
@@ -1,16 +1,55 @@
# 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,
) {
if $processes { validate_array($processes) }
if $process_matches { validate_array($process_matches) }

include collectd::params

collectd::plugin {'processes':
ensure => $ensure,
content => template('collectd/plugin/processes.conf.erb'),
order => $order,
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 => '<Plugin processes>',
target => "${collectd::params::plugin_conf_dir}/processes-config.conf",
}
concat::fragment{'collectd_plugin_processes_conf_footer':
ensure => $ensure,
order => '99',
content => '</Plugin>',
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
)
}

}
16 changes: 16 additions & 0 deletions manifests/plugin/processes/process.pp
Original file line number Diff line number Diff line change
@@ -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"
}

}
17 changes: 17 additions & 0 deletions manifests/plugin/processes/processmatch.pp
Original file line number Diff line number Diff line change
@@ -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"
}

}
67 changes: 31 additions & 36 deletions spec/classes/collectd_plugin_processes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,44 @@

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, 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
context ':ensure => present' do
context ':ensure => present and default parameters' do

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/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

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 => /<Plugin "processes">\n\s*Process "process1"\n\s*ProcessMatch "process-all" "process\[0-9\]"\n<\/Plugin>/,
})
end
end
it 'Will create /etc/collectd.d/conf.d/processes-config.conf' do
should contain_concat__fragment('collectd_plugin_processes_conf_header').with({
:content => /<Plugin processes>/,
:target => '/etc/collectd/conf.d/processes-config.conf',
:order => '00'
})
end

context ':ensure => absent' do
let :params do
{:ensure => 'absent'}
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

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

22 changes: 0 additions & 22 deletions templates/plugin/processes.conf.erb

This file was deleted.

0 comments on commit 27adfd3

Please sign in to comment.