aboutsummaryrefslogtreecommitdiffstats
path: root/tools/systeminfo.py
blob: 75b7aa0d67bff67bd939f922c390134cb352cb52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

@media only all and (prefers-color-scheme: dark) {
.highlight .hll { background-color: #49483e }
.highlight .c { color: #75715e } /* Comment */
.highlight .err { color: #960050; background-color: #1e0010 } /* Error */
.highlight .k { color: #66d9ef } /* Keyword */
.highlight .l { color: #ae81ff } /* Literal */
.highlight .n { color: #f8f8f2 } /* Name */
.highlight .o { color: #f92672 } /* Operator */
.highlight .p { color: #f8f8f2 } /* Punctuation */
.highlight .ch { color: #75715e } /* Comment.Hashbang */
.highlight .cm { color: #75715e } /* Comment.Multiline */
.highlight .cp { color: #75715e } /* Comment.Preproc */
.highlight .cpf { color: #75715e } /* Comment.PreprocFile */
.highlight .c1 { color: #75715e } /* Comment.Single */
.highlight .cs { color: #75715e } /* Comment.Special */
.highlight .gd { color: #f92672 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gi { color: #a6e22e } /* Generic.Inserted */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #75715e } /* Generic.Subheading */
.highlight .kc { color: #66d9ef } /* Keyword.Constant */
.highlight .kd { color: #66d9ef } /* Keyword.Declaration */
.highlight .kn { color: #f92672 } /* Keyword.Namespace */
.highlight .kp { color: #66d9ef } /* Keyword.Pseudo */
.highlight .kr { color: #66d9ef } /* Keyword.Reserved */
.highlight .kt { color: #66d9ef } /* Keyword.Type */
.highlight .ld { color: #e6db74 } /* Literal.Date */
.highlight .m { color: #ae81ff } /* Literal.Number */
.highlight .s { color: #e6db74 } /* Literal.String */
.highlight .na { color: #a6e22e } /* Name.Attribute */
.highlight .nb { color: #f8f8f2 } /* Name.Builtin */
.highlight .nc { color: #a6e22e } /* Name.Class */
.highlight .no { color: #66d9ef } /* Name.Constant */
.highlight .nd { color: #a6e22e } /* Name.Decorator */
.highlight .ni { color: #f8f8f2 } /* Name.Entity */
.highlight .ne { color: #a6e22e } /* Name.Exception */
.highlight .nf { color: #a6e22e } /* Name.Function */
.highlight .nl { color: #f8f8f2 } /* Name.Label */
.highlight .nn { color: #f8f8f2 } /* Name.Namespace */
.highlight .nx { color: #a6e22e } /* Name.Other */
.highlight .py { color: #f8f8f2 } /* Name.Property */
.highlight .nt { color: #f92672 } /* Name.Tag */
.highlight .nv { color: #f8f8f2 } /* Name.Variable */
.highlight .ow { color: #f92672 } /* Operator.Word */
.highlight .w { color: #f8f8f2 } /* Text.Whitespace */
.highlight .mb { color: #ae81ff } /* Literal.Number.Bin */
.highlight .mf { color: #ae81ff } /* Literal.Number.Float */
.highlight .mh { color: #ae81ff } /* Literal.Number.Hex */
.highlight .mi { color: #ae81ff } /* Literal.Number.Integer */
.highlight .mo { color: #ae81ff } /* Literal.Number.Oct */
.highlight .sa { color: #e6db74 } /* Literal.String.Affix */
.highlight .sb { color: #e6db74 } /* Literal.String.Backtick */
.highlight .sc { color: #e6db74 } /* Literal.String.Char */
.highlight .dl { color: #e6db74 } /* Literal.String.Delimiter */
.highlight .sd { color: #e6db74 } /* Literal.String.Doc */
.highlight .s2 { color: #e6db74 } /* Literal.String.Double */
.highlight .se { color: #ae81ff } /* Literal.String.Escape */
.highlight .sh { color: #e6db74 } /* Literal.String.Heredoc */
.highlight .si { color: #e6db74 } /* Literal.String.Interpol */
.highlight .sx { color: #e6db74 } /* Literal.String.Other */
.highlight .sr { color: #e6db74 } /* Literal.String.Regex */
.highlight .s1 { color: #e6db74 } /* Literal.String.Single */
.highlight .ss { color: #e6db74 } /* Literal.String.Symbol */
.highlight .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #a6e22e } /* Name.Function.Magic */
.highlight .vc { color: #f8f8f2 } /* Name.Variable.Class */
.highlight .vg { color: #f8f8f2 } /* Name.Variable.Global */
.highlight .vi { color: #f8f8f2 } /* Name.Variable.Instance */
.highlight .vm { color: #f8f8f2 } /* Name.Variable.Magic */
.highlight .il { color: #ae81ff } /* Literal.Number.Integer.Long */
}
@media (prefers-color-scheme: light) {
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
}
---
features:
  - |
    Per default, don't log a message in syslog for each incoming SNMP query.
    So set the default log level to '-LS0-5d'. Allow the operator to customize
    the log level via a parameter.
' href='#n339'>339 340 341 342 343 344 345 346
# Copyright 2015-2017 Intel Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Tools for access to OS details
"""

import os
import platform
import subprocess
import locale
import re
import distro

from conf import settings as S
from tools.version import Version

def match_line(file_name, pattern):
    """ loops through given file and returns first line matching given pattern

    :returns: string with the matching line without end of line or None
    """
    try:
        with open(file_name, encoding="latin-1") as file_:
            for line in file_:
                if not line.strip():
                    continue
                if not line.strip().startswith(pattern):
                    continue

                return line.strip().rstrip('\n')
        return None
    except OSError:
        return None

def get_os():
    """Get distro name.

    :returns: Return distro name as a string
    """
    return ' '.join(distro.linux_distribution())

def get_kernel():
    """Get kernel version.

    :returns: Return kernel version as a string
    """
    return platform.release()

def get_cpu():
    """Get CPU information.

    :returns: Return CPU information as a string
    """
    cpu = match_line('/proc/cpuinfo', 'model name')
    return cpu.split(':')[1] if cpu else cpu

def get_nic():
    """Get NIC(s) information.

    :returns: Return NIC(s) information as a list
    """
    nics = []
    output = subprocess.check_output('lspci', shell=True)
    output = output.decode(locale.getdefaultlocale()[1])
    for line in output.split('\n'):
        for nic in S.getValue('NICS'):
            # lspci shows PCI addresses without domain part, i.e. last 7 chars
            if line.startswith(nic['pci'][-7:]):
                nics.append(''.join(line.split(':')[2:]).strip())
    return nics

def get_platform():
    """Get platform information.

    Currently this is the motherboard vendor, name and socket
    count.

    :returns: Return platform information as a string
    """
    output = []

    with open('/sys/class/dmi/id/board_vendor', 'r') as file_:
        output.append(file_.readline().rstrip())

    with open('/sys/class/dmi/id/board_name', 'r') as file_:
        output.append(file_.readline().rstrip())

    num_nodes = len([name for name in os.listdir(
        '/sys/devices/system/node/') if name.startswith('node')])
    output.append(''.join(['[', str(num_nodes), ' sockets]']))

    return ' '.join(output).strip()

def get_cpu_cores():
    """Get number of CPU cores.

    :returns: Return number of CPU cores
    """
    cores = 0
    with open('/proc/cpuinfo') as file_:
        for line in file_:
            if line.rstrip('\n').startswith('processor'):
                cores += 1
            continue

    # this code must be executed by at leat one core...
    if cores < 1:
        cores = 1
    return cores

def get_memory():
    """Get memory information.

    :returns: amount of system memory as string together with unit
    """
    memory = match_line('/proc/meminfo', 'MemTotal')
    return memory.split(':')[1].strip() if memory else memory

def get_memory_bytes():
    """Get memory information in bytes

    :returns: amount of system memory
    """
    mem_list = get_memory().split(' ')
    mem = float(mem_list[0].strip())
    if mem_list.__len__() > 1:
        unit = mem_list[1].strip().lower()
        if unit == 'kb':
            mem *= 1024
        elif unit == 'mb':
            mem *= 1024 ** 2
        elif unit == 'gb':
            mem *= 1024 ** 3
        elif unit == 'tb':
            mem *= 1024 ** 4

    return int(mem)

def get_pids(proc_names_list):
    """ Get pid(s) of process(es) with given name(s)

    :returns: list with pid(s) of given processes or None if processes
        with given names are not running
    """

    try:
        pids = subprocess.check_output(['sudo', 'LC_ALL=' + S.getValue('DEFAULT_CMD_LOCALE'), 'pidof']
                                       + proc_names_list)
    except subprocess.CalledProcessError:
        # such process isn't running
        return None

    return list(map(str, map(int, pids.split())))

def get_pid(proc_name_str):
    """ Get pid(s) of process with given name

    :returns: list with pid(s) of given process or None if process
        with given name is not running
    """
    return get_pids([proc_name_str])

def pid_isalive(pid):
    """ Checks if given PID is alive

    :param pid: PID of the process
    :returns: True if given process is running, False otherwise
    """
    return os.path.isdir('/proc/' + str(pid))

def get_bin_version(binary, regex):
    """ get version of given binary selected by given regex

    :returns: version string or None
    """
    try:
        output = str(subprocess.check_output(
            binary, stderr=subprocess.STDOUT, shell=True).decode().rstrip('\n'))
    except subprocess.CalledProcessError:
        return None

    versions = re.findall(regex, output)
    if len(versions):
        return versions[0]
    else:
        return None

def get_git_tag(path):
    """ get tag of recent commit from repository located at 'path'

    :returns: git tag in form of string with commit hash or None if there
        isn't any git repository at given path
    """
    try:
        if os.path.isdir(path):
            return subprocess.check_output('cd {}; git rev-parse HEAD'.format(path), shell=True,
                                           stderr=subprocess.DEVNULL).decode().rstrip('\n')
        elif os.path.isfile(path):
            return subprocess.check_output('cd $(dirname {}); git log -1 --pretty="%H" {}'.format(path, path),
                                           shell=True, stderr=subprocess.DEVNULL).decode().rstrip('\n')
        else:
            return None
    except subprocess.CalledProcessError:
        return None

# This function uses long switch per purpose, so let us suppress pylint warning too-many-branches
# pylint: disable=too-many-branches, too-many-statements
def get_version(app_name):
    """ Get version of given application and its git tag

    :returns: dictionary {'name' : app_name, 'version' : app_version, 'git_tag' : app_git_tag) in case that
        version or git tag are not known or not applicaple, than None is returned for any unknown value

    """
    app_version_file = {
        'ovs' : r'Open vSwitch\) ([0-9.]+)',
        'testpmd' : r'RTE Version: \'\S+ ([0-9.]+)',
        'qemu' : r'QEMU emulator version ([0-9.]+)',
        'loopback_l2fwd' : os.path.join(S.getValue('ROOT_DIR'), 'src/l2fwd/l2fwd.c'),
        'ixnet' : os.path.join(S.getValue('TRAFFICGEN_IXNET_LIB_PATH'), 'pkgIndex.tcl'),
        'ixia' : os.path.join(S.getValue('TRAFFICGEN_IXIA_ROOT_DIR'), 'lib/ixTcl1.0/ixTclHal.tcl'),
    }



    app_version = None
    app_git_tag = None

    if app_name.lower().startswith('ovs'):
        app_version = get_bin_version('{} --version'.format(S.getValue('TOOLS')['ovs-vswitchd']),
                                      app_version_file['ovs'])
        if 'vswitch_src' in S.getValue('TOOLS'):
            app_git_tag = get_git_tag(S.getValue('TOOLS')['vswitch_src'])
    elif app_name.lower() in ['dpdk', 'testpmd']:
        app_version = get_bin_version('{} -v -h'.format(S.getValue('TOOLS')['testpmd']),
                                      app_version_file['testpmd'])
        # we have to consult PATHS settings to be sure, that dpdk/testpmd
        # were build from the sources
        if S.getValue('PATHS')[app_name.lower()]['type'] == 'src':
            app_git_tag = get_git_tag(S.getValue('TOOLS')['dpdk_src'])
    elif app_name.lower() == 'loopback_testpmd':
        # testpmd inside the guest is compiled from downloaded sources
        # stored at TOOS['dpdk_src'] directory
        tmp_ver = ['', '', '']
        dpdk_16 = False
        # TOOLS dictionary is created during runtime and it is not
        # available in some vsperf modes (e.g. -m trafficgen), thus
        # following definition can't be part of app_version_file dict above
        app_file = os.path.join(S.getValue('TOOLS')['dpdk_src'],
                                'lib/librte_eal/common/include/rte_version.h')
        with open(app_file) as file_:
            for line in file_:
                if not line.strip():
                    continue
                # DPDK version < 16
                if line.startswith('#define RTE_VER_MAJOR'):
                    tmp_ver[0] = line.rstrip('\n').split(' ')[2]
                # DPDK version < 16
                elif line.startswith('#define RTE_VER_PATCH_LEVEL'):
                    tmp_ver[2] = line.rstrip('\n').split(' ')[2]
                # DPDK version < 16
                elif line.startswith('#define RTE_VER_PATCH_RELEASE'):
                    release = line.rstrip('\n').split(' ')[2]
                    if not '16' in release:
                        tmp_ver[2] += line.rstrip('\n').split(' ')[2]
                # DPDK all versions
                elif line.startswith('#define RTE_VER_MINOR'):
                    if dpdk_16:
                        tmp_ver[2] = line.rstrip('\n').split(' ')[2]
                    else:
                        tmp_ver[1] = line.rstrip('\n').split(' ')[2]
                # DPDK all versions
                elif line.startswith('#define RTE_VER_SUFFIX'):
                    tmp_ver[2] += line.rstrip('\n').split('"')[1]
                # DPDK version >= 16
                elif line.startswith('#define RTE_VER_YEAR'):
                    dpdk_16 = True
                    tmp_ver[0] = line.rstrip('\n').split(' ')[2]
                # DPDK version >= 16
                elif line.startswith('#define RTE_VER_MONTH'):
                    tmp_ver[1] = '{:0>2}'.format(line.rstrip('\n').split(' ')[2])
                # DPDK version >= 16
                elif line.startswith('#define RTE_VER_RELEASE'):
                    release = line.rstrip('\n').split(' ')[2]
                    if not '16' in release:
                        tmp_ver[2] += line.rstrip('\n').split(' ')[2]

        if len(tmp_ver[0]):
            app_version = '.'.join(tmp_ver)
        app_git_tag = get_git_tag(S.getValue('TOOLS')['dpdk_src'])
    elif app_name.lower().startswith('qemu'):
        app_version = get_bin_version('{} --version'.format(S.getValue('TOOLS')['qemu-system']),
                                      app_version_file['qemu'])
        if 'qemu_src' in S.getValue('TOOLS'):
            app_git_tag = get_git_tag(S.getValue('TOOLS')['qemu_src'])
    elif app_name.lower() == 'ixnet':
        app_version = match_line(app_version_file['ixnet'], 'package provide IxTclNetwork')
        if app_version:
            app_version = app_version.split(' ')[3]
    elif app_name.lower() == 'ixia':
        app_version = match_line(app_version_file['ixia'], 'package provide IxTclHal')
        if app_version:
            app_version = app_version.split(' ')[3]
    elif app_name.lower() == 'xena':
        try:
            app_version = S.getValue('XENA_VERSION')
        except AttributeError:
            # setting was not available after execution
            app_version = 'N/A'
    elif app_name.lower() == 'dummy':
        # get git tag of file with Dummy implementation
        app_git_tag = get_git_tag(os.path.join(S.getValue('ROOT_DIR'), 'tools/pkt_gen/dummy/dummy.py'))
    elif app_name.lower() == 'vswitchperf':
        app_git_tag = get_git_tag(S.getValue('ROOT_DIR'))
    elif app_name.lower() == 'l2fwd':
        app_version = match_line(app_version_file['loopback_l2fwd'], 'MODULE_VERSION')
        if app_version:
            app_version = app_version.split('"')[1]
        app_git_tag = get_git_tag(app_version_file['loopback_l2fwd'])
    elif app_name.lower() in ['linux_bridge', 'buildin']:
        # without login into running VM, it is not possible to check bridge_utils version
        app_version = 'NA'
        app_git_tag = 'NA'

    return Version(app_name, app_version, app_git_tag)

def get_loopback_version(loopback_app_name):
    """ Get version of given guest loopback application and its git tag

    :returns: dictionary {'name' : app_name, 'version' : app_version, 'git_tag' : app_git_tag) in case that
        version or git tag are not known or not applicaple, than None is returned for any unknown value
    """
    version = get_version("loopback_{}".format(loopback_app_name))
    version.set_value('name', loopback_app_name)
    return version