From 1effd2422a9c718e90cfa2efb2743b1e594f23e3 Mon Sep 17 00:00:00 2001 From: Jan Wagner Date: Sat, 19 Feb 2022 20:12:22 +0100 Subject: [PATCH] check_vgfree: Adding new plugin --- check_vgfree/LICENSE | 21 ++++++ check_vgfree/Makefile | 3 + check_vgfree/README.md | 25 ++++++++ check_vgfree/check_vgfree | 130 ++++++++++++++++++++++++++++++++++++++ check_vgfree/control | 6 ++ check_vgfree/copyright | 1 + 6 files changed, 186 insertions(+) create mode 100644 check_vgfree/LICENSE create mode 100644 check_vgfree/Makefile create mode 100644 check_vgfree/README.md create mode 100644 check_vgfree/check_vgfree create mode 100644 check_vgfree/control create mode 120000 check_vgfree/copyright diff --git a/check_vgfree/LICENSE b/check_vgfree/LICENSE new file mode 100644 index 0000000..0e7d9ed --- /dev/null +++ b/check_vgfree/LICENSE @@ -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. diff --git a/check_vgfree/Makefile b/check_vgfree/Makefile new file mode 100644 index 0000000..cf9673d --- /dev/null +++ b/check_vgfree/Makefile @@ -0,0 +1,3 @@ +#/usr/bin/make -f + +include ../common.mk diff --git a/check_vgfree/README.md b/check_vgfree/README.md new file mode 100644 index 0000000..26f515a --- /dev/null +++ b/check_vgfree/README.md @@ -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 +``` diff --git a/check_vgfree/check_vgfree b/check_vgfree/check_vgfree new file mode 100644 index 0000000..94ed47e --- /dev/null +++ b/check_vgfree/check_vgfree @@ -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() diff --git a/check_vgfree/control b/check_vgfree/control new file mode 100644 index 0000000..ed577dc --- /dev/null +++ b/check_vgfree/control @@ -0,0 +1,6 @@ +Uploaders: Jan Wagner +Recommends: lvm2, python3-minimal +Version: d5bdbba +Homepage: https://github.com/alvfig/check_vgfree +Watch: https://github.com/alvfig/check_vgfree ]*>\s+([0-9a-f]+)\s+ +Description: Plugin script free space on LVM volume group diff --git a/check_vgfree/copyright b/check_vgfree/copyright new file mode 120000 index 0000000..7a694c9 --- /dev/null +++ b/check_vgfree/copyright @@ -0,0 +1 @@ +LICENSE \ No newline at end of file