Skip to content

Commit

Permalink
Merge pull request #829 from smortex/threshold
Browse files Browse the repository at this point in the history
Make the threshold plugin configurable
  • Loading branch information
bastelfreak authored Jul 15, 2018
2 parents eeffda6 + dd7fb6d commit 8a3b009
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 2 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
],
}
```

Expand Down
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}"
}
8 changes: 6 additions & 2 deletions manifests/plugin/threshold.pp
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# http://collectd.org/documentation/manpages/collectd.conf.5.shtml#plugin_threshold
class collectd::plugin::threshold (
$ensure = 'present',
$interval = undef,
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

collectd::plugin { 'threshold':
ensure => $ensure,
content => epp('collectd/plugin/threshold.conf.epp'),
interval => $interval,
}
}
111 changes: 111 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,66 @@
facts
end

let :custom_params do
{
'types' => [
{
'name' => 'foo',
'warning_min' => 0.00,
'warning_max' => 1000.00,
'failure_min' => 0.00,
'failure_max' => 1200.00,
'invert' => false,
'instance' => 'bar'
}
],
'plugins' => [
{
'name' => 'interface',
'instance' => 'eth0',
'types' => [
{
'name' => 'if_octets',
'failure_max' => 10_000_000,
'data_source' => 'rx'
}
]
}
],
'hosts' => [
{
'name' => 'hostname',
'types' => [
{
'name' => 'cpu',
'instance' => 'idle',
'failure_min' => 10
},
{
'name' => 'load',
'data_source' => 'midterm',
'failure_max' => 4,
'hits' => 3,
'hysteresis' => 3
}
],
'plugins' => [
{
'name' => 'memory',
'types' => [
{
'name' => '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 @@ -18,6 +78,57 @@
)
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{<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
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.lest || { [] }.each |$host| { -%>
<%= collectd::indent(epp('collectd/plugin/threshold/host.epp', { host => $host })) -%>
<%- } -%>
<%- $collectd::plugin::threshold::plugins.lest || { [] }.each |$plugin| { -%>
<%= collectd::indent(epp('collectd/plugin/threshold/plugin.epp', { plugin => $plugin })) -%>
<%- } -%>
<%- $collectd::plugin::threshold::types.lest || { [] }.each |$type| { -%>
<%= collectd::indent(epp('collectd/plugin/threshold/type.epp', { type => $type })) -%>
<%- } -%>
</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 "<%= $host['name'] %>">
<%- $host['plugins'].lest || { [] }.each |$plugin| { -%>
<%= collectd::indent(epp('collectd/plugin/threshold/plugin.epp', { plugin => $plugin })) -%>
<%- } -%>
<%- $host['types'].lest || { [] }.each |$type| { -%>
<%= collectd::indent(epp('collectd/plugin/threshold/type.epp', { type => $type })) -%>
<%- } -%>
</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 "<%= $plugin['name'] %>">
<%- if $plugin['instance'] != undef { -%>
Instance "<%= $plugin['instance'] %>"
<%- } -%>
<%- $plugin['types'].lest || { [] }.each |$type| { -%>
<%= collectd::indent(epp('collectd/plugin/threshold/type.epp', { type => $type })) -%>
<%- } -%>
</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 "<%= $type['name'] %>">
<%- if $type['instance'] != undef { -%>
Instance "<%= $type['instance'] %>"
<%- } -%>
<%- if $type['failure_max'] != undef { -%>
FailureMax <%= $type['failure_max'] %>
<%- } -%>
<%- if $type['failure_min'] != undef { -%>
FailureMin <%= $type['failure_min'] %>
<%- } -%>
<%- if $type['warning_max'] != undef { -%>
WarningMax <%= $type['warning_max'] %>
<%- } -%>
<%- if $type['warning_min'] != undef { -%>
WarningMin <%= $type['warning_min'] %>
<%- } -%>
<%- if $type['data_source'] != undef { -%>
DataSource "<%= $type['data_source'] %>"
<%- } -%>
<%- if $type['invert'] != undef { -%>
Invert <%= $type['invert'] %>
<%- } -%>
<%- if $type['persist'] != undef { -%>
Persist <%= $type['persist'] %>
<%- } -%>
<%- if $type['persist_ok'] != undef { -%>
PersistOK <%= $type['persist_ok'] %>
<%- } -%>
<%- if $type['percentage'] != undef { -%>
Percentage <%= $type['percentage'] %>
<%- } -%>
<%- if $type['hits'] != undef { -%>
Hits <%= $type['hits'] %>
<%- } -%>
<%- if $type['hysteresis'] != undef { -%>
Hysteresis <%= $type['hysteresis'] %>
<%- } -%>
<%- if $type['interesting'] != undef { -%>
Interesting <%= $type['interesting'] %>
<%- } -%>
</Type>
5 changes: 5 additions & 0 deletions types/threshold/host.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Collectd::Threshold::Host = Struct[{
name => String[1],
plugins => Optional[Array[Collectd::Threshold::Plugin]],
types => Optional[Array[Collectd::Threshold::Type]],
}]
5 changes: 5 additions & 0 deletions types/threshold/plugin.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Collectd::Threshold::Plugin = Struct[{
name => String[1],
instance => Optional[String[1]],
types => Optional[Array[Collectd::Threshold::Type]],
}]
16 changes: 16 additions & 0 deletions types/threshold/type.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
type Collectd::Threshold::Type = Struct[{
name => String[1],
instance => Optional[String[1]],
failure_max => Optional[Numeric],
warning_max => Optional[Numeric],
failure_min => Optional[Numeric],
warning_min => Optional[Numeric],
data_source => Optional[String[1]],
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 8a3b009

Please sign in to comment.