agent_edirectory_monitor 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/usr/bin/env python3
  2. '''special agent call file
  3. Set the environment variable ``LDAP_VERIFY_TLS=1`` or pass ``--verify-tls``
  4. to enforce TLS certificate verification.
  5. '''
  6. # -*- mode: Python; encoding: utf-8; indent-offset: 4; autowrap: nil -*-
  7. # (c) Michael Honkoop <mhonkoop@comsolve.nl>
  8. # License: GNU General Public License v2
  9. import os
  10. import argparse
  11. import sys
  12. import ldap
  13. from cmk_addons.plugins.edirectory_monitor.lib import (
  14. print_sections,
  15. )
  16. # possible subsections of cn=Monitor
  17. monitor_sections = [
  18. "Agent",
  19. "Dclient",
  20. "DHOST",
  21. "LDAP",
  22. "RecordManager",
  23. "IDM",
  24. ]
  25. def main():
  26. def parse_exclude_list(value):
  27. # Split by comma, strip whitespace, remove empty items
  28. return [item.strip() for item in value.split(',') if item.strip()]
  29. parser = argparse.ArgumentParser()
  30. parser.add_argument('-s', '--server', required=True, help='Server URI (required)')
  31. parser.add_argument('-u', '--user', required=True, help='Username (required)')
  32. parser.add_argument('-p', '--password', required=True, help='Password (required)')
  33. parser.add_argument('--exclude', type=parse_exclude_list, help='Excluded section(s) comma separated (optional)')
  34. parser.add_argument('--verify-tls', '--verify-tls', action='store_true', help='Enforce certificate validation (optional)')
  35. args = parser.parse_args()
  36. print(args)
  37. exclude_sections = []
  38. if args.exclude is not None:
  39. # Split the value by commas
  40. exclude_sections = args.exclude
  41. else:
  42. exclude_sections = None
  43. if args.verify_tls is not None:
  44. verify_tls = True
  45. else:
  46. verify_tls = False
  47. env_verify = os.environ.get("LDAP_VERIFY_TLS", "").lower()
  48. if env_verify in ("1", "true", "yes"):
  49. verify_tls = True
  50. ldap_uri = args.server
  51. binddn = args.user
  52. pw = args.password
  53. basedn = "cn=Monitor"
  54. searchFilter = "(objectClass=*)"
  55. searchAttribute = ["*"]
  56. searchScope = ldap.SCOPE_SUBTREE
  57. l = None
  58. try:
  59. if verify_tls:
  60. ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_DEMAND)
  61. else:
  62. ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
  63. # set timeout to 40 seconds for network
  64. ldap.set_option(ldap.OPT_NETWORK_TIMEOUT, 40)
  65. # Create LDAPObject instance with given uri
  66. l = ldap.initialize(ldap_uri)
  67. # Set LDAP protocol version used
  68. l.protocol_version = ldap.VERSION3
  69. try:
  70. # Attempt to bind with given credentials
  71. l.simple_bind_s(binddn, pw)
  72. if exclude_sections is None:
  73. ldap_result_id = l.search(basedn, searchScope, searchFilter, searchAttribute)
  74. result_set = []
  75. while True:
  76. result_type, result_data = l.result(ldap_result_id, 0, timeout=40)
  77. if (result_data == []):
  78. break
  79. elif result_type == ldap.RES_SEARCH_ENTRY:
  80. result_set.append(result_data)
  81. else:
  82. result_set = []
  83. for entry in monitor_sections:
  84. if entry.lower() in exclude_sections:
  85. continue
  86. else:
  87. ldap_result_id = l.search("cn=" + entry + "," + basedn, searchScope, searchFilter, searchAttribute)
  88. while True:
  89. result_type, result_data = l.result(ldap_result_id, 0, timeout=40)
  90. if (result_data == []):
  91. break
  92. elif result_type == ldap.RES_SEARCH_ENTRY:
  93. result_set.append(result_data)
  94. print('<<<edirectory_monitor_agent:sep(124)>>>')
  95. for i in range(len(result_set)):
  96. for val in result_set[i]:
  97. for element in val:
  98. if "cn=Agent" in element:
  99. print_sections(result_set[i])
  100. print('<<<edirectory_monitor_dclient:sep(124)>>>')
  101. for i in range(len(result_set)):
  102. for val in result_set[i]:
  103. for element in val:
  104. if "cn=Dclient" in element:
  105. print_sections(result_set[i])
  106. print('<<<edirectory_monitor_dhost:sep(124)>>>')
  107. for i in range(len(result_set)):
  108. for val in result_set[i]:
  109. for element in val:
  110. if "cn=DHOST" in element:
  111. print_sections(result_set[i])
  112. print('<<<edirectory_monitor_ldap:sep(124)>>>')
  113. for i in range(len(result_set)):
  114. for val in result_set[i]:
  115. for element in val:
  116. if "cn=LDAP" in element:
  117. print_sections(result_set[i])
  118. print('<<<edirectory_monitor_recordmanager:sep(124)>>>')
  119. for i in range(len(result_set)):
  120. for val in result_set[i]:
  121. for element in val:
  122. if "cn=RecordManager" in element:
  123. print_sections(result_set[i])
  124. print('<<<edirectory_monitor_idm:sep(124)>>>')
  125. for i in range(len(result_set)):
  126. for val in result_set[i]:
  127. for element in val:
  128. if "cn=IDM" in element:
  129. print_sections(result_set[i])
  130. except ldap.INVALID_CREDENTIALS as e:
  131. print(f"Authentication to the LDAP host has failed: {e}")
  132. return print(f"Authentication to the LDAP host has failed: {e}")
  133. except ldap.LDAPError as e:
  134. print(f"LDAP error during bind/search: {e}")
  135. return print(f"LDAP error during bind/search: {e}")
  136. except ldap.LDAPError as e:
  137. print(f"Failed to initialize LDAP connection: {e}")
  138. return print(f"Failed to initialize LDAP connection: {e}")
  139. finally:
  140. if l is not None:
  141. try:
  142. l.unbind_s()
  143. except ldap.LDAPError:
  144. pass
  145. if __name__ == "__main__":
  146. main()