From 1c33878b81c37b6f38693c417d0102c254fe766f Mon Sep 17 00:00:00 2001
From: anima
Date: Sun, 23 Feb 2025 12:41:24 +0100
Subject: [PATCH] add check memory usage
---
checks/check_api_swarmpit.py | 32 ++++++++++++++++++++++++++++++--
1 file changed, 30 insertions(+), 2 deletions(-)
diff --git a/checks/check_api_swarmpit.py b/checks/check_api_swarmpit.py
index c01f854..51d702d 100644
--- a/checks/check_api_swarmpit.py
+++ b/checks/check_api_swarmpit.py
@@ -6,7 +6,7 @@
- pip3 install requests
"""
-__version__ = '0.4.0'
+__version__ = '0.5.0'
__author__ = 'anima'
# imports
@@ -97,10 +97,31 @@ class DockerSwarmCoresResource(nagiosplugin.Resource):
nagiosplugin.Metric: single metric element (return)
"""
data = self.api.statistics()
+ memory = data['memory']
+ memory_usage_percent = round(memory['usage'], 2)
+
+ return nagiosplugin.Metric(name='load', value=memory_usage_percent, uom='%', context='scalar_context')
+
+
+#
+# Check Swarm memory
+#
+class DockerSwarmMemoryResource(nagiosplugin.Resource):
+ def __init__(self, api) -> None:
+ self.api = api
+
+ def probe(self) -> list:
+ """check memory usage of full swarm
+
+ Returns:
+ nagiosplugin.Metric: single metric element (return)
+ """
+ data = self.api.statistics()
+ print(data)
cpu = data['cpu']
cpu_usage_percent = round((cpu['usage'] / cpu['cores']) * 100, 2)
- return nagiosplugin.Metric(name='load', value=cpu_usage_percent, uom='%', context='scalar_context')
+ return nagiosplugin.Metric(name='memory', value=cpu_usage_percent, uom='%', context='scalar_context')
#
@@ -256,6 +277,7 @@ def parse_args() -> argparse.Namespace:
argp.add_argument('-m', '--check_mode',
choices=[
'load',
+ 'memory',
'service_states',
'service_updates'
],
@@ -295,6 +317,12 @@ def main():
nagiosplugin.ScalarContext(name='scalar_context', warning=args.warning, critical=args.critical),
nagiosplugin.Summary())
check.name = "swarm load"
+ case 'memory':
+ check = nagiosplugin.Check(
+ DockerSwarmMemoryResource(api=api),
+ nagiosplugin.ScalarContext(name='scalar_context', warning=args.warning, critical=args.critical),
+ nagiosplugin.Summary())
+ check.name = "swarm memory"
case 'service_states':
check = nagiosplugin.Check(
DockerSwarmServiceStatesResource(api=api),