Advertisement
Guest User

noc_firmware_update.py

a guest
Apr 18th, 2011
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.06 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. ##----------------------------------------------------------------------
  3. ## Firmware update
  4. ##----------------------------------------------------------------------
  5. ## Copyright (C) 2007-2011 The NOC Project
  6. ## See LICENSE for details
  7. ##----------------------------------------------------------------------
  8.  
  9. import os, psycopg2, sys, time
  10.  
  11. ########################## GET FROM NOC ###########################################
  12.  
  13. ### Внимание не все коммутаторы можно обновлять безопасно! В некоторых после обновления возможна установка настроек по умолчанию!
  14. ### Смотреть возможность обновления без изменения настроек согласно версии прошивки и оборудования на сайте производителя:
  15. ### Известные проблемы:
  16. ### http://zyxel.ru/content/support/knowledgebase/KB-1457 при обновлении к прошивки до версии 3.80 и выше!!!
  17.  
  18.  
  19. #Выбираем Profile:
  20. #DLink.DxS
  21. # Выбераем обновляемую платформу:
  22.  
  23. #platform='DES-3200-10'
  24. #platform='DES-3200-26'
  25. #platform='DES-3200-28'
  26. #hardware='A1'
  27. # Обновляемая версия прошивки
  28. #firmware_version='1.10.B015'
  29. # Путь к прошивке на TFTP сервере....
  30. #firmware_url='192.168.4.19 /DLink/DES-3200/DES-3200R_1.21.B006.had'
  31.  
  32. platform='DES-3028'
  33. hardware='A1'
  34. # Путь к прошивке на TFTP сервере....
  35. firmware_url='192.168.4.19 /DLink/DES-3028/des3028_3052_series_firmware_200b27.had'
  36. # Обновляемая версия прошивки
  37. firmware_version='1.00-B32'
  38. check='1533156'
  39.  
  40. #platform='DES-3526'
  41. #platform='DES-3010G'
  42.  
  43. #platform='DGS-3100'
  44. #hardware='04'
  45. # Обновляемая версия прошивки
  46. #firmware_version='1.10.B015'
  47. # Путь к прошивке на TFTP сервере....
  48. #firmware_url='192.168.4.19 /DLink/DGS-3100_B1/DGS-3100_series_FW_3.60.28.ros'
  49. #firmware_url='192.168.4.19 /DLink/DGS-3100_A/DGS-3100_series_A1A2_FW_v3.00.43.ros'
  50. # Необходимая версия Boot PROM
  51. #boot_version='1.01.05'
  52. # Путь к загрузчику прошивки на TFTP сервере....
  53. #boot_url='192.168.4.19 /DLink/DGS-3100_B1/DGS-3100_series_Boot_1.01.05.rfb'
  54. #boot_url='192.168.4.19 /DLink/DGS-3100_A/DGS-3100_series_Boot_1.01.05.rfb'
  55.  
  56. ####################################################################
  57.  
  58.  
  59. conn = psycopg2.connect("host=localhost dbname=noc user=noc")
  60. cur = conn.cursor()
  61.  
  62. # Пробегаем по всем id объектов только данной платформы и оборудования
  63. cur.execute( "SELECT managed_object_id FROM sa_managedobjectattribute WHERE key='platform' and value='"+platform+"';" )
  64. device = cur.fetchall()
  65. for i in range(len(device)):
  66.     id = str(device[i][0])
  67.     cur.execute( "SELECT address FROM sa_managedobject WHERE id="+id+";" )
  68.     IP = str(cur.fetchone()[0])
  69.  
  70.     # Проверяем доступность коммутатора.
  71.     if os.popen('ping -c 3 '+IP+"|grep ', 0% packet loss,'").read() <> '':
  72.     #{'platform': 'DGS-3426G', 'vendor': 'DLink', 'version': '2.61.B06'}
  73.     dev_firmware_version = os.popen( 'cd /opt/noc; python manage.py debug-script get_version '+id+' 2>&1 |grep "{'+"'platform': '"+'" |grep -v '+"'Script returns with result: ' |awk -F"+'"'+"'"+'" '+"'"+'{ORS="'+'"; print $12}'+"'" ).read()
  74. #   dev_firmware_version = firmware_version
  75.  
  76.     if dev_firmware_version == '':
  77.         print ( platform+", id="+id+", IP address="+IP+": Error get_version!..." )
  78.         sys.exit(1)
  79.  
  80.     elif firmware_version == dev_firmware_version:
  81.         sys.stdout.write ( platform+", id="+id+", IP address="+IP+": Firmware update from "+dev_firmware_version+"..." )
  82.         sys.stdout.flush()
  83.  
  84. # Сохраняем настройки свича:
  85.         if os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='"+'["save config"]'+"' 2>&1 |grep 'Command: save config' |grep 'Success.'" ).read() <> '':
  86.         print ( platform+", id="+id+", IP address="+IP+": Error save config!..." )
  87.         sys.exit(1)
  88.  
  89. # commands for DES-3200
  90. # Так делать плохо:
  91. #       os.popen( 'cd /opt/noc; python manage.py debug-script  commands '+id+" commands='["+'"download firmware_fromTFTP '+firmware_url+'", "config firmware image_id 1 boot_up", "reboot"]'+"'" ).read()
  92. #       os.popen( 'cd /opt/noc; python manage.py debug-script  commands '+id+" commands='["+'"download firmware_fromTFTP '+firmware_url+'", "reboot"]'+"'" ).read()
  93.  
  94. # Надо делать вот так:
  95. #
  96. # Если надо проверить версию Boot PROM и если нужно обновить...
  97. #       TODO... какой коммандой обновлять бутпром?
  98. #
  99. # Проверить image_id и определить свободный image_id=image_id (1 или 2)
  100.         fir_tab=os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='["+'"show firmware information"]'+"' 2>&1 |grep -m 1 'Command: show firmware information' |awk -F'Command: show firmware information' '{print $2}'" ).read().split('\\n\\r')
  101.         image_1 = fir_tab[4].split('  ')
  102.         image_2 = fir_tab[5].split('  ')
  103.         image_del = '0'
  104. # Определить номер загрузочного образа image_id=image_id (1 или 2)
  105.         if image_1[0] == '*1':
  106.         image_id='1'
  107.         if image_2[1] <> '':
  108.             image_del = '2'
  109.         elif image_2[0] == '*2':
  110.         image_id='2'
  111.         if image_1[1] <> '':
  112.             image_del = '1'
  113.         else:
  114.         print ( platform+", id="+id+", IP address="+IP+": Error no boot image..." )
  115.         sys.exit(1)
  116. #
  117. # Проверить объем свободного места, на старых прошивках нет
  118. #       os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='["+'"show utilization flash"]'+"'" ).read()
  119. #
  120. # Если надо освободить место удалив небутабельное firmware.
  121.         if image_del <> '0':
  122. #       os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='["+'"config firmware image_id '+image_del+' delete"]'+"'" ).read()
  123.         sys.stdout.write ( ' del='+image_del )
  124. #
  125. # Загружаем ружное firmware в image_id
  126. #       os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='["+'"download firmware_fromTFTP '+firmware_url+' image_id '+image_id+'"]'+"'" ).read()
  127.         sys.stdout.write ( ', download '+image_id )
  128. #
  129. # Проверить размер (хеш) image_id и определить успешность загрузки firmware с TFTP
  130.         fir_tab=os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='["+'"show firmware information"]'+"' 2>&1 |grep -m 1 'Command: show firmware information' |awk -F'Command: show firmware information' '{print $2}'" ).read().split('\\n\\r')
  131.         i=3+int(image_id)
  132.         image = fir_tab[i].split('  ')
  133.         if image[0] == '*'+image_id and image[2] == check:
  134. # Перегружаем свич
  135. #       os.popen( 'cd /opt/noc; python manage.py debug-script commands '+id+" commands='["+'"reboot"]'+"'" ).read()
  136.         sys.stdout.write ( ', reboot ...' )
  137.         else:
  138.         print ( platform+", id="+id+", IP address="+IP+": Error firmware checksum faild..." )
  139.         sys.exit(1)
  140.         quit()
  141.  
  142.         time.sleep(1)
  143.  
  144.         # Проверяем доступность коммутатора после перезагрузки. Если недоступен длительное время, выходим...
  145.         i = 0
  146.         while os.popen('ping -c 3 '+IP+"|grep ', 0% packet loss,'").read() == '':
  147.         sys.stdout.write ( "." )
  148.         sys.stdout.flush()
  149.         time.sleep(1)
  150.         i = i + 1
  151.         if i > 50:
  152.             print ( "!" )
  153.             print ( platform+", id="+id+", IP address="+IP+" is faild!!!" )
  154.             sys.exit(1)
  155.  
  156.         print ( " Updated! :)" )
  157.  
  158.     else:
  159.         print ( platform+", id="+id+", IP address="+IP+": Firmware "+dev_firmware_version+" skip update! :)" )
  160.  
  161.     else:
  162.     print ( platform+", id="+id+", IP address="+IP+" is down..." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement