Skip to content

Commit

Permalink
add $default param to smartd class
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Hoblitt committed Apr 14, 2014
1 parent 365a6ab commit a51ca20
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 12 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,51 @@ Smart daemon notifcation email address.
Smart daemon problem mail notification frequency. Valid values are:
`daily`,`once`,`diminishing`

#### `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 `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:

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

`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
apearing 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 would be better named `enable_default` but the `enable_` prefix was
no used so as to appear more consistent with the rest of the API. The API may
be refactored in a future major release to be more consistent with current API
naming best practices.

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

### Pedantic Example

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

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

case $ensure {
'present', 'latest': {
Expand Down
7 changes: 7 additions & 0 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
$mail_to = 'root'
$warning_schedule = 'daily' # other choices: once, diminishing

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

case $::osfamily {
'FreeBSD': {
$config_file = '/usr/local/etc/smartd.conf'
Expand Down
113 changes: 101 additions & 12 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 }}
describe 'devicescan =>' do
context '(default)' do
it do
should contain_file('/etc/smartd.conf').
with_ensure('present').
with_content(/^DEVICESCAN$/)
end
end

it { should contain_file('/etc/smartd.conf').with_ensure('present') }
it { should_not contain_file('/etc/smartd.conf').with_content(/^DEVICESCAN$/) }
end
context 'true' do
let(:params) {{ :devicescan => true }}

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

context 'default => false' do
before { params[: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

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

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

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,51 @@
end
end

describe '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/)
end
end
end # (default)

context 'true' do
let(:params) {{ :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) {{ :default => false }}
it do
should contain_file('/etc/smartd.conf').with_ensure('present').
without_content(/DEFAULT -m root -M daily/)
end
end

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

end


Expand All @@ -264,10 +351,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 +371,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
6 changes: 6 additions & 0 deletions templates/smartd.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Managed by Puppet -- do not edit!
<% if @default -%>
DEFAULT -m <%= @mail_to %> -M <%= @warning_schedule %>
<% 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 @default -%>
DEVICESCAN -m <%= @mail_to %> -M <%= @warning_schedule %><% if @devicescan_options %><%= ' ' + @devicescan_options %><% end %>
<% else -%>
DEVICESCAN<% if @devicescan_options %><%= ' ' + @devicescan_options %><% end %>
<% end -%>
<% end -%>

0 comments on commit a51ca20

Please sign in to comment.