Skip to content

Commit 22a1662

Browse files
committed
split out the snmp data and hosts into defines
this will make it a lot easier to have a "centralized" collectd snmp collector that will gather information from network devices and such which are puppetized but don't run their own collectd :)
1 parent 1990227 commit 22a1662

8 files changed

Lines changed: 198 additions & 10 deletions

File tree

manifests/plugin/snmp.pp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
$hosts = undef,
66
$interval = undef,
77
) {
8-
validate_hash($data, $hosts)
9-
108
if $::osfamily == 'Redhat' {
119
package { 'collectd-snmp':
1210
ensure => $ensure,

manifests/plugin/snmp/data.pp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# https://collectd.org/wiki/index.php/Plugin:SNMP
2+
define collectd::plugin::snmp::data (
3+
$ensure = present,
4+
$type,
5+
$table = false,
6+
$instance,
7+
$values,
8+
) {
9+
include collectd
10+
include collectd::plugin::snmp
11+
12+
$table_bool = str2bool($table)
13+
14+
$conf_dir = $collectd::params::plugin_conf_dir
15+
$root_group = $collectd::params::root_group
16+
17+
file { "snmp-data-${name}.conf":
18+
ensure => $ensure,
19+
path => "${conf_dir}/15-snmp-data-${name}.conf",
20+
owner => 'root',
21+
group => $root_group,
22+
mode => '0640',
23+
content => template('collectd/plugin/snmp/data.conf.erb'),
24+
notify => Service['collectd'];
25+
}
26+
}

manifests/plugin/snmp/host.pp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# https://collectd.org/wiki/index.php/Plugin:SNMP
2+
define collectd::plugin::snmp::host (
3+
$ensure = present,
4+
$address = $name,
5+
$version = 1,
6+
$community = 'public',
7+
$collect,
8+
$interval = undef,
9+
) {
10+
include collectd
11+
include collectd::plugin::snmp
12+
13+
validate_re($version, '^[12]$', 'only snmp versions 1 and 2 are supported')
14+
15+
$conf_dir = $collectd::params::plugin_conf_dir
16+
$root_group = $collectd::params::root_group
17+
18+
file { "snmp-host-${name}.conf":
19+
ensure => $ensure,
20+
path => "${conf_dir}/25-snmp-host-${name}.conf",
21+
owner => 'root',
22+
group => $root_group,
23+
mode => '0640',
24+
content => template('collectd/plugin/snmp/host.conf.erb'),
25+
notify => Service['collectd'];
26+
}
27+
}

spec/classes/collectd_plugin_snmp_spec.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,5 @@
6767
end
6868
end
6969

70-
context ':data is not a hash' do
71-
let :params do
72-
{:data => []}
73-
end
74-
it 'Will raise an error about :data being a Array' do
75-
expect {should}.to raise_error(Puppet::Error,/Array/)
76-
end
77-
end
7870
end
7971

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
require 'spec_helper'
2+
3+
describe 'collectd::plugin::snmp::data', :type => :define do
4+
let :facts do
5+
{:osfamily => 'Debian'}
6+
end
7+
8+
let (:title) { 'foo' }
9+
let (:required_params) {{
10+
:type => 'bar',
11+
:instance => 'baz',
12+
:values => 'bat',
13+
}}
14+
15+
let (:filename) { 'snmp-data-foo.conf' }
16+
17+
context 'required params' do
18+
let (:params) { required_params }
19+
20+
it { should contain_file(filename).with(
21+
:ensure => 'present',
22+
:path => '/etc/collectd/conf.d/15-snmp-data-foo.conf'
23+
) }
24+
25+
it { should contain_file('snmp-data-foo.conf').that_notifies('Service[collectd]') }
26+
it { should contain_file('snmp-data-foo.conf').with_content(/<Plugin snmp>/) }
27+
it { should contain_file('snmp-data-foo.conf').with_content(/<Data "foo">/) }
28+
it { should contain_file('snmp-data-foo.conf').with_content(/Type "bar"/) }
29+
it { should contain_file('snmp-data-foo.conf').with_content(/Instance "baz"/) }
30+
end
31+
32+
context 'values is an array' do
33+
let (:params) {
34+
required_params.merge({
35+
:values => %w{ foo bar baz }
36+
})
37+
}
38+
it { should contain_file('snmp-data-foo.conf').with_content(/Values foo bar baz/) }
39+
end
40+
41+
context 'values is just a string' do
42+
let (:params) {
43+
required_params.merge({
44+
:values => 'bat'
45+
})
46+
}
47+
it { should contain_file('snmp-data-foo.conf').with_content(/Values bat/) }
48+
end
49+
50+
context 'table is true' do
51+
let (:params) {{
52+
:table => true
53+
}.merge(required_params)}
54+
55+
it { should contain_file('snmp-data-foo.conf').with_content(/Table true/) }
56+
end
57+
58+
context 'table is false' do
59+
let (:params) {{
60+
:table => false
61+
}.merge(required_params)}
62+
63+
it { should contain_file('snmp-data-foo.conf').with_content(/Table false/) }
64+
end
65+
66+
end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
require 'spec_helper'
2+
3+
describe 'collectd::plugin::snmp::host', :type => :define do
4+
let :facts do
5+
{:osfamily => 'Debian'}
6+
end
7+
8+
let (:title) { 'foo.example.com' }
9+
let (:required_params) {{
10+
:collect => 'foo'
11+
}}
12+
13+
let (:filename) { 'snmp-host-foo.example.com.conf' }
14+
15+
context 'default params' do
16+
let (:params) { required_params }
17+
18+
it { should contain_file(filename).with(
19+
:ensure => 'present',
20+
:path => '/etc/collectd/conf.d/25-snmp-host-foo.example.com.conf'
21+
) }
22+
23+
it { should contain_file(filename).that_notifies('Service[collectd]') }
24+
it { should contain_file(filename).with_content(/<Plugin snmp>/) }
25+
it { should contain_file(filename).with_content(/<Host "foo\.example\.com">/) }
26+
it { should contain_file(filename).with_content(/Address "foo\.example\.com"/) }
27+
it { should contain_file(filename).with_content(/Version 1/) }
28+
it { should contain_file(filename).with_content(/Community "public"/) }
29+
it { should contain_file(filename).without_content(/Interval \d+/) }
30+
end
31+
32+
context 'all params set' do
33+
let (:params) {
34+
required_params.merge({
35+
:address => 'bar.example.com',
36+
:version => '2',
37+
:community => 'opensesame',
38+
:interval => '30',
39+
})
40+
}
41+
it { should contain_file(filename).with_content(/Address "bar\.example\.com"/) }
42+
it { should contain_file(filename).with_content(/Version 2/) }
43+
it { should contain_file(filename).with_content(/Community "opensesame"/) }
44+
it { should contain_file(filename).with_content(/Interval 30/) }
45+
end
46+
47+
context 'collect is an array' do
48+
let (:params) {{
49+
:collect => %w{ foo bar baz }
50+
}}
51+
it { should contain_file(filename).with_content(/Collect foo bar baz/) }
52+
end
53+
54+
context 'collect is just a string' do
55+
let (:params) {{
56+
:collect => 'bat'
57+
}}
58+
it { should contain_file(filename).with_content(/Collect bat/) }
59+
end
60+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Plugin snmp>
2+
<Data "<%= @name %>">
3+
Type "<%= @type %>"
4+
Table <%= @table_bool ? 'true' : 'false' %>
5+
Instance "<%= @instance %>"
6+
Values <%= Array(@values).join(' ') %>
7+
</Data>
8+
</Plugin>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<Plugin snmp>
2+
<Host "<%= @name %>">
3+
Address "<%= @address %>"
4+
Version <%= @version %>
5+
Community "<%= @community %>"
6+
Collect <%= Array(@collect).join(" ") %>
7+
<%- if !@interval.nil? -%>
8+
Interval <%= @interval %>
9+
<%- end -%>
10+
</Host>
11+
</Plugin>

0 commit comments

Comments
 (0)