stats.py 4.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2015, Daniele Venzano
#
# 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.

16 17 18 19
"""This module contains classes for statistics on various entities in the Zoe master."""

import time

20 21

class Stats:
22
    """Base statistics class.
23

24 25 26 27
    Every Stats object must have a timestamp that records when the stats it contains where recorded.
    """
    def __init__(self):
        self.timestamp = time.time()
28 29


30 31
class NodeStats(Stats):
    """Stats related to a single node."""
32 33 34 35 36 37
    def __init__(self, name):
        super().__init__()
        self.name = name
        self.container_count = 0
        self.cores_total = 0
        self.cores_reserved = 0
38
        self.cores_allocated = 0
39
        self.cores_in_use = 0
40
        self.memory_total = 0
41
        self.memory_allocated = 0
42
        self.memory_reserved = 0
43
        self.memory_in_use = 0
44
        self.labels = []
45
        self.status = 'offline'
46
        self.service_stats = {}
47
        self.images = []
Daniele Venzano's avatar
Daniele Venzano committed
48
        self.valid = False
49

50 51
    def serialize(self):
        """Convert the object into a dict."""
52
        ret = {
53 54 55 56
            'name': self.name,
            'container_count': self.container_count,
            'cores_total': self.cores_total,
            'cores_reserved': self.cores_reserved,
57
            'cores_allocated': self.cores_allocated,
58
            'cores_in_use': self.cores_in_use,
59 60
            'memory_total': self.memory_total,
            'memory_reserved': self.memory_reserved,
61
            'memory_allocated': self.memory_allocated,
62
            'memory_in_use': self.memory_in_use,
63
            'labels': list(self.labels),
64
            'status': self.status,
65 66
            'service_stats': self.service_stats,
            'images': self.images
67
        }
68
        return ret
69

70

71 72
class ClusterStats(Stats):
    """Stats related to the whole cluster."""
73 74 75 76
    def __init__(self):
        super().__init__()
        self.nodes = []

77 78 79 80 81 82
    def serialize(self):
        """Convert the object into a dict."""
        return {
            'container_count': self.container_count,
            'memory_total': self.memory_total,
            'cores_total': self.cores_total,
83 84 85 86
            'memory_reserved': sum([n.memory_reserved for n in self.nodes]),
            'cores_reserved': sum([n.cores_reserved for n in self.nodes]),
            'memory_in_use': sum([n.memory_in_use for n in self.nodes]),
            'cores_in_use': sum([n.cores_in_use for n in self.nodes]),
87 88
            'nodes': [x.serialize() for x in self.nodes]
        }
89 90 91 92 93 94 95 96 97 98 99

    @property
    def memory_total(self) -> int:
        """Total memory installed in the whole platform."""
        return sum([node.memory_total for node in self.nodes])

    @property
    def cores_total(self) -> int:
        """Total number of cores installed."""
        return sum([node.cores_total for node in self.nodes])

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    @property
    def memory_reserved(self) -> int:
        """Total memory reserved in the whole platform."""
        return sum([node.memory_reserved for node in self.nodes])

    @property
    def cores_reserved(self) -> int:
        """Total number of cores reserved."""
        return sum([node.cores_reserved for node in self.nodes])

    @property
    def memory_in_use(self) -> int:
        """Total memory in use in the whole platform."""
        return sum([node.memory_in_use for node in self.nodes])

    @property
    def cores_in_use(self) -> int:
        """Total number of cores in use."""
        return sum([node.cores_in_use for node in self.nodes])

120 121 122 123
    @property
    def container_count(self) -> int:
        """Total number of containers."""
        return sum([node.container_count for node in self.nodes])