Completed
Push — master ( 5964b2...27b81b )
by Kenny
01:18
created

Swap.check()   B

Complexity

Conditions 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 25
rs 8.5806
cc 4
1
# -*- coding: utf-8 -*-
2
"""Reader plugin to record swap metrics from /proc on Linux."""
3
import os.path
4
5
import plumd
6
from plumd.util import get_file_list
7
8
__author__ = 'Kenny Freeman'
9
__email__ = '[email protected]'
10
__license__ = "ISCL"
11
__docformat__ = 'reStructuredText'
12
13
14
class Swap(plumd.Reader):
15
    """Plugin to measure swap metrics from /proc/swap."""
16
17
    defaults = {
18
        'poll.interval': 10,
19
        'proc_path': '/proc',
20
    }
21
22
    def __init__(self, log, config):
23
        """Plugin to measure swap metrics from /proc.
24
25
        :param log: A logger
26
        :type log: logging.RootLogger
27
        :param config: a plumd.config.Conf configuration helper instance.
28
        :type config: plumd.config.Conf
29
        """
30
        super(Swap, self).__init__(log, config)
31
        config.defaults(Swap.defaults)
32
        self.proc_file = "{0}/swaps".format(config.get('proc_path'))
33
34
    def poll(self):
35
        """Poll for kernel metrics under /proc.
36
37
        :rtype: ResultSet
38
        """
39
        return plumd.ResultSet(self.check())
40
41
    def check(self):
42
        """Return swap file usage metrics from proc file swap.
43
44
        :rtype: plumd.Result
45
        """
46
        result = plumd.Result("swap")
47
        dat = get_file_list(self.proc_file)
48
        # header: file, type, size, used, priority
49
        if len(dat) >= 1:
50
            # remove header line
51
            dat.popleft()
52
        for entry in dat:
53
            try:
54
                sfname, stype, ssize, sused, sprio = entry.split()
0 ignored issues
show
Unused Code introduced by
The variable stype seems to be unused.
Loading history...
Unused Code introduced by
The variable sprio seems to be unused.
Loading history...
55
                sname = os.path.basename(sfname)
56
                mstr = "{0}.used".format(sname)
57
                result.add(plumd.Float(mstr, sused))
58
                mstr = "{0}.size".format(sname)
59
                result.add(plumd.Float(mstr, ssize))
60
                sfree = float(ssize) - float(sused)
61
                mstr = "{0}.free".format(sname)
62
                result.add(plumd.Float(mstr, sfree))
63
            except ValueError as exc:
0 ignored issues
show
Unused Code introduced by
The variable exc seems to be unused.
Loading history...
64
                self.log.error("invalid swap entry: {0}".format(entry))
65
        return [result]
66