From e745233412c73ff564aea062e190d2b8bcf80d35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Wed, 11 Jul 2018 14:54:58 +0200 Subject: [PATCH 1/5] Make the threshold plugin configurable ```puppet class { 'collectd::plugin::processes': process_matches => [ 'carbon-cache', 'python.+carbon-cache', ], } class { 'collectd::plugin::threshold': plugins => { processes => { instance => 'carbon-cache', types => { data_source => 'processes', warning_min => 2, failure_min => 1, }, }, }, } ``` Fixes #292 (Above example from this issue). --- functions/indent.pp | 4 + manifests/plugin/threshold.pp | 4 + .../classes/collectd_plugin_threshold_spec.rb | 78 +++++++++++++++++++ templates/plugin/threshold.conf.epp | 11 +++ templates/plugin/threshold/host.epp | 8 ++ templates/plugin/threshold/plugin.epp | 8 ++ templates/plugin/threshold/type.epp | 41 ++++++++++ types/threshold/host.pp | 4 + types/threshold/plugin.pp | 4 + types/threshold/type.pp | 15 ++++ 10 files changed, 177 insertions(+) create mode 100644 functions/indent.pp create mode 100644 templates/plugin/threshold.conf.epp create mode 100644 templates/plugin/threshold/host.epp create mode 100644 templates/plugin/threshold/plugin.epp create mode 100644 templates/plugin/threshold/type.epp create mode 100644 types/threshold/host.pp create mode 100644 types/threshold/plugin.pp create mode 100644 types/threshold/type.pp diff --git a/functions/indent.pp b/functions/indent.pp new file mode 100644 index 000000000..624a79fd1 --- /dev/null +++ b/functions/indent.pp @@ -0,0 +1,4 @@ +function collectd::indent(String $arg) >> String { + $body = regsubst($arg, "\n(.)", "\n \\1", 'G') + " ${body}" +} diff --git a/manifests/plugin/threshold.pp b/manifests/plugin/threshold.pp index 1095ec90b..53d86fbc6 100644 --- a/manifests/plugin/threshold.pp +++ b/manifests/plugin/threshold.pp @@ -2,12 +2,16 @@ class collectd::plugin::threshold ( $ensure = 'present', $interval = undef, + Hash[String, Collectd::Threshold::Type] $types = {}, + Hash[String, Collectd::Threshold::Plugin] $plugins = {}, + Hash[String, Collectd::Threshold::Host] $hosts = {}, ) { include ::collectd collectd::plugin { 'threshold': ensure => $ensure, + content => epp('collectd/plugin/threshold.conf.epp'), interval => $interval, } } diff --git a/spec/classes/collectd_plugin_threshold_spec.rb b/spec/classes/collectd_plugin_threshold_spec.rb index bc65e278b..1bcf7c795 100644 --- a/spec/classes/collectd_plugin_threshold_spec.rb +++ b/spec/classes/collectd_plugin_threshold_spec.rb @@ -7,6 +7,58 @@ facts end + let :params do + { + 'types' => { + 'foo' => { + 'warning_min' => 0.00, + 'warning_max' => 1000.00, + 'failure_min' => 0.00, + 'failure_max' => 1200.00, + 'invert' => false, + 'instance' => 'bar' + } + }, + 'plugins' => { + 'interface' => { + 'instance' => 'eth0', + 'types' => { + 'if_octets' => { + 'failure_max' => 10_000_000, + 'data_source' => 'rx' + } + } + } + }, + 'hosts' => { + 'hostname' => { + 'types' => { + 'cpu' => { + 'instance' => 'idle', + 'failure_min' => 10 + }, + 'load' => { + 'data_source' => 'midterm', + 'failure_max' => 4, + 'hits' => 3, + 'hysteresis' => 3 + } + }, + 'plugins' => { + 'memory' => { + 'types' => { + 'memory' => { + 'instance' => 'cached', + 'warning_min' => 100_000_000 + } + } + } + } + } + } + } + end + options = os_specific_options(facts) context ':ensure => present' do context ':ensure => present and default parameters' do @@ -17,6 +69,32 @@ content: %r{LoadPlugin threshold} ) end + + it { is_expected.to contain_file('threshold.load').with(content: %r{}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{WarningMin 0\.0}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{WarningMax 1000\.0}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{FailureMin 0\.0}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{FailureMax 1200\.0}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{Invert false}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{Instance "bar"}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{Instance "eth0"}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{FailureMax 10000000}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{DataSource "rx"}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{Instance "idle"}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{FailureMin 10}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{Instance "cached"}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{WarningMin 100000000}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{DataSource "midterm"}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{FailureMax 4}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{Hits 3}) } + it { is_expected.to contain_file('threshold.load').with(content: %r{Hysteresis 3}) } end end end diff --git a/templates/plugin/threshold.conf.epp b/templates/plugin/threshold.conf.epp new file mode 100644 index 000000000..1323d9835 --- /dev/null +++ b/templates/plugin/threshold.conf.epp @@ -0,0 +1,11 @@ + + <%- $collectd::plugin::threshold::hosts.each |$name, $values| { -%> +<%= collectd::indent(epp('collectd/plugin/threshold/host.epp', {name => $name, values => $values })) -%> + <%- } -%> + <%- $collectd::plugin::threshold::plugins.each |$name, $values| { -%> +<%= collectd::indent(epp('collectd/plugin/threshold/plugin.epp', {name => $name, values => $values })) -%> + <%- } -%> + <%- $collectd::plugin::threshold::types.each |$name, $values| { -%> +<%= collectd::indent(epp('collectd/plugin/threshold/type.epp', {name => $name, values => $values })) -%> + <%- } -%> + diff --git a/templates/plugin/threshold/host.epp b/templates/plugin/threshold/host.epp new file mode 100644 index 000000000..84388d276 --- /dev/null +++ b/templates/plugin/threshold/host.epp @@ -0,0 +1,8 @@ +"> + <%- $values['plugins'].each |$name, $values| { -%> +<%= collectd::indent(epp('collectd/plugin/threshold/plugin.epp', {name => $name, values => $values })) -%> + <%- } -%> + <%- $values['types'].each |$name, $values| { -%> +<%= collectd::indent(epp('collectd/plugin/threshold/type.epp', {name => $name, values => $values })) -%> + <%- } -%> + diff --git a/templates/plugin/threshold/plugin.epp b/templates/plugin/threshold/plugin.epp new file mode 100644 index 000000000..f4c7edbc0 --- /dev/null +++ b/templates/plugin/threshold/plugin.epp @@ -0,0 +1,8 @@ +"> + <%- if $values['instance'] != undef { -%> + Instance "<%= $values['instance'] %>" + <%- } -%> + <%- $values['types'].each |$name, $values| { -%> +<%= collectd::indent(epp('collectd/plugin/threshold/type.epp', {name => $name, values => $values })) -%> + <%- } -%> + diff --git a/templates/plugin/threshold/type.epp b/templates/plugin/threshold/type.epp new file mode 100644 index 000000000..9bf196c5d --- /dev/null +++ b/templates/plugin/threshold/type.epp @@ -0,0 +1,41 @@ +"> + <%- if $values['instance'] != undef { -%> + Instance "<%= $values['instance'] %>" + <%- } -%> + <%- if $values['failure_max'] != undef { -%> + FailureMax <%= $values['failure_max'] %> + <%- } -%> + <%- if $values['failure_min'] != undef { -%> + FailureMin <%= $values['failure_min'] %> + <%- } -%> + <%- if $values['warning_max'] != undef { -%> + WarningMax <%= $values['warning_max'] %> + <%- } -%> + <%- if $values['warning_min'] != undef { -%> + WarningMin <%= $values['warning_min'] %> + <%- } -%> + <%- if $values['data_source'] != undef { -%> + DataSource "<%= $values['data_source'] %>" + <%- } -%> + <%- if $values['invert'] != undef { -%> + Invert <%= $values['invert'] %> + <%- } -%> + <%- if $values['persist'] != undef { -%> + Persist <%= $values['persist'] %> + <%- } -%> + <%- if $values['persist_ok'] != undef { -%> + PersistOK <%= $values['persist_ok'] %> + <%- } -%> + <%- if $values['percentage'] != undef { -%> + Percentage <%= $values['percentage'] %> + <%- } -%> + <%- if $values['hits'] != undef { -%> + Hits <%= $values['hits'] %> + <%- } -%> + <%- if $values['hysteresis'] != undef { -%> + Hysteresis <%= $values['hysteresis'] %> + <%- } -%> + <%- if $values['interesting'] != undef { -%> + Interesting <%= $values['interesting'] %> + <%- } -%> + diff --git a/types/threshold/host.pp b/types/threshold/host.pp new file mode 100644 index 000000000..ddc466068 --- /dev/null +++ b/types/threshold/host.pp @@ -0,0 +1,4 @@ +type Collectd::Threshold::Host = Struct[{ + plugins => Hash[String, Collectd::Threshold::Plugin], + types => Hash[String, Collectd::Threshold::Type], +}] diff --git a/types/threshold/plugin.pp b/types/threshold/plugin.pp new file mode 100644 index 000000000..0bd849df7 --- /dev/null +++ b/types/threshold/plugin.pp @@ -0,0 +1,4 @@ +type Collectd::Threshold::Plugin = Struct[{ + instance => Optional[String], + types => Hash[String, Collectd::Threshold::Type], +}] diff --git a/types/threshold/type.pp b/types/threshold/type.pp new file mode 100644 index 000000000..7bde5faef --- /dev/null +++ b/types/threshold/type.pp @@ -0,0 +1,15 @@ +type Collectd::Threshold::Type = Struct[{ + instance => Optional[String], + failure_max => Optional[Numeric], + warning_max => Optional[Numeric], + failure_min => Optional[Numeric], + warning_min => Optional[Numeric], + data_source => Optional[String], + invert => Optional[Boolean], + persist => Optional[Boolean], + persist_ok => Optional[Boolean], + percentage => Optional[Boolean], + hits => Optional[Integer], + hysteresis => Optional[Integer], + interesting => Optional[Boolean], +}] From 49173a35d3c60eb53f1a70c9979d763f543ee5fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Wed, 11 Jul 2018 18:10:34 +0200 Subject: [PATCH 2/5] Do not allow empty strings to be passed --- manifests/plugin/threshold.pp | 6 +++--- types/threshold/host.pp | 4 ++-- types/threshold/plugin.pp | 4 ++-- types/threshold/type.pp | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/manifests/plugin/threshold.pp b/manifests/plugin/threshold.pp index 53d86fbc6..ea2e6c137 100644 --- a/manifests/plugin/threshold.pp +++ b/manifests/plugin/threshold.pp @@ -2,9 +2,9 @@ class collectd::plugin::threshold ( $ensure = 'present', $interval = undef, - Hash[String, Collectd::Threshold::Type] $types = {}, - Hash[String, Collectd::Threshold::Plugin] $plugins = {}, - Hash[String, Collectd::Threshold::Host] $hosts = {}, + Hash[String[1], Collectd::Threshold::Type] $types = {}, + Hash[String[1], Collectd::Threshold::Plugin] $plugins = {}, + Hash[String[1], Collectd::Threshold::Host] $hosts = {}, ) { include ::collectd diff --git a/types/threshold/host.pp b/types/threshold/host.pp index ddc466068..c5ca75c0a 100644 --- a/types/threshold/host.pp +++ b/types/threshold/host.pp @@ -1,4 +1,4 @@ type Collectd::Threshold::Host = Struct[{ - plugins => Hash[String, Collectd::Threshold::Plugin], - types => Hash[String, Collectd::Threshold::Type], + plugins => Hash[String[1], Collectd::Threshold::Plugin], + types => Hash[String[1], Collectd::Threshold::Type], }] diff --git a/types/threshold/plugin.pp b/types/threshold/plugin.pp index 0bd849df7..6143a07fc 100644 --- a/types/threshold/plugin.pp +++ b/types/threshold/plugin.pp @@ -1,4 +1,4 @@ type Collectd::Threshold::Plugin = Struct[{ - instance => Optional[String], - types => Hash[String, Collectd::Threshold::Type], + instance => Optional[String[1]], + types => Hash[String[1], Collectd::Threshold::Type], }] diff --git a/types/threshold/type.pp b/types/threshold/type.pp index 7bde5faef..7312ba064 100644 --- a/types/threshold/type.pp +++ b/types/threshold/type.pp @@ -1,10 +1,10 @@ type Collectd::Threshold::Type = Struct[{ - instance => Optional[String], + instance => Optional[String[1]], failure_max => Optional[Numeric], warning_max => Optional[Numeric], failure_min => Optional[Numeric], warning_min => Optional[Numeric], - data_source => Optional[String], + data_source => Optional[String[1]], invert => Optional[Boolean], persist => Optional[Boolean], persist_ok => Optional[Boolean], From 425ae23b6d0249bc9fe4bada66fab0a55d7d1dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Fri, 13 Jul 2018 09:16:31 +0200 Subject: [PATCH 3/5] Allow to configure blocks with the same name Change most data types from Hash to Array to allow multiple blocks with the same name. The key of each Hash is now the 'name' of each Struct. While here, add an usage example to the README. --- README.md | 36 +++++++++++++ manifests/plugin/threshold.pp | 6 +-- .../classes/collectd_plugin_threshold_spec.rb | 52 ++++++++++-------- templates/plugin/threshold.conf.epp | 12 ++--- templates/plugin/threshold/host.epp | 10 ++-- templates/plugin/threshold/plugin.epp | 10 ++-- templates/plugin/threshold/type.epp | 54 +++++++++---------- types/threshold/host.pp | 5 +- types/threshold/plugin.pp | 3 +- types/threshold/type.pp | 1 + 10 files changed, 118 insertions(+), 71 deletions(-) diff --git a/README.md b/README.md index 9acafdd57..6f692d1eb 100644 --- a/README.md +++ b/README.md @@ -1801,6 +1801,42 @@ class { '::collectd::plugin::thermal': ```puppet class { 'collectd::plugin::threshold': + hosts => [ + { + name => 'example.com', + plugins => [ + { + name => 'load', + types => [ + { + name => 'load', + data_source => 'shortterm', + warning_max => $facts.dig('processors', 'count') * 1.2, + failure_max => $facts.dig('processors', 'count') * 1.9, + }, + { + name => 'load', + data_source => 'midterm', + warning_max => $facts.dig('processors', 'count') * 1.1, + failure_max => $facts.dig('processors', 'count') * 1.7, + }, + { + name => 'load', + data_source => 'longterm', + warning_max => $facts.dig('processors', 'count'), + failure_max => $facts.dig('processors', 'count') * 1.5, + }, + ], + }, + ], + }, + ], + plugins => [ + # See plugin definition above + ], + types => [ + # See types definition above + ], } ``` diff --git a/manifests/plugin/threshold.pp b/manifests/plugin/threshold.pp index ea2e6c137..043f139c0 100644 --- a/manifests/plugin/threshold.pp +++ b/manifests/plugin/threshold.pp @@ -2,9 +2,9 @@ class collectd::plugin::threshold ( $ensure = 'present', $interval = undef, - Hash[String[1], Collectd::Threshold::Type] $types = {}, - Hash[String[1], Collectd::Threshold::Plugin] $plugins = {}, - Hash[String[1], Collectd::Threshold::Host] $hosts = {}, + Array[Collectd::Threshold::Type] $types = [], + Array[Collectd::Threshold::Plugin] $plugins = [], + Array[Collectd::Threshold::Host] $hosts = [], ) { include ::collectd diff --git a/spec/classes/collectd_plugin_threshold_spec.rb b/spec/classes/collectd_plugin_threshold_spec.rb index 1bcf7c795..a1431b360 100644 --- a/spec/classes/collectd_plugin_threshold_spec.rb +++ b/spec/classes/collectd_plugin_threshold_spec.rb @@ -9,8 +9,9 @@ let :params do { - 'types' => { - 'foo' => { + 'types' => [ + { + 'name' => 'foo', 'warning_min' => 0.00, 'warning_max' => 1000.00, 'failure_min' => 0.00, @@ -18,44 +19,51 @@ 'invert' => false, 'instance' => 'bar' } - }, - 'plugins' => { - 'interface' => { + ], + 'plugins' => [ + { + 'name' => 'interface', 'instance' => 'eth0', - 'types' => { - 'if_octets' => { + 'types' => [ + { + 'name' => 'if_octets', 'failure_max' => 10_000_000, 'data_source' => 'rx' } - } + ] } - }, - 'hosts' => { - 'hostname' => { - 'types' => { - 'cpu' => { + ], + 'hosts' => [ + { + 'name' => 'hostname', + 'types' => [ + { + 'name' => 'cpu', 'instance' => 'idle', 'failure_min' => 10 }, - 'load' => { + { + 'name' => 'load', 'data_source' => 'midterm', 'failure_max' => 4, 'hits' => 3, 'hysteresis' => 3 } - }, - 'plugins' => { - 'memory' => { - 'types' => { - 'memory' => { + ], + 'plugins' => [ + { + 'name' => 'memory', + 'types' => [ + { + 'name' => 'memory', 'instance' => 'cached', 'warning_min' => 100_000_000 } - } + ] } - } + ] } - } + ] } end diff --git a/templates/plugin/threshold.conf.epp b/templates/plugin/threshold.conf.epp index 1323d9835..844ef602c 100644 --- a/templates/plugin/threshold.conf.epp +++ b/templates/plugin/threshold.conf.epp @@ -1,11 +1,11 @@ - <%- $collectd::plugin::threshold::hosts.each |$name, $values| { -%> -<%= collectd::indent(epp('collectd/plugin/threshold/host.epp', {name => $name, values => $values })) -%> + <%- $collectd::plugin::threshold::hosts.each |$host| { -%> +<%= collectd::indent(epp('collectd/plugin/threshold/host.epp', { host => $host })) -%> <%- } -%> - <%- $collectd::plugin::threshold::plugins.each |$name, $values| { -%> -<%= collectd::indent(epp('collectd/plugin/threshold/plugin.epp', {name => $name, values => $values })) -%> + <%- $collectd::plugin::threshold::plugins.each |$plugin| { -%> +<%= collectd::indent(epp('collectd/plugin/threshold/plugin.epp', { plugin => $plugin })) -%> <%- } -%> - <%- $collectd::plugin::threshold::types.each |$name, $values| { -%> -<%= collectd::indent(epp('collectd/plugin/threshold/type.epp', {name => $name, values => $values })) -%> + <%- $collectd::plugin::threshold::types.each |$type| { -%> +<%= collectd::indent(epp('collectd/plugin/threshold/type.epp', { type => $type })) -%> <%- } -%> diff --git a/templates/plugin/threshold/host.epp b/templates/plugin/threshold/host.epp index 84388d276..4571da01b 100644 --- a/templates/plugin/threshold/host.epp +++ b/templates/plugin/threshold/host.epp @@ -1,8 +1,8 @@ -"> - <%- $values['plugins'].each |$name, $values| { -%> -<%= collectd::indent(epp('collectd/plugin/threshold/plugin.epp', {name => $name, values => $values })) -%> +"> + <%- $host['plugins'].each |$plugin| { -%> +<%= collectd::indent(epp('collectd/plugin/threshold/plugin.epp', { plugin => $plugin })) -%> <%- } -%> - <%- $values['types'].each |$name, $values| { -%> -<%= collectd::indent(epp('collectd/plugin/threshold/type.epp', {name => $name, values => $values })) -%> + <%- $host['types'].each |$type| { -%> +<%= collectd::indent(epp('collectd/plugin/threshold/type.epp', { type => $type })) -%> <%- } -%> diff --git a/templates/plugin/threshold/plugin.epp b/templates/plugin/threshold/plugin.epp index f4c7edbc0..c2b8e0134 100644 --- a/templates/plugin/threshold/plugin.epp +++ b/templates/plugin/threshold/plugin.epp @@ -1,8 +1,8 @@ -"> - <%- if $values['instance'] != undef { -%> - Instance "<%= $values['instance'] %>" +"> + <%- if $plugin['instance'] != undef { -%> + Instance "<%= $plugin['instance'] %>" <%- } -%> - <%- $values['types'].each |$name, $values| { -%> -<%= collectd::indent(epp('collectd/plugin/threshold/type.epp', {name => $name, values => $values })) -%> + <%- $plugin['types'].each |$type| { -%> +<%= collectd::indent(epp('collectd/plugin/threshold/type.epp', { type => $type })) -%> <%- } -%> diff --git a/templates/plugin/threshold/type.epp b/templates/plugin/threshold/type.epp index 9bf196c5d..d6b8fe220 100644 --- a/templates/plugin/threshold/type.epp +++ b/templates/plugin/threshold/type.epp @@ -1,41 +1,41 @@ -"> - <%- if $values['instance'] != undef { -%> - Instance "<%= $values['instance'] %>" +"> + <%- if $type['instance'] != undef { -%> + Instance "<%= $type['instance'] %>" <%- } -%> - <%- if $values['failure_max'] != undef { -%> - FailureMax <%= $values['failure_max'] %> + <%- if $type['failure_max'] != undef { -%> + FailureMax <%= $type['failure_max'] %> <%- } -%> - <%- if $values['failure_min'] != undef { -%> - FailureMin <%= $values['failure_min'] %> + <%- if $type['failure_min'] != undef { -%> + FailureMin <%= $type['failure_min'] %> <%- } -%> - <%- if $values['warning_max'] != undef { -%> - WarningMax <%= $values['warning_max'] %> + <%- if $type['warning_max'] != undef { -%> + WarningMax <%= $type['warning_max'] %> <%- } -%> - <%- if $values['warning_min'] != undef { -%> - WarningMin <%= $values['warning_min'] %> + <%- if $type['warning_min'] != undef { -%> + WarningMin <%= $type['warning_min'] %> <%- } -%> - <%- if $values['data_source'] != undef { -%> - DataSource "<%= $values['data_source'] %>" + <%- if $type['data_source'] != undef { -%> + DataSource "<%= $type['data_source'] %>" <%- } -%> - <%- if $values['invert'] != undef { -%> - Invert <%= $values['invert'] %> + <%- if $type['invert'] != undef { -%> + Invert <%= $type['invert'] %> <%- } -%> - <%- if $values['persist'] != undef { -%> - Persist <%= $values['persist'] %> + <%- if $type['persist'] != undef { -%> + Persist <%= $type['persist'] %> <%- } -%> - <%- if $values['persist_ok'] != undef { -%> - PersistOK <%= $values['persist_ok'] %> + <%- if $type['persist_ok'] != undef { -%> + PersistOK <%= $type['persist_ok'] %> <%- } -%> - <%- if $values['percentage'] != undef { -%> - Percentage <%= $values['percentage'] %> + <%- if $type['percentage'] != undef { -%> + Percentage <%= $type['percentage'] %> <%- } -%> - <%- if $values['hits'] != undef { -%> - Hits <%= $values['hits'] %> + <%- if $type['hits'] != undef { -%> + Hits <%= $type['hits'] %> <%- } -%> - <%- if $values['hysteresis'] != undef { -%> - Hysteresis <%= $values['hysteresis'] %> + <%- if $type['hysteresis'] != undef { -%> + Hysteresis <%= $type['hysteresis'] %> <%- } -%> - <%- if $values['interesting'] != undef { -%> - Interesting <%= $values['interesting'] %> + <%- if $type['interesting'] != undef { -%> + Interesting <%= $type['interesting'] %> <%- } -%> diff --git a/types/threshold/host.pp b/types/threshold/host.pp index c5ca75c0a..5ed2e17c4 100644 --- a/types/threshold/host.pp +++ b/types/threshold/host.pp @@ -1,4 +1,5 @@ type Collectd::Threshold::Host = Struct[{ - plugins => Hash[String[1], Collectd::Threshold::Plugin], - types => Hash[String[1], Collectd::Threshold::Type], + name => String[1], + plugins => Array[Collectd::Threshold::Plugin], + types => Array[Collectd::Threshold::Type], }] diff --git a/types/threshold/plugin.pp b/types/threshold/plugin.pp index 6143a07fc..4876909ba 100644 --- a/types/threshold/plugin.pp +++ b/types/threshold/plugin.pp @@ -1,4 +1,5 @@ type Collectd::Threshold::Plugin = Struct[{ + name => String[1], instance => Optional[String[1]], - types => Hash[String[1], Collectd::Threshold::Type], + types => Array[Collectd::Threshold::Type], }] diff --git a/types/threshold/type.pp b/types/threshold/type.pp index 7312ba064..4cc8cc70d 100644 --- a/types/threshold/type.pp +++ b/types/threshold/type.pp @@ -1,4 +1,5 @@ type Collectd::Threshold::Type = Struct[{ + name => String[1], instance => Optional[String[1]], failure_max => Optional[Numeric], warning_max => Optional[Numeric], From 29a87d1a44acb80b238e22ca2168032025cb5c9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Fri, 13 Jul 2018 09:36:01 +0200 Subject: [PATCH 4/5] Make hosts / plugins / types always optional The previous configuration did not allowed to create a host with only plugins or types which is quite common, just like the plugin allows to not pass hosts, plugins or types. For consistency, never require any "child" Struct. While here, extend and reorder the tests. --- .../classes/collectd_plugin_threshold_spec.rb | 27 ++++++++++++++++++- templates/plugin/threshold.conf.epp | 6 ++--- templates/plugin/threshold/host.epp | 4 +-- templates/plugin/threshold/plugin.epp | 2 +- types/threshold/host.pp | 4 +-- types/threshold/plugin.pp | 2 +- 6 files changed, 35 insertions(+), 10 deletions(-) diff --git a/spec/classes/collectd_plugin_threshold_spec.rb b/spec/classes/collectd_plugin_threshold_spec.rb index a1431b360..db5eb569d 100644 --- a/spec/classes/collectd_plugin_threshold_spec.rb +++ b/spec/classes/collectd_plugin_threshold_spec.rb @@ -7,7 +7,7 @@ facts end - let :params do + let :custom_params do { 'types' => [ { @@ -77,6 +77,31 @@ content: %r{LoadPlugin threshold} ) end + end + + context ':ensure => present and empty configurations' do + let :params do + { + 'hosts' => [ + { + 'name' => 'example.com' + } + ], + 'plugins' => [ + { + 'name' => 'interface' + } + ] + } + end + + it { is_expected.to compile } + end + + context ':ensure => present and custom parameters' do + let :params do + custom_params + end it { is_expected.to contain_file('threshold.load').with(content: %r{}) } it { is_expected.to contain_file('threshold.load').with(content: %r{WarningMin 0\.0}) } diff --git a/templates/plugin/threshold.conf.epp b/templates/plugin/threshold.conf.epp index 844ef602c..d4f8cb97b 100644 --- a/templates/plugin/threshold.conf.epp +++ b/templates/plugin/threshold.conf.epp @@ -1,11 +1,11 @@ - <%- $collectd::plugin::threshold::hosts.each |$host| { -%> + <%- $collectd::plugin::threshold::hosts.lest || { [] }.each |$host| { -%> <%= collectd::indent(epp('collectd/plugin/threshold/host.epp', { host => $host })) -%> <%- } -%> - <%- $collectd::plugin::threshold::plugins.each |$plugin| { -%> + <%- $collectd::plugin::threshold::plugins.lest || { [] }.each |$plugin| { -%> <%= collectd::indent(epp('collectd/plugin/threshold/plugin.epp', { plugin => $plugin })) -%> <%- } -%> - <%- $collectd::plugin::threshold::types.each |$type| { -%> + <%- $collectd::plugin::threshold::types.lest || { [] }.each |$type| { -%> <%= collectd::indent(epp('collectd/plugin/threshold/type.epp', { type => $type })) -%> <%- } -%> diff --git a/templates/plugin/threshold/host.epp b/templates/plugin/threshold/host.epp index 4571da01b..1ba4cd80b 100644 --- a/templates/plugin/threshold/host.epp +++ b/templates/plugin/threshold/host.epp @@ -1,8 +1,8 @@ "> - <%- $host['plugins'].each |$plugin| { -%> + <%- $host['plugins'].lest || { [] }.each |$plugin| { -%> <%= collectd::indent(epp('collectd/plugin/threshold/plugin.epp', { plugin => $plugin })) -%> <%- } -%> - <%- $host['types'].each |$type| { -%> + <%- $host['types'].lest || { [] }.each |$type| { -%> <%= collectd::indent(epp('collectd/plugin/threshold/type.epp', { type => $type })) -%> <%- } -%> diff --git a/templates/plugin/threshold/plugin.epp b/templates/plugin/threshold/plugin.epp index c2b8e0134..ce5f9df9d 100644 --- a/templates/plugin/threshold/plugin.epp +++ b/templates/plugin/threshold/plugin.epp @@ -2,7 +2,7 @@ <%- if $plugin['instance'] != undef { -%> Instance "<%= $plugin['instance'] %>" <%- } -%> - <%- $plugin['types'].each |$type| { -%> + <%- $plugin['types'].lest || { [] }.each |$type| { -%> <%= collectd::indent(epp('collectd/plugin/threshold/type.epp', { type => $type })) -%> <%- } -%> diff --git a/types/threshold/host.pp b/types/threshold/host.pp index 5ed2e17c4..2902722d8 100644 --- a/types/threshold/host.pp +++ b/types/threshold/host.pp @@ -1,5 +1,5 @@ type Collectd::Threshold::Host = Struct[{ name => String[1], - plugins => Array[Collectd::Threshold::Plugin], - types => Array[Collectd::Threshold::Type], + plugins => Optional[Array[Collectd::Threshold::Plugin]], + types => Optional[Array[Collectd::Threshold::Type]], }] diff --git a/types/threshold/plugin.pp b/types/threshold/plugin.pp index 4876909ba..e31501f71 100644 --- a/types/threshold/plugin.pp +++ b/types/threshold/plugin.pp @@ -1,5 +1,5 @@ type Collectd::Threshold::Plugin = Struct[{ name => String[1], instance => Optional[String[1]], - types => Array[Collectd::Threshold::Type], + types => Optional[Array[Collectd::Threshold::Type]], }] From dd7fb6d4fc8bb72301fd4ed80d894b787931f3e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Sun, 15 Jul 2018 15:46:18 +0200 Subject: [PATCH 5/5] Add missing data types for the threshold plugin While here, align all parameters and default values. --- manifests/plugin/threshold.pp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/manifests/plugin/threshold.pp b/manifests/plugin/threshold.pp index 043f139c0..4bb8db780 100644 --- a/manifests/plugin/threshold.pp +++ b/manifests/plugin/threshold.pp @@ -1,10 +1,10 @@ # http://collectd.org/documentation/manpages/collectd.conf.5.shtml#plugin_threshold class collectd::plugin::threshold ( - $ensure = 'present', - $interval = undef, - Array[Collectd::Threshold::Type] $types = [], - Array[Collectd::Threshold::Plugin] $plugins = [], - Array[Collectd::Threshold::Host] $hosts = [], + Enum['present', 'absent'] $ensure = 'present', + Optional[Integer] $interval = undef, + Array[Collectd::Threshold::Type] $types = [], + Array[Collectd::Threshold::Plugin] $plugins = [], + Array[Collectd::Threshold::Host] $hosts = [], ) { include ::collectd