metrics.py 2.92 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/python

import psutil as ps
import collections

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

class Collection (object):
    class Group (object):
Nagy Gergő committed
11 12 13 14 15 16 17 18
        class Metric (object):
            name = "unknown"
            collector_function = 0
            collector_function_arguments = {}
            collector_function_result_attr = ""

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

####################################################################################

class std (Collection):

    class cpu (Collection.Group):

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

    class memory (Collection.Group):

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


    class user (Collection.Group):

        class count (Collection.Group.Metric):
54
            name = "user.count"
root committed
55 56 57
            @classmethod
            def harvest(cls):
                return Metrics(cls.name, len(ps.get_users()))
58 59 60 61

    class network (Collection.Group):

        class packages_sent (Collection.Group.Metric):
Nagy Gergő committed
62 63 64
            name = "network.packages_sent"
            collector_function = ps.network_io_counters
            collector_function_result_attr = "packets_sent"
65 66

        class packages_received (Collection.Group.Metric):
Nagy Gergő committed
67 68 69
            name = "network.packages_received"
            collector_function = ps.network_io_counters
            collector_function_result_attr = "packets_recv"
70 71

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

        class bytes_received(Collection.Group.Metric):
Nagy Gergő committed
77 78 79
            name = "network.bytes_received"
            collector_function = ps.network_io_counters
            collector_function_result_attr = "bytes_recv"
80 81 82 83

    class system (Collection.Group):

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