openldap.chart.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # -*- coding: utf-8 -*-
  2. # Description: openldap netdata python.d module
  3. # Author: Manolis Kartsonakis (ekartsonakis)
  4. # SPDX-License-Identifier: GPL-3.0+
  5. try:
  6. import ldap
  7. HAS_LDAP = True
  8. except ImportError:
  9. HAS_LDAP = False
  10. from bases.FrameworkServices.SimpleService import SimpleService
  11. DEFAULT_SERVER = 'localhost'
  12. DEFAULT_PORT = '389'
  13. DEFAULT_TLS = False
  14. DEFAULT_CERT_CHECK = True
  15. DEFAULT_TIMEOUT = 1
  16. ORDER = [
  17. 'total_connections',
  18. 'bytes_sent',
  19. 'operations',
  20. 'referrals_sent',
  21. 'entries_sent',
  22. 'ldap_operations',
  23. 'waiters'
  24. ]
  25. CHARTS = {
  26. 'total_connections': {
  27. 'options': [None, 'Total Connections', 'connections/s', 'ldap', 'openldap.total_connections', 'line'],
  28. 'lines': [
  29. ['total_connections', 'connections', 'incremental']
  30. ]
  31. },
  32. 'bytes_sent': {
  33. 'options': [None, 'Traffic', 'KiB/s', 'ldap', 'openldap.traffic_stats', 'line'],
  34. 'lines': [
  35. ['bytes_sent', 'sent', 'incremental', 1, 1024]
  36. ]
  37. },
  38. 'operations': {
  39. 'options': [None, 'Operations Status', 'ops/s', 'ldap', 'openldap.operations_status', 'line'],
  40. 'lines': [
  41. ['completed_operations', 'completed', 'incremental'],
  42. ['initiated_operations', 'initiated', 'incremental']
  43. ]
  44. },
  45. 'referrals_sent': {
  46. 'options': [None, 'Referrals', 'referals/s', 'ldap', 'openldap.referrals', 'line'],
  47. 'lines': [
  48. ['referrals_sent', 'sent', 'incremental']
  49. ]
  50. },
  51. 'entries_sent': {
  52. 'options': [None, 'Entries', 'entries/s', 'ldap', 'openldap.entries', 'line'],
  53. 'lines': [
  54. ['entries_sent', 'sent', 'incremental']
  55. ]
  56. },
  57. 'ldap_operations': {
  58. 'options': [None, 'Operations', 'ops/s', 'ldap', 'openldap.ldap_operations', 'line'],
  59. 'lines': [
  60. ['bind_operations', 'bind', 'incremental'],
  61. ['search_operations', 'search', 'incremental'],
  62. ['unbind_operations', 'unbind', 'incremental'],
  63. ['add_operations', 'add', 'incremental'],
  64. ['delete_operations', 'delete', 'incremental'],
  65. ['modify_operations', 'modify', 'incremental'],
  66. ['compare_operations', 'compare', 'incremental']
  67. ]
  68. },
  69. 'waiters': {
  70. 'options': [None, 'Waiters', 'waiters/s', 'ldap', 'openldap.waiters', 'line'],
  71. 'lines': [
  72. ['write_waiters', 'write', 'incremental'],
  73. ['read_waiters', 'read', 'incremental']
  74. ]
  75. },
  76. }
  77. # Stuff to gather - make tuples of DN dn and attrib to get
  78. SEARCH_LIST = {
  79. 'total_connections': (
  80. 'cn=Total,cn=Connections,cn=Monitor', 'monitorCounter',
  81. ),
  82. 'bytes_sent': (
  83. 'cn=Bytes,cn=Statistics,cn=Monitor', 'monitorCounter',
  84. ),
  85. 'completed_operations': (
  86. 'cn=Operations,cn=Monitor', 'monitorOpCompleted',
  87. ),
  88. 'initiated_operations': (
  89. 'cn=Operations,cn=Monitor', 'monitorOpInitiated',
  90. ),
  91. 'referrals_sent': (
  92. 'cn=Referrals,cn=Statistics,cn=Monitor', 'monitorCounter',
  93. ),
  94. 'entries_sent': (
  95. 'cn=Entries,cn=Statistics,cn=Monitor', 'monitorCounter',
  96. ),
  97. 'bind_operations': (
  98. 'cn=Bind,cn=Operations,cn=Monitor', 'monitorOpCompleted',
  99. ),
  100. 'unbind_operations': (
  101. 'cn=Unbind,cn=Operations,cn=Monitor', 'monitorOpCompleted',
  102. ),
  103. 'add_operations': (
  104. 'cn=Add,cn=Operations,cn=Monitor', 'monitorOpInitiated',
  105. ),
  106. 'delete_operations': (
  107. 'cn=Delete,cn=Operations,cn=Monitor', 'monitorOpCompleted',
  108. ),
  109. 'modify_operations': (
  110. 'cn=Modify,cn=Operations,cn=Monitor', 'monitorOpCompleted',
  111. ),
  112. 'compare_operations': (
  113. 'cn=Compare,cn=Operations,cn=Monitor', 'monitorOpCompleted',
  114. ),
  115. 'search_operations': (
  116. 'cn=Search,cn=Operations,cn=Monitor', 'monitorOpCompleted',
  117. ),
  118. 'write_waiters': (
  119. 'cn=Write,cn=Waiters,cn=Monitor', 'monitorCounter',
  120. ),
  121. 'read_waiters': (
  122. 'cn=Read,cn=Waiters,cn=Monitor', 'monitorCounter',
  123. ),
  124. }
  125. class Service(SimpleService):
  126. def __init__(self, configuration=None, name=None):
  127. SimpleService.__init__(self, configuration=configuration, name=name)
  128. self.order = ORDER
  129. self.definitions = CHARTS
  130. self.server = configuration.get('server', DEFAULT_SERVER)
  131. self.port = configuration.get('port', DEFAULT_PORT)
  132. self.username = configuration.get('username')
  133. self.password = configuration.get('password')
  134. self.timeout = configuration.get('timeout', DEFAULT_TIMEOUT)
  135. self.use_tls = configuration.get('use_tls', DEFAULT_TLS)
  136. self.cert_check = configuration.get('cert_check', DEFAULT_CERT_CHECK)
  137. self.alive = False
  138. self.conn = None
  139. def disconnect(self):
  140. if self.conn:
  141. self.conn.unbind()
  142. self.conn = None
  143. self.alive = False
  144. def connect(self):
  145. try:
  146. if self.use_tls:
  147. self.conn = ldap.initialize('ldaps://%s:%s' % (self.server, self.port))
  148. else:
  149. self.conn = ldap.initialize('ldap://%s:%s' % (self.server, self.port))
  150. self.conn.set_option(ldap.OPT_NETWORK_TIMEOUT, self.timeout)
  151. if self.use_tls and not self.cert_check:
  152. self.conn.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER)
  153. if self.username and self.password:
  154. self.conn.simple_bind(self.username, self.password)
  155. except ldap.LDAPError as error:
  156. self.error(error)
  157. return False
  158. self.alive = True
  159. return True
  160. def reconnect(self):
  161. self.disconnect()
  162. return self.connect()
  163. def check(self):
  164. if not HAS_LDAP:
  165. self.error("'python-ldap' package is needed")
  166. return None
  167. return self.connect() and self.get_data()
  168. def get_data(self):
  169. if not self.alive and not self.reconnect():
  170. return None
  171. data = dict()
  172. for key in SEARCH_LIST:
  173. dn = SEARCH_LIST[key][0]
  174. attr = SEARCH_LIST[key][1]
  175. try:
  176. num = self.conn.search(dn, ldap.SCOPE_BASE, 'objectClass=*', [attr, ])
  177. result_type, result_data = self.conn.result(num, 1)
  178. except ldap.LDAPError as error:
  179. self.error("Empty result. Check bind username/password. Message: ",error)
  180. self.alive = False
  181. return None
  182. try:
  183. if result_type == 101:
  184. val = int(result_data[0][1].values()[0][0])
  185. except (ValueError, IndexError) as error:
  186. self.debug(error)
  187. continue
  188. data[key] = val
  189. return data