-
Notifications
You must be signed in to change notification settings - Fork 10
/
check-lv-usage.rb
executable file
·138 lines (122 loc) · 3.62 KB
/
check-lv-usage.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
#! /usr/bin/env ruby
# frozen_string_literal: false
#
# check-lv-usage
#
# DESCRIPTION:
# Uses the chef-ruby-lvm gem to get the Data% from LVS command
#
# OUTPUT:
# plain text
#
# PLATFORMS:
# Linux
#
# DEPENDENCIES:
# gem: sensu-plugin
# gem: chef-ruby-lvm
#
# USAGE:
# ./check-lv-usage.rb
#
# NOTES:
#
# LICENSE:
# Copyright 2016 Zach Bintliff <zbintliff@gmail.com>
# Released under the same terms as Sensu (the MIT license); see LICENSE
# for details.
#
require 'sensu-plugin/check/cli'
require 'lvm'
#
# Check Data and MetaData Usage on LV of LVM
#
class CheckLVUsage < Sensu::Plugin::Check::CLI
option :lv,
short: '-l LogicalVolume[,LogicalVolume]',
description: 'Name of logical volume (thinpool)',
proc: proc { |a| a.split(',') }
option :full_name,
short: '-f FullLogicalVolume[,FullLogicalVolume]',
description: 'Name of logical volume (docker/thinpool)',
proc: proc { |a| a.split(',') }
option :dwarn,
short: '-w PERCENT',
long: '--data-warn PERCENT',
description: 'Warn if PERCENT or more of logical data volume',
proc: proc(&:to_i),
default: 85
option :dcrit,
short: '-c PERCENT',
long: '--data-critical PERCENT',
description: 'Critical if PERCENT or more of logical data volume',
proc: proc(&:to_i),
default: 95
option :mwarn,
short: '-W PERCENT',
long: '--metadata-warn PERCENT',
description: 'Warn if PERCENT or more of logical metadata volume',
proc: proc(&:to_i),
default: 85
option :mcrit,
short: '-C PERCENT',
long: '--metadata-critical PERCENT',
description: 'Critical if PERCENT or more of logical metadata volume',
proc: proc(&:to_i),
default: 95
# Setup variables
#
def initialize(argv = ARGV)
super(argv)
@crit_lv = []
@warn_lv = []
end
def logical_volumes
@logical_volumes ||= LVM::LVM.new.logical_volumes.list
end
def empty_volumes_msg
<<~HEREDOC
An error occured getting the LVM info: got empty list of volumes.
Check to ensure sensu has been configured with appropriate permissions.
On linux systems it will generally need to allow executing `/sbin/lvm`
HEREDOC
end
def filter_volumes(list)
unknown empty_volumes_msg if list.empty?
begin
return list.select { |l| config[:lv].include?(l.name) } if config[:lv]
return list.select { |l| config[:full_name].include?(l.full_name) } if config[:full_name]
rescue StandardError
unknown 'An error occured getting the LVM info'
end
list
end
def check_usage(volume)
d_percent = volume.data_percent.to_i
m_percent = volume.metadata_percent.to_i
if d_percent >= config[:dcrit]
@crit_lv << "#{volume.full_name} data volume is #{d_percent}% used"
elsif d_percent >= config[:dwarn]
@warn_lv << "#{volume.full_name} data volume is #{d_percent}% used"
end
if m_percent >= config[:mcrit]
@crit_lv << "#{volume.full_name} metadata volume is #{m_percent}% used"
elsif m_percent >= config[:mwarn]
@warn_lv << "#{volume.full_name} metadata volume is #{m_percent}% used"
end
end
# Generate output
#
def check_output
(@crit_lv + @warn_lv).join(', ')
end
# Main function
#
def run
volumes = filter_volumes(logical_volumes)
volumes.each { |volume| check_usage(volume) }
critical check_output unless @crit_lv.empty?
warning check_output unless @warn_lv.empty?
ok "All logical volume data usage under #{config[:dwarn]}% and metadata usage under #{config[:mwarn]}%"
end
end