Skip to content
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
34 changes: 34 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@
# @param package_name
# Name of the package. Only set this if your platform is not supported or you know what you are doing.
#
# @param snmptrapd_package_name
# Name of the package provinding snmptrapd. Only set this if your platform is not supported or you know what you are doing.
#
# @param snmpd_options
# Commandline options passed to snmpd via init script.
#
Expand Down Expand Up @@ -241,6 +244,7 @@
$snmp_config = $snmp::params::snmp_config,
Boolean $autoupgrade = $snmp::params::autoupgrade,
$package_name = $snmp::params::package_name,
$snmptrapd_package_name = $snmp::params::snmptrapd_package_name,
$snmpd_options = $snmp::params::snmpd_options,
$service_config_perms = $snmp::params::service_config_perms,
$service_config_dir_group = $snmp::params::service_config_dir_group,
Expand Down Expand Up @@ -322,6 +326,14 @@
name => $package_name,
}

# Since ubuntu 16.04 platforms, there is a differente snmptrad package
if $snmp::snmptrapd_package_name {
package { 'snmptrapd':
ensure => $package_ensure,
name => $snmp::snmptrapd_package_name,
}
}

file { 'var-net-snmp':
ensure => 'directory',
mode => $snmp::params::varnetsnmp_perms,
Expand Down Expand Up @@ -398,6 +410,28 @@
require => Package['snmpd'],
notify => Service['snmptrapd'],
}
} elsif ( $facts['os']['name'] == 'Ubuntu' and versioncmp($facts['os']['release']['major'], '16.04') >= 0 ) or
( $facts['os']['name'] == 'Debian' and versioncmp($facts['os']['release']['major'], '8') >= 0 ) {
file { 'snmptrapd.sysconfig':
ensure => $file_ensure,
mode => '0644',
owner => 'root',
group => 'root',
path => $snmp::params::trap_sysconfig,
content => template($template_snmptrapd_sysconfig),
require => Package['snmptrapd'],
notify => Service['snmptrapd'],
}

service { 'snmptrapd':
ensure => $trap_service_ensure_real,
name => $trap_service_name,
enable => $trap_service_enable_real,
hasstatus => $trap_service_hasstatus,
hasrestart => $trap_service_hasrestart,
require => [ File['var-net-snmp'], Package['snmptrapd'], ],
subscribe => File['snmptrapd.conf'],
}
}

# Services
Expand Down
18 changes: 17 additions & 1 deletion manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@

