Skip to content

Commit

Permalink
Merge pull request jhoblitt#21 from jhoblitt/feature/smartd_conf_default
Browse files Browse the repository at this point in the history
Feature/smartd conf default
  • Loading branch information
Joshua Hoblitt committed Apr 14, 2014
2 parents 365a6ab + aad6db6 commit 4f558f2
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 16 deletions.
61 changes: 59 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Puppet smartd Module
1. [Overview](#overview)
2. [Description](#description)
3. [Usage](#usage)
* [Version `2.x.x` _incompatilbe_ API change](#version-2xx-incompatilbe-api-change)
* [Version `2.x.x` _incompatible_ API change](#version-2xx-incompatible-api-change)
* [Simple Usage](#simple-usage)
* [Parameters](#parameters)
* [Pedantic Example](#pedantic-example)
Expand Down Expand Up @@ -173,7 +173,7 @@ Explicit list of raw block devices to check. Eg.

`String` defaults to: `root`

Smart daemon notifcation email address.
Smart daemon notification email address.

#### `warning_schedule`

Expand All @@ -182,6 +182,61 @@ Smart daemon notifcation email address.
Smart daemon problem mail notification frequency. Valid values are:
`daily`,`once`,`diminishing`

#### `enable_default`

`Bool` defaults to: `true` if `$::smartmontools_version >= 5.43`, otherwise `false`

Enables/disables the `DEFAULT` directive in the `smartd.conf` file. This
directive was added in the 5.43 release of smartmontools and is unsupported in
previous versions.

If `enable_default` is set to `false` the the values from the [`mail_to`](#mail-to) and [`warning_schedule`](#warning-schedule) parameters are set on the `DEVICESCAN` directive (if enabled) instead of the [absent] `DEFAULT` directive.

Example `smartd.conf` content based on this setting:

`enable_default => true`
```
# Managed by Puppet -- do not edit!
DEFAULT -m root -M daily
DEVICESCAN
```

`enable_default => false`
```
# Managed by Puppet -- do not edit!
DEVICESCAN -m root -M daily
```

Here is an example of the error message generated by the `DEFFAULT` directive
appearing in the configuration file of 5.42.

```
smartd 5.42 2011-10-20 r3458 [i686-linux-2.6.18-371.6.1.el5PAE] (local build)
Copyright (C) 2002-11 by Bruce Allen, http://smartmontools.sourceforge.net
Opened configuration file /etc/smartd.conf
Drive: DEFAULT, implied '-a' Directive on line 2 of file /etc/smartd.conf
Drive: DEVICESCAN, implied '-a' Directive on line 3 of file /etc/smartd.conf
Configuration file /etc/smartd.conf was parsed, found DEVICESCAN, scanning devices
Device: DEFAULT, unable to autodetect device type
```

This option could not be named `default` to be consistent with the naming
convention of the other parameters in this module due to this bug
[PUP-2244](https://tickets.puppetlabs.com/browse/PUP-2244) that affects puppet
2.7.x.

Note that RHEL5 ships with 5.42 while RHEL6 ships with 5.43.

#### `default_options`

`String` defaults to: `undef`

Additional arguments to be set on the `DEFAULT` directive.

If `default` is set to `false`, this parameter's value will be set on the
`DEVICESCAN` directive (if enabled) instead of the [absent] `DEFAULT`
directive.

### Pedantic Example

```puppet
Expand All @@ -199,6 +254,8 @@ Smart daemon problem mail notification frequency. Valid values are:
],
mail_to => 'root',
warning_schedule => 'diminishing',
default => 'false',
default_options => '-H',
}
```

Expand Down
4 changes: 4 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@
$devices = $smartd::params::devices,
$mail_to = $smartd::params::mail_to,
$warning_schedule = $smartd::params::warning_schedule,
$enable_default = $smartd::params::enable_default,
$default_options = $smartd::params::default_options,
) inherits smartd::params {
validate_re($ensure, '^present$|^latest$|^absent$|^purged$')
validate_string($package_name)
Expand All @@ -119,6 +121,8 @@
validate_string($mail_to)
validate_re($warning_schedule, '^daily$|^once$|^diminishing$',
'$warning_schedule must be either daily, once, or diminishing.')
validate_bool($enable_default)
validate_string($default_options)

case $ensure {
'present', 'latest': {
Expand Down
8 changes: 8 additions & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
$devices = []
$mail_to = 'root'
$warning_schedule = 'daily' # other choices: once, diminishing
$default_options = undef

# smartd.conf < 5.43 does not support the 'DEFAULT' directive
if versioncmp($::smartmontools_version, 5.43) >= 0 {
$enable_default = true
} else {
$enable_default = false
}

case $::osfamily {
'FreeBSD': {
Expand Down
197 changes: 184 additions & 13 deletions spec/classes/smartd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,21 @@

describe 'on a supported osfamily, default parameters' do
describe 'for osfamily RedHat' do
let(:facts) {{ :osfamily => 'RedHat' }}
let(:facts) {{ :osfamily => 'RedHat', :smartmontools_version => '5.43' }}

it_behaves_like 'default', {}
it { should_not contain_shell_config('start_smartd') }
end

describe 'for osfamily Debian' do
let(:facts) {{ :osfamily => 'Debian' }}
let(:facts) {{ :osfamily => 'Debian', :smartmontools_version => '5.43' }}

it_behaves_like 'default', {}
it { should contain_shell_config('start_smartd') }
end

describe 'for osfamily FreeBSD' do
let(:facts) {{ :osfamily => 'FreeBSD' }}
let(:facts) {{ :osfamily => 'FreeBSD', :smartmontools_version => '5.43' }}

it_behaves_like 'default', { :config_file => '/usr/local/etc/smartd.conf' }
it { should_not contain_shell_config('start_smartd') }
Expand All @@ -68,7 +68,7 @@
end

describe 'on a supported osfamily, custom parameters' do
let(:facts) {{ :osfamily => 'RedHat' }}
let(:facts) {{ :osfamily => 'RedHat', :smartmontools_version => '5.43' }}

describe 'ensure => present' do
let(:params) {{ :ensure => 'present' }}
Expand Down Expand Up @@ -136,12 +136,54 @@
end
end

describe 'devicescan => false' do
let(:params) {{ :devicescan => false }}

it { should contain_file('/etc/smartd.conf').with_ensure('present') }
it { should_not contain_file('/etc/smartd.conf').with_content(/^DEVICESCAN$/) }
end
describe 'devicescan =>' do
context '(default)' do
it do
should contain_file('/etc/smartd.conf').
with_ensure('present').
with_content(/^DEVICESCAN$/)
end
end # (default)

context 'true' do
let(:params) {{ :devicescan => true }}

it do
should contain_file('/etc/smartd.conf').
with_ensure('present').
with_content(/^DEVICESCAN$/)
end

context 'enable_default => false' do
before { params[:enable_default] = false }

it 'should have the same arguments as DEFAULT would have' do
should contain_file('/etc/smartd.conf').
with_ensure('present').
with_content(/^DEVICESCAN -m root -M daily$/)
end
end
end # true

context 'false' do
let(:params) {{ :devicescan => false }}

it do
should contain_file('/etc/smartd.conf').
with_ensure('present').
without_content(/^DEVICESCAN$/)
end
end # false

context 'foo' do
let(:params) {{ :devicescan => 'foo' }}
it 'should fail' do
expect {
should raise_error(Puppet::Error, /is not a boolean../)
}
end
end
end # devicescan =>

describe 'devicescan_options => somevalue' do
let(:params) {{ :devicescan_options => 'somevalue' }}
Expand Down Expand Up @@ -256,6 +298,133 @@
end
end

describe 'enable_default => ' do
context '(default)' do
context 'fact smartmontool_version = "5.43"' do
before { facts[:smartmontools_version] = '5.43' }
it do
should contain_file('/etc/smartd.conf').with_ensure('present').
with_content(/DEFAULT -m root -M daily/)
end
end

context 'fact smartmontool_version = "5.42"' do
before { facts[:smartmontools_version] = '5.42' }
it do
should contain_file('/etc/smartd.conf').with_ensure('present').
without_content(/DEFAULT -m root -M daily/).
with_content(/DEVICESCAN -m root -M daily/)
end
end
end # (default)

context 'true' do
let(:params) {{ :enable_default => true }}
it do
should contain_file('/etc/smartd.conf').with_ensure('present').
with_content(/DEFAULT -m root -M daily/)
end
end

context 'false' do
let(:params) {{ :enable_default => false }}
it do
should contain_file('/etc/smartd.conf').with_ensure('present').
without_content(/DEFAULT -m root -M daily/).
with_content(/DEVICESCAN -m root -M daily/)
end
end

context 'foo' do
let(:params) {{ :enable_default => 'foo' }}
it 'should fail' do
expect {
should raise_error(Puppet::Error, /is not a boolean../)
}
end
end
end # enable_default =>

describe 'default_options => ' do
context '(default)' do
let(:params) {{ }}

context 'default => true' do
before { params[:enable_default] = true }

it do
should contain_file('/etc/smartd.conf').with_ensure('present').
with_content(/DEFAULT -m root -M daily/)
end
end

context 'enable_default => false' do
before { params[:enable_default] = false }

it do
should contain_file('/etc/smartd.conf').with_ensure('present').
without_content(/DEFAULT -m root -M daily/).
with_content(/DEVICESCAN -m root -M daily/)
end
end
end # (default)

context 'undef' do
let(:params) {{ :default_options => nil }}

context 'enable_default => true' do
before { params[:enable_default] = true }

it do
should contain_file('/etc/smartd.conf').with_ensure('present').
with_content(/DEFAULT -m root -M daily/)
end
end

context 'enable_default => false' do
before { params[:enable_default] = false }

it do
should contain_file('/etc/smartd.conf').with_ensure('present').
without_content(/DEFAULT -m root -M daily/).
with_content(/DEVICESCAN -m root -M daily/)
end
end
end # undef

context '-H' do
let(:params) {{ :default_options => '-H'}}

context 'enable_default => true' do
before { params[:enable_default] = true }

it do
should contain_file('/etc/smartd.conf').with_ensure('present').
with_content(/DEFAULT -m root -M daily -H/)
end
end

context 'enable_default => false' do
before { params[:enable_default] = false }

it do
should contain_file('/etc/smartd.conf').with_ensure('present').
without_content(/DEFAULT -m root -M daily -H/).
with_content(/DEVICESCAN -m root -M daily -H/)
end
end
end # -H

context '[]' do
let(:params) {{ :default_options => [] }}
it 'should fail' do
expect {
should raise_error(Puppet::Error, /is not an Array../)
}
end
end # []
end # default_options =>

end


Expand All @@ -264,10 +433,11 @@
describe 'without params + megaraid facts' do
let(:facts) do
{
:osfamily=> 'RedHat',
:osfamily => 'RedHat',
:megaraid_adapters => '1',
:megaraid_virtual_drives => 'sdb,sda',
:megaraid_physical_drives => '2,1',
:smartmontools_version => '5.43',
}
end

Expand All @@ -283,13 +453,14 @@
end
end

describe 'without params + megaraid facts' do
describe 'with params + megaraid facts' do
let(:facts) do
{
:osfamily=> 'RedHat',
:osfamily => 'RedHat',
:megaraid_adapters => '1',
:megaraid_virtual_drives => 'sdb,sda',
:megaraid_physical_drives => '2,1',
:smartmontools_version => '5.43',
}
end
let(:params) do
Expand Down
8 changes: 7 additions & 1 deletion templates/smartd.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Managed by Puppet -- do not edit!
DEFAULT -m <%= @mail_to %> -M <%= @warning_schedule %>
<% if @enable_default -%>
DEFAULT -m <%= @mail_to %> -M <%= @warning_schedule %><% if @default_options %><%= ' ' + @default_options %><% end %>
<% end -%>
<% @devices.each do |dev| -%>
<% next if dev['device'] == 'megaraid' -%>
<%= dev['device'] %><% if dev.has_key?('options') -%><%= ' ' + dev['options'] %><% end %>
Expand All @@ -24,5 +26,9 @@ if megaraid_device and megaraid_device != '' and
<% end -%>
<% end -%>
<% if @devicescan -%>
<% unless @enable_default -%>
DEVICESCAN -m <%= @mail_to %> -M <%= @warning_schedule %><% if @default_options %><%= ' ' + @default_options %><% end %><% if @devicescan_options %><%= ' ' + @devicescan_options %><% end %>
<% else -%>
DEVICESCAN<% if @devicescan_options %><%= ' ' + @devicescan_options %><% end %>
<% end -%>
<% end -%>

0 comments on commit 4f558f2

Please sign in to comment.