edir-monitor.py 3.4 KB

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