aboutsummaryrefslogtreecommitdiffstats
path: root/kubernetes/contrib/tools/oomstat.py
blob: 464290d3f6ca00b55d7d816c87b6214578e78a80 (plain)
1
2
3
4
.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 */
.. This work is licensed under a Creative Commons Attribution 4.0
.. International License.  http://creativecommons.org/licenses/by/4.0
.. Copyright 2019 Nokia


.. index:: Overview

ONAP Overview
=============

.. _overview:

.. toctree::
   :maxdepth: 2

   overview.rst
='n201' href='#n201'>201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
#!/usr/bin/env python

#
#     Copyright (c) 2018 Orange
#
#     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.
#

"""
Provides utilities to display oom (sub)modules resources stats
"""

import os
import sys
import getopt
from fnmatch import fnmatch as match
import yaml

def info(thing):
    if thing:
        sys.stderr.write("{}\n".format(thing))

try:
    from tabulate import tabulate
except ImportError as e:
    info("Warning: cannot import tabulate module (): {}".format(str(e)))
    def tabulate(lines, headers, tablefmt=None):
        ''' basic tabulate function '''
        fmt = ""
        nbco = len(headers)
        lenco = map(len, headers)
        for line in lines:
            for i in range(nbco):
                lenco[i] = max(lenco[i], len(str(line[i])))

        fmt = map(lambda n: "{{:<{}}}".format(n), map(lambda i: i+2, lenco))
        fmt = "  ".join(fmt)
        sep = map(lambda x: '-'*(x+2), lenco)

        output = [fmt.format(*headers), fmt.format(*sep)]
        for line in lines:
            output.append(fmt.format(*line))
        return "\n".join(output)


def values(root='.'):
    ''' Get the list of values.yaml files '''
    a = []
    for dirname, dirnames, filenames in os.walk(root):
        for filename in filenames:
            if filename == 'values.yaml':
                a.append((dirname, filename))

        if '.git' in dirnames:
            # don't go into any .git directories.
            dirnames.remove('.git')
    return a


def keys(dic, prefix=None):
    ''' recursively traverse the specified dict to collect existing keys '''
    result = []
    if dic:
        for k, v in dic.items():
            if prefix:
                k = '.'.join((prefix, k))
            if isinstance(v, dict):
                result += keys(v, k)
            else:
                result.append(k)
    return result


class Project:
    '''
    class to access to oom (sub)module (aka project) resources
    '''

    def __init__(self, dirname, filename):
        self.dirname = os.path.normpath(dirname)
        self.name = self.explicit()
        self.filename = os.path.join(dirname, filename)
        self.resources = None
        self.load()

    def load(self):
        ''' load resources from yaml description '''
        with open(self.filename, 'r') as istream:
            try:
                v = yaml.load(istream)
                if v:
                    self.resources = v.get('resources', None)
            except Exception as e:
                print(e)
                raise

    def explicit(self):
        ''' return an explicit name for the project '''
        path = []
        head, name = os.path.split(self.dirname)
        if not name:
            return head
        while head:
            head, tail = os.path.split(head)
            if tail:
                path.append(tail)
            else:
                path.append(head)
                head = None
        path.reverse()
        index = path.index('charts') if 'charts' in path else None
        if index:
            name = os.path.join(path[index-1], name)
        return name

    def __contains__(self, key):
        params = self.resources
        if key:
            for k in key.split('.'):
                if params and k in params:
                    params = params[k]
                else:
                    return False
        return True

    def __getitem__(self, key):
        params = self.resources
        for k in key.split('.'):
            if k in params:
                params = params[k]
        if params != self.resources:
            return params

    def get(self, key, default="-"):
        """ mimic dict method """
        if key in self:
            return self[key]
        return default

    def keys(self):
        """ mimic dict method """
        return keys(self.resources)


#
#
#

def usage(status=None):
    """ usage doc """
    arg0 = os.path.basename(os.path.abspath(sys.argv[0]))
    print("""Usage: {} [options] <root-directory>""".format(arg0))
    print((
        "\n"
        "Options:\n"
        "-h, --help           Show this help message and exit\n"
        "-t, --table <format> Use the specified format to display the result table.\n"
        "                     Valid formats are those from the python `tabulate'\n"
        "                     module. When not available, a basic builtin tabular\n"
        "                     function is used and this field has no effect\n"
        "-f, --fields         Comma separated list of resources fields to display.\n"
        "                     You may use wildcard patterns, eg small.*. Implicit\n"
        "                     value is *, ie all available fields will be used\n"
        "Examples:\n"
        "    # {0} /opt/oom/kubernetes\n"
        "    # {0} -f small.\\* /opt/oom/kubernetes\n"
        "    # {0} -f '*requests.*' -t fancy_grid /opt/oom/kubernetes\n"
        "    # {0} -f small.requests.cpu,small.requests.memory /opt/oom/kubernetes\n"
    ).format(arg0))
    if status is not None:
        sys.exit(status)


def getopts():
    """ read options from cmdline """
    opts, args = getopt.getopt(sys.argv[1:],
                               "hf:t:",
                               ["help", "fields=", "table="])
    if len(args) != 1:
        usage(1)

    root = args[0]
    table = None
    fields = ['*']
    patterns = []

    for opt, arg in opts:
        if opt in ("-h", '--help'):
            usage(0)
        elif opt in ("-f", "--fields"):
            fields = arg.split(',')
        elif opt in ("-t", "--table"):
            table = arg

    return root, table, fields, patterns


def main():
    """ main """
    try:
        root, table, fields, patterns = getopts()
    except getopt.GetoptError as e:
        print("Error: {}".format(e))
        usage(1)

    if not os.path.isdir(root):
        info("Cannot open {}: Not a directory".format(root))
        return

    # find projects
    projects = []
    for dirname, filename in values(root):
        projects.append(Project(dirname, filename))
    if not projects:
        info("No projects found in {} directory".format(root))
        return

    # check if we want to use pattern matching (wildcard only)
    if fields and reduce(lambda x, y: x or y,
                         map(lambda string: '*' in string, fields)):
        patterns = fields
        fields = []

    # if fields are not specified or patterns are used, discover available fields
    #  and use them (sort for readability)
    if patterns or not fields:
        avail = sorted(set(reduce(lambda x, y: x+y,
                                  map(lambda p: p.keys(), projects))))
        if patterns:
            for pattern in patterns:
                fields += filter(lambda string: match(string, pattern), avail)
        else:
            fields = avail

    # collect values for each project
    results = map(lambda project: [project.name] + map(project.get,
                                                       fields),
                  projects)

    # and then print
    if results:
        headers = ['project'] + fields
        print(tabulate(sorted(results), headers, tablefmt=table))


main()