Ver código fonte

Update 'plugins/edirectory_monitor/libexec/agent_edirectory_monitor'

Michael Honkoop 2 meses atrás
pai
commit
df7b11d472

+ 28 - 24
plugins/edirectory_monitor/libexec/agent_edirectory_monitor

@@ -11,6 +11,7 @@ to enforce TLS certificate verification.
 # License: GNU General Public License v2
 
 import os
+import argparse
 import sys
 import ldap
 from cmk_addons.plugins.edirectory_monitor.lib import (
@@ -28,34 +29,38 @@ monitor_sections = [
 ]
 
 def main():
-    verify_tls = False
+    def parse_exclude_list(value):
+    # Split by comma, strip whitespace, remove empty items
+        return [item.strip() for item in value.split(',') if item.strip()]
+
+    parser = argparse.ArgumentParser()
+    parser.add_argument('-s', '--server', required=True, help='Server URI (required)')
+    parser.add_argument('-u', '--user', required=True, help='Username (required)')
+    parser.add_argument('-p', '--password', required=True, help='Password (required)')
+    parser.add_argument('--exclude', type=parse_exclude_list, help='Excluded section(s) comma separated (optional)')
+    parser.add_argument('--verify-tls', '--verify-tls', help='Enforce certificate validation (optional)')
+
+    args = parser.parse_args()
+    print(args)
     exclude_sections = []
-    for args in sys.argv[1:]:  # Skip the script name (sys.argv[0])
-        if args.startswith("--exclude="):
-            # Extract the part after "--exclude "
-            exclude_params = args.split("=", 1)[1]            
-            # Split the value by commas, strip whitespace, and ignore empty parts
-            exclude_sections = exclude_params.split(",")
-            #print(exclude_sections)
-        else:
-            exclude_sections = None
-
-        if "--verify-tls" in args:
-            verify_tls = True
-
-    args = sys.argv[1:]
+    if args.exclude is not None:
+        # Split the value by commas
+        exclude_sections = args.exclude
+    else:
+        exclude_sections = None
 
+    if args.verify_tls is not None:
+        verify_tls = True
+    else:
+        verify_tls = False
+            
     env_verify = os.environ.get("LDAP_VERIFY_TLS", "").lower()
     if env_verify in ("1", "true", "yes"):
         verify_tls = True
-
-    if len(args) < 3:
-        print("Usage: script.py [--verify-tls] [--exclude=<section(s)>] <LDAP_URI> <BIND_DN> <PASSWORD>")
-        return 1
-    
-    ldap_uri = args[0]
-    binddn = args[1] 
-    pw = args[2]
+       
+    ldap_uri = args.server
+    binddn = args.user 
+    pw = args.password
     basedn = "cn=Monitor"
     searchFilter = "(objectClass=*)"
     searchAttribute = ["*"]
@@ -162,4 +167,3 @@ def main():
 
 if __name__ == "__main__":
     main()
-