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.

102 lines
3.3 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. qselect_last_seen = "select * from was_last_seen;"
  28. def update_seen(sql, id, dbhost, dbuser, dbpass, dbbase):
  29. try:
  30. conn = MySQLdb.connect(host=dbhost, user=dbuser,
  31. passwd=dbpass, db=dbbase)
  32. except MySQLdb.Error as err:
  33. print("Connection error: {}".format(err))
  34. conn.close()
  35. try:
  36. cur = conn.cursor(MySQLdb.cursors.DictCursor)
  37. print(sql % id)
  38. cur.execute(sql % id)
  39. data = cur.fetchall()
  40. except MySQLdb.Error as err:
  41. print("Query error: {}".format(err))
  42. conn.commit()
  43. conn.close()
  44. return data
  45. def update_hosts(sql, id, dbhost, dbuser, dbpass, dbbase):
  46. try:
  47. conn = MySQLdb.connect(host=dbhost, user=dbuser,
  48. passwd=dbpass, db=dbbase)
  49. except MySQLdb.Error as err:
  50. print("Connection error: {}".format(err))
  51. conn.close()
  52. try:
  53. cur = conn.cursor(MySQLdb.cursors.DictCursor)
  54. print(sql % id)
  55. cur.execute(sql % id)
  56. data = cur.fetchall()
  57. except MySQLdb.Error as err:
  58. print("Query error: {}".format(err))
  59. conn.commit()
  60. conn.close()
  61. return data
  62. def main():
  63. conf = read_conf()
  64. dbconf = [conf['dbhost'], conf['dbuser'], conf['dbpass'], conf['dbbase']]
  65. online_hosts = read_table(qonline, *dbconf)
  66. print(online_hosts)
  67. seen = read_table(qselect_last_seen, *dbconf)
  68. print(seen)
  69. seen_list = []
  70. for seen_h in seen:
  71. seen_list.append(seen_h['id_1c_import'])
  72. print(seen_list)
  73. for online_host in online_hosts:
  74. if online_host['1c_id'] in seen_list:
  75. print('need to update host 1c_id = %s' % online_host['1c_id'])
  76. update_seen(qupdate_last_seen, online_host['1c_id'], *dbconf)
  77. elif online_host not in seen:
  78. print('need to add host 1c_id = %s' % online_host['1c_id'])
  79. update_seen(qinsert_last_seen, online_host['1c_id'], *dbconf)
  80. seen = read_table(qselect_last_seen, *dbconf)
  81. for seen_host in seen:
  82. if seen_host['datetime'] < (datetime.datetime.now() - datetime.timedelta(minutes=1)):
  83. print('need to remove host 1c_id = %s' % seen_host['id_1c_import'])
  84. update_hosts(qdelete_host, seen_host['id_1c_import'], *dbconf)
  85. update_seen(qdelete_last_seen, seen_host['id_1c_import'], *dbconf)
  86. if __name__ == '__main__':
  87. main()

Powered by TurnKey Linux.