Advertisement
Guest User

Untitled

a guest
Aug 9th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. ##----------------------------------------------------------------------
  3. ## Huawei.VRP.get_interface_status
  4. ##----------------------------------------------------------------------
  5. ## Copyright (C) 2007-2012 The NOC Project
  6. ## See LICENSE for details
  7. ##----------------------------------------------------------------------
  8. """
  9. """
  10. from noc.sa.script import Script as NOCScript
  11. from noc.sa.interfaces import IGetInterfaceStatus
  12. import re
  13.  
  14. rx_ifc_status = re.compile(
  15. r"^\s*(?P<interface>[^ ]+) current state :.*?(?P<status>up|down)",
  16. re.IGNORECASE)
  17. rx_ifc_block = re.compile(
  18. r"Interface\s+(PHY|Physical)\s+Protocol[^\n]+\n(?P<block>.*)$",
  19. re.MULTILINE | re.DOTALL | re.IGNORECASE)
  20. rx_ifc_br_status = re.compile(
  21. r"^\s*(?P<interface>[^ ]+)\s+(?P<status>up|down|\*down).*$", re.IGNORECASE)
  22.  
  23.  
  24. class Script(NOCScript):
  25. name = "Huawei.VRP.get_interface_status"
  26. implements = [IGetInterfaceStatus]
  27.  
  28. def execute(self, interface=None):
  29. if self.snmp and self.access_profile.snmp_ro:
  30. try:
  31. # Get interface status
  32. r = []
  33. v = ""
  34. # IF-MIB::ifName, IF-MIB::ifOperStatus
  35. # for n, s in self.snmp.join_tables("1.3.6.1.2.1.31.1.1.1.1",
  36. # "1.3.6.1.2.1.2.2.1.8", bulk=True):
  37. # # ifOperStatus up(1)
  38. # r += [{"interface":n, "status":int(s) == 1}]
  39. for v in self.snmp.get_tables(["1.3.6.1.2.1.31.1.1.1.1",
  40. "1.3.6.1.2.1.2.2.1.8","1.3.6.1.2.1.2.2.1.7"], bulk=True):
  41. # ifOperStatus,ifAdminStatus,up(1)
  42. r += [{"interface":v[1], "status":int(v[2]) == 1, "adminstatus":int(v[3]) == 1} ]
  43. return r
  44. except self.snmp.TimeOutError:
  45. pass
  46. # Fallback to CLI
  47. r = []
  48. ##
  49. ## VRP3 style
  50. ##
  51. if self.match_version(version__startswith="3."):
  52. for l in self.cli("display interface").splitlines():
  53. if (l.find(" current state :") != -1 \
  54. and l.find("Line protocol ") == -1):
  55. match_int = rx_ifc_status.match(l)
  56. if match_int:
  57. r += [{
  58. "interface": match_int.group("interface"),
  59. "status": match_int.group("status").lower() == "up"
  60. }]
  61. ##
  62. ## Other (VRP5 style)
  63. ##
  64. else:
  65. cli = self.cli("display interface brief", cached=True)
  66. match = rx_ifc_block.search(cli)
  67. if match:
  68. for l in match.group("block").splitlines():
  69. match_int = rx_ifc_br_status.match(l)
  70. if match_int:
  71. r += [{
  72. "interface": match_int.group("interface"),
  73. "status": match_int.group("status").lower() == "up"
  74. }]
  75. return r
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement