From 8031829490c4d2f7c414e160e124f0d67370c933 Mon Sep 17 00:00:00 2001 From: Jeremy Kitchen Date: Thu, 15 Jan 2015 21:04:15 +0000 Subject: [PATCH 01/13] java plugin --- manifests/plugin/java.pp | 12 +++++ spec/classes/collectd_plugin_java_spec.rb | 57 +++++++++++++++++++++++ templates/plugin/java.conf.erb | 7 +++ 3 files changed, 76 insertions(+) create mode 100644 manifests/plugin/java.pp create mode 100644 spec/classes/collectd_plugin_java_spec.rb create mode 100644 templates/plugin/java.conf.erb diff --git a/manifests/plugin/java.pp b/manifests/plugin/java.pp new file mode 100644 index 000000000..1ef6960ca --- /dev/null +++ b/manifests/plugin/java.pp @@ -0,0 +1,12 @@ +# https://collectd.org/wiki/index.php/Plugin:Java +class collectd::plugin::java ( + $ensure = present, + $jvmarg = [], + $interval = undef, +) { + collectd::plugin { 'java': + ensure => $ensure, + content => template('collectd/plugin/java.conf.erb'), + interval => $interval, + } +} diff --git a/spec/classes/collectd_plugin_java_spec.rb b/spec/classes/collectd_plugin_java_spec.rb new file mode 100644 index 000000000..86bddfd04 --- /dev/null +++ b/spec/classes/collectd_plugin_java_spec.rb @@ -0,0 +1,57 @@ +require 'spec_helper' + +describe 'collectd::plugin::java', :type => :class do + let :facts do + {:osfamily => 'RedHat'} + end + + context ':ensure => present, defaults' do + it 'Will load the plugin' do + should contain_file('java.load').with({ + :ensure => 'present', + :path => '/etc/collectd.d/10-java.conf', + }) + + should contain_file('java.load').with_content(//) + should contain_file('java.load').without_content(/JVMArg/) + end + end + + context ':ensure => absent' do + let (:params) {{ + :ensure => 'absent', + }} + + it 'will not load the plugin' do + should contain_file('java.load').with({ + :ensure => 'absent', + :path => '/etc/collectd.d/10-java.conf', + }) + end + end + + context 'jvmarg parameter array' do + let (:params) {{ + :jvmarg => %w{ foo bar baz } + }} + + it 'will have multiple JVMArg bits' do + should contain_file('java.load').with_content(/JVMArg "foo"[\s\n]+JVMArg "bar"[\s\n]+JVMArg "baz"/) + end + end + + context 'jvmarg parameter string' do + let (:params) {{ + :jvmarg => 'bat' + }} + + it 'will have a JVMArg bit' do + should contain_file('java.load').with_content(/JVMArg "bat"/) + end + + it 'will only have one JVMArg bit' do + should contain_file('java.load').without_content(/(.*JVMArg.*){2,}/) + end + end +end + diff --git a/templates/plugin/java.conf.erb b/templates/plugin/java.conf.erb new file mode 100644 index 000000000..bf7d2ce8c --- /dev/null +++ b/templates/plugin/java.conf.erb @@ -0,0 +1,7 @@ + + <% if @jvmarg -%> + <% Array(@jvmarg).each do |arg| -%> + JVMArg "<%= arg %>" + <% end -%> + <% end -%> + From 9276be2bb58ac344d5c5ea0770397a9695cbc955 Mon Sep 17 00:00:00 2001 From: Jeremy Kitchen Date: Fri, 16 Jan 2015 19:51:35 +0000 Subject: [PATCH 02/13] collectd doesn't like having an empty stanza --- spec/classes/collectd_plugin_java_spec.rb | 11 ++++++++++- templates/plugin/java.conf.erb | 6 +++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/spec/classes/collectd_plugin_java_spec.rb b/spec/classes/collectd_plugin_java_spec.rb index 86bddfd04..69c92cffc 100644 --- a/spec/classes/collectd_plugin_java_spec.rb +++ b/spec/classes/collectd_plugin_java_spec.rb @@ -12,7 +12,6 @@ :path => '/etc/collectd.d/10-java.conf', }) - should contain_file('java.load').with_content(//) should contain_file('java.load').without_content(/JVMArg/) end end @@ -53,5 +52,15 @@ should contain_file('java.load').without_content(/(.*JVMArg.*){2,}/) end end + + context 'jvmarg parameter empty' do + let (:params) {{ + :jvmarg => [], + }} + + it 'will not have a stanza' do + should contain_file('java.load').without_content(//) + end + end end diff --git a/templates/plugin/java.conf.erb b/templates/plugin/java.conf.erb index bf7d2ce8c..0ab321810 100644 --- a/templates/plugin/java.conf.erb +++ b/templates/plugin/java.conf.erb @@ -1,7 +1,7 @@ +<% if !Array(@jvmarg).empty? -%> - <% if @jvmarg -%> - <% Array(@jvmarg).each do |arg| -%> + <% Array(@jvmarg).each do |arg| -%> JVMArg "<%= arg %>" - <% end -%> <% end -%> +<% end -%> From 55054bda415487c01bc08e6785bcfc8121f2d35c Mon Sep 17 00:00:00 2001 From: Jeremy Kitchen Date: Fri, 16 Jan 2015 20:55:18 +0000 Subject: [PATCH 03/13] test the interface not the result --- spec/classes/collectd_plugin_java_spec.rb | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/spec/classes/collectd_plugin_java_spec.rb b/spec/classes/collectd_plugin_java_spec.rb index 69c92cffc..589441257 100644 --- a/spec/classes/collectd_plugin_java_spec.rb +++ b/spec/classes/collectd_plugin_java_spec.rb @@ -7,12 +7,11 @@ context ':ensure => present, defaults' do it 'Will load the plugin' do - should contain_file('java.load').with({ + should contain_collectd__plugin('java').with({ :ensure => 'present', - :path => '/etc/collectd.d/10-java.conf', }) - should contain_file('java.load').without_content(/JVMArg/) + should contain_collectd__plugin('java').without_content(/JVMArg/) end end @@ -22,9 +21,8 @@ }} it 'will not load the plugin' do - should contain_file('java.load').with({ - :ensure => 'absent', - :path => '/etc/collectd.d/10-java.conf', + should contain_collectd__plugin('java').with({ + :ensure => 'absent' }) end end @@ -35,7 +33,7 @@ }} it 'will have multiple JVMArg bits' do - should contain_file('java.load').with_content(/JVMArg "foo"[\s\n]+JVMArg "bar"[\s\n]+JVMArg "baz"/) + should contain_collectd__plugin('java').with_content(/JVMArg "foo"[\s\n]+JVMArg "bar"[\s\n]+JVMArg "baz"/) end end @@ -45,11 +43,11 @@ }} it 'will have a JVMArg bit' do - should contain_file('java.load').with_content(/JVMArg "bat"/) + should contain_collectd__plugin('java').with_content(/JVMArg "bat"/) end it 'will only have one JVMArg bit' do - should contain_file('java.load').without_content(/(.*JVMArg.*){2,}/) + should contain_collectd__plugin('java').without_content(/(.*JVMArg.*){2,}/) end end @@ -59,8 +57,7 @@ }} it 'will not have a stanza' do - should contain_file('java.load').without_content(//) + should contain_collectd__plugin('java').without_content(//) end end end - From d0379e7589ff10cd34043f39299a227a6c95dbd5 Mon Sep 17 00:00:00 2001 From: Jeremy Kitchen Date: Sat, 17 Jan 2015 02:34:27 +0000 Subject: [PATCH 04/13] consistency! --- spec/classes/collectd_plugin_java_spec.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/spec/classes/collectd_plugin_java_spec.rb b/spec/classes/collectd_plugin_java_spec.rb index 589441257..cc74f0bd0 100644 --- a/spec/classes/collectd_plugin_java_spec.rb +++ b/spec/classes/collectd_plugin_java_spec.rb @@ -10,8 +10,6 @@ should contain_collectd__plugin('java').with({ :ensure => 'present', }) - - should contain_collectd__plugin('java').without_content(/JVMArg/) end end @@ -32,7 +30,7 @@ :jvmarg => %w{ foo bar baz } }} - it 'will have multiple JVMArg bits' do + it 'will have multiple jvmarg parameters' do should contain_collectd__plugin('java').with_content(/JVMArg "foo"[\s\n]+JVMArg "bar"[\s\n]+JVMArg "baz"/) end end @@ -42,11 +40,11 @@ :jvmarg => 'bat' }} - it 'will have a JVMArg bit' do + it 'will have a JVMArg parameter' do should contain_collectd__plugin('java').with_content(/JVMArg "bat"/) end - it 'will only have one JVMArg bit' do + it 'will only have one JVMArg parameter' do should contain_collectd__plugin('java').without_content(/(.*JVMArg.*){2,}/) end end @@ -59,5 +57,8 @@ it 'will not have a stanza' do should contain_collectd__plugin('java').without_content(//) end + it 'will not have any jvmarg parameters' do + should contain_collectd__plugin('java').without_content(/JVMArg/) + end end end From faf3c6ccb86f9b20e0917663e098a377c53bc6fe Mon Sep 17 00:00:00 2001 From: Jeremy Kitchen Date: Sat, 17 Jan 2015 02:38:42 +0000 Subject: [PATCH 05/13] genericjmx plugin base --- manifests/plugin/genericjmx.pp | 31 ++++++++ .../collectd_plugin_genericjmx_spec.rb | 79 +++++++++++++++++++ templates/plugin/genericjmx.conf.header.erb | 7 ++ templates/plugin/genericjmx/mbean.conf.erb | 25 ++++++ 4 files changed, 142 insertions(+) create mode 100644 manifests/plugin/genericjmx.pp create mode 100644 spec/classes/collectd_plugin_genericjmx_spec.rb create mode 100644 templates/plugin/genericjmx.conf.header.erb create mode 100644 templates/plugin/genericjmx/mbean.conf.erb diff --git a/manifests/plugin/genericjmx.pp b/manifests/plugin/genericjmx.pp new file mode 100644 index 000000000..1cc2081d8 --- /dev/null +++ b/manifests/plugin/genericjmx.pp @@ -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 => " \n\n", + target => $config_file; + } + +} diff --git a/spec/classes/collectd_plugin_genericjmx_spec.rb b/spec/classes/collectd_plugin_genericjmx_spec.rb new file mode 100644 index 000000000..96ff5b771 --- /dev/null +++ b/spec/classes/collectd_plugin_genericjmx_spec.rb @@ -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 => /\s+LoadPlugin "org\.collectd\.java\.GenericJMX"\s+/s + }) + end + + it do + should contain_concat__fragment('collectd_plugin_genericjmx_conf_footer').with({ + :order => '99', + :target => config_filename, + :content => %r{\s+}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 + diff --git a/templates/plugin/genericjmx.conf.header.erb b/templates/plugin/genericjmx.conf.header.erb new file mode 100644 index 000000000..26e52d30e --- /dev/null +++ b/templates/plugin/genericjmx.conf.header.erb @@ -0,0 +1,7 @@ + +<% Array(@jvmarg).each do |jvmarg| -%> + JVMArg "<%= jvmarg %>" +<% end -%> + + LoadPlugin "org.collectd.java.GenericJMX" + diff --git a/templates/plugin/genericjmx/mbean.conf.erb b/templates/plugin/genericjmx/mbean.conf.erb new file mode 100644 index 000000000..9677c0f03 --- /dev/null +++ b/templates/plugin/genericjmx/mbean.conf.erb @@ -0,0 +1,25 @@ +"> +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| -%> + + 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 -%> + +<% end -%> + From 43ee029ee9e541103d96fdf404965c16b29ea134 Mon Sep 17 00:00:00 2001 From: Jeremy Kitchen Date: Mon, 19 Jan 2015 19:45:25 +0000 Subject: [PATCH 06/13] MBean stanzas --- manifests/plugin/genericjmx/connection.pp | 0 manifests/plugin/genericjmx/mbean.pp | 17 ++ .../collectd_plugin_genericjmx_mbean_spec.rb | 175 ++++++++++++++++++ templates/plugin/genericjmx/mbean.conf.erb | 32 ++-- 4 files changed, 209 insertions(+), 15 deletions(-) create mode 100644 manifests/plugin/genericjmx/connection.pp create mode 100644 manifests/plugin/genericjmx/mbean.pp create mode 100644 spec/defines/collectd_plugin_genericjmx_mbean_spec.rb diff --git a/manifests/plugin/genericjmx/connection.pp b/manifests/plugin/genericjmx/connection.pp new file mode 100644 index 000000000..e69de29bb diff --git a/manifests/plugin/genericjmx/mbean.pp b/manifests/plugin/genericjmx/mbean.pp new file mode 100644 index 000000000..68f0e1435 --- /dev/null +++ b/manifests/plugin/genericjmx/mbean.pp @@ -0,0 +1,17 @@ +# https://collectd.org/wiki/index.php/Plugin:GenericJMX +define collectd::plugin::genericjmx::mbean ( + $object_name, + $instance_prefix = undef, + $instance_from = undef, + $values, +) { + include collectd::plugin::genericjmx + validate_array($values) + + concat::fragment { + "collectd_plugin_genericjmx_conf_${name}": + order => '10', + content => template('collectd/plugin/genericjmx/mbean.conf.erb'), + target => $collectd::plugin::genericjmx::config_file; + } +} diff --git a/spec/defines/collectd_plugin_genericjmx_mbean_spec.rb b/spec/defines/collectd_plugin_genericjmx_mbean_spec.rb new file mode 100644 index 000000000..e1ba5a8e4 --- /dev/null +++ b/spec/defines/collectd_plugin_genericjmx_mbean_spec.rb @@ -0,0 +1,175 @@ +require 'spec_helper' + +describe 'collectd::plugin::genericjmx::mbean', :type => :define do + + let (:facts) {{ + :osfamily => 'Debian', + :concat_basedir => tmpfilename('collectd-genericjmx-mbean'), + }} + + let (:config_filename) { '/etc/collectd/conf.d/15-genericjmx.conf' } + + let (:default_params) {{ + :object_name => 'bar', + :values => [], + }} + + let (:title) { 'foo' } + let (:concat_fragment_name) { 'collectd_plugin_genericjmx_conf_foo' } + + # empty values array is technically not valid, but we'll test those cases later + context 'defaults' do + let (:params) { default_params } + it 'provides an MBean stanza concat fragment' do + should contain_concat__fragment(concat_fragment_name).with({ + :target => config_filename, + :order => '10', + }) + end + + it { should contain_concat__fragment(concat_fragment_name).with_content(%r{\s+ObjectName "bar".+}m) } + it { should contain_concat__fragment(concat_fragment_name).without_content(/InstancePrefix/) } + it { should contain_concat__fragment(concat_fragment_name).without_content(/InstanceFrom/) } + end + + context 'instance_prefix set' do + let (:params) { + default_params.merge({ + :instance_prefix => 'baz' + }) + } + + it { should contain_concat__fragment(concat_fragment_name).with_content(/InstancePrefix "baz"/) } + end + + context 'instance_from array' do + let (:params) { + default_params.merge({ + :instance_from => %w{ foo bar baz } + }) + } + + it { should contain_concat__fragment(concat_fragment_name).with_content(/InstanceFrom "foo"\s+InstanceFrom "bar"\s+InstanceFrom "baz"/) } + end + + context 'instance_from string' do + let (:params) { + default_params.merge({ + :instance_from => 'bat' + }) + } + + it { should contain_concat__fragment(concat_fragment_name).with_content(/InstanceFrom "bat"/) } + it { should contain_concat__fragment(concat_fragment_name).without_content(/(.*InstanceFrom.*){2,}/) } + end + + let (:default_values_args) {{ + 'type' => 'foo', + 'attribute' => 'bar' + }} + + + # testing the Value template section is going to be messy + context 'value section defaults' do + let (:params) { + default_params.merge({ + :values => [default_values_args] + }) + } + + it 'should have a value stanza' do + should contain_concat__fragment(concat_fragment_name).with_content(%r{.*}m) + end + + it 'should have only one value stanza' do + should contain_concat__fragment(concat_fragment_name).without_content(%r{(.*.*){2,}}) + end + + it { should contain_concat__fragment(concat_fragment_name).with_content(/Type "foo"/) } + it { should contain_concat__fragment(concat_fragment_name).with_content(/Table false/) } + it { should contain_concat__fragment(concat_fragment_name).with_content(/Attribute "bar"/) } + it { should contain_concat__fragment(concat_fragment_name).without_content(/InstancePrefix/) } + it { should contain_concat__fragment(concat_fragment_name).without_content(/InstanceFrom/) } + end + + context 'value section instance_prefix set' do + let (:params) { + default_params.merge({ + :values => [default_values_args.merge({ + 'instance_prefix' => 'baz', + })] + }) + } + + it { should contain_concat__fragment(concat_fragment_name).with_content(/InstancePrefix "baz"/) } + it { should contain_concat__fragment(concat_fragment_name).without_content(/InstanceFrom/) } + end + + context 'value section instance_from array' do + let (:params) { + default_params.merge({ + :values => [default_values_args.merge({ + 'instance_from' => %w{ alice bob carol } + })] + }) + } + + it { should contain_concat__fragment(concat_fragment_name).with_content(/InstanceFrom "alice"/) } + it { should contain_concat__fragment(concat_fragment_name).with_content(/InstanceFrom "bob"/) } + it { should contain_concat__fragment(concat_fragment_name).with_content(/InstanceFrom "carol"/) } + it { should contain_concat__fragment(concat_fragment_name).without_content(/InstancePrefix/) } + end + + context 'value section instance_from string' do + let (:params) { + default_params.merge({ + :values => [default_values_args.merge({ + 'instance_from' => 'dave', + })] + }) + } + + it { should contain_concat__fragment(concat_fragment_name).with_content(/InstanceFrom "dave"/) } + it { should contain_concat__fragment(concat_fragment_name).without_content(/(.*InstancePrefix.*){2,}/) } + it { should contain_concat__fragment(concat_fragment_name).without_content(/InstancePrefix/) } + end + + context 'value section table true-like' do + ['true', true].each do |truthy| + let (:params) { + default_params.merge({ + :values => [default_values_args.merge({ + 'table' => truthy + })] + }) + } + + it { should contain_concat__fragment(concat_fragment_name).with_content(/Table true/) } + end + end + + context 'value section table false-like' do + ['false', false].each do |truthy| + let (:params) { + default_params.merge({ + :values => [default_values_args.merge({ + 'table' => truthy + })] + }) + } + + it { should contain_concat__fragment(concat_fragment_name).with_content(/Table false/) } + end + end + + context 'multiple values' do + let (:params) { + default_params.merge({ + :values => [default_values_args,default_values_args] + }) + } + + it { should contain_concat__fragment(concat_fragment_name).with_content(%r{(.*.*.*){2}}m) } + end + +end diff --git a/templates/plugin/genericjmx/mbean.conf.erb b/templates/plugin/genericjmx/mbean.conf.erb index 9677c0f03..cced7724a 100644 --- a/templates/plugin/genericjmx/mbean.conf.erb +++ b/templates/plugin/genericjmx/mbean.conf.erb @@ -1,25 +1,27 @@ "> -ObjectName "<%= @object_name %>" -<% if @instance_prefix -%> + 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 -%> + <% if @instance_from -%> + <% Array(@instance_from).each do |instance_from_item| -%> + InstanceFrom "<%= instance_from_item %>" + <% end -%> + <% end -%> -<% @values.each do |value| -%> + <% @values.each do |value| -%> Type "<%= value['type'] %>" - <% if value['instance_prefix'] -%> + <% 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 -%> + <% if value['instance_from'] -%> + <% Array(value['instance_from']).each do |instance_from_item| -%> + InstanceFrom "<%= instance_from_item %>" + <% end -%> + <% end -%> + Table <%= scope.function_str2bool([value['table'] || false ]) ? 'true' : 'false' %> + Attribute "<%= value['attribute'] %>" -<% end -%> + <% end -%> From 6737a8d7db09fc4d48339b4e20694e7c70c4029b Mon Sep 17 00:00:00 2001 From: Jeremy Kitchen Date: Mon, 19 Jan 2015 19:53:06 +0000 Subject: [PATCH 07/13] use appropriate regex modifier was wondering why s wasn't working. silly ruby --- spec/classes/collectd_plugin_genericjmx_spec.rb | 8 ++++---- spec/classes/collectd_plugin_java_spec.rb | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/spec/classes/collectd_plugin_genericjmx_spec.rb b/spec/classes/collectd_plugin_genericjmx_spec.rb index 96ff5b771..adc8cee2a 100644 --- a/spec/classes/collectd_plugin_genericjmx_spec.rb +++ b/spec/classes/collectd_plugin_genericjmx_spec.rb @@ -29,7 +29,7 @@ should contain_concat__fragment('collectd_plugin_genericjmx_conf_header').with({ :order => '00', :target => config_filename, - :content => /\s+LoadPlugin "org\.collectd\.java\.GenericJMX"\s+/s + :content => /.+LoadPlugin "org\.collectd\.java\.GenericJMX".+/m }) end @@ -37,7 +37,7 @@ should contain_concat__fragment('collectd_plugin_genericjmx_conf_footer').with({ :order => '99', :target => config_filename, - :content => %r{\s+}s, + :content => %r{.+}m, }) end @@ -55,7 +55,7 @@ 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) + should contain_concat__fragment('collectd_plugin_genericjmx_conf_header').with_content(/JVMArg "foo".*JVMArg "bar".*JVMArg "baz"/m) end end @@ -65,7 +65,7 @@ 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) + should contain_concat__fragment('collectd_plugin_genericjmx_conf_header').without_content(/(.*JVMArg.*){2,}/m) end end diff --git a/spec/classes/collectd_plugin_java_spec.rb b/spec/classes/collectd_plugin_java_spec.rb index cc74f0bd0..ad5bb2fa7 100644 --- a/spec/classes/collectd_plugin_java_spec.rb +++ b/spec/classes/collectd_plugin_java_spec.rb @@ -31,7 +31,7 @@ }} it 'will have multiple jvmarg parameters' do - should contain_collectd__plugin('java').with_content(/JVMArg "foo"[\s\n]+JVMArg "bar"[\s\n]+JVMArg "baz"/) + should contain_collectd__plugin('java').with_content(/JVMArg "foo".+JVMArg "bar".+JVMArg "baz"/m) end end @@ -45,7 +45,7 @@ end it 'will only have one JVMArg parameter' do - should contain_collectd__plugin('java').without_content(/(.*JVMArg.*){2,}/) + should contain_collectd__plugin('java').without_content(/(.*JVMArg.*){2,}/m) end end From f68c147a9862eb7ce00a9bec41fc29e882f249a9 Mon Sep 17 00:00:00 2001 From: Jeremy Kitchen Date: Mon, 19 Jan 2015 21:11:21 +0000 Subject: [PATCH 08/13] genericjmx connection stanza --- manifests/plugin/genericjmx/connection.pp | 16 +++ ...lectd_plugin_genericjmx_connection_spec.rb | 103 ++++++++++++++++++ .../plugin/genericjmx/connection.conf.erb | 16 +++ 3 files changed, 135 insertions(+) create mode 100644 spec/defines/collectd_plugin_genericjmx_connection_spec.rb create mode 100644 templates/plugin/genericjmx/connection.conf.erb diff --git a/manifests/plugin/genericjmx/connection.pp b/manifests/plugin/genericjmx/connection.pp index e69de29bb..23d475acb 100644 --- a/manifests/plugin/genericjmx/connection.pp +++ b/manifests/plugin/genericjmx/connection.pp @@ -0,0 +1,16 @@ +# https://collectd.org/wiki/index.php/Plugin:GenericJMX +define collectd::plugin::genericjmx::connection ( + $host = $name, + $service_url, + $user = undef, + $password = undef, + $instance_prefix = undef, + $collect, +) { + include collectd::plugin::genericjmx + concat::fragment { "collectd_plugin_genericjmx_conf_${name}": + order => 20, + content => template('collectd/plugin/genericjmx/connection.conf.erb'), + target => $collectd::plugin::genericjmx::config_file; + } +} diff --git a/spec/defines/collectd_plugin_genericjmx_connection_spec.rb b/spec/defines/collectd_plugin_genericjmx_connection_spec.rb new file mode 100644 index 000000000..f93427de8 --- /dev/null +++ b/spec/defines/collectd_plugin_genericjmx_connection_spec.rb @@ -0,0 +1,103 @@ +require 'spec_helper' +describe 'collectd::plugin::genericjmx::connection', :type => :define do + + let (:facts) {{ + :osfamily => 'Debian', + :concat_basedir => tmpfilename('collectd-genericjmx-connection'), + }} + + let (:config_filename) { '/etc/collectd/conf.d/15-genericjmx.conf' } + + let (:default_params) {{ + :service_url => 'foo:bar:baz', + }} + + let (:title) { 'foo.example.com' } + let (:concat_fragment_name) { 'collectd_plugin_genericjmx_conf_foo.example.com' } + + context 'required params' do + let (:params) { + default_params.merge({ + :collect => [], + }) + } + + it 'provides a Connection concat fragment' do + should contain_concat__fragment(concat_fragment_name).with({ + :target => config_filename, + :order => '20', + }) + end + + it { should contain_concat__fragment(concat_fragment_name).with_content(%r{.*}m) } + it { should contain_concat__fragment(concat_fragment_name).with_content(/Host "foo\.example\.com"/) } + it { should contain_concat__fragment(concat_fragment_name).with_content(/ServiceURL "foo:bar:baz"/) } + it { should contain_concat__fragment(concat_fragment_name).without_content(/User/) } + it { should contain_concat__fragment(concat_fragment_name).without_content(/Password/) } + it { should contain_concat__fragment(concat_fragment_name).without_content(/InstancePrefix/) } + end + + context 'hostname override' do + let (:params) { + default_params.merge({ + :host => 'bar.example.com', + :collect => [], + }) + } + + it 'provides a Connection concat fragment' do + should contain_concat__fragment(concat_fragment_name).with({ + :target => config_filename, + :order => '20', + }) + end + + it { should contain_concat__fragment(concat_fragment_name).with_content(/Host "bar\.example\.com"/) } + end + + context 'collect array' do + let (:params) { + default_params.merge({ + :collect => %w{ foo bar baz } + }) + } + + it { should contain_concat__fragment(concat_fragment_name).with_content(/Collect "foo".*Collect "bar".*Collect "baz"/m) } + end + + context 'collect string' do + let (:params) { + default_params.merge({ + :collect => 'bat' + }) + } + + it { should contain_concat__fragment(concat_fragment_name).with_content(/Collect "bat"/) } + it { should contain_concat__fragment(concat_fragment_name).without_content(/(.*Collect.*){2,}/m) } + end + + context 'username and password' do + let (:params) { + default_params.merge({ + :user => 'alice', + :password => 'aoeuhtns', + :collect => [], + }) + } + + it { should contain_concat__fragment(concat_fragment_name).with_content(/User "alice"/) } + it { should contain_concat__fragment(concat_fragment_name).with_content(/Password "aoeuhtns"/) } + end + + context 'instance_prefix 'do + let (:params) { + default_params.merge({ + :instance_prefix => 'bat', + :collect => [], + }) + } + + it { should contain_concat__fragment(concat_fragment_name).with_content(/InstancePrefix "bat"/) } + end + +end diff --git a/templates/plugin/genericjmx/connection.conf.erb b/templates/plugin/genericjmx/connection.conf.erb new file mode 100644 index 000000000..ea32ce9df --- /dev/null +++ b/templates/plugin/genericjmx/connection.conf.erb @@ -0,0 +1,16 @@ + + Host "<%= @host %>" + ServiceURL "<%= @service_url %>" + <% Array(@collect).each do |collect| -%> + Collect "<%= collect %>" + <% end -%> + <% if @user -%> + User "<%= @user %>" + <% end -%> + <% if @password -%> + Password "<%= @password %>" + <% end -%> + <% if @instance_prefix -%> + InstancePrefix "<%= @instance_prefix %>" + <% end -%> + From e7850503f20ceaeeb35ffe79ded47523c0aefaf3 Mon Sep 17 00:00:00 2001 From: Jeremy Kitchen Date: Tue, 20 Jan 2015 01:20:46 +0000 Subject: [PATCH 09/13] collectd doesn't auto-include its java dir in class path :( --- manifests/params.pp | 7 +++++++ manifests/plugin/genericjmx.pp | 2 +- spec/classes/collectd_plugin_genericjmx_spec.rb | 12 ++++++------ templates/plugin/genericjmx.conf.header.erb | 1 + 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/manifests/params.pp b/manifests/params.pp index f2ce6c92b..b7070ea14 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -10,6 +10,7 @@ $service_name = 'collectd' $config_file = "${collectd_dir}/collectd.conf" $root_group = 'root' + $java_dir = '/usr/share/collectd/java' } 'Solaris': { $package = 'CSWcollectd' @@ -19,6 +20,7 @@ $service_name = 'collectd' $config_file = "${collectd_dir}/collectd.conf" $root_group = 'root' + # FIXME: $java_dir } 'Redhat': { $package = 'collectd' @@ -28,6 +30,7 @@ $service_name = 'collectd' $config_file = '/etc/collectd.conf' $root_group = 'root' + $java_dir = '/usr/share/collectd/java' } 'Suse': { $package = 'collectd' @@ -37,6 +40,7 @@ $service_name = 'collectd' $config_file = '/etc/collectd.conf' $root_group = 'root' + # FIXME: $java_dir } 'FreeBSD': { $package = 'collectd5' @@ -46,6 +50,7 @@ $service_name = 'collectd' $config_file = '/usr/local/etc/collectd.conf' $root_group = 'wheel' + # FIXME: $java_dir } 'Archlinux': { $package = 'collectd' @@ -55,6 +60,7 @@ $service_name = 'collectd' $config_file = '/etc/collectd.conf' $root_group = 'wheel' + # FIXME: $java_dir } 'Gentoo': { $package = 'app-admin/collectd' @@ -64,6 +70,7 @@ $service_name = 'collectd' $config_file = '/etc/collectd.conf' $root_group = 'collectd' + # FIXME: $java_dir } default: { diff --git a/manifests/plugin/genericjmx.pp b/manifests/plugin/genericjmx.pp index 1cc2081d8..eb5df8c33 100644 --- a/manifests/plugin/genericjmx.pp +++ b/manifests/plugin/genericjmx.pp @@ -6,7 +6,7 @@ include collectd include collectd::params include collectd::plugin::java - + $class_path = "${collectd::params::java_dir}/collectd-api.jar:${collectd::params::java_dir}/generic-jmx.jar" $config_file = "${collectd::params::plugin_conf_dir}/15-genericjmx.conf" concat { $config_file: diff --git a/spec/classes/collectd_plugin_genericjmx_spec.rb b/spec/classes/collectd_plugin_genericjmx_spec.rb index adc8cee2a..5dc0a84e9 100644 --- a/spec/classes/collectd_plugin_genericjmx_spec.rb +++ b/spec/classes/collectd_plugin_genericjmx_spec.rb @@ -2,11 +2,11 @@ describe 'collectd::plugin::genericjmx', :type => :class do let (:facts) {{ - :osfamily => 'RedHat', + :osfamily => 'Debian', :concat_basedir => tmpfilename('collectd-genericjmx'), }} - let (:config_filename) { '/etc/collectd.d/15-genericjmx.conf' } + let (:config_filename) { '/etc/collectd/conf.d/15-genericjmx.conf' } context ':ensure => present, defaults' do it 'will include the java plugin' do @@ -64,15 +64,15 @@ 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,}/m) + it 'should have ONLY one jvmarg parameter other than classpath' do + should contain_concat__fragment('collectd_plugin_genericjmx_conf_header').without_content(/(.*JVMArg.*){3,}/m) 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/) + it 'should wnot have any jvmarg parameters other than classpath' do + should contain_concat__fragment('collectd_plugin_genericjmx_conf_header').without_content(/(.*JVMArg.*){2,}/m) end end end diff --git a/templates/plugin/genericjmx.conf.header.erb b/templates/plugin/genericjmx.conf.header.erb index 26e52d30e..1a8ec7fc9 100644 --- a/templates/plugin/genericjmx.conf.header.erb +++ b/templates/plugin/genericjmx.conf.header.erb @@ -1,4 +1,5 @@ + JVMArg "-Djava.class.path=<%= @class_path %>" <% Array(@jvmarg).each do |jvmarg| -%> JVMArg "<%= jvmarg %>" <% end -%> From c605da8e9000b2aaf1dc5e478363d598ec8124da Mon Sep 17 00:00:00 2001 From: Jeremy Kitchen Date: Thu, 22 Jan 2015 04:33:06 +0000 Subject: [PATCH 10/13] set java path to undef on unsupported platforms --- manifests/params.pp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/manifests/params.pp b/manifests/params.pp index b7070ea14..0960c6ee6 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -20,7 +20,7 @@ $service_name = 'collectd' $config_file = "${collectd_dir}/collectd.conf" $root_group = 'root' - # FIXME: $java_dir + $java_dir = undef } 'Redhat': { $package = 'collectd' @@ -40,7 +40,7 @@ $service_name = 'collectd' $config_file = '/etc/collectd.conf' $root_group = 'root' - # FIXME: $java_dir + $java_dir = undef } 'FreeBSD': { $package = 'collectd5' @@ -50,7 +50,7 @@ $service_name = 'collectd' $config_file = '/usr/local/etc/collectd.conf' $root_group = 'wheel' - # FIXME: $java_dir + $java_dir = undef } 'Archlinux': { $package = 'collectd' @@ -60,7 +60,7 @@ $service_name = 'collectd' $config_file = '/etc/collectd.conf' $root_group = 'wheel' - # FIXME: $java_dir + $java_dir = undef } 'Gentoo': { $package = 'app-admin/collectd' @@ -70,7 +70,7 @@ $service_name = 'collectd' $config_file = '/etc/collectd.conf' $root_group = 'collectd' - # FIXME: $java_dir + $java_dir = undef } default: { From bb978ee6ed23f6a06e6d826bb1bfe866bc32f3cf Mon Sep 17 00:00:00 2001 From: Jeremy Kitchen Date: Thu, 22 Jan 2015 04:40:46 +0000 Subject: [PATCH 11/13] add id and path facts to test context --- spec/classes/collectd_plugin_genericjmx_spec.rb | 2 ++ spec/defines/collectd_plugin_genericjmx_connection_spec.rb | 2 ++ spec/defines/collectd_plugin_genericjmx_mbean_spec.rb | 2 ++ 3 files changed, 6 insertions(+) diff --git a/spec/classes/collectd_plugin_genericjmx_spec.rb b/spec/classes/collectd_plugin_genericjmx_spec.rb index 5dc0a84e9..c8eb126c5 100644 --- a/spec/classes/collectd_plugin_genericjmx_spec.rb +++ b/spec/classes/collectd_plugin_genericjmx_spec.rb @@ -3,7 +3,9 @@ describe 'collectd::plugin::genericjmx', :type => :class do let (:facts) {{ :osfamily => 'Debian', + :id => 'root', :concat_basedir => tmpfilename('collectd-genericjmx'), + :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', }} let (:config_filename) { '/etc/collectd/conf.d/15-genericjmx.conf' } diff --git a/spec/defines/collectd_plugin_genericjmx_connection_spec.rb b/spec/defines/collectd_plugin_genericjmx_connection_spec.rb index f93427de8..105887c2d 100644 --- a/spec/defines/collectd_plugin_genericjmx_connection_spec.rb +++ b/spec/defines/collectd_plugin_genericjmx_connection_spec.rb @@ -3,7 +3,9 @@ let (:facts) {{ :osfamily => 'Debian', + :id => 'root', :concat_basedir => tmpfilename('collectd-genericjmx-connection'), + :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', }} let (:config_filename) { '/etc/collectd/conf.d/15-genericjmx.conf' } diff --git a/spec/defines/collectd_plugin_genericjmx_mbean_spec.rb b/spec/defines/collectd_plugin_genericjmx_mbean_spec.rb index e1ba5a8e4..de8f961cc 100644 --- a/spec/defines/collectd_plugin_genericjmx_mbean_spec.rb +++ b/spec/defines/collectd_plugin_genericjmx_mbean_spec.rb @@ -4,7 +4,9 @@ let (:facts) {{ :osfamily => 'Debian', + :id => 'root', :concat_basedir => tmpfilename('collectd-genericjmx-mbean'), + :path => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin', }} let (:config_filename) { '/etc/collectd/conf.d/15-genericjmx.conf' } From 606481310bcefce7343c78e7b331b67e6f00db77 Mon Sep 17 00:00:00 2001 From: Jeremy Kitchen Date: Thu, 22 Jan 2015 04:58:54 +0000 Subject: [PATCH 12/13] remove the ensure parameter I don't think it's really necessary anyways, since the conf.d dir is being purged --- manifests/plugin/genericjmx.pp | 2 -- spec/classes/collectd_plugin_genericjmx_spec.rb | 13 ++----------- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/manifests/plugin/genericjmx.pp b/manifests/plugin/genericjmx.pp index eb5df8c33..5db8e89db 100644 --- a/manifests/plugin/genericjmx.pp +++ b/manifests/plugin/genericjmx.pp @@ -1,6 +1,5 @@ # https://collectd.org/wiki/index.php/Plugin:GenericJMX class collectd::plugin::genericjmx ( - $ensure = present, $jvmarg = [], ) { include collectd @@ -10,7 +9,6 @@ $config_file = "${collectd::params::plugin_conf_dir}/15-genericjmx.conf" concat { $config_file: - ensure => $ensure, mode => '0640', owner => 'root', group => $collectd::params::root_group, diff --git a/spec/classes/collectd_plugin_genericjmx_spec.rb b/spec/classes/collectd_plugin_genericjmx_spec.rb index c8eb126c5..2393e3544 100644 --- a/spec/classes/collectd_plugin_genericjmx_spec.rb +++ b/spec/classes/collectd_plugin_genericjmx_spec.rb @@ -10,7 +10,7 @@ let (:config_filename) { '/etc/collectd/conf.d/15-genericjmx.conf' } - context ':ensure => present, defaults' do + context 'defaults' do it 'will include the java plugin' do should contain_class('collectd::plugin::java') end @@ -45,15 +45,6 @@ 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 @@ -73,7 +64,7 @@ context 'jvmarg parameter empty' do let (:params) {{ :jvmarg => [] }} - it 'should wnot have any jvmarg parameters other than classpath' do + it 'should not have any jvmarg parameters other than classpath' do should contain_concat__fragment('collectd_plugin_genericjmx_conf_header').without_content(/(.*JVMArg.*){2,}/m) end end From fcd63aa5631c5627daf95f6ee89f20ee48aa3de3 Mon Sep 17 00:00:00 2001 From: Jeremy Kitchen Date: Sat, 24 Jan 2015 04:51:22 +0000 Subject: [PATCH 13/13] adding README.md docs for java and genericjmx --- README.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/README.md b/README.md index 1d395f39e..40b54fa69 100644 --- a/README.md +++ b/README.md @@ -69,9 +69,11 @@ documentation for each plugin for configurable attributes. * `entropy` (see [collectd::plugin::entropy](#class-collectdpluginentropy) below) * `exec` (see [collectd::plugin::exec](#class-collectdpluginexec) below) * `filecount` (see [collectd::plugin::filecount](#class-collectdpluginfilecount) below) +* `genericjmx` (see [collectd::plugin::genericjmx](#class-collectdplugingenericjmx) below) * `interface` (see [collectd::plugin::interface](#class-collectdplugininterface) below) * `iptables` (see [collectd::plugin::iptables](#class-collectdpluginiptables) below) * `irq` (see [collectd::plugin::irq](#class-collectdpluginirq) below) +* `java` (see [collectd::plugin::java](#class-collectdpluginjava) below) * `load` (see [collectd::plugin::load](#class-collectdpluginload) below) * `logfile` (see [collectd::plugin::logfile](#class-collectdpluginlogfile) below) * `libvirt` (see [collectd::plugin::libvirt](#class-collectdpluginlibvirt) below) @@ -302,6 +304,41 @@ class { 'collectd::plugin::filecount': }, } ``` + +####Class: `collectd::plugin::genericjmx` + +```puppet +include collectd::plugin::genericjmx + +collectd::plugin::genericjmx::mbean { + 'garbage_collector': + object_name => 'java.lang:type=GarbageCollector,*', + instance_prefix => 'gc-', + instance_from => 'name', + values => [ + { + type => 'invocations', + table => false, + attribute => 'CollectionCount', + }, + { + type => 'total_time_in_ms', + instance_prefix => 'collection_time', + table => false, + attribute => 'CollectionTime', + }, + ]; +} + +collectd::plugin::genericjmx::connection { + 'java_app': + host => $fqdn, + service_url => 'service:jmx:rmi:///jndi/rmi://localhost:3637/jmxrmi', + collect => [ 'memory-heap', 'memory-nonheap','garbage_collector' ], +} + +``` + ####Class: `collectd::plugin::interface` ```puppet @@ -331,6 +368,12 @@ class { 'collectd::plugin::iptables': } ``` +####Class: `collectd::plugin::java` + +```puppet +class { 'collectd::plugin::java': } +``` + ####Class: `collectd::plugin::load` ```puppet