sensu-plugins-disk-usage 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 87993f3afe5c1f43afb52e49ce432607e2c81ceb
4
+ data.tar.gz: b6ca5b7e0dce4db451592071baa7ad30f7435a13
5
+ SHA512:
6
+ metadata.gz: 1b35b0d41f9f69ecc958c9ae791c1d5b89b24413643ee90db13f8d5f7b50711d022e95df6c3c252e07fc6c00d9a7f5b912d8471d819a92a43bfbd563c06027d4
7
+ data.tar.gz: 5fb18017ad8419e3ac541fbe9a860da373434d582bb33eb7495299bd6d8ccc4c00e361a23687d19e91a28175aeacf0be909199b4ea8e9fa1185230614b22addc
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Change Log
2
+ This project adheres to [Semantic Versioning](http://semver.org/).
3
+
4
+ This CHANGELOG follows the format listed at [Keep A Changelog](http://keepachangelog.com/)
5
+
6
+ ## Unreleased
7
+
8
+ ## [0.0.1] - 2015-12-29
9
+ ### Added
10
+ - Initial release
data/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2015 Matteo Cerutti
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Sensu plugin for monitoring disk usage
2
+
3
+ A sensu plugin to monitor disk usage on Linux.
4
+
5
+ The plugin generates multiple OK/WARN/CRIT/UNKNOWN events via the sensu client socket (https://sensuapp.org/docs/latest/clients#client-socket-input)
6
+ so that you do not miss state changes when monitoring multiple volumes.
7
+
8
+ ## Usage
9
+
10
+ The plugin accepts the following command line options:
11
+
12
+ ```
13
+ Usage: check-disk-usage.rb (options)
14
+ -c, --config <PATH> Optional configuration file (default: ./disk-usage.json)
15
+ --crit-inodes <PERCENT> Critical if PERCENT or more of inodes used
16
+ --crit-space <PERCENT> Critical if PERCENT or more of disk space used
17
+ --fstype <TYPE> Comma separated list of file system type(s) (default: all)
18
+ --ignore-fstype <TYPE> Comma separated list of file system type(s) to ignore
19
+ --ignore-mount <MOUNTPOINT> Comma separated list of mount point(s) to ignore
20
+ --ignore-mount-regex <MOUNTPOINT>
21
+ Comma separated list of mount point(s) to ignore (regex)
22
+ --mount <MOUNTPOINT> Comma separated list of mount point(s) (default: all)
23
+ --mount-regex <MOUNTPOINT> Comma separated list of mount point(s) (regex)
24
+ -w, --warn Warn instead of throwing a critical failure
25
+ --warn-inodes <PERCENT> Warn if PERCENT or more of inodes used
26
+ --warn-space <PERCENT> Warn if PERCENT or more of disk space used
27
+ ```
28
+
29
+ By default, the warning and critical parameters are global to all mountpoints. However, each mountpoint can override the defaults in an optional JSON configuration file which must be placed
30
+ in the same location as the plugin.
31
+
32
+ JSON example:
33
+
34
+ ```
35
+ {
36
+ "mountpoints": {
37
+ "/": {
38
+ "warn_space": 70,
39
+ "crit_space": 85,
40
+ "warn_inodes": 65,
41
+ "crit_inodes": 75
42
+ },
43
+ ..
44
+ }
45
+ }
46
+ ```
47
+
48
+ ## Author
49
+ Matteo Cerutti - <matteo.cerutti@hotmail.co.uk>
@@ -0,0 +1,248 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # check-disk-usage.rb
4
+ #
5
+ # Author: Matteo Cerutti <matteo.cerutti@hotmail.co.uk>
6
+ #
7
+
8
+ require 'sensu-plugin/check/cli'
9
+ require 'json'
10
+ require 'socket'
11
+ require 'sys/filesystem'
12
+
13
+ class CheckDiskUsage < Sensu::Plugin::Check::CLI
14
+ option :fstype,
15
+ :description => "Comma separated list of file system type(s) (default: all)",
16
+ :long => "--fstype <TYPE>",
17
+ :proc => proc { |a| a.split(',') },
18
+ :default => []
19
+
20
+ option :ignore_fstype,
21
+ :description => "Comma separated list of file system type(s) to ignore",
22
+ :long => "--ignore-fstype <TYPE>",
23
+ :proc => proc { |a| a.split(',') },
24
+ :default => []
25
+
26
+ option :mount,
27
+ :description => "Comma separated list of mount point(s) (default: all)",
28
+ :long => "--mount <MOUNTPOINT>",
29
+ :proc => proc { |a| a.split(',') },
30
+ :default => []
31
+
32
+ option :mount_regex,
33
+ :description => "Comma separated list of mount point(s) (regex)",
34
+ :long => "--mount-regex <MOUNTPOINT>",
35
+ :proc => proc { |a| a.split(',') },
36
+ :default => []
37
+
38
+ option :ignore_mount,
39
+ :description => "Comma separated list of mount point(s) to ignore",
40
+ :long => "--ignore-mount <MOUNTPOINT>",
41
+ :proc => proc { |a| a.split(',') },
42
+ :default => []
43
+
44
+ option :ignore_mount_regex,
45
+ :description => "Comma separated list of mount point(s) to ignore (regex)",
46
+ :long => "--ignore-mount-regex <MOUNTPOINT>",
47
+ :proc => proc { |a| a.split(',') },
48
+ :default => []
49
+
50
+ option :config_file,
51
+ :description => "Optional configuration file (default: #{File.dirname(__FILE__)}/disk-usage.json)",
52
+ :short => "-c <PATH>",
53
+ :long => "--config <PATH>",
54
+ :default => File.dirname(__FILE__) + "/disk-usage.json"
55
+
56
+ option :warn_space,
57
+ :description => "Warn if PERCENT or more of disk space used",
58
+ :short => "--warn-space <PERCENT>",
59
+ :proc => proc(&:to_i),
60
+ :default => 85
61
+
62
+ option :crit_space,
63
+ :description => "Critical if PERCENT or more of disk space used",
64
+ :short => "--crit-space <PERCENT>",
65
+ :proc => proc(&:to_i),
66
+ :default => 95
67
+
68
+ option :warn_inodes,
69
+ :description => "Warn if PERCENT or more of inodes used",
70
+ :short => "--warn-inodes <PERCENT>",
71
+ :proc => proc(&:to_i),
72
+ :default => 85
73
+
74
+ option :crit_inodes,
75
+ :description => "Critical if PERCENT or more of inodes used",
76
+ :short => "--crit-inodes <PERCENT>",
77
+ :proc => proc(&:to_i),
78
+ :default => 95
79
+
80
+ option :warn,
81
+ :description => "Warn instead of throwing a critical failure",
82
+ :short => "-w",
83
+ :long => "--warn",
84
+ :boolean => false
85
+
86
+ def initialize()
87
+ super
88
+
89
+ # discover mount points
90
+ @mounts = get_mounts()
91
+
92
+ @json_config = {}
93
+ if File.exists?(config[:config_file])
94
+ @json_config = JSON.parse(File.read(config[:config_file]))
95
+ end
96
+ end
97
+
98
+ def get_mounts()
99
+ mounts = []
100
+
101
+ Sys::Filesystem.mounts.each do |fs|
102
+ if config[:ignore_fstype].size > 0
103
+ next if config[:ignore_fstype].include?(fs.mount_type)
104
+ end
105
+
106
+ if config[:fstype].size > 0
107
+ next unless config[:fstype].include?(fs.mount_type)
108
+ end
109
+
110
+ if config[:ignore_mount].size > 0
111
+ next if config[:ignore_mount].include?(fs.mount_type)
112
+ end
113
+
114
+ if config[:ignore_mount_regex].size > 0
115
+ b = false
116
+ config[:ignore_mount_regex].each do |mnt|
117
+ if fs.mount_point =~ Regexp.new(mnt)
118
+ b = true
119
+ break
120
+ end
121
+ end
122
+ next if b
123
+ end
124
+
125
+ if config[:mount].size > 0
126
+ next unless config[:mount].include?(fs.mount_type)
127
+ end
128
+
129
+ if config[:mount_regex].size > 0
130
+ b = true
131
+ config[:mount_regex].each do |mnt|
132
+ if fs.mount_point =~ Regexp.new(mnt)
133
+ b = false
134
+ break
135
+ end
136
+ end
137
+ next if b
138
+ end
139
+
140
+ mounts << fs.mount_point
141
+ end
142
+
143
+ mounts
144
+ end
145
+
146
+ def send_client_socket(data)
147
+ sock = UDPSocket.new
148
+ sock.send(data + "\n", 0, "127.0.0.1", 3030)
149
+ end
150
+
151
+ def send_ok(check_name, msg)
152
+ event = {"name" => check_name, "status" => 0, "output" => "OK: #{msg}", "handler" => config[:handler]}
153
+ send_client_socket(event.to_json)
154
+ end
155
+
156
+ def send_warning(check_name, msg)
157
+ event = {"name" => check_name, "status" => 1, "output" => "WARNING: #{msg}", "handler" => config[:handler]}
158
+ send_client_socket(event.to_json)
159
+ end
160
+
161
+ def send_critical(check_name, msg)
162
+ event = {"name" => check_name, "status" => 2, "output" => "CRITICAL: #{msg}", "handler" => config[:handler]}
163
+ send_client_socket(event.to_json)
164
+ end
165
+
166
+ def send_unknown(check_name, msg)
167
+ event = {"name" => check_name, "status" => 3, "output" => "UNKNOWN: #{msg}", "handler" => config[:handler]}
168
+ send_client_socket(event.to_json)
169
+ end
170
+
171
+ def run
172
+ problems = 0
173
+
174
+ @mounts.uniq.each do |mount|
175
+ stat = Sys::Filesystem.stat(mount)
176
+
177
+ if stat.bytes_total > 0
178
+ if @json_config.has_key?('mountpoints') and @json_config['mountpoints'].has_key?(mount) and @json_config['mountpoints'][mount].has_key?("warn_space")
179
+ warn_space = @json_config['mountpoints'][mount]['warn_space']
180
+ else
181
+ warn_space = config[:warn_space]
182
+ end
183
+
184
+ if @json_config.has_key?('mountpoints') and @json_config['mountpoints'].has_key?(mount) and @json_config['mountpoints'][mount].has_key?("crit_space")
185
+ crit_space = @json_config['mountpoints'][mount]['crit_space']
186
+ else
187
+ crit_space = config[:crit_space]
188
+ end
189
+
190
+ percent_bytes_used = (100.0 - (100.0 * stat.bytes_free / stat.bytes_total)).round(2)
191
+
192
+ check_name = "disk-usage-space-#{mount.gsub('/', '_')}"
193
+ msg = "Filesystem #{mount} space usage is #{percent_bytes_used}%"
194
+
195
+ if percent_bytes_used >= crit_space
196
+ msg += ", expected < #{crit_space}%"
197
+ send_critical(check_name, msg)
198
+ problems += 1
199
+ elsif percent_bytes_used >= warn_space
200
+ msg += ", expected < #{warn_space}%"
201
+ send_warning(check_name, msg)
202
+ problems += 1
203
+ else
204
+ send_ok(check_name, msg)
205
+ end
206
+ end
207
+
208
+ if stat.inodes > 0
209
+ if @json_config.has_key?('mountpoints') and @json_config['mountpoints'].has_key?(mount) and @json_config['mountpoints'][mount].has_key?("warn_inodes")
210
+ warn_inodes = @json_config['mountpoints'][mount]['warn_inodes']
211
+ else
212
+ warn_inodes = config[:warn_inodes]
213
+ end
214
+
215
+ if @json_config.has_key?('mountpoints') and @json_config['mountpoints'].has_key?(mount) and @json_config['mountpoints'][mount].has_key?("crit_inodes")
216
+ crit_inodes = @json_config['mountpoints'][mount]['crit_inodes']
217
+ else
218
+ crit_inodes = config[:crit_inodes]
219
+ end
220
+
221
+ percent_inodes_used = (100.0 - (100.0 * stat.inodes_free / stat.inodes)).round(2)
222
+
223
+ check_name = "disk-usage-inodes-#{mount.gsub('/', '_')}"
224
+ msg = "Filesystem #{mount} inodes usage is #{percent_inodes_used}%"
225
+
226
+ if percent_inodes_used >= crit_inodes
227
+ msg += ", expected < #{crit_inodes}%"
228
+ send_critical(check_name, msg)
229
+ problems += 1
230
+ elsif percent_inodes_used >= warn_inodes
231
+ msg += ", expected < #{warn_inodes}%"
232
+ send_warning(check_name, msg)
233
+ problems += 1
234
+ else
235
+ send_ok(check_name, msg)
236
+ end
237
+ end
238
+ end
239
+
240
+ if problems > 0
241
+ message "Found #{problems} problems"
242
+ warning if config[:warn]
243
+ critical
244
+ else
245
+ ok "All mountpoints (#{@mounts.join(', ')}) are OK"
246
+ end
247
+ end
248
+ end
@@ -0,0 +1,7 @@
1
+ {
2
+ "mountpoints": {
3
+ "/": {
4
+ "warn_space": 10
5
+ }
6
+ }
7
+ }
@@ -0,0 +1,9 @@
1
+ module SensuPluginsDiskUsage
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ PATCH = 1
6
+
7
+ VER_STRING = [MAJOR, MINOR, PATCH].compact.join('.')
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ require 'sensu-plugins-disk-usage/version'
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensu-plugins-disk-usage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matteo Cerutti
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sensu-plugin
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 1.2.0
27
+ description: This plugin provides facilities for monitoring disk usage
28
+ email: "<matteo.cerutti@hotmail.co.uk>"
29
+ executables:
30
+ - check-disk-usage.rb
31
+ - disk-usage.json.example
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - CHANGELOG.md
36
+ - LICENSE
37
+ - README.md
38
+ - bin/check-disk-usage.rb
39
+ - bin/disk-usage.json.example
40
+ - lib/sensu-plugins-disk-usage.rb
41
+ - lib/sensu-plugins-disk-usage/version.rb
42
+ homepage: https://github.com/m4ce/sensu-plugins-disk-usage
43
+ licenses:
44
+ - MIT
45
+ metadata:
46
+ maintainer: "@m4ce"
47
+ development_status: active
48
+ production_status: stable
49
+ release_draft: 'false'
50
+ release_prerelease: 'false'
51
+ post_install_message: You can use the embedded Ruby by setting EMBEDDED_RUBY=true
52
+ in /etc/default/sensu
53
+ rdoc_options: []
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 1.9.3
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 2.4.5.1
69
+ signing_key:
70
+ specification_version: 4
71
+ summary: Sensu plugins for monitoring disk usage
72
+ test_files: []