metrics.py 3.09 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 12
class Collection (object):
    class Group (object):
Nagy Gergő committed
13 14 15 16 17 18 19 20
        class Metric (object):
            name = "unknown"
            collector_function = 0
            collector_function_arguments = {}
            collector_function_result_attr = ""

            @classmethod
            def harvest(cls):
root committed
21
                query = cls.collector_function.im_func(**cls.collector_function_arguments)
Nagy Gergő committed
22
                if ((isinstance(query, list)) or (isinstance(query, dict))):
root committed
23
                    return Metrics(cls.name,
24 25
                                   query[cls.collector_function_result_attr])
                elif (isinstance(query, tuple)):
root committed
26
                    return Metrics(cls.name,
27
                                   query.__getattribute__(cls.collector_function_result_attr))
Nagy Gergő committed
28
                else:
29 30 31
                    return Metrics(cls.name, query)

##############################################################################
32 33 34 35 36 37 38


class std (Collection):

    class cpu (Collection.Group):

        class usage (Collection.Group.Metric):
root committed
39
            name = "cpu.usage"
Nagy Gergő committed
40 41
            collector_function = ps.cpu_percent
            collector_function_arguments = {
root committed
42
                'interval': 0.0,
Nagy Gergő committed
43
            }
44 45 46 47

    class memory (Collection.Group):

        class usage(Collection.Group.Metric):
Nagy Gergő committed
48 49 50
            name = "memory.usage"
            collector_function = ps.virtmem_usage
            collector_function_result_attr = "percent"
51

root committed
52 53 54 55 56 57
    class swap (Collection.Group):

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

    class user (Collection.Group):

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

root committed
64 65 66
            @classmethod
            def harvest(cls):
                return Metrics(cls.name, len(ps.get_users()))
67 68 69 70

    class network (Collection.Group):

        class packages_sent (Collection.Group.Metric):
Nagy Gergő committed
71 72 73
            name = "network.packages_sent"
            collector_function = ps.network_io_counters
            collector_function_result_attr = "packets_sent"
74 75

        class packages_received (Collection.Group.Metric):
Nagy Gergő committed
76 77 78
            name = "network.packages_received"
            collector_function = ps.network_io_counters
            collector_function_result_attr = "packets_recv"
79 80

        class bytes_sent (Collection.Group.Metric):
81 82 83
            name = "network.bytes_sent"
            collector_function = ps.network_io_counters
            collector_function_result_attr = "bytes_sent"
84 85

        class bytes_received(Collection.Group.Metric):
Nagy Gergő committed
86 87 88
            name = "network.bytes_received"
            collector_function = ps.network_io_counters
            collector_function_result_attr = "bytes_recv"
89 90 91 92

    class system (Collection.Group):

        class boot_time (Collection.Group.Metric):
Nagy Gergő committed
93
            name = "system.boot_time"
94
            collector_function = ps.get_boot_time