Skip to content

Commit

Permalink
Make the threshold plugin configurable
Browse files Browse the repository at this point in the history
```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).
  • Loading branch information
smortex committed Jul 11, 2018
1 parent 5748e4a commit a1a3b32
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 0 deletions.
4 changes: 4 additions & 0 deletions functions/indent.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function collectd::indent(String $arg) >> String {
$body = regsubst($arg, "\n(.)", "\n \\1", 'G')
" ${body}"
}
4 changes: 4 additions & 0 deletions manifests/plugin/threshold.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
78 changes: 78 additions & 0 deletions spec/classes/collectd_plugin_threshold_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,6 +69,32 @@
content: %r{LoadPlugin threshold}
)
end

it { is_expected.to contain_file('threshold.load').with(content: %r{<Type "foo">}) }
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{<Plugin "interface">}) }
it { is_expected.to contain_file('threshold.load').with(content: %r{Instance "eth0"}) }
it { is_expected.to contain_file('threshold.load').with(content: %r{<Type "if_octets">}) }
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{<Host "hostname">}) }
it { is_expected.to contain_file('threshold.load').with(content: %r{<Type "cpu">}) }
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{<Plugin "memory">}) }
it { is_expected.to contain_file('threshold.load').with(content: %r{<Type "memory">}) }
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{<Type "load">}) }
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
Expand Down
11 changes: 11 additions & 0 deletions templates/plugin/threshold.conf.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Plugin "threshold">
<%- $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 })) -%>
<%- } -%>
</Plugin>
8 changes: 8 additions & 0 deletions templates/plugin/threshold/host.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Host "<%= $name %>">
<%- $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 })) -%>
<%- } -%>
</Host>
8 changes: 8 additions & 0 deletions templates/plugin/threshold/plugin.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Plugin "<%= $name %>">
<%- if $values['instance'] != undef { -%>
Instance "<%= $values['instance'] %>"
<%- } -%>
<%- $values['types'].each |$name, $values| { -%>
<%= collectd::indent(epp('collectd/plugin/threshold/type.epp', {name => $name, values => $values })) -%>
<%- } -%>
</Plugin>
41 changes: 41 additions & 0 deletions templates/plugin/threshold/type.epp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<Type "<%= $name %>">
<%- 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'] %>
<%- } -%>
</Type>
4 changes: 4 additions & 0 deletions types/threshold/host.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Collectd::Threshold::Host = Struct[{
plugins => Hash[String, Collectd::Threshold::Plugin],
types => Hash[String, Collectd::Threshold::Type],
}]
4 changes: 4 additions & 0 deletions types/threshold/plugin.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Collectd::Threshold::Plugin = Struct[{
instance => Optional[String],
types => Hash[String, Collectd::Threshold::Type],
}]
15 changes: 15 additions & 0 deletions types/threshold/type.pp
Original file line number Diff line number Diff line change
@@ -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],
}]

0 comments on commit a1a3b32

Please sign in to comment.