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

LoadAverage.check()   A

Complexity

Conditions 3

Size

Total Lines 19

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 19
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 traceback
9
10
import plumd
11
import plumd.plugins
12
from plumd.calc import Differential
13
from plumd.util import get_file
14
15
16
class LoadAverage(plumd.plugins.Reader):
17
    """Plugin to measure various kernel metrics from /proc."""
18
    defaults = {
19
        'poll.interval': 10,
20
        'proc_path': '/proc'
21
    }
22
23
    def __init__(self, log, config):
24
        """Plugin to measure various kernel 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(LoadAverage, self).__init__(log, config)
32
        config.defaults(LoadAverage.defaults)
33
        self.proc_file = "{0}/loadavg".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 1, 5 and 15 minute load averages from proc file loadavg.
46
47
        :rtype: plumd.Result
48
        """
49
        result = plumd.Result("loadavg")
50
        dat = []
51
        # read and process /proc/stat
52
        try:
53
            dat = get_file(self.proc_file).split()
54
        except Exception as e:
55
            tb = traceback.format_exc()
56
            self.log.error("proc_loadavg: exception: {0} : {1}".format(e, tb))
57
            return result
58
        if len(dat) >= 3:
59
            result.add(plumd.Float("1", dat[0]))
60
            result.add(plumd.Float("5", dat[1]))
61
            result.add(plumd.Float("15", dat[2]))
62
        return [result]
63