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

DiskSpace.proc_mounts()   B

Complexity

Conditions 5

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 5
c 1
b 1
f 0
dl 0
loc 18
rs 8.5454
1
# -*- coding: utf-8 -*-
2
3
__author__ = 'Kenny Freeman'
4
__email__ = '[email protected]'
5
__license__ = "ISCL"
6
__docformat__ = 'reStructuredText'
7
8
import os
9
import sys
10
11
PY3 = sys.version_info > (3,)
12
13
import plumd
14
import plumd.plugins
15
from plumd.util import get_file_list
16
17
18
class DiskSpace(plumd.plugins.Reader):
19
    """Plugin to measure disk usage."""
20
    defaults = {
21
        'poll.interval': 60,
22
        'fs_types': ['xfs', 'ext2', 'ext3', 'ext4']
23
    }
24
25
    def __init__(self, log, config):
26
        """Plugin to measure disk usage.
27
28
        :param log: A logger
29
        :type log: logging.RootLogger
30
        :param config: a plumd.config.Conf configuration helper instance.
31
        :type config: plumd.config.Conf
32
        """
33
        super(DiskSpace, self).__init__(log, config)
34
        self.config.defaults(DiskSpace.defaults)
35
        # characters to keep in device names
36
        kch = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-'
37
        # characters to remove from device names
38
        self.filter_chars = "".join(char for char in map(chr, range(256)) \
39
                                    if not char in kch)
40
41
42
    def proc_mounts(self):
43
        fs_types = self.config.get('fs_types')
44
        ret = []
45
        fname = "/proc/mounts"
46
        mounts = get_file_list(fname)
47
        filter_chars = self.filter_chars
48
        for entry in mounts:
49
            if not entry:
50
                continue
51
            dev, path, fs, opts, d1, d2 = entry.split()
52
            if fs not in fs_types:
53
                continue
54
            if PY3:
55
                dev = dev.translate(dict.fromkeys(map(ord, filter_chars), None))
56
            else:
57
                dev = dev.translate(None, filter_chars)
58
            ret.append((dev.replace("/dev/", "").replace("/", "_"), path))
59
        return ret
60
61
62
    def poll(self):
63
        """Poll for disk space usage.
64
65
        :rtype: ResultSet
66
        """
67
        result = plumd.Result("diskusage")
68
        mpoints = self.proc_mounts()
69
        for dev, mpoint in mpoints:
70
            mp_stat = os.statvfs(mpoint)
71
            free = (mp_stat.f_bavail * mp_stat.f_frsize)
72
            total = (mp_stat.f_blocks * mp_stat.f_frsize)
73
            used = (mp_stat.f_blocks - mp_stat.f_bfree) * mp_stat.f_frsize
74
            total_inodes = mp_stat.f_files
75
            free_inodes = mp_stat.f_favail
76
            used_inodes = total_inodes - free_inodes
77
            result.add(plumd.Int("{0}.free".format(dev), free))
78
            result.add(plumd.Int("{0}.total".format(dev), total))
79
            result.add(plumd.Int("{0}.used".format(dev), used))
80
            result.add(plumd.Int("{0}.total_inodes".format(dev), total_inodes))
81
            result.add(plumd.Int("{0}.free_inodes".format(dev), free_inodes))
82
            result.add(plumd.Int("{0}.used_inodes".format(dev), used_inodes))
83
        return plumd.ResultSet([result])
84