Completed
Branch master (7e8cc2)
by Kenny
03:21 queued 19s
created

LoadAverage.check()   A

Complexity

Conditions 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 14
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 plumd
9
import plumd.plugins
10
from plumd.util import get_file
11
12
13
class LoadAverage(plumd.plugins.Reader):
14
    """Plugin to measure various kernel metrics from /proc."""
15
    defaults = {
16
        'poll.interval': 10,
17
        'proc_path': '/proc'
18
    }
19
20
    def __init__(self, log, config):
21
        """Plugin to measure various kernel metrics from /proc.
22
23
        :param log: A logger
24
        :type log: logging.RootLogger
25
        :param config: a plumd.config.Conf configuration helper instance.
26
        :type config: plumd.config.Conf
27
        """
28
        super(LoadAverage, self).__init__(log, config)
29
        config.defaults(LoadAverage.defaults)
30
        self.proc_file = "{0}/loadavg".format(config.get('proc_path'))
31
32
33
    def poll(self):
34
        """Poll for kernel metrics under /proc.
35
36
        :rtype: ResultSet
37
        """
38
        return plumd.ResultSet(self.check())
39
40
41
    def check(self):
42
        """Return 1, 5 and 15 minute load averages from proc file loadavg.
43
44
        :rtype: plumd.Result
45
        """
46
        result = plumd.Result("loadavg")
47
        dat = []
48
        # read and process /proc/stat
49
        dat = get_file(self.proc_file).split()
50
        if len(dat) >= 3:
51
            result.add(plumd.Float("1", dat[0]))
52
            result.add(plumd.Float("5", dat[1]))
53
            result.add(plumd.Float("15", dat[2]))
54
        return [result]
55