1
0

edir-monitor.py 3.9 KB

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