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.

95 lines
3.0 KiB

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

Powered by TurnKey Linux.