From 4bf16df586bb7ee18b6e09616cbd8e7a66cc28f5 Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Thu, 29 Oct 2020 18:50:03 -0500 Subject: ietf-interfaces: Add statistics nodes --- ncserver/module/interfaces.py | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'ncserver/module/interfaces.py') 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.""" -- cgit v1.2.3-60-g2f50