Skip to content

Commit

Permalink
bugfix: #1
Browse files Browse the repository at this point in the history
  • Loading branch information
xdai555 committed Jun 14, 2022
1 parent 6d576e1 commit 110c4aa
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 70 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Looking forward to your testing and feedback :).
- :white_check_mark: get_facts()
- :white_check_mark: get_interfaces()
- :white_check_mark: get_interfaces_ip()
- :white_check_mark: get_interfaces_counters()
- ~~:white_check_mark: get_interfaces_counters():x:needs to be rewritten~~
- :white_check_mark: get_lldp_neighbors()
- :white_check_mark: get_lldp_neighbors_detail()
- :white_check_mark: get_environment()
Expand Down
2 changes: 1 addition & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
- :white_check_mark: get_facts():获取设备基础信息,如型号、软件版本、序列号等
- :white_check_mark: get_interfaces():获取所有接口列表
- :white_check_mark: get_interfaces_ip():获取当前存在 IP 地址的接口列表,目前只支持 IPv4
- :white_check_mark: get_interfaces_counters():获取接口的计数信息,包括收发包、收发字节、错包、丢包、CRC 等
- ~~:white_check_mark: get_interfaces_counters():获取接口的计数信息,包括收发包、收发字节、错包、丢包、CRC 等:x:需要重写~~
- :white_check_mark: get_lldp_neighbors():获取 LLDP 邻居信息
- :white_check_mark: get_lldp_neighbors_detail():获取 LLDP 的详细信息,包括对端的系统名称及描述、接口名称及描述等
- :white_check_mark: get_environment():获取设备运行信息,包括 CPU、内存、电源、风扇、温度等状态
Expand Down
2 changes: 1 addition & 1 deletion napalm_h3c_comware/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
from napalm_h3c_comware.comware import ComwareDriver

__all__ = ("ComwareDriver",)
__version__ = "0.1.4"
__version__ = "0.1.5"
58 changes: 30 additions & 28 deletions napalm_h3c_comware/comware.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,34 +369,36 @@ def get_environment(self):

return environment

def get_interfaces_counters(self):
counters = {}
keys = (
"tx_errors", "rx_errors", "rx_crc",
"tx_discards", "rx_discards",
"tx_octets", "rx_octets",
"tx_packets", "rx_packets",
"tx_unicast_packets", "rx_unicast_packets",
"tx_multicast_packets", "rx_multicast_packets",
"tx_broadcast_packets", "rx_broadcast_packets",
)
structured_int_info = self._get_structured_output("display interface")

for interface in structured_int_info:
values = itemgetter(
"tx_errors", "rx_errors", "rx_crc",
"tx_aborts", "rx_aborts",
"tx_bytes", "rx_bytes",
"tx_pkts", "rx_pkts",
"tx_unicast", "rx_unicast",
"tx_multicast", "rx_multicast",
"tx_broadcast", "rx_broadcast",
)(interface)

values = (parse_null(value, -1, int) for value in values)
counters[interface.get("interface")] = dict(zip(keys, values))

return counters
# TODO: 从 textfsm 的 display interface 模板中移除,模板无法支持太多类型的输出,重构为正则表达式比较好

# def get_interfaces_counters(self):
# counters = {}
# keys = (
# "tx_errors", "rx_errors", "rx_crc",
# "tx_discards", "rx_discards",
# "tx_octets", "rx_octets",
# "tx_packets", "rx_packets",
# "tx_unicast_packets", "rx_unicast_packets",
# "tx_multicast_packets", "rx_multicast_packets",
# "tx_broadcast_packets", "rx_broadcast_packets",
# )
# structured_int_info = self._get_structured_output("display interface")

# for interface in structured_int_info:
# values = itemgetter(
# "tx_errors", "rx_errors", "rx_crc",
# "tx_aborts", "rx_aborts",
# "tx_bytes", "rx_bytes",
# "tx_pkts", "rx_pkts",
# "tx_unicast", "rx_unicast",
# "tx_multicast", "rx_multicast",
# "tx_broadcast", "rx_broadcast",
# )(interface)

