You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.4 KiB

  1. #! /usr/bin/python3
  2. # coding=utf-8
  3. import time
  4. import MySQLdb
  5. import json
  6. import datetime
  7. from kea_hosts_generane import read_table, read_conf
  8. qinsert_last_seen = "insert into was_last_seen (id_1c_import, datetime) " \
  9. "values (%s, now());"
  10. qupdate_last_seen = "update was_last_seen set datetime = now() where id_1c_import = '%s';"
  11. qdelete_last_seen = "delete from was_last_seen where id_1c_import = '%s';"
  12. qdelete_host = "DELETE " \
  13. "FROM" \
  14. " hosts " \
  15. "WHERE" \
  16. " dhcp_identifier " \
  17. "IN " \
  18. " (SELECT " \
  19. " unhex(replace(hw_addr, ':', '')) AS hw_addr" \
  20. " FROM 1c_import" \
  21. " WHERE id like '%s');"
  22. qonline = "select " \
  23. " 1c_import.id as 1c_id " \
  24. "from 1c_import" \
  25. " right join lease4 " \
  26. " on (unhex(replace(1c_import.hw_addr, ':', '')) = lease4.hwaddr)" \
  27. "WHERE " \
  28. "INET_NTOA(address) not like '10.%.0.%';"
  29. qselect_last_seen = "select * from was_last_seen;"
  30. def update_seen(sql, id, dbhost, dbuser, dbpass, dbbase):
  31. try:
  32. conn = MySQLdb.connect(host=dbhost, user=dbuser,
  33. passwd=dbpass, db=dbbase)
  34. except MySQLdb.Error as err:
  35. print("Connection error: {}".format(err))
  36. conn.close()
  37. try:
  38. cur = conn.cursor(MySQLdb.cursors.DictCursor)
  39. print(sql % id)
  40. cur.execute(sql % id)
  41. data = cur.fetchall()
  42. except MySQLdb.Error as err:
  43. print("Query error: {}".format(err))
  44. conn.commit()
  45. conn.close()
  46. return data
  47. def update_hosts(sql, id, dbhost, dbuser, dbpass, dbbase):
  48. try:
  49. conn = MySQLdb.connect(host=dbhost, user=dbuser,
  50. passwd=dbpass, db=dbbase)
  51. except MySQLdb.Error as err:
  52. print("Connection error: {}".format(err))
  53. conn.close()
  54. try:
  55. cur = conn.cursor(MySQLdb.cursors.DictCursor)
  56. print(sql % id)
  57. cur.execute(sql % id)
  58. data = cur.fetchall()
  59. except MySQLdb.Error as err:
  60. print("Query error: {}".format(err))
  61. conn.commit()
  62. conn.close()
  63. return data
  64. def main():
  65. conf = read_conf()
  66. dbconf = [conf['dbhost'], conf['dbuser'], conf['dbpass'], conf['dbbase']]
  67. online_hosts = read_table(qonline, *dbconf)
  68. print(online_hosts)
  69. seen = read_table(qselect_last_seen, *dbconf)
  70. print(seen)
  71. seen_list = []
  72. for seen_h in seen:
  73. seen_list.append(seen_h['id_1c_import'])
  74. print(seen_list)
  75. for online_host in online_hosts:
  76. if online_host['1c_id'] in seen_list:
  77. print('need to update host 1c_id = %s' % online_host['1c_id'])
  78. update_seen(qupdate_last_seen, online_host['1c_id'], *dbconf)
  79. elif online_host not in seen:
  80. print('need to add host 1c_id = %s' % online_host['1c_id'])
  81. update_seen(qinsert_last_seen, online_host['1c_id'], *dbconf)
  82. seen = read_table(qselect_last_seen, *dbconf)
  83. for seen_host in seen:
  84. if seen_host['datetime'] < (datetime.datetime.now() - datetime.timedelta(minutes=1)):
  85. print('need to remove host 1c_id = %s' % seen_host['id_1c_import'])
  86. update_hosts(qdelete_host, seen_host['id_1c_import'], *dbconf)
  87. update_seen(qdelete_last_seen, seen_host['id_1c_import'], *dbconf)
  88. if __name__ == '__main__':
  89. main()

Powered by TurnKey Linux.