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

Netstat.check()   C

Complexity

Conditions 7

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
c 1
b 0
f 0
dl 0
loc 30
rs 5.5
1
# -*- coding: utf-8 -*-
2
3
__author__ = 'Kenny Freeman'
4
__email__ = '[email protected]'
5
__license__ = "ISCL"
6
__docformat__ = 'reStructuredText'
7
8
import time
9
from collections import deque
10
11
import plumd
12
import plumd.plugins
13
from plumd.calc import Differential
14
from plumd.util import get_file_list, Filter
15
16
17
class Netstat(plumd.plugins.Reader):
18
    """Plugin to measure various kernel metrics from /proc."""
19
    defaults = {
20
        'poll.interval': 10,
21
        'proc_path': '/proc',
22
        'skip_proc_net_netstat': ["TcpExt.ArpFilter","TcpExt.BusyPollRxPackets",
23
            "TcpExt.DelayedACKLocked","TcpExt.DelayedACKLost",
24
            "TcpExt.DelayedACKs","TcpExt.EmbryonicRsts",
25
            "TcpExt.IPReversePathFilter","TcpExt.LockDroppedIcmps",
26
            "TcpExt.OfoPruned","TcpExt.OutOfWindowIcmps",
27
            "TcpExt.PAWSActive","TcpExt.PAWSEstab", "TcpExt.PAWSPassive",
28
            "TcpExt.PruneCalled","TcpExt.RcvPruned",
29
            "TcpExt.TCPACKSkippedChallenge","TcpExt.TCPACKSkippedFinWait2",
30
            "TcpExt.TCPACKSkippedPAWS","TcpExt.TCPACKSkippedSeq",
31
            "TcpExt.TCPACKSkippedSynRecv","TcpExt.TCPACKSkippedTimeWait",
32
            "TcpExt.TCPAutoCorking","TcpExt.TCPChallengeACK",
33
            "TcpExt.TCPDirectCopyFromBacklog","TcpExt.TCPDirectCopyFromPrequeue",
34
            "TcpExt.TCPDSACKIgnoredNoUndo","TcpExt.TCPDSACKIgnoredOld",
35
            "TcpExt.TCPDSACKOfoRecv","TcpExt.TCPDSACKOfoSent",
36
            "TcpExt.TCPDSACKOldSent","TcpExt.TCPDSACKRecv","TcpExt.TCPDSACKUndo",
37
            "TcpExt.TCPFACKReorder","TcpExt.TCPFromZeroWindowAdv",
38
            "TcpExt.TCPFullUndo","TcpExt.TCPHPAcks","TcpExt.TCPHPHits",
39
            "TcpExt.TCPHPHitsToUser","TcpExt.TCPHystartDelayCwnd",
40
            "TcpExt.TCPHystartDelayDetect","TcpExt.TCPHystartTrainCwnd",
41
            "TcpExt.TCPHystartTrainDetect","TcpExt.TCPLossFailures",
42
            "TcpExt.TCPLossProbeRecovery","TcpExt.TCPLossProbes",
43
            "TcpExt.TCPLossUndo","TcpExt.TCPLostRetransmit",
44
            "TcpExt.TCPMD5NotFound","TcpExt.TCPMD5Unexpected",
45
            "TcpExt.TCPMTUPFail","TcpExt.TCPMTUPSuccess","TcpExt.TCPOFODrop",
46
            "TcpExt.TCPOFOMerge","TcpExt.TCPOFOQueue","TcpExt.TCPOrigDataSent",
47
            "TcpExt.TCPPartialUndo","TcpExt.TCPPrequeued",
48
            "TcpExt.TCPPrequeueDropped","TcpExt.TCPPureAcks",
49
            "TcpExt.TCPRcvCoalesce","TcpExt.TCPRcvCollapsed",
50
            "TcpExt.TCPRenoFailures","TcpExt.TCPRenoRecovery",
51
            "TcpExt.TCPRenoRecoveryFail","TcpExt.TCPRenoReorder",
52
            "TcpExt.TCPRetransFail","TcpExt.TCPSACKDiscard",
53
            "TcpExt.TCPSackFailures","TcpExt.TCPSackMerged",
54
            "TcpExt.TCPSackRecovery","TcpExt.TCPSackRecoveryFail",
55
            "TcpExt.TCPSACKReneging","TcpExt.TCPSACKReorder",
56
            "TcpExt.TCPSackShifted","TcpExt.TCPSackShiftFallback",
57
            "TcpExt.TCPSchedulerFailed","TcpExt.TCPSpuriousRTOs",
58
            "TcpExt.TCPSpuriousRtxHostQueues","TcpExt.TCPSYNChallenge",
59
            "TcpExt.TCPToZeroWindowAdv","TcpExt.TCPTSReorder",
60
            "TcpExt.TCPWantZeroWindowAdv","TcpExt.TCPWinProbe",
61
            "TcpExt.TCPKeepAlive", "TcpExt.TCPFastOpenCookieReqd",
62
            "IpExt.InNoECTPkts", "IpExt.InCEPkts"]
63
    }
64
65
    def __init__(self, log, config):
66
        """Plugin to measure various kernel metrics from /proc.
67
68
        :param log: A logger
69
        :type log: logging.RootLogger
70
        :param config: a plumd.config.Conf configuration helper instance.
71
        :type config: plumd.config.Conf
72
        """
73
        super(Netstat, self).__init__(log, config)
74
        config.defaults(Netstat.defaults)
75
        self.calc = Differential()
76
        self.proc_file = "{0}/net/netstat".format(config.get('proc_path'))
77
        self.filter = Filter()
78
79
80
    def poll(self):
81
        """Poll for kernel metrics under proc file net/netstat.
82
83
        :rtype: ResultSet
84
        """
85
        return plumd.ResultSet(self.check())
86
87
88
    def check(self):
89
        """Return detailed network statitistics proc file net/netstat.
90
91
        Note: add entries to the configuration value 'skip_proc_net_netstat' to
92
        skip metric names (eg. 'TcpExt.TCPMTUPSuccess'). Defaults should be
93
        reasonable however.
94
95
        Note: ECT1Pkts and ECT0Pkts relate to ECT congestion notifications.
96
97
        :rtype: plumd.Result
98
        """
99
        skip = self.config.get('skip_proc_net_netstat')
100
        result = plumd.Result("netstat")
101
        dat = {}
102
        # read and process - dat is a list of lines from fname
103
        dat = get_file_list(self.proc_file)
104
        ts = time.time()
105
        while len(dat) > 1:
106
            headers = deque(dat.popleft().split())
107
            if len(dat) < 1 or len(headers) < 1:
108
                break
109
            mvals = deque([ int(i) for i in dat.popleft().split()[1:] ])
110
            mstr = self.filter.process(headers.popleft()) # eg. TcpExt:
111
            for mname in headers:
112
                metric = "{0}.{1}".format(mstr, mname)
113
                if metric in skip: # todo: make this fast
114
                    continue
115
                dval = self.calc.per_second(metric, mvals.popleft(), ts)
116
                result.add(plumd.Int(metric, dval))
117
        return [result]
118