Completed
Push — master ( 8451c1...a45803 )
by Kenny
01:18
created

Swap.check()   A

Complexity

Conditions 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
dl 0
loc 23
rs 9.0856
1
# -*- coding: utf-8 -*-
2
3
__author__ = 'Kenny Freeman'
4
__email__ = '[email protected]'
5
__license__ = "ISCL"
6
__docformat__ = 'reStructuredText'
7
8
import os.path
9
10
import plumd
11
import plumd.plugins
12
from plumd.util import get_file_list
13
14
15
class Swap(plumd.plugins.Reader):
16
    """Plugin to measure swap metrics from /proc/swap."""
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
35
    def poll(self):
36
        """Poll for kernel metrics under /proc.
37
38
        :rtype: ResultSet
39
        """
40
        return plumd.ResultSet(self.check())
41
42
43
    def check(self):
44
        """Return swap file usage metrics from proc file swap.
45
46
        :rtype: plumd.Result
47
        """
48
        result = plumd.Result("swap")
49
        dat = get_file_list(self.proc_file)
50
        # header: file, type, size, used, priority
51
        if len(dat) > 1:
52
            # remove header line
53
            dat.popleft()
54
        for entry in dat:
55
            #sfname, stype, ssize, sused, sprio = ("", None, 0, 0, 0)
56
            sfname, stype, ssize, sused, sprio = entry.split()
57
            sname = os.path.basename(sfname)
58
            mstr = "{0}.used".format(sname)
59
            result.add(plumd.Float(mstr, sused))
60
            mstr = "{0}.size".format(sname)
61
            result.add(plumd.Float(mstr, ssize))
62
            sfree = float(ssize) - float(sused)
63
            mstr = "{0}.free".format(sname)
64
            result.add(plumd.Float(mstr, sfree))
65
        return [result]
66