metrics.py 3.29 KB
Newer Older
1 2 3 4 5
#!/usr/bin/python

import psutil as ps
import collections

6 7

#############################################################################
8 9
Metrics = collections.namedtuple("Metrics", ["name", "value"])

10

11
class Collection(object):
12

Bach Dániel committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    class Group(object):

        class Metric(object):
            name = "unknown"
            collector_function = 0
            collector_function_arguments = {}
            collector_function_result_attr = ""

            @classmethod
            def harvest(cls):
                query = cls.collector_function.im_func(
                    **cls.collector_function_arguments)
                if ((isinstance(query, list)) or (isinstance(query, dict))):
                    return Metrics(cls.name,
                                   query[cls.collector_function_result_attr])
                elif (isinstance(query, tuple)):
                    return Metrics(cls.name, query.__getattribute__(
                        cls.collector_function_result_attr))
                else:
                    return Metrics(cls.name, query)
33 34

##############################################################################
35 36


37
class std(Collection):
38

Bach Dániel committed
39 40 41 42 43
    class cpu(Collection.Group):
        class usage(Collection.Group.Metric):
            name = "cpu.usage"
            collector_function = ps.cpu_percent
            collector_function_arguments = {'interval': 0.0}
44

45
        class times(Collection.Group.Metric):
46 47 48 49 50 51 52
            name = "cpu.times"

            @classmethod
            def harvest(cls):
                return Metrics(cls.name,
                               ps.cpu_times().user + ps.cpu_times().system)

Bach Dániel committed
53 54 55
    class memory(Collection.Group):
        class usage(Collection.Group.Metric):
            name = "memory.usage"
56
            collector_function = ps.virtual_memory
Bach Dániel committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
            collector_function_result_attr = "percent"

    class swap(Collection.Group):
        class usage(Collection.Group.Metric):
            name = "swap.usage"
            collector_function = ps.swap_memory
            collector_function_result_attr = "percent"

    class user(Collection.Group):
        class count(Collection.Group.Metric):
            name = "user.count"

            @classmethod
            def harvest(cls):
                return Metrics(cls.name, len(ps.get_users()))

    class network(Collection.Group):
        class packages_sent(Collection.Group.Metric):
75
            name = "network.packets_sent"
Bach Dániel committed
76 77 78 79
            collector_function = ps.network_io_counters
            collector_function_result_attr = "packets_sent"

        class packages_received(Collection.Group.Metric):
80
            name = "network.packets_recv"
Bach Dániel committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
            collector_function = ps.network_io_counters
            collector_function_result_attr = "packets_recv"

        class bytes_sent(Collection.Group.Metric):
            name = "network.bytes_sent"
            collector_function = ps.network_io_counters
            collector_function_result_attr = "bytes_sent"

        class bytes_received(Collection.Group.Metric):
            name = "network.bytes_received"
            collector_function = ps.network_io_counters
            collector_function_result_attr = "bytes_recv"

    class system(Collection.Group):
        class boot_time(Collection.Group.Metric):
            name = "system.boot_time"
            collector_function = ps.get_boot_time