Completed
Branch master (2f3d56)
by Kenny
01:12
created

Swap.poll()   A

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 1
c 3
b 1
f 1
dl 0
loc 6
rs 9.4285
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
import traceback
10
11
import plumd
12
import plumd.plugins
13
from plumd.util import get_file_list
14
15
16
class Swap(plumd.plugins.Reader):
17
    """Plugin to measure swap metrics from /proc/swap."""
18
    defaults = {
19
        'poll.interval': 10,
20
        'proc_path': '/proc',
21
    }
22
23
    def __init__(self, log, config):
24
        """Plugin to measure swap metrics from /proc.
25
26
        :param log: A logger
27
        :type log: logging.RootLogger
28
        :param config: a plumd.config.Conf configuration helper instance.
29
        :type config: plumd.config.Conf
30
        """
31
        super(Swap, self).__init__(log, config)
32
        config.defaults(Swap.defaults)
33
        self.proc_file ="{0}/swaps".format(config.get('proc_path'))
34
35
36
    def poll(self):
37
        """Poll for kernel metrics under /proc.
38
39
        :rtype: ResultSet
40
        """
41
        return plumd.ResultSet(self.check())
42
43
44
    def check(self):
45
        """Return swap file usage metrics from proc file swap.
46
47
        :rtype: plumd.Result
48
        """
49
        result = plumd.Result("swap")
50
        dat = get_file_list(self.proc_file)
51
        # header: file, type, size, used, priority
52
        if len(dat) > 1:
53
            # remove header line
54
            dat.popleft()
55
        for entry in dat:
56
            if not entry:
57
                continue
58
            #sfname, stype, ssize, sused, sprio = ("", None, 0, 0, 0)
59
            try:
60
                sfname, stype, ssize, sused, sprio = entry.split()
61
            except Exception as e:
62
                tb = traceback.format_exc()
63
                self.log.error("proc_swap: exception: {0}: {1}".format(e, tb))
64
                continue
65
            sname = os.path.basename(sfname)
66
            mstr = "{0}.used".format(sname)
67
            result.add(plumd.Float(mstr, sused))
68
            mstr = "{0}.size".format(sname)
69
            result.add(plumd.Float(mstr, ssize))
70
            sfree = float(ssize) - float(sused)
71
            mstr = "{0}.free".format(sname)
72
            result.add(plumd.Float(mstr, sfree))
73
        return [result]
74