forked from pixelpark/monitoring-checks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_zfs.rb
executable file
·276 lines (222 loc) · 7.72 KB
/
check_zfs.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#! /opt/puppetlabs/puppet/bin/ruby
# Author: Robert Waffen <robert.waffen@pixelpark.com>
# Frank Brehm <frank.brehm@pixelpark.com>
# Date: 2021-01-27
# ./check_zfs.rb -p rpool -w 70 -c 80 -u g
# OK - rpool
# State: ONLINE
# Errors: No known data errors
# Action:
# Scan: none requested
# Capacity: 65.32% Total: 117.14 GB Used: 76.52 GB Free: 40.62 GB | Capacity=65.32%;70.0;80.0; Total=125778788352;;; Used=82161868288;;; Free=43616920064;;;
require 'optparse'
require 'pp'
Unit = Struct.new(:mu, :o_mu, :divisor)
def find_mu mu
not_found = ->{ raise "No measuring unit found for mu #{mu}" }
[ Unit.new('b', 'B', 1),
Unit.new('k', 'KB', 1024),
Unit.new('m', 'MB', 1024 * 1024),
Unit.new('g', 'GB', 1024 * 1024 * 1024),
Unit.new('t', 'TB', 1024 * 1024 * 1024 * 1024) ].find(not_found) do |u|
u.mu == mu
end
end
# Cross-platform way of finding an executable in the $PATH.
#
# which('ruby') #=> /usr/bin/ruby
def which(cmd)
std_paths = [ '/bin', '/sbin', '/usr/bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin' ]
exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
exts.each do |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable?(exe) && !File.directory?(exe)
end
end
std_paths.each do |path|
exts.each do |ext|
exe = File.join(path, "#{cmd}#{ext}")
return exe if File.executable?(exe) && !File.directory?(exe)
end
end
nil
end
class OptparseCheckZfs
Version = '0.2.5'
class ScriptOptions
attr_accessor :verbose, :pools, :warn, :crit, :unit
def initialize
self.pools = []
self.warn = 80.0
self.crit = 90.0
self.unit = find_mu('b')
self.verbose = false
end
def define_options(parser)
parser.accept(Unit) do |mu|
find_mu mu
end
msg = "Nagios/Icinga plugin to check usage of a particular ZFS pool or all ZFS pools."
msg += "\n\n"
msg += "Usage: check_zfs.rb [-p POOL] [-w USAGE_PERCENT] [-c USAGE_PERCENT] [-u b|k|m|g|t]"
parser.banner = msg
parser.separator ""
parser.separator "Check options:"
# add check options
define_pools(parser)
define_unit(parser)
define_warn(parser)
define_crit(parser)
boolean_verbose_option(parser)
parser.separator ""
parser.separator "Common options:"
parser.on_tail("-h", "--help", "Show this message") do
puts parser
exit 0
end
parser.on_tail("-V", "--version", "Show version") do
puts Version
exit 0
end
end
def define_pools(parser)
parser.on("-p", "--pool POOL", Array,
"The ZFS pool to check, multiple pools as a comma separated list.",
"If not given, all pools are checked.") do |pool|
# self.pools.push(pool)
self.pools = pool
end
end
def define_unit(parser)
parser.on("-u", "--unit UNIT", Unit,
'The measuring unit for output of sizes - <b|k|m|g|t>') do |unit|
self.unit = unit
end
end
def define_warn(parser)
parser.on("-w", "--warn USAGE_PERCENT", Float,
'The warning level of the pool(s) of percent usage.') do |warn|
self.warn = warn
end
end
def define_crit(parser)
parser.on("-c", "--crit USAGE_PERCENT", Float,
'The critical level of the pool(s) of percent usage.') do |crit|
self.crit = crit
end
end
def boolean_verbose_option(parser)
# Boolean switch.
parser.on("-v", "--[no-]verbose", "Run verbosely") do |v|
self.verbose = v
end
end
end
#
# Return a structure describing the options.
#
def parse(args)
# The options specified on the command line will be collected in
# *options*.
@options = ScriptOptions.new
@args = OptionParser.new do |parser|
@options.define_options(parser)
parser.parse!(args)
end
@options
end
attr_reader :parser, :options
end # class OptparseExample
checkfs = OptparseCheckZfs.new
options = checkfs.parse(ARGV)
if options.verbose then
pp options # checkfs.options
end
zpool = which 'zpool'
unless zpool then
puts "UNKNOWN - Command 'zpool' not found."
exit 3
end
if options.verbose then
pp zpool
end
zfs = which 'zfs'
unless zfs then
puts "UNKNOWN - Command 'zfs' not found."
exit 3
end
if options.verbose then
pp zfs
end
if options.pools.empty? then
pools = %x( #{zpool} list -H | awk '{print $1}' ).strip!.split(/\n/)
if pools.empty? then
puts "UNKNOWN - No ZFS pools found."
exit 3
end
else
pools = options.pools
end
if options.verbose then
pp pools
end
unit = options.unit.o_mu
divisor = options.unit.divisor
output = ''
perfdata = ''
state_total = [0, 'OK']
for pool in pools do
uhu = %x( #{zpool} list #{pool} >/dev/null 2>&1 )
unless $?.success? then
puts "UNKNOWN - ZFS Pool '#{pool}' does not exists."
exit 3
end
pool_values = %x( #{zfs} get -o value -Hp used,available #{pool}).split("\n")
pool_state = %x( #{zpool} status #{pool} | grep 'state:' | awk -F':' '{print $2}' ).strip!
pool_errors = %x( #{zpool} status #{pool} | grep 'errors:' | awk -F':' '{print $2}' ).strip!
pool_action = %x( #{zpool} status #{pool} | grep 'action:' | awk -F':' '{print $2}' ).strip!
pool_scan = %x( #{zpool} status #{pool} | grep 'scan:' | sed -e 's/[ ]*scan://' ).strip!
zused = pool_values[0].to_i
zavailable = pool_values[1].to_i
ztotal = zused + zavailable
zusage = ((zused.to_f / ztotal.to_f) * 100).round(2)
if divisor != 1 then
limit_usage_warn = (ztotal * options.warn / 100 / divisor).round(2)
limit_usage_crit = (ztotal * options.crit / 100 / divisor).round(2)
else
limit_usage_warn = (ztotal * options.warn / 100).round(0)
limit_usage_crit = (ztotal * options.crit / 100).round(0)
end
if state_total[0] < 2 then
state_total = [1, 'WARNING'] if zusage >= options.warn
state_total = [2, 'CRITICAL'] if zusage >= options.crit
state_total = [2, 'CRITICAL'] if pool_state != 'ONLINE'
state_total = [2, 'CRITICAL'] if pool_errors != 'No known data errors'
end
output += "\n\nZFS Pool '#{pool}'\n"
output += "State: #{pool_state}\n"
output += "Errors: #{pool_errors}\n"
output += "Action: #{pool_action}\n"
output += "Scan: #{pool_scan}\n"
output += "Usage: #{zusage}%"
unless perfdata == '' then
perfdata += ' '
end
if divisor != 1 then
o_zused = (zused.to_f / divisor).round(2)
o_zavailable = (zavailable.to_f / divisor).round(2)
o_ztotal = (ztotal.to_f / divisor).round(2)
else
o_zused = zused
o_zavailable = zavailable
o_ztotal = ztotal
end
perfdata += "usage_#{pool}=#{zusage}%;#{options.warn};#{options.crit}; "
perfdata += "total_#{pool}=#{o_ztotal}#{unit};;; "
perfdata += "used_#{pool}=#{o_zused}#{unit};#{limit_usage_warn};#{limit_usage_crit}; "
perfdata += "free_#{pool}=#{o_zavailable}#{unit};;;"
end
puts "#{state_total[1]} - Status of ZFS pools#{output} | #{perfdata}"
exit state_total[0]
# vim: ts=4 et