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

NetDev   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 68
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A poll() 0 6 1
B check() 0 29 6
A __init__() 0 15 1
1
# -*- coding: utf-8 -*-
2
3
__author__ = 'Kenny Freeman'
4
__email__ = '[email protected]'
5
__license__ = "ISCL"
6
__docformat__ = 'reStructuredText'
7
8
import time
9
import traceback
10
11
import plumd
12
import plumd.plugins
13
from plumd.calc import Differential
14
from plumd.util import get_file_map, Filter
15
16
17
class NetDev(plumd.plugins.Reader):
18
    """Plugin to measure various kernel metrics from /proc."""
19
    defaults = {
20
        'poll.interval': 10,
21
        'proc_path': '/proc',
22
        'net_dev_re': "(virbr\d+)|(vnet\d+)", # not used atm
23
        'net_dev_devs': ["eth0", "eth1", "bond0", "bond1", "lo"],
24
        'net_dev_cols': ["rx_bytes", "rx_pkt", "rx_errs", "rx_drop",
25
                         "rx_fifo_errs", "rx_frame_errs", "rx_compressed",
26
                         "rx_mcast", "tx_bytes", "tx_pkt", "tx_errs", "tx_drop",
27
                         "tx_fifo_errs", "collissions", "carrier",
28
                         "tx_compressed"]
29
    }
30
31
    def __init__(self, log, config):
32
        """Plugin to measure network interface metrics from proc file net/dev.
33
34
        :param log: A logger
35
        :type log: logging.RootLogger
36
        :param config: a plumd.config.Conf configuration helper instance.
37
        :type config: plumd.config.Conf
38
        """
39
        super(NetDev, self).__init__(log, config)
40
        config.defaults(NetDev.defaults)
41
        self.proc_file = "{0}/net/dev".format(config.get('proc_path'))
42
        self.net_dev_cols = config.get('net_dev_cols')
43
        self.net_dev_devs = config.get('net_dev_devs')
44
        self.filter = Filter()
45
        self.calc = Differential()
46
47
48
    def poll(self):
49
        """Return network interface metrics from proc file net/dev.
50
51
        :rtype: ResultSet
52
        """
53
        return plumd.ResultSet(self.check())
54
55
56
    def check(self):
57
        """Return network interface metrics from proc file net/dev.
58
59
        :rtype: list
60
        """
61
        result = plumd.Result("net")
62
        dat = {}
63
        cols = self.net_dev_cols
64
        devs = self.net_dev_devs
65
        # read and process /proc/stat
66
        try:
67
            dat = get_file_map(self.proc_file, 0, 0)
68
        except Exception as e:
69
            tb = traceback.format_exc()
70
            self.log.error("proc_net_dev: exception: {0}: {1}".format(e, tb))
71
            return [result]
72
        ts = time.time()
73
        for key, val in dat.items():
74
            key = self.filter.process(key)
75
            if key not in devs:
76
                continue
77
            for mname in cols:
78
                if len(val) < 1:
79
                    break
80
                mval = int(val.popleft())
81
                mstr = "{0}.{1}".format(key, mname)
82
                dval = self.calc.per_second(mstr, mval, ts)
83
                result.add(plumd.Int(mstr, dval))
84
        return [result]
85