/
check-windows-disk.rb
executable file
·106 lines (95 loc) · 2.9 KB
/
check-windows-disk.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#! /usr/bin/env ruby
#
# check-windows-disk.rb
#
# DESCRIPTION:
# This is mostly copied from the original check-disk.rb plugin and modified
# to use WMIC. This is our first attempt at writing a plugin for Windows.
#
# OUTPUT:
# plain text
#
# PLATFORMS:
# Windows
#
# DEPENDENCIES:
# gem: sensu-plugin
# REQUIRES: ActiveSupport version 4.0 or above.
#
# USAGE:
#
# NOTES:
# Uses Windows WMIC facility. Warning/critical levels are percentages only.
#
# LICENSE:
# Copyright 2013 <bp-parks@wiu.edu> and <mr-mencel@wiu.edu>
# Released under the same terms as Sensu (the MIT license); see LICENSE for details.
#
require 'sensu-plugin/check/cli'
class CheckDisk < Sensu::Plugin::Check::CLI
option :fstype,
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(',') }
option :ignoremnt,
short: '-i MNT',
proc: proc { |a| a.split(',') }
option :ignorelabel,
short: '-I LABEL_REGEXP',
proc: proc { |a| Regexp.new(a) }
option :warn,
short: '-w PERCENT',
proc: proc(&:to_i),
default: 85
option :crit,
short: '-c PERCENT',
proc: proc(&:to_i),
default: 95
def initialize
super
@crit_fs = []
@warn_fs = []
end
def read_wmic
`wmic volume where DriveType=3 list brief /format:csv`.split("\n").drop(1).each do |line|
begin
# #YELLOW
_hostname, capacity, type, _fs, _avail, label, mnt = line.split(',') # rubocop:disable Lint/UnderscorePrefixedVariableName
next if /\S/ !~ line
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)
next if config[:ignorelabel] && config[:ignorelabel].match(label)
rescue
unknown "malformed line from df: #{line}"
end
# If label value is not set, the drive letter will end up in that column. Set mnt to label in that case.
mnt = label if mnt.nil?
prct_used = (100 * (1 - (_avail.to_f / capacity.to_f)))
if prct_used >= config[:crit]
@crit_fs << "#{mnt} #{prct_used.round(2)}"
elsif prct_used >= config[:warn]
@warn_fs << "#{mnt} #{prct_used.round(2)}"
end
end
end
def usage_summary
(@crit_fs + @warn_fs).join(', ')
end
def run
read_wmic
critical usage_summary unless @crit_fs.empty?
warning usage_summary unless @warn_fs.empty?
ok "All disk usage under #{config[:warn]}%"
end
end