Repository: napalm-automation/napalm

Issue:
IOS: get_interfaces_counters broken with Managment Interface


### Environment


### Description of Issue/Question
Driver: ios
On call device.get_interfaces_counters() a KeyError is raised on Managment0 beeing missing

This is due to the Managment interface mgmt0 not beeing translated to canonical.

### Did you follow the steps from https://github.com/napalm-automation/napalm#faq
(Place an ``x`` between the square brackets where applicable)

- [w] Yes
- [] No


### Setup

### napalm version
Git Current Develop

### Network operating system version

```
Cisco IOS Software, s6t64 Software (s6t64-ADVENTERPRISEK9-M), Version 15.5(1)SY4, RELEASE SOFTWARE (fc4)
Cisco C6807-XL ( Intel(R) Core(TM) i3-3227U CPU @ 2.50GHz ) processor (revision ) with 2879443K/524288K bytes of memory
```

### Steps to Reproduce the Issue
call to device.get_interfaces_counters()

### Error Traceback
```
Traceback (most recent call last):
  File "list-vlan2.py", line 19, in <module>
    print(yaml.dump(device.get_interfaces_counters()))
  File "/srv/home/adm-brin-f/Devel/pythonspace/test-napalm/.venv/lib/python3.6/site-packages/napalm/ios/ios.py", line 2133, in get_interfaces_counters
    counters[interface]["rx_discards"] = int(match.group("IQD"))
KeyError: 'Management0'
```


Discussion:

Resolving patch:
diff --git a/napalm/ios/ios.py b/napalm/ios/ios.py
--- a/napalm/ios/ios.py
+++ b/napalm/ios/ios.py
@@ -2129,9 +2129,17 @@ def get_interfaces_counters(self):
                     )
                     match = re.search(regex, line)
                     if match:
-                        interface = canonical_interface_name(interface)
-                        counters[interface]["rx_discards"] = int(match.group("IQD"))
-                        counters[interface]["tx_discards"] = int(match.group("OQD"))
+                        can_interface = canonical_interface_name(interface)
+                        try:
+                            counters[can_interface]["rx_discards"] = int(
+                                match.group("IQD")
+                            )
+                            counters[can_interface]["tx_discards"] = int(
+                                match.group("OQD")
+                            )
+                        except KeyError:
+                            counters[interface]["rx_discards"] = int(match.group("IQD"))
+                            counters[interface]["tx_discards"] = int(match.group("OQD"))
 
         return counters
 


Test patch:
diff --git a/test/ios/mocked_data/test_get_interfaces_counters/no_canonical_for_mgmt/expected_result.json b/test/ios/mocked_data/test_get_interfaces_counters/no_canonical_for_mgmt/expected_result.json
new file mode 100644
--- /dev/null
+++ b/test/ios/mocked_data/test_get_interfaces_counters/no_canonical_for_mgmt/expected_result.json
@@ -0,0 +1,16 @@
+{
+    "mgmt0": {
+        "rx_unicast_packets": 10039584,
+        "rx_octets": 860372462,
+        "rx_broadcast_packets": 10025891,
+        "rx_multicast_packets": 6,
+        "rx_errors": 0,
+        "rx_discards": 0,
+        "tx_unicast_packets": 309048,
+        "tx_octets": 110610635,
+        "tx_broadcast_packets": -1,
+        "tx_multicast_packets": -1,
+        "tx_errors": 0,
+        "tx_discards": 0
+    }
+}
diff --git a/test/ios/mocked_data/test_get_interfaces_counters/no_canonical_for_mgmt/show_interface_summary.txt b/test/ios/mocked_data/test_get_interfaces_counters/no_canonical_for_mgmt/show_interface_summary.txt
new file mode 100644
--- /dev/null
+++ b/test/ios/mocked_data/test_get_interfaces_counters/no_canonical_for_mgmt/show_interface_summary.txt
@@ -0,0 +1,12 @@
+
+
+ *: interface is up
+ IHQ: pkts in input hold queue     IQD: pkts dropped from input queue
+ OHQ: pkts in output hold queue    OQD: pkts dropped from output queue
+ RXBS: rx rate (bits/sec)          RXPS: rx rate (pkts/sec)
+ TXBS: tx rate (bits/sec)          TXPS: tx rate (pkts/sec)
+ TRTL: throttle count
+
+  Interface                   IHQ       IQD       OHQ       OQD      RXBS      RXPS      TXBS      TXPS      TRTL
+-----------------------------------------------------------------------------------------------------------------
+* mgmt0                         0         0         0         0         0         1         0         0         0
\ No newline at end of file
diff --git a/test/ios/mocked_data/test_get_interfaces_counters/no_canonical_for_mgmt/show_interfaces.txt b/test/ios/mocked_data/test_get_interfaces_counters/no_canonical_for_mgmt/show_interfaces.txt
new file mode 100644
--- /dev/null
+++ b/test/ios/mocked_data/test_get_interfaces_counters/no_canonical_for_mgmt/show_interfaces.txt
@@ -0,0 +1,31 @@
+mgmt0 is up, line protocol is up (connected)
+  Hardware is I82580 MGMT, address is 0008.e3ff.fd68 (bia 0008.e3ff.fd68)
+  Internet address is 10.211.127.1/24
+  MTU 1500 bytes, BW 1000000 Kbit/sec, DLY 10 usec,
+     reliability 255/255, txload 1/255, rxload 1/255
+  Encapsulation ARPA, loopback not set
+  Keepalive not supported
+  Full-duplex, 1000Mb/s
+  Media-type configured as RJ45 connector
+  input flow-control is off, output flow-control is unsupported
+  Clock mode is auto
+  ARP type: ARPA, ARP Timeout 04:00:00
+  Last input 00:00:01, output never, output hang never
+  Last clearing of "show interface" counters 23w5d
+  Input queue: 0/1000/0/0 (size/max/drops/flushes); Total output drops: 0
+  Queueing strategy: fifo
+  Output queue: 0/40 (size/max)
+  5 minute input rate 0 bits/sec, 0 packets/sec
+  5 minute output rate 0 bits/sec, 0 packets/sec
+     10039584 packets input, 860372462 bytes, 0 no buffer
+     Received 10025891 broadcasts (416 IP multicasts)
+     0 runts, 0 giants, 0 throttles
+     0 input errors, 0 CRC, 0 frame, 0 overrun, 0 ignored
+     0 watchdog, 0 multicast, 0 pause input
+     0 input packets with dribble condition detected
+     309048 packets output, 110610635 bytes, 0 underruns
+     0 output errors, 0 collisions, 25 interface resets
+     0 unknown protocol drops
+     0 babbles, 0 late collision, 0 deferred
+     0 lost carrier, 0 no carrier, 0 pause output
+     0 output buffer failures, 0 output buffers swapped out
\ No newline at end of file