Guest User

Untitled

a guest
Oct 18th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. ##----------------------------------------------------------------------
  3. ## Add new link from /tmp/mac-port.csv to sa_client_links
  4. ##----------------------------------------------------------------------
  5. ## Copyright (C) 2007-2011 The NOC Project
  6. ## See LICENSE for details
  7. ##----------------------------------------------------------------------
  8.  
  9. # Python modules
  10. import os
  11. # NOC modules
  12. import set_staticvlan_env
  13. from noc.staticvlan.models import connect, mysql_connect
  14.  
  15. c = connect(); conn = c[0]; cur = c[1]
  16. c = mysql_connect(); mysql_conn = c[0]; mysql_cur = c[1]
  17.  
  18. mysql_cur.execute("SELECT * FROM `netup`.`user_mac`")
  19. clients = mysql_cur.fetchall()
  20. mysql_cur.close()
  21. mysql_conn.close()
  22.  
  23. #clients_links = open('/tmp/mac-port.csv', 'r').split(';\n')
  24. #clients_links.close()
  25. clients_links = os.popen('sort /tmp/mac-port.csv').read().split('\r\n')
  26.  
  27. # Записываем связи:
  28. k=-1
  29. for i in range(len(clients_links)-1):
  30. cli_link = clients_links[i].split(',')
  31. mac = cli_link[2]
  32. for j in range(len(clients)):
  33. if mac == clients[j][3]:
  34. k=j
  35. break
  36. if k <> -1:
  37. switch = cli_link[0]
  38. port = cli_link[1]
  39. # Fix for DLink DGS-3100-24TG
  40. if len(port.split(':')) > 1:
  41. port = port.split(':')[0] + '00' + port.split(':')[1]
  42.  
  43. id_cli = clients[k][0]
  44. id_account = clients[k][1]
  45. id_s_link = clients[k][2]
  46. # name = clients[k][4]
  47.  
  48. cur.execute( "SELECT id FROM sa_managedobject WHERE address = '%s'"%switch )
  49. mo_id = cur.fetchall()[0][0]
  50.  
  51. cur.execute( 'SELECT id FROM sa_client_links WHERE switch_id = %s and port = %s', ( mo_id, port ) )
  52. cli_link_id = cur.fetchall()
  53. cur.execute( "SELECT id FROM sa_client_links WHERE mac = '%s'"%mac )
  54. cli_mac_id = cur.fetchall()
  55. cur.execute( 'SELECT id FROM sa_managedobject_links WHERE switch1_id = %s and port1 = %s', ( mo_id, port ) )
  56. switch1_link_id = cur.fetchall()
  57. cur.execute( 'SELECT id FROM sa_managedobject_links WHERE switch2_id = %s and port2 = %s', ( mo_id, port ) )
  58. switch2_link_id = cur.fetchall()
  59.  
  60. if not switch1_link_id and not switch2_link_id:
  61. if not cli_mac_id and not cli_link_id:
  62. cur.execute( "INSERT INTO sa_client_links (switch_id, port, mac, id_cli, id_account, id_s_link, name, properties ) VALUES ( %s, %s, %s, %s, %s, %s, %s, %s )", ( mo_id, port, mac, id_cli, id_account, id_s_link, 'Client', 'auto discovery' ) )
  63.  
  64. elif not cli_mac_id and cli_link_id:
  65. cli_link_id = cli_link_id[0]
  66. cur.execute( "UPDATE sa_client_links SET mac=%s, id_cli=%s, id_account=%s, id_s_link=%s WHERE id=%s", ( mac, id_cli, id_account, id_s_link, cli_link_id ) )
  67.  
  68. elif cli_mac_id and not cli_link_id:
  69. cli_mac_id = cli_mac_id[0][0]
  70. cur.execute( "DELETE FROM sa_client_links WHERE id = %s"%str(cli_mac_id) )
  71. cur.execute( "INSERT INTO sa_client_links (switch_id, port, mac, id_cli, id_account, id_s_link, name, properties ) VALUES ( %s, %s, %s, %s, %s, %s, %s, %s )", ( mo_id, port, mac, id_cli, id_account, id_s_link, 'Client', 'auto discovery' ) )
  72.  
  73. elif cli_mac_id and cli_link_id and cli_mac_id <> cli_link_id:
  74. cli_mac_id = cli_mac_id[0][0]
  75. cli_link_id = cli_link_id[0]
  76. cur.execute( "DELETE FROM sa_client_links WHERE id = %s"%str(cli_mac_id) )
  77. cur.execute( "UPDATE sa_client_links SET mac=%s, id_cli=%s, id_account=%s, id_s_link=%s WHERE id=%s", ( mac, id_cli, id_account, id_s_link, cli_link_id ) )
  78. k=-1
  79.  
  80. conn.commit()
  81. cur.close()
  82. conn.close()
Add Comment
Please, Sign In to add comment