diff --git a/.rubocop.yml b/.rubocop.yml index 0ba1665..a6e55e3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -28,3 +28,7 @@ RegexpLiteral: Style/Documentation: Enabled: false + +Style/BlockLength: + Exclude: + - 'test/**/*.rb' diff --git a/CHANGELOG.md b/CHANGELOG.md index e444216..6a9c58a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ This CHANGELOG follows the format listed at [Our CHANGELOG Guidelines ](https:// Which is based on [Keep A Changelog](http://keepachangelog.com/) ## [Unreleased] +### Added +- `check-windows-service.rb`: adds --mount_points option to explicitly specify which drive mounts to check (@absolutejam) ### Added - minimal `appveyor.yml` (@majormoses) diff --git a/bin/check-windows-disk.rb b/bin/check-windows-disk.rb index 8eda3dc..78deb05 100755 --- a/bin/check-windows-disk.rb +++ b/bin/check-windows-disk.rb @@ -32,6 +32,11 @@ class CheckDisk < Sensu::Plugin::Check::CLI short: '-t TYPE', proc: proc { |a| a.split(',') } + option :mount_points, + short: '-m MNT', + long: '--mount_points', + proc: proc { |a| a.split(',') } + option :ignoretype, short: '-x TYPE', proc: proc { |a| a.split(',') } @@ -69,6 +74,7 @@ def read_wmic next if _avail.nil? next if line.include?('System Reserved') next if line.include?('\Volume') + next if config[:mount_points] && !config[:mount_points].include?(mnt) next if config[:fstype] && !config[:fstype].include?(type) next if config[:ignoretype] && config[:ignoretype].include?(type) next if config[:ignoremnt] && config[:ignoremnt].include?(mnt) diff --git a/test/check-windows-disk.rb b/test/check-windows-disk.rb new file mode 100644 index 0000000..676bcd8 --- /dev/null +++ b/test/check-windows-disk.rb @@ -0,0 +1,55 @@ +require_relative '../bin/check-windows-disk.rb' + +describe CheckDisk do + before(:all) do + CheckDisk.class_variable_set(:@@autorun, nil) + end + + describe '--mount_points flag' do + let(:check) do + CheckDisk.new + end + + before(:each) do + # Stub the ` method + mock_wmic = ' +Node,Capacity,DriveType,FileSystem,FreeSpace,Label,Name +Localhost,249532772352,3,NTFS,6416957440,CRITICALDrive,C:\\ +Localhost,1152921504606846976,3,FAT32,1152921504574169088,OKDrive,D:\\' + + allow(described_class) + .to receive(:`) + .with('wmic volume where DriveType=3 list brief /format:csv') + .and_return(mock_wmic) + end + + def check_mount_point(check) + expect do + begin + check.run + rescue SystemExit # rubocop:disable HandleExceptions + end + end + end + + it 'should match CRITICAL when checking C: drive with long flags' do + check.parse_options ['--mount_points', 'C:\\'] + check_mount_point(check).to output(/CheckDisk CRITICAL/).to_stdout + end + + it 'should match CRITICAL when checking C: drive with short flags' do + check.parse_options ['--m', 'C:\\'] + check_mount_point(check).to output(/CheckDisk CRITICAL/).to_stdout + end + + it 'should match OK when checking D: drive with long flags' do + check.parse_options ['--mount_points', 'D:\\'] + check_mount_point(check).to output(/CheckDisk OK/).to_stdout + end + + it 'should match OK when checking D: drive with short flags' do + check.parse_options ['--mount_points', 'D:\\'] + check_mount_point(check).to output(/CheckDisk OK/).to_stdout + end + end +end