From d39bfa3301794d18ea486e3a90acff9b2998b944 Mon Sep 17 00:00:00 2001
From: Jan Pavlicek <469355@mail.muni.cz>
Date: Mon, 19 Jun 2023 15:40:43 +0200
Subject: [PATCH] feat: script to execute multiple monitoring probes

---
 README.md                            |  9 ++++-
 config_templates/run_probes_cfg.yaml | 41 ++++++++++++++++++++
 perun/proxy/utils/run_probes.py      | 57 ++++++++++++++++++++++++++++
 setup.py                             |  1 +
 4 files changed, 107 insertions(+), 1 deletion(-)
 create mode 100644 config_templates/run_probes_cfg.yaml
 create mode 100644 perun/proxy/utils/run_probes.py

diff --git a/README.md b/README.md
index 2cb5521..a935391 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,14 @@
-# ProxyIdP scripts
+# Perun proxy utils
 
 ## Scripts
 
+### run_probes.py
+
+- script designed to execute multiple monitoring probes
+- output is compatible with CheckMK
+- it is required to put configuration file to `/etc/run_probes_cfg.yaml`
+- for usage run: `./run_probes.py` or `python3 -m perun.proxy.utils.run_probes`
+
 ### separate_ssp_script.py
 
 - Script for remove all logs from test accounts from SimpleSAMLlogs
diff --git a/config_templates/run_probes_cfg.yaml b/config_templates/run_probes_cfg.yaml
new file mode 100644
index 0000000..8b5c81e
--- /dev/null
+++ b/config_templates/run_probes_cfg.yaml
@@ -0,0 +1,41 @@
+check_mongodb:
+  # module with checks
+  module: perun.proxy.utils.nagios.check_mongodb
+  check_mongodb_shared: &check_mongodb_shared
+    host: "hostname"
+    u: "username"
+    p: "password"
+    tls: true
+    tls-ca-file: "/etc/ssl/chain.crt"
+    tls-cert-key-file: "/etc/ssl/certificate_and_key.pem"
+  runs:
+    # check with parameter
+    check_mongodb_connect:
+      <<: *check_mongodb_shared
+      A: connect
+      W: 2
+      C: 4
+    check_mongodb_connections:
+      <<: *check_mongodb_shared
+      A: connections
+      W: 70
+      C: 80
+    check_mongodb_replication_lag:
+      <<: *check_mongodb_shared
+      A: replication_lag
+      W: 15
+      C: 30
+    check_mongodb_replset_state:
+      <<: *check_mongodb_shared
+      A: replset_state
+      W: 0
+      C: 0
+
+check_rpc_status:
+  module: perun.proxy.utils.nagios.check_rpc_status
+  runs:
+    check_rpc_status:
+      u: "username"
+      p: "password"
+      d: "domain"
+      i: 1
diff --git a/perun/proxy/utils/run_probes.py b/perun/proxy/utils/run_probes.py
new file mode 100644
index 0000000..3599e9b
--- /dev/null
+++ b/perun/proxy/utils/run_probes.py
@@ -0,0 +1,57 @@
+#!/usr/bin/python3
+import re
+import subprocess
+import sys
+from threading import Thread
+
+import yaml
+
+
+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(probe_name, command):
+    result = subprocess.run(command, text=True, capture_output=True)
+    search = re.search(r" - .*", result.stdout)
+    if search:
+        print(f"{result.returncode} {probe_name} {search.group()}")
+    else:
+        print(f"{result.returncode} {probe_name} - {result.stdout}")
+    return result.returncode
+
+
+def main(config_filepath):
+    config = yaml.safe_load(open_file(config_filepath))
+    if not config:
+        return
+
+    for _, options in config.items():
+        module = options["module"]
+        for name, args in options.get("runs").items():
+            command = ["python", "-m", module]
+            for arg_name, arg_val in args.items():
+                if len(arg_name) == 1:
+                    arg_name = "-" + arg_name
+                else:
+                    arg_name = "--" + arg_name
+                if arg_val is True:
+                    arg_val = "true"
+                elif arg_val is False:
+                    arg_val = "false"
+                command.append(arg_name)
+                command.append(str(arg_val))
+            Thread(target=run_probe, args=[name, command]).start()
+
+
+if __name__ == "__main__":
+    config_filepath = "/etc/run_probes_cfg.yaml"
+    main(config_filepath)
diff --git a/setup.py b/setup.py
index cfb2f03..9109acc 100644
--- a/setup.py
+++ b/setup.py
@@ -15,6 +15,7 @@ setuptools.setup(
         "beautifulsoup4~=4.12",
         "requests~=2.31",
         "ldap3~=2.9.1",
+        "PyYAML~=6.0",
         "check_syncrepl_extended @ git+https://gitlab.ics.muni.cz/perun-proxy-aai/"
         "python/check_syncrepl_extended.git@main",
         "check_nginx_status @ git+https://gitlab.ics.muni.cz/perun-proxy-aai/"
-- 
GitLab