Skip to content

Commit 2cf3b6d

Browse files
author
Fabien Wernli
committedJul 24, 2014
add support for perl plugins
1 parent c545678 commit 2cf3b6d

File tree

7 files changed

+274
-0
lines changed

7 files changed

+274
-0
lines changed
 

‎README.md

+69
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ documentation for each plugin for configurable attributes.
8181
* `nginx` (see [collectd::plugin::nginx](#class-collectdpluginnginx) below)
8282
* `ntpd` (see [collectd::plugin::ntpd](#class-collectdpluginntpd) below)
8383
* `openvpn` (see [collectd::plugin::openvpn](#class-collectdpluginopenvpn) below)
84+
* `perl` (see [collectd::plugin::perl](#class-collectdpluginperl) below)
8485
* `ping` (see [collectd::plugin::ping](#class-collectdpluginping) below)
8586
* `postgresql` (see [collectd::plugin::postgresql](#class-collectdpluginpostgresql) below)
8687
* `processes` (see [collectd::plugin:processes](#class-collectdpluginprocesses) below)
@@ -383,6 +384,74 @@ class { 'collectd::plugin::openvpn':
383384
}
384385
```
385386

387+
####Class: `collectd::plugin::perl`
388+
389+
This class has no parameters and will load the actual perl plugin.
390+
It will be automatically included if any `perl::plugin` is defined.
391+
392+
#####Example:
393+
```puppet
394+
include collectd::plugin::perl
395+
```
396+
397+
####Define: `collectd::plugin::perl::plugin`
398+
399+
This define will load a new perl plugin.
400+
401+
#####Parameters:
402+
403+
* `module` (String): name of perl module to load (mandatory)
404+
* `enable_debugger` (False or String): whether to load the perl debugger. See *collectd-perl* manpage for more details.
405+
* `include_dir` (String or Array): directories to add to *@INC*
406+
* `provider` (`"package"`,`"cpan"`,`"file"` or `false`): method to get the plugin code
407+
* `source` (String): this parameter is consumed by the provider to infer the source of the plugin code
408+
* `destination (String or false): path to plugin code if `provider` is `file`. Ignored otherwise.
409+
* `order` (String containing numbers): order in which the plugin should be loaded. Defaults to `"00"`
410+
* `config` (Hash): plugin configuration in form of a hash. This will be converted to a suitable structure understood by *liboconfig* which is the *collectd* configuration parser. Defaults to `{}`
411+
412+
#####Examples:
413+
414+
######Using a preinstalled plugin:
415+
```puppet
416+
collectd::plugin::perl::plugin { 'foo':
417+
module => 'Collectd::Plugins::Foo',
418+
enable_debugger => "",
419+
include_dir => '/usr/lib/collectd/perl5/lib',
420+
}
421+
```
422+
423+
######Using a plugin from a file from *source*:
424+
```puppet
425+
collectd::plugin::perl::plugin { 'baz':
426+
module => 'Collectd::Plugins::Baz',
427+
provider => 'file',
428+
source => 'puppet:///modules/myorg/baz_collectd.pm',
429+
destination => '/path/to/my/perl5/modules'
430+
}
431+
```
432+
433+
######Using a plugin from cpan (requires the [puppet cpan module](https://forge.puppetlabs.com/meltwater/cpan)):
434+
```puppet
435+
collectd::plugin::perl::plugin {
436+
'openafs_vos':
437+
module => 'Collectd::Plugins::OpenAFS::VOS',
438+
provider => 'cpan',
439+
source => 'Collectd::Plugins::OpenAFS',
440+
config => {'VosBin' => '/usr/afsws/etc/vos'},
441+
}
442+
```
443+
444+
######Using a plugin from package source:
445+
```puppet
446+
collectd::plugin::perl::plugin {
447+
'bar':
448+
module => 'Collectd::Plugins::Bar',
449+
provider => 'package',
450+
source => 'perl-Collectd-Plugins-Bar',
451+
config => {'foo' => 'bar'},
452+
}
453+
```
454+
386455
####Class: `collectd::plugin::ping`
387456

388457
```puppet

‎files/tests/Foo.pm

Whitespace-only changes.

‎manifests/plugin/perl.pp

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# See http://collectd.org/documentation/manpages/collectd-perl.5.shtml
2+
class collectd::plugin::perl ()
3+
{
4+
include collectd::params
5+
$conf_dir = $collectd::params::plugin_conf_dir
6+
$filename = "${conf_dir}/perl.conf"
7+
file { $filename:
8+
mode => '0644',
9+
owner => $collectd::params::root_user,
10+
group => $collectd::params::root_group,
11+
notify => Service['collectd'],
12+
content => template('collectd/plugin/perl.conf.erb'),
13+
}
14+
file { "${conf_dir}/perl":
15+
ensure => directory,
16+
mode => '0755',
17+
owner => $collectd::params::root_user,
18+
group => $collectd::params::root_group,
19+
}
20+
}
21+

‎manifests/plugin/perl/plugin.pp

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#
2+
define collectd::plugin::perl::plugin (
3+
$module,
4+
$enable_debugger = false,
5+
$include_dir = false,
6+
$provider = false,
7+
$source = false,
8+
$destination = false,
9+
$order = '01',
10+
$config = {},
11+
) {
12+
include collectd::params
13+
if ! defined(Class['Collectd::Plugin::Perl']) {
14+
include collectd::plugin::perl
15+
}
16+
17+
validate_hash($config)
18+
validate_re($order, '\d+')
19+
if $enable_debugger {
20+
validate_string($enable_debugger)
21+
}
22+
if $include_dir {
23+
if is_string($include_dir) {
24+
$include_dirs = [ $include_dir ]
25+
} elsif is_array($include_dir) {
26+
$include_dirs = $include_dir
27+
} else {
28+
fail("include_dir must be either array or string: ${include_dir}")
29+
}
30+
} else {
31+
$include_dirs = []
32+
}
33+
34+
$conf_dir = $collectd::params::plugin_conf_dir
35+
$base_filename = $collectd::plugin::perl::filename
36+
$filename = "${conf_dir}/perl/plugin-${order}_${name}.conf"
37+
38+
file { $filename:
39+
owner => $collectd::params::root_user,
40+
group => $collectd::params::root_group,
41+
mode => '0644',
42+
content => template('collectd/plugin/perl/plugin.erb'),
43+
}
44+
45+
case $provider {
46+
'package': {
47+
validate_string($source)
48+
package { $source:
49+
require => File[$base_filename],
50+
}
51+
}
52+
'cpan': {
53+
validate_string($source)
54+
include cpan
55+
cpan { $source:
56+
require => File[$base_filename],
57+
}
58+
}
59+
'file': {
60+
validate_string($source)
61+
validate_string($destination)
62+
file { "collectd_plugin_perl_${name}.pm":
63+
path => "${destination}/${module}.pm",
64+
mode => '0644',
65+
source => $source,
66+
require => File[$base_filename],
67+
}
68+
}
69+
false: {
70+
# this will fail if perl collectd plugin module is not installed
71+
exec { "perl -M${module} -e 1": path => $::path }
72+
}
73+
default: {
74+
fail("Unsupported provider: ${provider}. Use 'package', 'cpan', 'file' or false.")
75+
}
76+
}
77+
}

‎templates/plugin/perl.conf.erb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#
2+
<LoadPlugin perl>
3+
Globals true
4+
</LoadPlugin>
5+
6+
Include "<%= @conf_dir %>/perl/*.conf"
7+

‎templates/plugin/perl/plugin.erb

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<Plugin perl>
2+
<% if @enable_debugger -%>
3+
EnableDebugger "<% @enable_debugger %>"
4+
<% end -%>
5+
<% if @include_dir -%>
6+
<% @include_dirs.each do |dir| -%>
7+
IncludeDir "<%= dir %>"
8+
<% end -%>
9+
<% end -%>
10+
LoadPlugin "<%= @module %>"
11+
<% if ! @config.empty? -%>
12+
<Plugin "<%= @name %>">
13+
<% @config.each do |key,value| -%>
14+
<% if value.is_a?(Array) -%>
15+
<% value.each do |v| -%>
16+
<%= key %> "<%= v %>"
17+
<% end -%>
18+
<% elsif value.is_a?(Hash) -%>
19+
<<%=key%>>
20+
<% value.each do |k,v| -%>
21+
<% if v.is_a?(String) -%>
22+
<%=k%> "<%=v%>"
23+
<% elsif v.is_a?(Array) -%>
24+
<% v.each do |l| -%>
25+
<%= k %> "<%= l %>"
26+
<% end -%>
27+
<% elsif v.is_a?(Hash) -%>
28+
<<%=k%>>
29+
<% v.each do |i,j| -%>
30+
<% if j.is_a?(Array) -%>
31+
<% j.each do |k| -%>
32+
<%=i%> "<%=k%>"
33+
<% end -%>
34+
<% else -%>
35+
<%=i%> "<%=j%>"
36+
<% end -%>
37+
<% end -%>
38+
</<%=k%>>
39+
<% end -%>
40+
<% end -%>
41+
</<%=key%>>
42+
<% else -%>
43+
<%= key -%> "<%= value -%>"
44+
<% end -%>
45+
<% end -%>
46+
</Plugin>
47+
<% end -%>
48+
</Plugin>

‎tests/plugins/perl.pp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class { collectd:
2+
purge_config => true,
3+
purge => true,
4+
recurse => true,
5+
}
6+
7+
include collectd::plugin::perl
8+
9+
collectd::plugin::perl::plugin { 'foo':
10+
include_dir => '/tmp',
11+
module => 'Collectd::Plugins::Foo',
12+
provider => 'file',
13+
source => 'puppet:///modules/collectd/tests/Foo.pm',
14+
destination => '/tmp',
15+
order => 99,
16+
config => {
17+
'foo' => 'bar',
18+
'key' => [ 'val1', 'val2' ],
19+
}
20+
}
21+
22+
collectd::plugin::perl::plugin { 'bar':
23+
module => 'B',
24+
enable_debugger => "DProf",
25+
include_dir => ['/tmp', '/tmp/lib' ]
26+
}
27+
28+
#collectd::plugin::perl {
29+
# 'openafs_vos':
30+
# module => 'Collectd::Plugins::OpenAFS::VOS',
31+
# provider => 'cpan',
32+
# source => 'Collectd::Plugins::OpenAFS',
33+
# config => {'VosBin' => '/usr/afsws/etc/vos'},
34+
#}
35+
36+
collectd::plugin::perl::plugin {
37+
'baar':
38+
module => 'Collectd::Plugins::Bar',
39+
provider => 'package',
40+
source => 'perl-Collectd-Plugins-Bar',
41+
config => {
42+
'foo' => 'bar',
43+
'more' => {
44+
'complex' => 'structure',
45+
'no' => [ "a", "b" ],
46+
'yes' => {
47+
'last' => 'level',
48+
'and' => [ "array" , "thing" ]
49+
},
50+
},
51+
},
52+
}

0 commit comments

Comments
 (0)
Please sign in to comment.