edir-monitor.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/python
  2. #
  3. # Version 0.1
  4. #
  5. # This Localcheck is still a work in progress - do not use it for production !!
  6. import sys
  7. import ldap
  8. import json
  9. import re
  10. def print_list(the_list):
  11. for each_item in the_list:
  12. if isinstance(each_item, list):
  13. print_list(each_item)
  14. else:
  15. jsonArray = json.loads(json.dumps(each_item))
  16. # start creating resultArray elements
  17. resultArray = ['0 \"eDirectory ']
  18. detailArray = []
  19. # add result efrom sublist key, but strip leading and trasiling spaces
  20. resultArray.append(' '.join(jsonArray[0].split(',')[::-1]).replace('cn=Monitor', '').replace('cn=', '').strip() + "\" ")
  21. for k,v in jsonArray[1].items():
  22. # objectclass is not intersting for monitoring, so omitting it
  23. if k == "objectclass":
  24. continue
  25. else:
  26. # put the resulting keys in an array, and reverse the order
  27. keyresult = jsonArray[0].split(',')[::-1]
  28. # strip whitespaces and other unwanted information
  29. keytxtresult = (' '.join(keyresult).replace('cn=Monitor', '').replace('cn=', '').strip())
  30. # join the values into a string
  31. valueresult = ' '.join(str(e) for e in v)
  32. # sanitize the timestamps if they are present
  33. valueresult = re.sub('(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})([Z])$', r'\3-\2-\1 \4:\5:\6', valueresult)
  34. # usecases of formatting data differently
  35. if "BackGroundProc" in keytxtresult:
  36. resultArray.append("- ")
  37. resultArray.append(k + ":" + valueresult)
  38. resultArray.append(' ')
  39. if "Agent Status" in keytxtresult:
  40. resultArray.append("- ")
  41. resultArray.append(k + ":" + valueresult)
  42. resultArray.append(' ')
  43. else:
  44. # append items to resultArray
  45. resultArray.append(k + "=" + valueresult)
  46. resultArray.append('|')
  47. detailArray.append(k + ":" + valueresult)
  48. #remove last item from array
  49. resultArray.pop()
  50. # print the resulting array as a string removing unwanted text
  51. print(''.join(str(e) for e in resultArray).replace(" Bytes", "").replace(" KB", "") + " " + ' '.join(str(e) for e in detailArray))
  52. l = ldap.initialize('ldaps://localhost:636')
  53. binddn = "my_bind_user"
  54. pw = "my_bind_user_password"
  55. basedn = "cn=Monitor"
  56. searchFilter = "(objectClass=*)"
  57. searchAttribute = ["*"]
  58. #this will scope the entire subtree under Monitor
  59. searchScope = ldap.SCOPE_SUBTREE
  60. #Bind to the server
  61. try:
  62. l.protocol_version = ldap.VERSION3
  63. l.simple_bind_s(binddn, pw)
  64. except ldap.INVALID_CREDENTIALS:
  65. print "Your username or password is incorrect."
  66. sys.exit(0)
  67. except ldap.LDAPError, e:
  68. if type(e.message) == dict and e.message.has_key('desc'):
  69. print e.message['desc']
  70. else:
  71. print e
  72. sys.exit(0)
  73. try:
  74. ldap_result_id = l.search(basedn, searchScope, searchFilter, searchAttribute)
  75. result_set = []
  76. while True:
  77. result_type, result_data = l.result(ldap_result_id, 0)
  78. if (result_data == []):
  79. break
  80. else:
  81. if result_type == ldap.RES_SEARCH_ENTRY:
  82. result_set.append(result_data)
  83. # print(type(result_set))
  84. for i in range(len(result_set)):
  85. print_list(result_set[i])
  86. except ldap.LDAPError, e:
  87. print e
  88. l.unbind_s()