-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_zypper
executable file
·70 lines (55 loc) · 2.74 KB
/
check_zypper
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
63
64
65
66
67
68
69
70
#!/usr/bin/env python3
import argparse
import subprocess
import xml.etree.ElementTree as ET
from enum import Enum
class IcingaState(Enum):
OK = 0
WARNING = 1
CRITICAL = 2
UNKNOWN = 3
CHECK_NAME = "zypper"
def analyze_updates(result: str, updates: dict) -> dict:
root = ET.fromstring(result)
for elem in root.findall('.//update'):
kind = elem.attrib["kind"]
if kind == "package":
updates["packages"] = updates["packages"] + 1
if kind == "patch":
if elem.attrib["category"] == "security":
updates["security"] = updates["security"] + 1
else:
updates["patches"] = updates["patches"] + 1
return updates
def run(args):
updates = dict()
updates["packages"] = 0
updates["security"] = 0
updates["patches"] = 0
result_lu = subprocess.run(["/usr/bin/zypper", "-x", "lu"], check=True, stdout=subprocess.PIPE)
updates = analyze_updates(result_lu.stdout, updates)
result_lp = subprocess.run(["/usr/bin/zypper", "-x", "lp"], check=True, stdout=subprocess.PIPE)
updates = analyze_updates(result_lp.stdout, updates)
generate_icinga_response(updates, args)
def generate_icinga_response(updates: dict, args):
icinga_state: IcingaState = IcingaState.UNKNOWN
if updates["packages"] > args.pack_w or updates["patches"] > args.patch_w:
icinga_state = IcingaState.WARNING
else:
icinga_state = IcingaState.OK
if (updates["security"] > 0) or (updates["packages"] > args.pack_c) or (updates["patches"] > args.patch_c):
icinga_state = IcingaState.CRITICAL
print("%s %s: %s updates available | updates=%d;%d;%d;; patches=%d;%d;%d;; security=%d;;;%d;%d" % (CHECK_NAME, icinga_state.name,
updates["packages"] + updates["security"] + updates["patches"],
updates["packages"], args.pack_w, args.pack_c,
updates["patches"], args.patch_w, args.patch_c,
updates["security"], 0, 1))
exit(icinga_state.value)
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="check_zypper", description="Checker Plugin for Icinga2")
parser.add_argument("-pack_w", type=int, default=10)
parser.add_argument("-pack_c", type=int, default=50)
parser.add_argument("-patch_w", type=int, default=5)
parser.add_argument("-patch_c", type=int, default=10)
args = parser.parse_args()
run(args)