Mem   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 6
Bugs 2 Features 1
Metric Value
wmc 7
c 6
b 2
f 1
dl 0
loc 65
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A poll() 0 6 1
A __init__() 0 13 1
B check() 0 29 5
1
# -*- coding: utf-8 -*-
2
"""Memory metrics reader for /proc on Linux."""
3
import plumd
4
from plumd.util import Filter
5
6
__author__ = 'Kenny Freeman'
7
__email__ = '[email protected]'
8
__license__ = "ISCL"
9
__docformat__ = 'reStructuredText'
10
11
12
class Mem(plumd.Reader):
13
    """Plugin to measure various kernel metrics from /proc."""
14
15
    defaults = {
16
        'poll.interval': 10,
17
        'proc_path': '/proc',
18
        'proc_meminfo_gauges': ["MemTotal", "MemFree", "MemAvailable",
19
                                "Buffers", "Cached", "Active", "Inactive",
20
                                "Unevictable", "Mlocked", "SwapTotal",
21
                                "SwapFree", "Dirty", "Mapped", "Slab",
22
                                "SReclaimable", "SUnreclaim", "CommitLimit",
23
                                "Committed_AS", "VmallocTotal", "VmallocUsed",
24
                                "VmallocChunk", "HardwareCorrupted"]
25
    }
26
27
    def __init__(self, log, config):
28
        """Plugin to measure various kernel metrics from /proc.
29
30
        :param log: A logger
31
        :type log: logging.RootLogger
32
        :param config: a plumd.config.Conf configuration helper instance.
33
        :type config: plumd.config.Conf
34
        """
35
        super(Mem, self).__init__(log, config)
36
        self.config.defaults(Mem.defaults)
37
        self.proc_file = "{0}/meminfo".format(config.get('proc_path'))
38
        self.filter = Filter()
39
        self.gauges = self.config.get('proc_meminfo_gauges')
40
41
    def poll(self):
42
        """Poll for kernel metrics under /proc.
43
44
        :rtype: ResultSet
45
        """
46
        return plumd.ResultSet(self.check())
47
48
    def check(self):
49
        """Return memory utilization metrics from proc file mem.
50
51
        :rtype: plumd.Result
52
        """
53
        result = plumd.Result("mem")
54
        dat = []
55
        metrics = {}
56
        # read and process /proc/meminfo
57
        with open(self.proc_file, 'r') as pfd:
58
            dat = pfd.read().strip().split("\n")
59
60
        # list of eg. ['MemTotal: 7902804 kB', ...]
61
        for line in dat:
62
            vals = line.split()
63
            name = vals[0][:-1]
64
            val = vals[1]
65
            metrics[name] = val
66
67
        # now record each metric configured
68
        for mname in self.gauges:
69
            if mname not in metrics:
70
                self.log.error("proc: mem: unknown metric: {0}".format(mname))
71
                self.gauges.remove(mname)
72
                continue
73
            mstr = self.filter.process(mname)
74
            result.add(plumd.Int(mstr, metrics[mname]))
75
76
        return [result]
77