This repository has been archived by the owner on May 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
check_openshift_node_resources
executable file
·151 lines (111 loc) · 3.85 KB
/
check_openshift_node_resources
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/python3
import argparse
import nagiosplugin
import re
from vshn_npo import constants
from vshn_npo import nagiosutils
from vshn_npo import oc_client
from vshn_npo import utils
RES_NAMES = \
sorted("{}-{}".format(i, j)
for i in ["cpu", "memory"]
for j in ["limits", "requests"])
class ResourcesNotFound(Exception):
pass
def _col_pattern(name):
return "".join([
r"\d+(?:|\.\d+)[a-z]{0,5}\s+\(\s*(?P<",
name,
r">\d+(?:|\.\d+))\s*%\s*\)",
])
def _extract_resources(lines):
"""Parse output from "oc describe node <node>".
There is no easy way to retrieve this information in a machine-readable
format.
"""
while lines:
line = lines.pop(0)
if re.match(r"^Allocated\s+resources\s*:\s*$", line, re.I):
break
if not lines:
raise ResourcesNotFound()
while lines:
line = lines.pop(0)
# Changed output formats can be handled in separate branches
if re.match((r"^\s*CPU\s+Requests\s+CPU\s+Limits\s+Memory\s+Requests"
r"\s+Memory\s+Limits\s*$"),
line, re.I):
# Remove separator (if any)
if lines and re.match(r"^[-\s]{10,}$", lines[0]):
del lines[0]
pattern = "".join([
r"(?i)^\s*",
_col_pattern("cpu_requests"),
r"\s+",
_col_pattern("cpu_limits"),
r"\s+",
_col_pattern("memory_requests"),
r"\s+",
_col_pattern("memory_limits"),
r"\s*$",
])
if lines:
m = re.match(pattern, lines[0])
if m:
# Extract value for each name
return dict((i, float(m.group(i.replace("-", "_"))))
for i in RES_NAMES)
raise ResourcesNotFound()
class NodeResources(nagiosplugin.Resource):
def __init__(self, client, node):
self._client = client
self._node = node
def probe(self):
output = self._client.capture_output(["describe", "node", self._node])
try:
for (key, value) in sorted(_extract_resources(output.splitlines()).items()):
yield nagiosplugin.Metric(key, value, uom="%", min=0, context=key)
except ResourcesNotFound:
raise nagiosplugin.CheckError("Allocated resources not found in output")
def _parse_range(value):
parts = value.split("=", 1)
if len(parts) != 2:
raise argparse.ArgumentTypeError("Must use format NAME=RANGE")
return parts
def _convert_ranges(values):
result = dict.fromkeys(RES_NAMES, None)
if values:
for (name, i) in values:
if name not in result:
raise ValueError("Unknown value name: {}".format(name))
if not i:
raise ValueError("Value \"{}\" requires range".format(name))
result[name] = i
return result
@nagiosplugin.guarded
def main():
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
utils.add_verbose_argument(parser)
oc_client.add_oc_arguments(parser)
parser.add_argument("-n", "--node", required=True, help="Node name")
parser.add_argument("-w", "--warning", metavar="NAME=WARN",
type=_parse_range, action="append",
help="Warn if named value is outside given range")
parser.add_argument("-c", "--critical", metavar="NAME=CRIT",
type=_parse_range, action="append",
help="Critical if named value is outside given range")
args = parser.parse_args()
utils.setup_basic_logging(args.verbose)
client = oc_client.Client.from_arguments(args)
warn = _convert_ranges(args.warning)
crit = _convert_ranges(args.critical)
checks = [
NodeResources(client, args.node),
nagiosutils.FullSummary(),
]
checks.extend(nagiosplugin.ScalarContext(i, warning=warn[i], critical=crit[i])
for i in RES_NAMES)
nagiosplugin.Check(*checks).main(verbose=args.verbose, timeout=None)
if __name__ == "__main__":
main()
# vim: set sw=2 sts=2 et :