From 5041bcdea29e4f5d9f0954777c9272a14e87f3dd Mon Sep 17 00:00:00 2001
From: Jan Pavlicek <469355@mail.muni.cz>
Date: Wed, 19 Apr 2023 12:25:41 +0200
Subject: [PATCH] feat: script for running monitoring probes

---
 .../config_templates/run_probes_cfg.yaml      | 16 ++++++
 perun/proxy/utils/run_probes.py               | 49 +++++++++++++++++++
 setup.py                                      |  1 +
 3 files changed, 66 insertions(+)
 create mode 100644 perun/proxy/config_templates/run_probes_cfg.yaml
 create mode 100644 perun/proxy/utils/run_probes.py

diff --git a/perun/proxy/config_templates/run_probes_cfg.yaml b/perun/proxy/config_templates/run_probes_cfg.yaml
new file mode 100644
index 0000000..76a48e0
--- /dev/null
+++ b/perun/proxy/config_templates/run_probes_cfg.yaml
@@ -0,0 +1,16 @@
+
+check_mongodb:
+  path: nagios/check_mongodb.py
+  runs:
+    - host: hostname
+      u: mongoadmin
+      p: password
+      A: connect
+      W: 2
+      C: 4
+    - host: hostname
+      u: mongoadmin
+      p: password
+      A: connections
+      W: 70
+      C: 80
diff --git a/perun/proxy/utils/run_probes.py b/perun/proxy/utils/run_probes.py
new file mode 100644
index 0000000..6d4dbbe
--- /dev/null
+++ b/perun/proxy/utils/run_probes.py
@@ -0,0 +1,49 @@
+import argparse
+import os
+import sys
+from threading import Thread
+
+import yaml
+
+
+def parse_args():
+    parser = argparse.ArgumentParser(
+        prog="MonitoringProbes", description="runs monitoring probes defined in config"
+    )
+    parser.add_argument("-c", "--config", required=True, help="Filepath to config")
+    args = parser.parse_args()
+    return args.config
+
+
+def open_file(filepath):
+    try:
+        with open(filepath) as f:
+            return f.read()
+    except OSError as e:
+        print(f"Cannot open config with path: {filepath}, error: {e.strerror}", file=sys.stderr)
+        exit(2)
+
+
+def run_probe(command):
+    os.system(command)
+
+
+def main():
+    config = yaml.safe_load(open_file(parse_args()))
+    if not config:
+        return
+
+    for name, options in config.items():
+        for args in options.get("runs"):
+            probe_run_builder = "python " + options["path"]
+            for arg_name, arg_val in args.items():
+                if len(arg_name) == 1:
+                    arg_name = "-" + arg_name
+                else:
+                    arg_name = "--" + arg_name
+                probe_run_builder += " " + arg_name + " " + str(arg_val)
+            Thread(target=run_probe, args=[probe_run_builder]).start()
+
+
+if __name__ == "__main__":
+    main()
diff --git a/setup.py b/setup.py
index 6894cb4..23189ab 100644
--- a/setup.py
+++ b/setup.py
@@ -13,5 +13,6 @@ setuptools.setup(
         "asyncssh~=2.13",
         "docker~=6.0",
         "beautifulsoup4~=4.12",
+        "yaml~=6.0",
     ],
 )
-- 
GitLab