edir-monitor.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 or "Agent Status" in keytxtresult:
  36. resultArray.append("- ")
  37. resultArray.append(k + ":" + valueresult)
  38. resultArray.append(' ')
  39. else:
  40. # append items to resultArray, but skip non-interesting items
  41. if "CheckPointThreadForceStartTime" in k or "CheckPointThreadStartTime" in k:
  42. continue
  43. else:
  44. resultArray.append(k + "=" + valueresult)
  45. resultArray.append('|')
  46. detailArray.append(k + ":" + valueresult)
  47. #remove last item from array
  48. resultArray.pop()
  49. # print the resulting array as a string removing unwanted text
  50. resultStr = ''.join(str(e) for e in resultArray).replace(" Bytes", "").replace(" KB", "") + " " + ' '.join(str(e) for e in detailArray)
  51. if "BackGroundProc" in resultStr:
  52. continue
  53. else:
  54. print(resultStr)
  55. l = ldap.initialize('ldaps://localhost:636')
  56. binddn = "my_bind_user"
  57. pw = "my_bind_user_password"
  58. basedn = "cn=Monitor"
  59. searchFilter = "(objectClass=*)"
  60. searchAttribute = ["*"]
  61. #this will scope the entire subtree under Monitor
  62. searchScope = ldap.SCOPE_SUBTREE
  63. #Bind to the server
  64. try:
  65. l.protocol_version = ldap.VERSION3
  66. l.simple_bind_s(binddn, pw)
  67. except ldap.INVALID_CREDENTIALS:
  68. print "Your username or password is incorrect."
  69. sys.exit(0)
  70. except ldap.LDAPError, e:
  71. if type(e.message) == dict and e.message.has_key('desc'):
  72. print e.message['desc']
  73. else:
  74. print e
  75. sys.exit(0)
  76. try:
  77. ldap_result_id = l.search(basedn, searchScope, searchFilter, searchAttribute)
  78. result_set = []
  79. while True:
  80. result_type, result_data = l.result(ldap_result_id, 0)
  81. if (result_data == []):
  82. break
  83. else:
  84. if result_type == ldap.RES_SEARCH_ENTRY:
  85. result_set.append(result_data)
  86. # print(type(result_set))
  87. for i in range(len(result_set)):
  88. print_list(result_set[i])
  89. except ldap.LDAPError, e:
  90. print e
  91. l.unbind_s()