diff --git a/lib/facter/smartd.rb b/lib/facter/smartd.rb new file mode 100644 index 0000000..3b737b0 --- /dev/null +++ b/lib/facter/smartd.rb @@ -0,0 +1,5 @@ +Facter.add(:smartd) do + setcode do + Facter::Util::Resolution.which('smartd') + end +end diff --git a/lib/facter/smartmontools_version.rb b/lib/facter/smartmontools_version.rb new file mode 100644 index 0000000..b38dda9 --- /dev/null +++ b/lib/facter/smartmontools_version.rb @@ -0,0 +1,12 @@ +Facter.add(:smartmontools_version) do + smartd = Facter.value(:smartd) + setcode do + unless smartd.nil? + output = Facter::Util::Resolution.exec("#{smartd} --version") + next if output.nil? + m = output.match(/smartmontools release ([\d\.]+)/) + next unless m.size == 2 + m[1] + end + end +end diff --git a/spec/fixtures/smartd/version-smartmontools-5.42-2.el5 b/spec/fixtures/smartd/version-smartmontools-5.42-2.el5 new file mode 100644 index 0000000..d81e996 --- /dev/null +++ b/spec/fixtures/smartd/version-smartmontools-5.42-2.el5 @@ -0,0 +1,14 @@ +smartd 5.42 2011-10-20 r3458 [x86_64-linux-2.6.18-348.6.1.el5] (local build) +Copyright (C) 2002-11 by Bruce Allen, http://smartmontools.sourceforge.net + +smartd comes with ABSOLUTELY NO WARRANTY. This is free +software, and you are welcome to redistribute it under +the terms of the GNU General Public License Version 2. +See http://www.gnu.org for further details. + +smartmontools release 5.42 dated 2011-10-20 at 19:19:34 UTC +smartmontools SVN rev 3458 dated 2011-10-20 at 19:20:23 +smartmontools build host: x86_64-redhat-linux-gnu +smartmontools build configured: 2013-01-09 08:24:57 UTC +smartd compile dated Jan 9 2013 at 03:25:25 +smartmontools configure arguments: '--build=x86_64-redhat-linux-gnu' '--host=x86_64-redhat-linux-gnu' '--target=x86_64-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib64' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/usr/com' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--with-selinux' '--without-systemdsystemunitdir' 'CFLAGS=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' 'CXXFLAGS=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' 'build_alias=x86_64-redhat-linux-gnu' 'host_alias=x86_64-redhat-linux-gnu' 'target_alias=x86_64-redhat-linux-gnu' diff --git a/spec/fixtures/smartd/version-smartmontools-5.43-1.el6.x86_64 b/spec/fixtures/smartd/version-smartmontools-5.43-1.el6.x86_64 new file mode 100644 index 0000000..19d7e8d --- /dev/null +++ b/spec/fixtures/smartd/version-smartmontools-5.43-1.el6.x86_64 @@ -0,0 +1,15 @@ +smartctl 5.43 2012-06-30 r3573 [x86_64-linux-2.6.32-431.5.1.el6.x86_64] (local build) +Copyright (C) 2002-12 by Bruce Allen, http://smartmontools.sourceforge.net + +smartctl comes with ABSOLUTELY NO WARRANTY. This is free +software, and you are welcome to redistribute it under +the terms of the GNU General Public License; either +version 2, or (at your option) any later version. +See http://www.gnu.org for further details. + +smartmontools release 5.43 dated 2012-06-30 at 14:03:01 UTC +smartmontools SVN rev 3573 dated 2012-06-30 at 14:03:55 +smartmontools build host: x86_64-redhat-linux-gnu +smartmontools build configured: 2012-09-25 16:04:33 UTC +smartctl compile dated Sep 25 2012 at 12:04:43 +smartmontools configure arguments: '--build=x86_64-redhat-linux-gnu' '--host=x86_64-redhat-linux-gnu' '--target=x86_64-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib64' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/var/lib' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--with-selinux' '--with-libcap-ng=yes' '-with-systemdsystemunitdir=no' 'build_alias=x86_64-redhat-linux-gnu' 'host_alias=x86_64-redhat-linux-gnu' 'target_alias=x86_64-redhat-linux-gnu' 'CXXFLAGS=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' 'CFLAGS=-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' 'PKG_CONFIG_PATH=/usr/lib64/pkgconfig:/usr/share/pkgconfig' diff --git a/spec/unit/facts/smartd_spec.rb b/spec/unit/facts/smartd_spec.rb new file mode 100644 index 0000000..1583063 --- /dev/null +++ b/spec/unit/facts/smartd_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe 'smartd', :type => :fact do + before(:each) { Facter.clear } + + context 'not in path' do + it do + Facter::Util::Resolution.stubs(:which).with('smartd').returns(nil) + Facter.fact(:smartd).value.should be_nil + end + end + + context 'in path' do + it do + Facter::Util::Resolution.stubs(:which).with('smartd').returns('/usr/sbin/smartd') + Facter.fact(:smartd).value.should == '/usr/sbin/smartd' + end + end +end + diff --git a/spec/unit/facts/smartmontools_version_spec.rb b/spec/unit/facts/smartmontools_version_spec.rb new file mode 100644 index 0000000..fbbf728 --- /dev/null +++ b/spec/unit/facts/smartmontools_version_spec.rb @@ -0,0 +1,39 @@ +require 'spec_helper' + +describe 'smartmontools_version', :type => :fact do + before(:each) { Facter.clear } + + context 'smartd fact not set' do + it 'should return nil' do + Facter.fact(:smartd).stubs(:value).returns(nil) + Facter.fact(:smartmontools_version).value.should be_nil + end + end + + context 'smartd fact is broken' do + it 'should return nil' do + Facter.fact(:smartd).stubs(:value).returns('foobar') + Facter.fact(:smartmontools_version).value.should be_nil + end + end + + context 'smartd fact is working' do + it 'should get the version string' do + Facter.fact(:smartd).stubs(:value).returns('/usr/sbin/smartd') + Facter::Util::Resolution.stubs(:exec). + with('/usr/sbin/smartd --version'). + returns(File.read(fixtures('smartd', 'version-smartmontools-5.42-2.el5'))) + Facter.fact(:smartmontools_version).value.should == '5.42' + end + + it 'should get the version string' do + Facter.fact(:smartd).stubs(:value).returns('/usr/sbin/smartd') + Facter::Util::Resolution.stubs(:exec). + with('/usr/sbin/smartd --version'). + returns(File.read(fixtures('smartd', 'version-smartmontools-5.43-1.el6.x86_64'))) + Facter.fact(:smartmontools_version).value.should == '5.43' + end + end + +end +