-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFreePhysicalMemorySize.py
81 lines (66 loc) · 2.85 KB
/
FreePhysicalMemorySize.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from subprocess import CalledProcessError
from jmxquery import JMXConnection
from jmxquery import JMXQuery
import argparse
def parsing_arguments():
p = argparse.ArgumentParser(description="Get memory physical size check")
p.add_argument("string_host", metavar="'host1:port'",
help="an string with the host nad port jmx")
p.add_argument("-w", help="warning threshold in bytes", type=float, required=True, metavar="warning_threashold")
p.add_argument("-c", help="critical threshold in bytes", type=float, required=True, metavar="critical_threshold")
p.add_argument('-u', metavar="user_jmx", help="user name jmx")
p.add_argument('-pw', metavar="password", help="password for the user jmx")
arg1 = p.parse_args()
if (arg1.u and not arg1.pw) or (not arg1.u and arg1.pw):
p.print_usage()
p.exit(3, "Error: if you are using authentication jmx, options -u and -pw are requested")
return arg1, p
if __name__ == "__main__":
arg, parser = parsing_arguments()
host = arg.string_host
critical = arg.c
warn = arg.w
type_jmx = "OperatingSystem"
name = "FreePhysicalMemorySize"
metric_value_total = 0
try:
if len(host.split(':')) == 1:
parser.print_usage()
parser.exit(3, "Error : port missing in string '%s'" % host)
if arg.u:
jmxConnection = JMXConnection("service:jmx:rmi:///jndi/rmi://" + host + "/jmxrmi",
jmx_username=arg.u,
jmx_password=arg.pw)
else:
jmxConnection = JMXConnection("service:jmx:rmi:///jndi/rmi://" + host + "/jmxrmi")
jmxQuery = [JMXQuery("java.lang:type=" + type_jmx,
metric_name="kafka_cluster_{type}_{name}",
metric_labels={"host": host})]
metrics = jmxConnection.query(jmxQuery)
for metric in metrics:
if f"{metric.attribute}" == name:
used = metric.value
break
# print(f"{metric.metric_name}<{metric.attribute}.{metric.attributeKey}> == {metric.value}")
if not used:
print("Error - not values found, it cannot be processed the size of the memory")
exit(3)
if used <= critical:
print("CRITICAL - " + name + " - " + str(used))
exit(2)
elif used <= warn:
print("WARNING - " + name + " - " + str(used))
exit(1)
else:
print("OK - " + name + " - " + str(used))
exit(0)
except TimeoutError as e:
err = "Error : connexion failed '%s'" % host
print(err)
exit(3)
raise Exception(err)
except CalledProcessError as e:
err = "Error : connexion failed '%s'" % host
print(err)
exit(3)
raise Exception(err)