$trap_service_config = '/etc/snmp/snmptrapd.conf'
$trap_service_name = 'snmptrapd'
$snmptrapd_package_name = undef
}
'Debian': {
if $facts['os']['name'] == 'Debian' and versioncmp($majordistrelease, '9') >= 0 {
Expand All @@ -125,8 +126,20 @@
$client_config = '/etc/snmp/snmp.conf'

$trap_service_config = '/etc/snmp/snmptrapd.conf'
$trap_service_name = undef
$snmptrapd_options = '-Lsd -p /var/run/snmptrapd.pid'

if $facts['os']['name'] == 'Ubuntu' and versioncmp($majordistrelease, '16.04') >= 0 {
$trap_service_name = 'snmptrapd'
$snmptrapd_package_name = 'snmptrapd'
$trap_sysconfig = '/etc/default/snmptrapd'
} elsif $facts['os']['name'] == 'Debian' and versioncmp($majordistrelease, '8') >= 0 {
$trap_service_name = 'snmptrapd'
$snmptrapd_package_name = 'snmptrapd'
$trap_sysconfig = '/etc/default/snmptrapd'
} else {
$trap_service_name = undef
$snmptrapd_package_name = undef
}
}
'Suse': {
$package_name = 'net-snmp'
Expand All @@ -147,6 +160,7 @@
$trap_service_config = '/etc/snmp/snmptrapd.conf'
$trap_service_name = 'snmptrapd'
$snmptrapd_options = undef
$snmptrapd_package_name = undef
}
'FreeBSD': {
$package_name = 'net-mgmt/net-snmp'
Expand All @@ -169,6 +183,7 @@
$trap_service_config = '/usr/local/etc/snmp/snmptrapd.conf'
$trap_service_name = 'snmptrapd'
$snmptrapd_options = undef
$snmptrapd_package_name = undef
}
'OpenBSD': {
$package_name = 'net-snmp'
Expand All @@ -191,6 +206,7 @@
$trap_service_config = '/etc/snmp/snmptrapd.conf'
$trap_service_name = 'netsnmptrapd'
$snmptrapd_options = undef
$snmptrapd_package_name = undef
}
default: {
fail("Module does not support ${facts['os']['family']}.")
Expand Down
23 changes: 23 additions & 0 deletions spec/acceptance/snmp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class { 'snmp':
describe process('snmpd') do
it { is_expected.to be_running }
end
describe process('snmptrapd') do
it { is_expected.not_to be_running }
end
describe file(service_config.to_s) do
it { is_expected.to be_file }
it { is_expected.not_to contain 'rocommunity' }
Expand Down Expand Up @@ -97,4 +100,24 @@ class { 'snmp':
its(:exit_status) { is_expected.to eq 0 }
end
end

context 'with snmptrapd running' do
it 'installs snmpd idempotently' do
pp = %(
class { 'snmp':
trap_service_ensure => 'running',
agentaddress => [ 'udp:127.0.0.1:161' ],
snmptrapdaddr => [ 'udp:127.0.0.1:162' ],
}
)
apply_manifest(pp, catch_failures: true)
apply_manifest(pp, catch_changes: true)
end
describe process('snmpd') do
it { is_expected.to be_running }
end
describe process('snmptrapd') do
it { is_expected.to be_running }
end
end
end
77 changes: 62 additions & 15 deletions spec/classes/snmp_init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -575,14 +575,36 @@
path: '/etc/snmp/snmptrapd.conf'
).that_requires('Package[snmpd]').that_notifies('Service[snmpd]')
}
it { is_expected.not_to contain_file('snmptrapd.sysconfig') }
case facts[:os]['release']['major']
Copy link
Member

Choose a reason for hiding this comment

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

yeeess structured facts \o/

when '8', '9', '16.04', '18.04'
it {
is_expected.to contain_file('snmptrapd.sysconfig').with(
ensure: 'present',
mode: '0644',
owner: 'root',
group: 'root',
path: '/etc/default/snmptrapd'
).that_requires('Package[snmptrapd]').that_notifies('Service[snmptrapd]')
}
it {
is_expected.to contain_service('snmptrapd').with(
ensure: 'stopped',
name: 'snmptrapd',
enable: false,
hasstatus: true,
hasrestart: true
).that_requires(['Package[snmptrapd]', 'File[var-net-snmp]'])
}
else
it { is_expected.not_to contain_file('snmptrapd.sysconfig') }
it { is_expected.not_to contain_service('snmptrapd') }
end
it 'contains File[snmpd.sysconfig] with contents "TRAPDOPTS=\'-Lsd -p /var/run/snmptrapd.pid\'"' do
verify_contents(catalogue, 'snmpd.sysconfig', [
'TRAPDRUN=no',
'TRAPDOPTS=\'-Lsd -p /var/run/snmptrapd.pid\''
])
end
it { is_expected.not_to contain_service('snmptrapd') }

case facts[:os]['release']['major']
when '9', '18.04'
Expand Down Expand Up @@ -623,21 +645,46 @@
end
end

describe 'service_ensure => stopped and trap_service_ensure => running' do
let :params do
{
service_ensure: 'stopped',
trap_service_ensure: 'running'
}
case facts[:os]['release']['major']
when '8', '9', '16.04', '18.04'
describe 'service_ensure => stopped and trap_service_ensure => running' do
let :params do
{
service_ensure: 'stopped',
trap_service_ensure: 'running'
}
end

it { is_expected.to contain_service('snmpd').with_ensure('running') }
it { is_expected.to contain_service('snmptrapd').with_ensure('running') }
it 'contains File[snmpd.sysconfig] with content "SNMPDRUN=no"' do
verify_contents(catalogue, 'snmpd.sysconfig', [
'SNMPDRUN=no'
])
end
it 'contains File[snmptrapd.sysconfig] with content "TRAPDRUN=yes"' do
verify_contents(catalogue, 'snmptrapd.sysconfig', [
'TRAPDRUN=yes'
])
end
end
else
describe 'service_ensure => stopped and trap_service_ensure => running' do
let :params do
{
service_ensure: 'stopped',
trap_service_ensure: 'running'
}
end

it { is_expected.to contain_service('snmpd').with_ensure('running') }
it { is_expected.not_to contain_service('snmptrapd') }
it 'contains File[snmpd.sysconfig] with contents "SNMPDRUN=no" and "TRAPDRUN=yes"' do
verify_contents(catalogue, 'snmpd.sysconfig', [
'SNMPDRUN=no',
'TRAPDRUN=yes'
])
it { is_expected.to contain_service('snmpd').with_ensure('running') }
it { is_expected.not_to contain_service('snmptrapd') }
it 'contains File[snmpd.sysconfig] with contents "SNMPDRUN=no" and "TRAPDRUN=yes"' do
verify_contents(catalogue, 'snmpd.sysconfig', [
'SNMPDRUN=no',
'TRAPDRUN=yes'
])
end
end
end

Expand Down
11 changes: 11 additions & 0 deletions templates/snmptrapd.sysconfig-Debian.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
###
### File managed by Puppet
###

# snmptrapd control (yes means start daemon). As of net-snmp version
# 5.0, master agentx support must be enabled in snmpd before snmptrapd
# can be run. See snmpd.conf(5) for how to do this.
TRAPDRUN=<%= @trapdrun %>

# snmptrapd command line options
TRAPDOPTS="<%= @snmptrapd_options %>"