Merge pull request #23 from waja/feature/15-Add-check_vg_size-or-check_vgfree

Add check_vgfree
This commit is contained in:
waja 2022-02-19 20:21:10 +01:00 committed by GitHub
commit b91fc10fa0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 186 additions and 0 deletions

21
check_vgfree/LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 Alvaro Figueiredo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

3
check_vgfree/Makefile Normal file
View file

@ -0,0 +1,3 @@
#/usr/bin/make -f
include ../common.mk

25
check_vgfree/README.md Normal file
View file

@ -0,0 +1,25 @@
# check_vgfree
Nagios plugin to check free space on LVM volume group
```console
$ chmod +x check_vgfree
$ ./check_vgfree --help
Usage: check_vgfree [options]
try: check_vgfree --help
Options:
-h, --help show this help message and exit
-c CRITICAL, --critical=CRITICAL
critical size limit
-C CP, --critical-percent=CP
% critical limit
-g VG, --volume-group=VG
volume group to check
-w WARNING, --warning=WARNING
warning size limit
-W WP, --warning-percent=WP
% warning limit
--units=UNIT size in these units: (b)ytes, (k)ilobytes,
(m)egabytes, *(g)igabytes (*DEFAULT), (t)erabytes,
(p)etabytes, (e)xabytes
```

130
check_vgfree/check_vgfree Normal file
View file

@ -0,0 +1,130 @@
#!/usr/bin/python3
# check_vgfree - Nagios plugin to check free space on LVM volume group
from __future__ import print_function, division
import optparse
import os
import subprocess
import sys
# Subclass to return code 3 at error.
class OptionParser(optparse.OptionParser):
def error(self, msg=None):
self.print_usage(sys.stderr)
self.exit(3, "%s: error: %s\n" % (self.get_prog_name(), msg))
class NagiosStatus(Exception):
Label = ['OK', 'WARNING', 'CRITICAL', 'UNDEFINED']
def __init__(self, code, message):
self.code = code
self.message = message
def __str__(self):
return "%s - %s" % (NagiosStatus.Label[self.code], self.message)
def returnStatus(self):
print(self)
sys.exit(self.code)
def parsecmdline():
"Parse command line options."
usage = 'Usage: %prog [options]\n try: %prog --help'
optionparser = OptionParser(usage)
optionparser.add_option("-c", "--critical", type="float", default=0,
help="critical size limit")
optionparser.add_option("-C", "--critical-percent", type="float", dest="cp",
default=0, help="% critical limit")
optionparser.add_option("-g", "--volume-group", dest="vg", default="vg0",
help="volume group to check")
optionparser.add_option("-w", "--warning", type="float", default=0,
help="warning size limit")
optionparser.add_option("-W", "--warning-percent", type="float", dest="wp",
default=0, help="% warning limit")
optionparser.add_option("--units", type="choice", dest="unit", default="g",
choices=("b", "k", "m", "g", "t", "p", "e"),
help="size in these units: (b)ytes, (k)ilobytes, (m)egabytes, "
"*(g)igabytes (*DEFAULT), (t)erabytes, (p)etabytes, (e)xabytes")
options, args = optionparser.parse_args()
return options
def pickvgscommand():
"Pick vgs command path"
command = "/sbin/vgs"
if not os.path.isfile(command):
command = "/usr" + command
if not os.path.isfile(command):
msg = 'vgs command not found (is lvm2 package installed?)'
raise NagiosStatus(3, msg)
return command
def pickvgspace(vg, unit):
"Pick free space on volume group."
command = pickvgscommand()
command = [command, "--noheadings", "--nosuffix",
"--options=vg_size,vg_free", "--units=%s" % unit, vg]
environment = {"LANG": "C"}
process = subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, env=environment)
output, error = process.communicate()
if 0 < process.returncode:
error = error.split('\n')[0].strip()
raise NagiosStatus(3, error)
size, free = [float(x) for x in output.split()]
return size, free
def checkstatus(free, percent, vg, critical, cp, warning, wp, unit):
"Check the limits."
result = "%g%sB (%g%%) free on volume group %s" % (free, unit.upper(),
percent, vg)
if 0 < critical and free <= critical or 0 < cp and percent <= cp:
return NagiosStatus(2, result)
elif 0 < warning and free <= warning or 0 < wp and percent <= wp:
return NagiosStatus(1, result)
else:
return NagiosStatus(0, result)
def main():
# Parse command line options.
options = parsecmdline()
# Pick free space on volume group.
try:
size, free = pickvgspace(options.vg, options.unit)
except NagiosStatus as ns:
ns.returnStatus()
percent = 100 * free / size
# Check the limits.
ns = checkstatus(
free,
percent,
options.vg,
options.critical,
options.cp,
options.warning,
options.wp,
options.unit
)
ns.returnStatus()
if __name__ == '__main__':
main()

6
check_vgfree/control Normal file
View file

@ -0,0 +1,6 @@
Uploaders: Jan Wagner <waja@cyconet.org>
Recommends: lvm2, python3-minimal
Version: d5bdbba
Homepage: https://github.com/alvfig/check_vgfree
Watch: https://github.com/alvfig/check_vgfree <a class="commit-tease-sha"[^>]*>\s+([0-9a-f]+)\s+</a>
Description: Plugin script free space on LVM volume group

1
check_vgfree/copyright Symbolic link
View file

@ -0,0 +1 @@
LICENSE