-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_reboot_required.py
executable file
·62 lines (54 loc) · 1.52 KB
/
check_reboot_required.py
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
#! /usr/bin/python3
#
# This Icinga plugin checks if the machine needs rebooting.
#
# Copy this file into /usr/local/lib/nagios/plugins/
#
# Usage: check_reboot_required.py [-w seconds] [-c seconds]
#
import argparse
from datetime import timedelta
import os.path
import time
import sys
PATH = "/var/run/reboot-required"
PATH_PKGS = "/var/run/reboot-required.pkgs"
parser = argparse.ArgumentParser(description="Icinga Reboot-Required Plugin")
parser.add_argument(
"-w",
"--warning",
type=int,
default=0,
help="the timespan in seconds before a reboot becomes a warning (default: 0)",
)
parser.add_argument(
"-c",
"--critical",
type=int,
default=7 * 86400,
help="the timespan in seconds before a reboot becomes critical (default: 7 days)",
)
args = parser.parse_args()
try:
delay = 0.0
delay_msg = ""
if os.path.isfile(PATH):
delay = time.time() - os.path.getmtime(PATH)
delay_msg = f" - Machine requires a reboot. ({timedelta(seconds = int(delay))})"
with open(PATH_PKGS, "r") as fp:
pkgs = [line.strip() for line in fp]
delay_msg += " Packages: " + ", ".join(pkgs)
if delay > args.critical:
print(f"REBOOT ERROR{delay_msg}")
sys.exit(2)
if delay > args.warning:
print(f"REBOOT WARNING{delay_msg}")
sys.exit(1)
if delay > 0:
print("REBOOT OK{delay_msg}")
sys.exit(0)
print("REBOOT OK")
sys.exit(0)
except Exception as exc:
print("UNKNOWN UNKNOWN: %s" % str(exc))
sys.exit(3)