Skip to content

Commit

Permalink
genericjmx plugin base
Browse files Browse the repository at this point in the history
  • Loading branch information
kitchen committed Jan 17, 2015
1 parent d0379e7 commit faf3c6c
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
31 changes: 31 additions & 0 deletions manifests/plugin/genericjmx.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# https://collectd.org/wiki/index.php/Plugin:GenericJMX
class collectd::plugin::genericjmx (
$ensure = present,
$jvmarg = [],
) {
include collectd
include collectd::params
include collectd::plugin::java

$config_file = "${collectd::params::plugin_conf_dir}/15-genericjmx.conf"

concat { $config_file:
ensure => $ensure,
mode => '0640',
owner => 'root',
group => $collectd::params::root_group,
notify => Service['collectd'],
ensure_newline => true,
}
concat::fragment {
'collectd_plugin_genericjmx_conf_header':
order => '00',
content => template('collectd/plugin/genericjmx.conf.header.erb'),
target => $config_file;
'collectd_plugin_genericjmx_conf_footer':
order => '99',
content => " </Plugin>\n</Plugin>\n",
target => $config_file;
}

}
79 changes: 79 additions & 0 deletions spec/classes/collectd_plugin_genericjmx_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
require 'spec_helper'

describe 'collectd::plugin::genericjmx', :type => :class do
let (:facts) {{
:osfamily => 'RedHat',
:concat_basedir => tmpfilename('collectd-genericjmx'),
}}

let (:config_filename) { '/etc/collectd.d/15-genericjmx.conf' }

context ':ensure => present, defaults' do
it 'will include the java plugin' do
should contain_class('collectd::plugin::java')
end

it 'will load the genericjmx plugin' do
should contain_concat(config_filename).with({
:ensure => 'present',
:mode => '0640',
:owner => 'root',
:group => 'root',
:ensure_newline => true
})
end

it { should contain_concat(config_filename).that_notifies('Service[collectd]') }

it do
should contain_concat__fragment('collectd_plugin_genericjmx_conf_header').with({
:order => '00',
:target => config_filename,
:content => /<Plugin "java">\s+LoadPlugin "org\.collectd\.java\.GenericJMX"\s+<Plugin "GenericJMX">/s
})
end

it do
should contain_concat__fragment('collectd_plugin_genericjmx_conf_footer').with({
:order => '99',
:target => config_filename,
:content => %r{</Plugin>\s+</Plugin>}s,
})
end

end

context ':ensure => absent' do
let (:params) {{ :ensure => 'absent' }}
it 'should not load the plugin' do
should contain_concat(config_filename).with({
:ensure => 'absent',
})
end
end

context 'jvmarg parameter array' do
let (:params) {{ :jvmarg => %w{ foo bar baz } }}
it 'should have multiple jvmarg parameters' do
should contain_concat__fragment('collectd_plugin_genericjmx_conf_header').with_content(/JVMArg "foo"\s*JVMArg "bar"\s*JVMArg "baz"/s)
end
end

context 'jvmarg parameter string' do
let (:params) {{ :jvmarg => "bat" }}
it 'should have one jvmarg parameter' do
should contain_concat__fragment('collectd_plugin_genericjmx_conf_header').with_content(/JVMArg "bat"/)
end
it 'should have ONLY one jvmarg parameter' do
should contain_concat__fragment('collectd_plugin_genericjmx_conf_header').without_content(/(.*JVMArg.*){2,}/s)
end
end

context 'jvmarg parameter empty' do
let (:params) {{ :jvmarg => [] }}
it 'should not have any jvmarg parameters' do
should contain_concat__fragment('collectd_plugin_genericjmx_conf_header').without_content(/JVMArg/)
end
end
end

7 changes: 7 additions & 0 deletions templates/plugin/genericjmx.conf.header.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Plugin "java">
<% Array(@jvmarg).each do |jvmarg| -%>
JVMArg "<%= jvmarg %>"
<% end -%>

LoadPlugin "org.collectd.java.GenericJMX"
<Plugin "GenericJMX">
25 changes: 25 additions & 0 deletions templates/plugin/genericjmx/mbean.conf.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<MBean "<%= @name %>">
ObjectName "<%= @object_name %>"
<% if @instance_prefix -%>
InstancePrefix "<%= @instance_prefix %>"
<% end -%>
<% if @instance_from -%>
<% Array(@instance_from).each do |instance_from_item| -%>
InstanceFrom "<%= instance_from_item %>"
<% end -%>
<% end -%>

<% @values.each do |value| -%>
<Value>
Type "<%= value['type'] %>"
<% if value['instance_prefix'] -%>
InstancePrefix "<%= value['instance_prefix'] %>"
<% end -%>
<% if value['instance_from'] -%>
<% Array(value['instance_from']).each do |instance_from_item| -%>
InstanceFrom "<%= instance_from_item %>"
<% end -%>
<% end -%>
</Value>
<% end -%>
</MBean>

0 comments on commit faf3c6c

Please sign in to comment.