summaryrefslogblamecommitdiff
path: root/ncserver/module/system.py
blob: 285b272f95cee739ca1e768cd3e7bb0f3d3cb473 (plain) (tree)





















































































                                                                               
"""
NETCONF for APK Distributions server:
    ietf-system module

Copyright © 2020 Adélie Software in the Public Benefit, Inc.

Released under the terms of the NCSA license.  See the LICENSE file included
with this source distribution for more information.

SPDX-License-Identifier: NCSA
"""

import platform
import subprocess
import time

from datetime import datetime
from math import floor
from netconf import util


M_ABI_VERSION = 1
"""The ABI version of this NETCONF module."""


M_PREFIX = "sys"
"""The XML tag prefix for this module's tags."""


M_NS = "urn:ietf:params:xml:ns:yang:ietf-system"
"""The XML namespace for this module."""


M_NAME = "ietf-system"
"""The YANG model name for this module."""


M_REVISION = "2014-08-06"
"""The YANG revision date for this module."""


M_IMPORTS = {
    'ietf-yang-types@2013-07-15': {
        'ns': "urn:ietf:params:xml:ns:yang:ietf-yang-types", 'prefix': "yang"
    },
    'ietf-inet-types@2013-07-15': {
        'ns': "urn:ietf:params:xml:ns:yang:ietf-inet-types", 'prefix': "inet"
    },
    'ietf-netconf-acm@2018-02-14': {
        'ns': "urn:ietf:params:xml:ns:yang:ietf-netconf-acm", 'prefix': "nacm"
    },
    'iana-crypt-hash@2014-08-06': {
        'ns': "urn:ietf:params:xml:ns:yang:iana-crypt-hash", 'prefix': "ianach"
    }
}
"""The imported YANG modules for this module."""


def running(node):
    """Retrieve the running configuration for this system."""


def operational(node):
    """Retrieve the operational configuration for this system."""
    state = util.subelm(node, 'sys:system-state')

    plat = util.subelm(state, 'sys:platform')
    plat.append(util.leaf_elm('sys:os-name', platform.system()))
    plat.append(util.leaf_elm('sys:os-release', platform.release()))
    osv = subprocess.run(['/bin/sh', '-c',
                          '( . /etc/os-release && echo -n $PRETTY_NAME )'],
                         stdout=subprocess.PIPE)
    plat.append(util.leaf_elm('sys:os-version', osv.stdout.decode('utf-8')))
    plat.append(util.leaf_elm('sys:machine', platform.machine()))

    clock = util.subelm(state, 'sys:clock')
    clock.append(util.leaf_elm(
        'sys:current-datetime',
        datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%f')
    ))

    with open('/proc/uptime', 'r') as upfile:
        raw = upfile.read().split(' ')[0]
        boot = floor(time.time() - float(raw))
        fmted = datetime.fromtimestamp(boot).strftime('%Y-%m-%dT%H:%M:%S.%f')
        clock.append(util.leaf_elm('sys:boot-datetime', fmted))