From 10220d790eb1e615ee60ccc238f409712c293039 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] 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..89deed2e1 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],