Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SNMPv3 support #665

Merged
merged 2 commits into from
Jul 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1528,6 +1528,33 @@ class {'collectd::plugin::snmp':
}
```

```puppet
class { 'collectd::plugin::snmp':
data => {
hc_octets => {
'type' => 'if_octets',
'table' => true,
'instance' => 'IF-MIB::ifName',
'values' => ['IF-MIB::ifHCInOctets', 'IF-MIB::ifHCOutOctets'],
},
},
hosts => {
router => {
'address' => '192.0.2.1',
'version' => 3,
'security_level' => 'authPriv',
'username' => 'collectd',
'auth_protocol' => 'SHA',
'auth_passphrase' => 'mekmitasdigoat',
'privacy_protocol' => 'AES',
'privacy_passphrase' => 'mekmitasdigoat',
'collect' => ['hc_octets'],
'interval' => 10,
},
},
}
```

#### Class: `collectd::plugin::statsd`

```puppet
Expand Down
10 changes: 5 additions & 5 deletions manifests/plugin/snmp.pp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# https://collectd.org/wiki/index.php/Plugin:SNMP
class collectd::plugin::snmp (
$ensure = 'present',
$manage_package = undef,
$data = {},
$hosts = {},
$interval = undef,
Enum['present', 'absent'] $ensure = 'present',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

datatypes \o/

Optional[Boolean] $manage_package = undef,
Hash[String[1], Collectd::SNMP::Data] $data = {},
Hash[String[1], Collectd::SNMP::Host] $hosts = {},
Optional[Integer[0]] $interval = undef,
) {

include ::collectd
Expand Down
22 changes: 10 additions & 12 deletions manifests/plugin/snmp/data.pp
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# https://collectd.org/wiki/index.php/Plugin:SNMP
define collectd::plugin::snmp::data (
$instance,
$type,
$values,
$ensure = 'present',
$instanceprefix = undef,
$scale = undef,
$shift = undef,
$table = false,
$ignore = undef,
$invertmatch = false,
String $instance,
String[1] $type,
Variant[String[1], Array[String[1], 1]] $values,
Enum['present', 'absent'] $ensure = 'present',
Optional[String[1]] $instance_prefix = undef,
Optional[Numeric] $scale = undef,
Optional[Numeric] $shift = undef,
Boolean $table = false,
Optional[Variant[String[1], Array[String[1], 1]]] $ignore = undef,
Boolean $invert_match = false,
) {

include ::collectd
include ::collectd::plugin::snmp

$table_bool = str2bool($table)
$invertmatch_bool = str2bool($invertmatch)
$conf_dir = $collectd::plugin_conf_dir
$root_group = $collectd::root_group

Expand Down
23 changes: 15 additions & 8 deletions manifests/plugin/snmp/host.pp
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
# https://collectd.org/wiki/index.php/Plugin:SNMP
define collectd::plugin::snmp::host (
$collect,
$ensure = 'present',
$address = $name,
$version = '1',
$community = 'public',
$interval = undef,
Variant[String[1], Array[String[1], 1]] $collect,
Enum['present', 'absent'] $ensure = 'present',
String[1] $address = $name,
Collectd::SNMP::Version $version = '1',
Optional[Integer[0]] $interval = undef,
# SNMPv1/2c
Optional[String[1]] $community = 'public',
# SNMPv3
Optional[String[1]] $username = undef,
Optional[Collectd::SNMP::SecurityLevel] $security_level = undef,
Optional[String[1]] $context = undef,
Optional[Collectd::SNMP::AuthProtocol] $auth_protocol = undef,
Optional[String[1]] $auth_passphrase = undef,
Optional[Collectd::SNMP::PrivacyProtocol] $privacy_protocol = undef,
Optional[String[1]] $privacy_passphrase = undef,
) {

include ::collectd
include ::collectd::plugin::snmp

validate_re($version, '^[12]$', 'only snmp versions 1 and 2 are supported')

$conf_dir = $collectd::plugin_conf_dir
$root_group = $collectd::root_group

Expand Down
35 changes: 35 additions & 0 deletions spec/classes/collectd_plugin_snmp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,41 @@
)
end
end

context 'SNMPv3' do
let :params do
{
data: {
'hc_octets' => {
'type' => 'if_octets',
'table' => true,
'instance' => 'IF-MIB::ifName',
'values' => ['IF-MIB::ifHCInOctets', 'IF-MIB::ifHCOutOctets']
}
},
hosts: {
'router' => {
'address' => '192.0.2.1',
'version' => 3,
'username' => 'collectd',
'security_level' => 'authPriv',
'auth_protocol' => 'SHA',
'auth_passphrase' => 'mekmitasdigoat',
'privacy_protocol' => 'AES',
'privacy_passphrase' => 'mekmitasdigoat',
'collect' => ['hc_octets'],
'interval' => 30
}
}
}
end

it "Will create #{options[:plugin_conf_dir]}/10-snmp.conf" do
is_expected.to contain_file('snmp.load').with(ensure: 'present',
path: "#{options[:plugin_conf_dir]}/10-snmp.conf",
content: %r{Data "hc_octets".+Instance "IF-MIB::ifName".+Host "router".+Username "collectd".+SecurityLevel "authPriv".+AuthProtocol "SHA".+PrivacyProtocol "AES"}m)
end
end
end
end
end
4 changes: 2 additions & 2 deletions spec/defines/collectd_plugin_snmp_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
context 'InvertMatch is true' do
let(:params) do
{
invertmatch: true
invert_match: true
}.merge(required_params)
end

Expand All @@ -103,7 +103,7 @@
context 'InvertMatch is false' do
let(:params) do
{
invertmatch: false
invert_match: false
}.merge(required_params)
end

Expand Down
28 changes: 26 additions & 2 deletions spec/defines/collectd_plugin_snmp_host_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
it { is_expected.to contain_file(filename).without_content(%r{Interval \d+}) }
end

context 'all params set' do
context 'all SNMPv2 params set' do
let(:params) do
required_params.merge(address: 'bar.example.com',
version: '2',
community: 'opensesame',
interval: '30')
interval: 30)
end

it { is_expected.to contain_file(filename).with_content(%r{Address "bar\.example\.com"}) }
Expand All @@ -50,6 +50,30 @@
it { is_expected.to contain_file(filename).with_content(%r{Interval 30}) }
end

context 'all SNMPv3 params set' do
let(:params) do
required_params.merge(address: 'bar.example.com',
version: 3,
username: 'collectd',
security_level: 'authPriv',
auth_protocol: 'SHA',
auth_passphrase: 'mekmitasdigoat',
privacy_protocol: 'AES',
privacy_passphrase: 'mekmitasdigoat',
interval: 30)
end

it { is_expected.to contain_file(filename).with_content(%r{Address "bar\.example\.com"}) }
it { is_expected.to contain_file(filename).with_content(%r{Version 3}) }
it { is_expected.to contain_file(filename).with_content(%r{Username "collectd"}) }
it { is_expected.to contain_file(filename).with_content(%r{SecurityLevel "authPriv"}) }
it { is_expected.to contain_file(filename).with_content(%r{AuthProtocol "SHA"}) }
it { is_expected.to contain_file(filename).with_content(%r{AuthPassphrase "mekmitasdigoat"}) }
it { is_expected.to contain_file(filename).with_content(%r{PrivacyProtocol "AES"}) }
it { is_expected.to contain_file(filename).with_content(%r{PrivacyPassphrase "mekmitasdigoat"}) }
it { is_expected.to contain_file(filename).with_content(%r{Interval 30}) }
end

context 'collect is an array' do
let(:params) do
{
Expand Down
30 changes: 24 additions & 6 deletions templates/plugin/snmp.conf.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<% if @data or @hosts -%>
<Plugin snmp>
<% @data.sort_by {|k,v| k}.each do |key,val| -%>
<Data "<%=key %>">
<Data "<%= key %>">
Type "<%= val['type'] %>"
<% if val['table'] -%>
Table <%= val['table'] %>
<% unless val['table'].nil? -%>
Table <%= val['table'] ? 'true' : 'false' %>
<% end -%>
<% if val['instance'] -%>
Instance "<%= val['instance'] %>"
Expand All @@ -22,19 +22,37 @@
<% if val['ignore'] -%>
Ignore <% Array(val['ignore']).each do |x| -%>"<%= x %>" <% end %>
<% end -%>
<% if val['invert_match'] -%>
InvertMatch <%= val['invert_match'] %>
<% unless val['invert_match'].nil? -%>
InvertMatch <%= val['invert_match'] ? 'true' : 'false' %>
<% end -%>
</Data>

<% end -%>
<% @hosts.sort_by {|k,v| k}.each do |key,val| -%>
<Host "<%=key %>">
<Host "<%= key %>">
Address "<%= val['address'] %>"
Version <%= val['version'] %>
<% if val['version'].to_i < 3 -%>
Community "<%= val['community'] %>"
<% else -%>
Username "<%= val['username'] %>"
<% if val['context'] -%>
Context "<%= val['context'] %>"
<% end -%>
SecurityLevel "<%= val['security_level'] %>"
<% if ['authPriv', 'authNoPriv'].include?(val['security_level']) -%>
AuthProtocol "<%= val['auth_protocol'] %>"
AuthPassphrase "<%= val['auth_passphrase'] %>"
<% end -%>
<% if val['security_level'].eql?('authPriv') -%>
PrivacyProtocol "<%= val['privacy_protocol'] %>"
PrivacyPassphrase "<%= val['privacy_passphrase'] %>"
<% end -%>
<% end -%>
Collect <% Array(val['collect']).sort.each do |x| -%>"<%= x -%>" <% end %>
<% if val['interval'] -%>
Interval <%= val['interval'] %>
<% end -%>
</Host>
<% end -%>
</Plugin>
Expand Down
12 changes: 6 additions & 6 deletions templates/plugin/snmp/data.conf.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Plugin snmp>
<Data "<%= @name %>">
Type "<%= @type %>"
Table <%= @table_bool ? 'true' : 'false' %>
Table <%= @table ? 'true' : 'false' %>
Instance "<%= @instance %>"
<% if @instanceprefix -%>
InstancePrefix "<%= @instanceprefix %>"
<% if @instance_prefix -%>
InstancePrefix "<%= @instance_prefix %>"
<% end -%>
Values <%= Array(@values).map { |x| %Q{"#{x}"} }.join(' ') %>
<% if @scale -%>
Expand All @@ -14,9 +14,9 @@
Shift <%= @shift %>
<% end -%>
<% if @ignore -%>
Ignore <%= Array(@ignore).map { |x| %Q{"#{x}"} }.join(' ') %>
Ignore <%= Array(@ignore).map { |x| %Q{"#{x}"} }.join(' ') %>
<% end -%>
InvertMatch <%= @invertmatch_bool ? 'true' : 'false' %>
InvertMatch <%= @invert_match ? 'true' : 'false' %>
</Data>
</Plugin>


20 changes: 18 additions & 2 deletions templates/plugin/snmp/host.conf.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@
<Host "<%= @name %>">
Address "<%= @address %>"
Version <%= @version %>
<% if @version.to_i < 3 -%>
Community "<%= @community %>"
<% else -%>
Username "<%= @username %>"
<% if @context -%>
Context "<%= @context %>"
<% end -%>
SecurityLevel "<%= @security_level %>"
<% if ['authPriv', 'authNoPriv'].include?(@security_level) -%>
AuthProtocol "<%= @auth_protocol %>"
AuthPassphrase "<%= @auth_passphrase %>"
<% end -%>
<% if @security_level.eql?('authPriv') -%>
PrivacyProtocol "<%= @privacy_protocol %>"
PrivacyPassphrase "<%= @privacy_passphrase %>"
<% end -%>
<% end -%>
Collect <%= Array(@collect).map { |x| %Q{"#{x}"} }.join(' ') %>
<%- if !@interval.nil? -%>
<% if @interval -%>
Interval <%= @interval %>
<%- end -%>
<% end -%>
</Host>
</Plugin>
2 changes: 2 additions & 0 deletions types/snmp/authprotocol.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#
type Collectd::SNMP::AuthProtocol = Enum['MD5', 'SHA']
2 changes: 2 additions & 0 deletions types/snmp/data.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#
type Collectd::SNMP::Data = Struct[{Optional['instance'] => String, NotUndef['type'] => String[1], NotUndef['values'] => Variant[String[1], Array[String[1], 1]], Optional['instance_prefix'] => String[1], Optional['scale'] => Numeric, Optional['shift'] => Numeric, Optional['table'] => Boolean, Optional['ignore'] => Variant[String[1], Array[String[1], 1]], Optional['invert_match'] => Boolean}]
2 changes: 2 additions & 0 deletions types/snmp/host.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#
type Collectd::SNMP::Host = Variant[Collectd::SNMP::Host::V2, Collectd::SNMP::Host::V3]
2 changes: 2 additions & 0 deletions types/snmp/host/v2.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#
type Collectd::SNMP::Host::V2 = Struct[{NotUndef['address'] => String[1], NotUndef['version'] => Collectd::SNMP::Version::V2, NotUndef['community'] => String[1], NotUndef['collect'] => Variant[String[1], Array[String[1], 1]], Optional['interval'] => Integer[0]}]
2 changes: 2 additions & 0 deletions types/snmp/host/v3.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#
type Collectd::SNMP::Host::V3 = Struct[{NotUndef['address'] => String[1], NotUndef['version'] => Collectd::SNMP::Version::V3, NotUndef['username'] => String[1], Optional['context'] => String[1], NotUndef['security_level'] => Collectd::SNMP::SecurityLevel, Optional['auth_protocol'] => Collectd::SNMP::AuthProtocol, Optional['auth_passphrase'] => String[1], Optional['privacy_protocol'] => Collectd::SNMP::PrivacyProtocol, Optional['privacy_passphrase'] => String[1], NotUndef['collect'] => Variant[String[1], Array[String[1], 1]], Optional['interval'] => Integer[0]}]
2 changes: 2 additions & 0 deletions types/snmp/privacyprotocol.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#
type Collectd::SNMP::PrivacyProtocol = Enum['AES', 'DES']
2 changes: 2 additions & 0 deletions types/snmp/securitylevel.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#
type Collectd::SNMP::SecurityLevel = Enum['authPriv', 'authNoPriv', 'noAuthNoPriv']
2 changes: 2 additions & 0 deletions types/snmp/version.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#
type Collectd::SNMP::Version = Variant[Collectd::SNMP::Version::V2, Collectd::SNMP::Version::V3]
2 changes: 2 additions & 0 deletions types/snmp/version/v2.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#
type Collectd::SNMP::Version::V2 = Variant[Integer[1, 2], Enum['1', '2']]
2 changes: 2 additions & 0 deletions types/snmp/version/v3.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#
type Collectd::SNMP::Version::V3 = Variant[Integer[3, 3], Enum['3']]