2022-10-08 10:09:21 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
Icinga/Nagios plugin to check if pip upgrade is available
|
|
|
|
|
|
|
|
Copyright (c) 2022 Benjamin Renard <brenard@zionetrix.net>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License version 3
|
|
|
|
as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import json
|
|
|
|
import logging
|
2024-03-15 09:07:44 +01:00
|
|
|
import os
|
|
|
|
import subprocess # nosec
|
|
|
|
import sys
|
2022-10-08 10:09:21 +02:00
|
|
|
import traceback
|
|
|
|
|
|
|
|
# nagios exit code
|
2024-03-15 09:07:44 +01:00
|
|
|
STATUS = {"OK": 0, "WARNING": 1, "CRITICAL": 2, "UNKNOWN": 3}
|
2022-10-08 10:09:21 +02:00
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
2024-03-15 09:07:44 +01:00
|
|
|
parser.add_argument("-d", "--debug", action="store_true", dest="debug", default=False)
|
2022-10-08 10:09:21 +02:00
|
|
|
|
2024-03-15 09:07:44 +01:00
|
|
|
parser.add_argument("-b", "--bin", action="store", dest="bin", help="Python binary path", type=str)
|
2022-10-08 10:09:21 +02:00
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
action="store",
|
|
|
|
dest="packages",
|
2024-03-15 09:07:44 +01:00
|
|
|
help=("Python package(s) to check. By default, all installed" "packages are checked."),
|
|
|
|
nargs="*",
|
|
|
|
default=[],
|
2022-10-08 10:09:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
options = parser.parse_args()
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
level=logging.DEBUG if options.debug else logging.INFO,
|
2024-03-15 09:07:44 +01:00
|
|
|
format="%(asctime)s - %(levelname)s - %(message)s",
|
|
|
|
)
|
2022-10-08 10:09:21 +02:00
|
|
|
|
|
|
|
if options.bin and not os.path.exists(options.bin):
|
|
|
|
print(f'UNKNOWN - python executable "{options.bin}" not found')
|
2024-03-15 09:07:44 +01:00
|
|
|
sys.exit(STATUS["UNKNOWN"])
|
2022-10-08 10:09:21 +02:00
|
|
|
|
|
|
|
cmd = [
|
2024-03-15 09:07:44 +01:00
|
|
|
options.bin if options.bin else sys.executable,
|
|
|
|
"-m",
|
|
|
|
"pip",
|
|
|
|
"list",
|
|
|
|
"--format",
|
|
|
|
"json",
|
|
|
|
"--outdated",
|
2022-10-08 10:09:21 +02:00
|
|
|
]
|
2024-03-15 09:07:44 +01:00
|
|
|
logging.debug("Execute external command: %s", " ".join(cmd))
|
|
|
|
output = subprocess.check_output(cmd) # nosec
|
|
|
|
logging.debug("Output:\n%s", output)
|
2022-10-08 10:09:21 +02:00
|
|
|
|
|
|
|
try:
|
2024-03-15 09:07:44 +01:00
|
|
|
outdated_packages = {
|
|
|
|
package["name"]: package
|
2022-10-08 10:09:21 +02:00
|
|
|
for package in json.loads(output)
|
2024-03-15 09:07:44 +01:00
|
|
|
if not options.packages or package["name"] in options.packages
|
|
|
|
}
|
2022-12-17 00:23:15 +01:00
|
|
|
except Exception as exc: # pylint: disable=broad-except
|
2024-03-15 09:07:44 +01:00
|
|
|
print("UNKNOWN - Exception occurred parsing pip output")
|
2022-10-08 10:09:21 +02:00
|
|
|
traceback.print_exc(exc)
|
2024-03-15 09:07:44 +01:00
|
|
|
sys.exit(STATUS["UNKNOWN"])
|
2022-10-08 10:09:21 +02:00
|
|
|
|
|
|
|
if outdated_packages:
|
|
|
|
if len(outdated_packages) > 1:
|
2024-03-15 09:07:44 +01:00
|
|
|
print(f"WARNING - {len(outdated_packages)} upgrades available")
|
|
|
|
print(
|
|
|
|
"\n".join(
|
|
|
|
[
|
|
|
|
f' - {name} ({p["version"]} => {p["latest_version"]})'
|
|
|
|
for name, p in outdated_packages.items()
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2022-10-08 10:09:21 +02:00
|
|
|
else:
|
|
|
|
name = next(iter(outdated_packages))
|
|
|
|
p = outdated_packages[name]
|
|
|
|
print(
|
2024-03-15 09:07:44 +01:00
|
|
|
f"WARNING - available upgrade for {name} " f'({p["version"]} => {p["latest_version"]})'
|
2022-10-08 10:09:21 +02:00
|
|
|
)
|
2024-03-15 09:07:44 +01:00
|
|
|
sys.exit(STATUS["WARNING"])
|
2022-10-08 10:09:21 +02:00
|
|
|
|
|
|
|
if options.packages and len(options.packages) == 1:
|
2024-03-15 09:07:44 +01:00
|
|
|
print(f"OK - {options.packages[0]} is up-to-date")
|
2022-10-08 10:09:21 +02:00
|
|
|
else:
|
2024-03-15 09:07:44 +01:00
|
|
|
print("OK - all packages is up-to-date")
|
|
|
|
sys.exit(STATUS["OK"])
|