From a1a3b325becdef712ed01aa3bffa6d0b38f071b1 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] 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 => 16, failure_min => 12, }, }, }, } ``` 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], +}]