Procházet zdrojové kódy

refactored routines to ensure timeouts are handled correctly

Michael Honkoop před 3 měsíci
rodič
revize
333f0746ed

+ 66 - 60
plugins/edirectory_monitor/libexec/agent_edirectory_monitor

@@ -47,75 +47,81 @@ def main():
             ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_DEMAND)
         else:
             ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
+        # set timeout to 40 seconds for network
+        ldap.set_option(ldap.OPT_NETWORK_TIMEOUT, 40)
         # Create LDAPObject instance with given uri
         l = ldap.initialize(ldap_uri)
         # Set LDAP protocol version used
         l.protocol_version = ldap.VERSION3
-    except ldap.LDAPError as e:
-        print(f"Failed to initialize LDAP connection: {e}")
-        sys.exit(f"Failed to initialize LDAP connection: {e}")
-    try:
-        # Attempt to bind with given credentials
-        l.simple_bind_s(binddn, pw)
-    except ldap.INVALID_CREDENTIALS:
-          print("Authentication to the LDAP host has failed.")
-          sys.exit("Authentication to the LDAP host has failed.")
-    except ldap.LDAPError as e:
-        print(f"LDAP error during bind: {e}")
-        sys.exit(f"LDAP error during bind: {e}")
-    try:
-        ldap_result_id = l.search(basedn, searchScope, searchFilter, searchAttribute)
-        result_set = []
-
-        while True:
-            result_type, result_data = l.result(ldap_result_id, 0, timeout=40)
-            if (result_data == []):
-                break
-            elif result_type == ldap.RES_SEARCH_ENTRY:
-                result_set.append(result_data)
-        print('<<<edirectory_monitor_agent:sep(124)>>>')
-        for i in range(len(result_set)):
-            for val in result_set[i]:
-                for element in val:
-                    if "cn=Agent" in element:
-                        print_sections(result_set[i])
-        print('<<<edirectory_monitor_dclient:sep(124)>>>')
-        for i in range(len(result_set)):
-            for val in result_set[i]:
-                for element in val:
-                    if "cn=Dclient" in element:
-                        print_sections(result_set[i])
-        print('<<<edirectory_monitor_dhost:sep(124)>>>')
-        for i in range(len(result_set)):
-            for val in result_set[i]:
-                for element in val:
-                    if "cn=DHOST" in element:
-                        print_sections(result_set[i])
-        print('<<<edirectory_monitor_ldap:sep(124)>>>')
-        for i in range(len(result_set)):
-            for val in result_set[i]:
-                for element in val:
-                    if "cn=LDAP" in element:
-                        print_sections(result_set[i])
-        print('<<<edirectory_monitor_recordmanager:sep(124)>>>')
-        for i in range(len(result_set)):
-            for val in result_set[i]:
-                for element in val:
-                    if "cn=RecordManager" in element:
-                        print_sections(result_set[i])
-        print('<<<edirectory_monitor_idm:sep(124)>>>')
-        for i in range(len(result_set)):
-            for val in result_set[i]:
-                for element in val:
-                    if "cn=IDM" in element:
-                        print_sections(result_set[i])
 
+        try:
+            # Attempt to bind with given credentials
+            l.simple_bind_s(binddn, pw)
+
+            ldap_result_id = l.search(basedn, searchScope, searchFilter, searchAttribute)
+            result_set = []
+
+            while True:
+                result_type, result_data = l.result(ldap_result_id, 0, timeout=40)
+                if (result_data == []):
+                    break
+                elif result_type == ldap.RES_SEARCH_ENTRY:
+                    result_set.append(result_data)
+
+            print('<<<edirectory_monitor_agent:sep(124)>>>')
+            for i in range(len(result_set)):
+                for val in result_set[i]:
+                    for element in val:
+                        if "cn=Agent" in element:
+                            print_sections(result_set[i])
+            print('<<<edirectory_monitor_dclient:sep(124)>>>')
+            for i in range(len(result_set)):
+                for val in result_set[i]:
+                    for element in val:
+                        if "cn=Dclient" in element:
+                            print_sections(result_set[i])
+            print('<<<edirectory_monitor_dhost:sep(124)>>>')
+            for i in range(len(result_set)):
+                for val in result_set[i]:
+                    for element in val:
+                        if "cn=DHOST" in element:
+                            print_sections(result_set[i])
+            print('<<<edirectory_monitor_ldap:sep(124)>>>')
+            for i in range(len(result_set)):
+                for val in result_set[i]:
+                    for element in val:
+                        if "cn=LDAP" in element:
+                            print_sections(result_set[i])
+            print('<<<edirectory_monitor_recordmanager:sep(124)>>>')
+            for i in range(len(result_set)):
+                for val in result_set[i]:
+                    for element in val:
+                        if "cn=RecordManager" in element:
+                            print_sections(result_set[i])
+            print('<<<edirectory_monitor_idm:sep(124)>>>')
+            for i in range(len(result_set)):
+                for val in result_set[i]:
+                    for element in val:
+                        if "cn=IDM" in element:
+                            print_sections(result_set[i])
+
+        except ldap.INVALID_CREDENTIALS as e:
+            print(f"Authentication to the LDAP host has failed: {e}")
+            return print(f"Authentication to the LDAP host has failed: {e}")
+        
+        except ldap.LDAPError as e:
+            print(f"LDAP error during bind/search: {e}")
+            return print(f"LDAP error during bind/search: {e}")
+        
     except ldap.LDAPError as e:
-        print(f"LDAP search failed: {e}")
-        sys.exit(f"LDAP search failed: {e}")
+        print(f"Failed to initialize LDAP connection: {e}")
+        return print(f"Failed to initialize LDAP connection: {e}")
     finally:
         if l is not None:
+            try:
                 l.unbind_s()
+            except ldap.LDAPError:
+                pass
 
 if __name__ == "__main__":
     main()