# values = (parse_null(value, -1, int) for value in values)
# counters[interface.get("interface")] = dict(zip(keys, values))

# return counters

def get_lldp_neighbors_detail(self, interface: str = ""):
lldp = {}
Expand Down
40 changes: 1 addition & 39 deletions napalm_h3c_comware/utils/textfsm_templates/display_interface.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,10 @@ Value PVID (\d+)
Value LINK_TYPE (\S+)
Value LAST_FLAPPING (.+)
Value PRIMARY_IPADDR (\d+\.\d+\.\d+\.\d+/\d+)
Value RX_PKTS (\d+|-)
Value RX_BYTES (\d+|-)
Value RX_UNICAST (\d+|-)
Value RX_BROADCAST (\d+|-)
Value RX_MULTICAST (\d+|-)
Value RX_ERRORS (\d+|-)
Value RX_ABORTS (\d+|-)
Value RX_CRC (\d+|-)
Value TX_PKTS (\d+|-)
Value TX_BYTES (\d+|-)
Value TX_UNICAST (\d+|-)
Value TX_BROADCAST (\d+|-)
Value TX_MULTICAST (\d+|-)
Value TX_ERRORS (\d+|-)
Value TX_ABORTS (\d+|-)

Start
# 接口名所在行是一个独立的字符串,以此开始记录
^\S+\s*$$ -> Continue.Record
# 路由器某个 Serial 接口后面出现了空格,莫名奇妙的 comware bug
^${INTERFACE}\s*$$
^(C|c)urrent\s+state:\s+${LINK_STATUS}
^(L|l)ine\s+protocol\s+state:\s+${PROTOCOL_STATUS}
Expand All @@ -46,26 +30,4 @@ Start
^PVID:\s+${PVID}
^Port\s+link-type:\s+${LINK_TYPE}
^Last\s+link\s+flapping:\s+${LAST_FLAPPING}
# 如果接口不存在计数信息(例如 Serial),直接开始匹配下一个接口
^\s*$$ -> Start
# 虚拟接口计数匹配
^\s?Input:\s*${RX_PKTS}\s+packets,\s+${RX_BYTES}\s+bytes(,\s+\d+\s+buffers)?(,\s+${RX_ABORTS}\s+drops)?
^\s?Output:\s*${TX_PKTS}\s+packets,\s+${TX_BYTES}\s+bytes(,\s+\d+\s+buffers)?(,\s+${TX_ABORTS}\s+drops)? -> Start
# 物理接口计数匹配
^\s*Input\s+\(total\) -> INPUT



# for physical interfaces
INPUT
^\s*Input\s+\(normal\):\s+${RX_PKTS}\s+packets,\s+${RX_BYTES}\s+bytes
^\s*${RX_UNICAST}\s+unicasts,\s+${RX_BROADCAST}\s+broadcasts,\s+${RX_MULTICAST}\s+multicasts,\s+\d+\s+pauses
^\s*Input:\s+${RX_ERRORS}\s+input\s+errors,
^\s*${RX_CRC}\s+CRC,\s+\d+\s+frame,\s+\d+\s+overruns,\s+${RX_ABORTS}\s+aborts -> OUTPUT

# for physical interfaces
OUTPUT
^\s*Output\s+\(normal\):\s+${TX_PKTS}\s+packets,\s+${TX_BYTES}\s+bytes
^\s*${TX_UNICAST}\s+unicasts,\s+${TX_BROADCAST}\s+broadcasts,\s+${TX_MULTICAST}\s+multicasts,\s+\d+\s+pauses
^\s*Output:\s+${TX_ERRORS}\s+output\s+errors,
^\s*${TX_ABORTS}\s+aborts,\s+\d+\s+deferred, -> Start
^\s*$$

0 comments on commit 110c4aa

Please sign in to comment.