Completed
Push — master ( ee7b96...42a2db )
by Kenny
01:20
created

DiskSpace.proc_mounts()   A

Complexity

Conditions 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 4
c 1
b 1
f 0
dl 0
loc 13
rs 9.2
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 time
10
import os.path
11
import multiprocessing
12
from collections import deque
13
14
import plumd
15
import plumd.plugins
16
from plumd.util import get_file_map, get_file_list, get_file
17
18
19
## todo: switch from list with pop(0) to deque
20
class DiskSpace(plumd.plugins.Reader):
21
    """Plugin to measure disk usage."""
22
    defaults = {
23
        'poll.interval': 60,
24
        'fs_types': ['xfs', 'ext2', 'ext3', 'ext4']
25
    }
26
27
    def __init__(self, log, config):
28
        """Plugin to measure disk usage.
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(DiskSpace, self).__init__(log, config)
36
        self.config.defaults(DiskSpace.defaults)
37
38
39
    def proc_mounts(self):
40
        fs_types = self.config.get('fs_types')
41
        ret = []
42
        fname = "/proc/mounts"
43
        mounts = get_file_list(fname)
44
        for entry in mounts:
45
            if not entry:
46
                continue
47
            dev, path, fs, opts, d1, d2 = entry.split()
48
            if fs not in fs_types:
49
                continue
50
            ret.append((dev.replace("/dev/", "").replace("/", "_"), path))
51
        return ret
52
53
54
    def poll(self):
55
        """Poll for disk space usage.
56
57
        :rtype: ResultSet
58
        """
59
        result = plumd.Result("diskusage")
60
        fs_types = ["xfs", "ext4"]
61
        mpoints = self.proc_mounts()
62
        for dev, mpoint in mpoints:
63
            mp_stat = os.statvfs(mpoint)
64
            free = (mp_stat.f_bavail * mp_stat.f_frsize)
65
            total = (mp_stat.f_blocks * mp_stat.f_frsize)
66
            used = (mp_stat.f_blocks - mp_stat.f_bfree) * mp_stat.f_frsize
67
            total_inodes = mp_stat.f_files
68
            free_inodes = mp_stat.f_favail
69
            used_inodes = total_inodes - free_inodes
70
            result.add(plumd.Int("{0}.free".format(dev), free))
71
            result.add(plumd.Int("{0}.total".format(dev), total))
72
            result.add(plumd.Int("{0}.used".format(dev), used))
73
            result.add(plumd.Int("{0}.total_inodes".format(dev), total_inodes))
74
            result.add(plumd.Int("{0}.free_inodes".format(dev), free_inodes))
75
            result.add(plumd.Int("{0}.used_inodes".format(dev), used_inodes))
76
        return plumd.ResultSet([result])
77