Zabbix Sync AD Users

Source: Internet
Author: User
Tags ldap

  1. #!/usr/bin/env python
  2. #coding:utf-8
  3. import ldap,ldif3,sys,re
  4. importMySQLdb
  5. ldap_host="ldap://xx.xx.xx.xx"
  6. ldap_user="[email protected]"#[email protected]
  7. ldap_pass="xxxx"
  8. basedn ="OU=group,DC=domain,DC=com"
  9. db_host="10.1.180.166"
  10. db_port=3306
  11. db_user="zabbix"
  12. db_pass="zabbixpwd"
  13. db_Name="zabbix"
  14. #insert user to zabbix
  15. #insert into users (userid,alias,passwd,autologin,type) (select max(userid)+1 as userid,‘test‘,‘5fce1b3e34b520afeffb37ce08c7cd66‘,1,3 from users);
  16. #select users
  17. #select alias from zabbix.users where alias not regexp ‘AR|Admin|guest‘ ;
  18. def __mysql_operation(sql):
  19. try:
  20. conn = MySQLdb connect ( host = db_host user = db_ User passwd = db_pass port = db_port db = db_name )
  21. cur = conn.cursor()
  22. count = cur.execute(sql)
  23. if count ==0:
  24. zbx_result =0
  25. else:
  26. zbx_result = cur.fetchall()
  27. conn.commit()
  28. cur.close()
  29. conn.close()
  30. #print zbx_result
  31. return zbx_result
  32. exceptMySQLdb.Error,e:
  33. print"Mysql Error:",e
  34. def __ldap_query():
  35. conn = ldap.initialize(ldap_host)
  36. # set domain protocol version
  37. conn.protocol_version =3
  38. conn.set_option(ldap.OPT_REFERRALS,0)
  39. # bind domain user
  40. conn.simple_bind_s(ldap_user,ldap_pass)
  41. ldif_writer = ldif3.LDIFWriter(sys.stdout)
  42. retrieveAttributes =None
  43. results = conn.search_s(basedn,ldap.SCOPE_SUBTREE,"(cn=*)",retrieveAttributes)
  44. # for dn,entry in results:
  45. # ldif_writer.unparse(dn,entry)
  46. cn_list =[]
  47. for result in results:
  48. result_dn = result[0]
  49. result_attrs = result[1]
  50. if"member"in result_attrs:
  51. for member in result_attrs["member"]:
  52. re_result = re.search(r‘\w+\s\w+‘,member)
  53. if re_result:
  54. cn_list.append(re_result.group().replace(‘ ‘,‘‘).lower())
  55. #print member
  56. user_list = sorted(set(cn_list))
  57. return user_list
  58. def main():
  59. select_sql =‘‘‘select alias from users where alias not regexp ‘AR|Admin|guest‘ ;‘‘‘
  60. select_result = __mysql_operation(select_sql)
  61. ldap_result = __ldap_query()
  62. zabbix_user_list =[]
  63. for s_row in select_result:
  64. zabbix_user_list.append(s_row[0])
  65. #print zabbix_user_list
  66. # add domain user to zabbix
  67. for l_row in ldap_result:
  68. if l_row in zabbix_user_list:
  69. print"The %s user alrady exist ! "%(l_row)
  70. else:
  71. insert_sql =‘‘‘insert into users (userid,alias,passwd,autologin,autologout,type) (select max(userid)+1 as userid,‘%s‘,‘5fce1b3e34b520afeffb37ce08c7cd66‘,1,0,1 from users);‘‘‘%(l_row)
  72. __mysql_operation(insert_sql)
  73. print"Add %s user successed !"%(l_row)
  74. # if zabbix user not exist for domain , delete this user.
  75. for s_row in zabbix_user_list:
  76. if s_row notin ldap_result:
  77. delete_sql =‘‘‘delete from users where alias = "%s" ;‘‘‘%(s_row)
  78. __mysql_operation(delete_sql)
  79. print"Delete invalid %s user succeesed !"%(s_row)
  80. else:
  81. print"Not have invalid users !"
  82. if __name__ ==‘__main__‘:
  83. main()
First: Create a Zabbix user in Windows AD, preferably with the Zabbix default account and then configure the LDAP execution script, the ad all users are synchronized, regular script execution, will automatically add delete users, synchronization ad



From for notes (Wiz)



Zabbix Sync AD Users

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.