Skip to content

Commit

Permalink
Merge pull request redhat-openstack#255 from deric/python
Browse files Browse the repository at this point in the history
multiple Python modules support
  • Loading branch information
blkperl committed May 7, 2015
2 parents 6343c5b + 7e58baa commit 6a9f949
Show file tree
Hide file tree
Showing 10 changed files with 547 additions and 126 deletions.
59 changes: 50 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ collectd::plugin::curl::page {
}
```

You can as well configure this plugin with a parameterized class :
You can as well configure this plugin with a parameterized class :

```puppet
class { 'collectd::plugin::curl':
Expand Down Expand Up @@ -449,7 +449,7 @@ collectd::plugin::network::listener{'hostname':
}
```

You can as well configure this plugin with a parameterized class :
You can as well configure this plugin with a parameterized class :

```puppet
class { 'collectd::plugin::network':
Expand Down Expand Up @@ -527,7 +527,7 @@ This define will load a new perl plugin.
#####Parameters:

* `module` (String): name of perl module to load (mandatory)
* `enable_debugger` (False or String): whether to load the perl debugger. See *collectd-perl* manpage for more details.
* `enable_debugger` (False or String): whether to load the perl debugger. See *collectd-perl* manpage for more details.
* `include_dir` (String or Array): directories to add to *@INC*
* `provider` (`"package"`,`"cpan"`,`"file"` or `false`): method to get the plugin code
* `source` (String): this parameter is consumed by the provider to infer the source of the plugin code
Expand Down Expand Up @@ -650,13 +650,54 @@ class { 'collectd::plugin::processes':

####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.
* `modules` a Hash containing configuration of Python modules, where the key is the module name
* `globals` Unlike most other plugins, this one should set `Globals true`. This will cause collectd to export the name of all objects in the Python interpreter for all plugins to see. If you don't do this or your platform does not support it, the embedded interpreter will start anyway but you won't be able to load certain Python modules, e.g. "time".
* `interactive` when `true` it will launch an interactive Python interpreter that reads from and writes to the terminal (default: `false`)
* `logtraces` if a Python script throws an exception it will be logged by collectd with the name of the exception and the message (default: `false`)

See [collectd-python documentation](https://collectd.org/documentation/manpages/collectd-python.5.shtml) for more details.

NOTE: Since `v3.4.0` the syntax of this plugin has changed. Make sure to update your existing configuration. Now you can specify multiple Python modules at once:

```puppet
class { 'collectd::plugin::python':
modulepaths => ['/usr/share/collectd/python'],
modules => {
'elasticsearch': {
'script_source' => 'puppet:///modules/myorg/elasticsearch_collectd_python.py',
'config' => {'Cluster' => 'elasticsearch'},
},
'another-module': {
'config' => {'Verbose' => 'true'},
}
}
logtraces => true,
interactive => false
}
```
When `script_source` provided, a file called `{module}.py` will be created in `$modulepath/$module.py`.

Or define single module:

```puppet
collectd::plugin::python {
'elasticsearch':
modulepath => '/usr/lib/collectd',
module => 'elasticsearch',
script_source => 'puppet:///modules/myorg/elasticsearch_collectd_python.py',
config => {'Cluster' => 'elasticsearch'},
collectd::plugin::python::module {'zk-collectd':
script_source => 'puppet:///modules/myorg/zk-collectd.py',
config => {
'Hosts' => "localhost:2181"
}
}
```

Each plugin might use different `modulepath`, however make sure that all paths are included in `collectd::plugin::python` variable `modulepaths`. If no `modulepath` is specified, OS default will be used.

```puppet
collectd::plugin::python::module {'my-module':
modulepath => '/var/share/collectd',
script_source => 'puppet:///modules/myorg/my-module.py',
config => {
'Key' => "value"
}
}
```

Expand Down
7 changes: 7 additions & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
$config_file = "${collectd_dir}/collectd.conf"
$root_group = 'root'
$java_dir = '/usr/share/collectd/java'
$python_dir = '/usr/share/collectd/python'
}
'Solaris': {
$package = 'CSWcollectd'
Expand All @@ -21,6 +22,7 @@
$config_file = "${collectd_dir}/collectd.conf"
$root_group = 'root'
$java_dir = undef
$python_dir = '/opt/csw/share/collectd/python'
}
'Redhat': {
$package = 'collectd'
Expand All @@ -31,6 +33,7 @@
$config_file = '/etc/collectd.conf'
$root_group = 'root'
$java_dir = '/usr/share/collectd/java'
$python_dir = '/usr/share/collectd/python'
}
'Suse': {
$package = 'collectd'
Expand All @@ -41,6 +44,7 @@
$config_file = '/etc/collectd.conf'
$root_group = 'root'
$java_dir = undef
$python_dir = '/usr/share/collectd/python'
}
'FreeBSD': {
$package = 'collectd5'
Expand All @@ -51,6 +55,7 @@
$config_file = '/usr/local/etc/collectd.conf'
$root_group = 'wheel'
$java_dir = undef
$python_dir = '/usr/local/share/collectd/python'
}
'Archlinux': {
$package = 'collectd'
Expand All @@ -61,6 +66,7 @@
$config_file = '/etc/collectd.conf'
$root_group = 'wheel'
$java_dir = undef
$python_dir = '/usr/share/collectd/python'
}
'Gentoo': {
$package = 'app-admin/collectd'
Expand All @@ -71,6 +77,7 @@
$config_file = '/etc/collectd.conf'
$root_group = 'collectd'
$java_dir = undef
$python_dir = '/usr/share/collectd/python'
}

default: {
Expand Down
104 changes: 70 additions & 34 deletions manifests/plugin/python.pp
Original file line number Diff line number Diff line change
@@ -1,45 +1,81 @@
# See http://collectd.org/documentation/manpages/collectd.conf.5.shtml#plugin_python
define collectd::plugin::python (
$modulepath,
$module,
$script_source,
$ensure = present,
$config = {},
$order = '10',
class collectd::plugin::python (
$modulepaths = [],
$ensure = present,
$modules = {},
# Unlike most other plugins, this one should set "Globals true". This will cause collectd
# to export the name of all objects in the Python interpreter for all plugins to see.
$globals = true,
$order = '10',
$interval = undef,
# Python 2 defaults to 'ascii' and Python 3 to 'utf-8'
$encoding = undef,
$interactive = false,
$logtraces = false,
) {
include collectd::params

validate_hash($config)
validate_hash($modules)
validate_bool($interactive)
validate_bool($logtraces)
validate_bool($globals)
validate_array($modulepaths)

$conf_dir = $collectd::params::plugin_conf_dir
$module_dirs = empty($modulepaths) ? {
true => [$collectd::params::python_dir],
# use paths provided by the user
false => $modulepaths
}

collectd::plugin {'python':
ensure => $ensure,
interval => $interval,
order => $order,
globals => $globals,
}

$ensure_modulepath = $ensure ? {
'absent' => $ensure,
default => 'directory',
}

# 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",
ensure_resource('file', $module_dirs,
{
'ensure' => $ensure_modulepath,
'mode' => '0750',
'owner' => 'root',
'group' => $collectd::params::root_group,
'require' => Package[$collectd::params::package]
}
)

# should be loaded after global plugin configuration
$python_conf = "${collectd::params::plugin_conf_dir}/python-config.conf"

concat{ $python_conf:
ensure => $ensure,
mode => '0640',
owner => 'root',
group => $collectd::params::root_group,
notify => Service['collectd'],
ensure_newline => true,
}
# End deprecation

file {
"${name}.load":
ensure => $ensure,
path => "${conf_dir}/${order}-${name}.conf",
owner => 'root',
group => $collectd::params::root_group,
mode => '0640',
content => template('collectd/python.conf.erb'),
notify => Service['collectd'],

concat::fragment{'collectd_plugin_python_conf_header':
order => '00',
content => template('collectd/plugin/python/header.conf.erb'),
target => $python_conf,
}

concat::fragment{'collectd_plugin_python_conf_footer':
order => '99',
content => '</Plugin>',
target => $python_conf,
}

file {
"${name}.script":
ensure => $ensure,
path => "${modulepath}/${module}.py",
owner => 'root',
group => $collectd::params::root_group,
mode => '0640',
source => $script_source,
require => File["${name}.load"],
notify => Service['collectd'],
$defaults = {
'ensure' => $ensure,
'modulepath' => $module_dirs[0],
}
create_resources(collectd::plugin::python::module, $modules, $defaults)
}
39 changes: 39 additions & 0 deletions manifests/plugin/python/module.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Single module definition
define collectd::plugin::python::module (
$config,
$modulepath = undef,
$script_source = undef,
$module = $title,
$ensure = 'present',
){
include collectd::params
include collectd::plugin::python

validate_hash($config)

$module_dir = $modulepath ? {
undef => $collectd::params::python_dir,
default => $modulepath
}
validate_absolute_path($module_dir)

if $script_source {
file { "${module}.script":
ensure => $ensure,
path => "${module_dir}/${module}.py",
owner => 'root',
group => $collectd::params::root_group,
mode => '0640',
source => $script_source,
require => File[$module_dir],
notify => Service['collectd'],
}
}

concat::fragment{"collectd_plugin_python_conf_${module}":
ensure => $ensure,
order => '50', # somewhere between header and footer
target => $collectd::plugin::python::python_conf,
content => template('collectd/plugin/python/module.conf.erb'),
}
}

0 comments on commit 6a9f949

Please sign in to comment.