Skip to content

Commit

Permalink
Feat/check disk only label (#53)
Browse files Browse the repository at this point in the history
* Adds --mount_points argument

* Tweaks for Rubocop

* Sanitises PC name

* Fixes short flag

* Improves tests for -m flag

* Removes whitespace

* Fixes line endings to LF
  • Loading branch information
absolutejam authored and majormoses committed Dec 9, 2017
1 parent f5b68ec commit 67d7ca5
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Expand Up @@ -28,3 +28,7 @@ RegexpLiteral:

Style/Documentation:
Enabled: false

Style/BlockLength:
Exclude:
- 'test/**/*.rb'
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions bin/check-windows-disk.rb
Expand Up @@ -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(',') }
Expand Down Expand Up @@ -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)
Expand Down
55 changes: 55 additions & 0 deletions 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

0 comments on commit 67d7ca5

Please sign in to comment.