diff options
-rw-r--r-- | doc/roadmap.rst | 10 | ||||
-rw-r--r-- | ncserver/module/interfaces.py | 41 |
2 files changed, 45 insertions, 6 deletions
diff --git a/doc/roadmap.rst b/doc/roadmap.rst index 25b6e95..e76e2d1 100644 --- a/doc/roadmap.rst +++ b/doc/roadmap.rst @@ -228,16 +228,16 @@ Resolved TBDs -[ ] Interfaces module +[/] Interfaces module --------------------- -* [ ] Configuration nodes +* [/] Configuration nodes - * [ ] Name, description, type + * [/] Name, description, type * [ ] Enabled -* [ ] State nodes +* [/] State nodes * [ ] Operational state @@ -251,7 +251,7 @@ Resolved TBDs * [ ] Link speed (bits/second) - * [ ] Statistics (``ifctrstat``) + * [x] Statistics (``ifctrstat``) The interfaces module allows configuration and inspection of the physical layer diff --git a/ncserver/module/interfaces.py b/ncserver/module/interfaces.py index f8c4f45..497c63a 100644 --- a/ncserver/module/interfaces.py +++ b/ncserver/module/interfaces.py @@ -12,6 +12,7 @@ SPDX-License-Identifier: NCSA import logging import os +import pathlib import subprocess from lxml import etree @@ -55,11 +56,17 @@ M_IMPORTS = { """The imported YANG modules for this module.""" +M_FEATURES = ['pre-provisioning'] +"""The supported features declared in YANG for this module.""" + + def _add_running_contents(ifaces): """Retrieve the interface configuration for this device. Allows returning the 'config true' data for both datastores.""" - # foo + for ifname in pathlib.Path('/sys/class/net').iterdir(): + iface = util.subelm(ifaces, 'if:interface') + iface.append(util.leaf_elm('if:name', ifname.name)) def running(node): @@ -73,6 +80,38 @@ def operational(node): ifaces = util.subelm(node, 'if:interfaces') _add_running_contents(ifaces) + counter_tags = { + 'rx.octets': 'if:in-octets', + 'rx.discard': 'if:in-discards', + 'rx.errors': 'if:in-errors', + 'tx.octets': 'if:out-octets', + 'tx.discard': 'if:out-discards', + 'tx.errors': 'if:out-errors' + } + + for iface in ifaces.iterchildren(): + name = iface.find('{'+M_NS+'}name').text + stats = util.subelm(iface, 'if:statistics') + # XXX BAD vvv + stats.append(util.leaf_elm('if:discontinuity-time', '2020-01-01T01:01:01.011Z')) + # XXX BAD ^^^ + + result = subprocess.run(['/sbin/ifupdown', 'ifctrstat', name], + stdout=subprocess.PIPE) + if result.returncode != 0: + LOGGER.error(_('ifctrstat failed for %s: %s'), + name, result.returncode) + continue + + counters = result.stdout.decode('utf-8').split('\n') + counters.pop() + for counter, value in [data.split(': ') for data in counters]: + if counter not in counter_tags.keys(): + LOGGER.warning(_('unhandled ifctrstat counter for %s: %s'), + name, counter) + continue + stats.append(util.leaf_elm(counter_tags[counter], value)) + def edit(session, rpc, node, def_op): """Edit the interface configuration for this device.""" |