Code Duplication    Length = 54-55 lines in 2 locations

plumd/plugins/readers/linux/proc/netstat.py 1 location

@@ 65-119 (lines=55) @@
62
        return plumd.ResultSet(self.check())
63
64
65
    def check(self):
66
        """Return detailed network statitistics proc file net/netstat.
67
68
        Note: add entries to the configuration value 'proc_netstat_record'
69
        to record them. See /proc/net/netstat for a list of available metrics.
70
71
        Note: ECT1Pkts and ECT0Pkts relate to ECT congestion notifications.
72
73
        :rtype: plumd.Result
74
        """
75
        # what metrics do we want to record?
76
        record = self.config.get('proc_netstat_record')
77
        result = plumd.Result("netstat")
78
79
        # read the proc file
80
        dat = None
81
        with open(self.proc_file, 'r') as f:
82
            dat = f.read().strip().split("\n")
83
84
        # timestamp for Differential calculations
85
        ts = time.time()
86
87
        # should always have a header row and value row, ie. divisible by 2
88
        if len(dat) % 2 != 0:
89
            self.log.error("netstat: cannot parse {0}".format(self.proc_file))
90
            return result
91
92
        # split values into lists
93
        dlist = deque()
94
        for entry in dat:
95
            dlist.append(entry.split())
96
97
        # put lists into key: value dict
98
        metrics = {}
99
        while dlist:
100
            headers = dlist.popleft()
101
            values = dlist.popleft()
102
            # { 'IpExt': {'InNoRoutes': 0, ...} } - [:-1] on IpExt: removes :
103
            metrics[headers[0][:-1]] = dict(zip(headers, values))
104
105
        # now, we have a list of items to record, just need to record them
106
        for ext, mnames in record.items():
107
            if ext not in metrics:
108
                self.log.warn("netstat: unknown extension: {0}".format(ext))
109
                continue
110
            values = metrics[ext]
111
            for mname in mnames:
112
                if mname in values:
113
                    mstr = "{0}.{1}".format(ext, mname)
114
                    mval = self.calc.per_second(mstr, int(values[mname]), ts)
115
                    result.add(plumd.Int(mstr, mval))
116
                else:
117
                    self.log.warn("netstat: unknown metric {0}".format(mname))
118
119
        return [result]
120

plumd/plugins/readers/linux/proc/netsnmp.py 1 location

@@ 68-121 (lines=54) @@
65
        return plumd.ResultSet(self.check())
66
67
68
    def check(self):
69
        """Return network protocol metrics from proc file net/snmp.
70
71
        Add entries to the configuration value 'skip_proc_net_snmp' to skip
72
        metrics.
73
74
        Add entries to the configuration value 'net_snmp_items' to match the
75
        format/order of the proc file net/snmp entries on the system.
76
77
        :rtype: plumd.Result
78
        """
79
        record = self.config.get('proc_netsnmp_record')
80
        result = plumd.Result("net_snmp")
81
82
        # read the proc file
83
        dat = None
84
        with open(self.proc_file, 'r') as f:
85
            dat = f.read().strip().split("\n")
86
87
        # timestamp for Differential calculations
88
        ts = time.time()
89
90
        # should always have a header row and value row, ie. divisible by 2
91
        if len(dat) % 2 != 0:
92
            self.log.error("netsnmp: cannot parse {0}".format(self.proc_file))
93
            return result
94
95
        # split values into lists
96
        dlist = deque()
97
        for entry in dat:
98
            dlist.append(entry.split())
99
100
        # put lists into key: value dict
101
        metrics = {}
102
        while dlist:
103
            headers = dlist.popleft()
104
            values = dlist.popleft()
105
            # { 'IpExt': {'InNoRoutes': 0, ...} } - [:-1] on IpExt: removes :
106
            metrics[headers[0][:-1]] = dict(zip(headers, values))
107
108
        # now, we have a list of items to record, just need to record them
109
        for proto, mnames in record.items():
110
            if proto not in metrics:
111
                self.log.warn("netsnmp: unknown protocol: {0}".format(proto))
112
                continue
113
            values = metrics[proto]
114
            for mname in mnames:
115
                if mname in values:
116
                    mstr = "{0}.{1}".format(proto, mname)
117
                    mval = self.calc.per_second(mstr, int(values[mname]), ts)
118
                    result.add(plumd.Int(mstr, mval))
119
                else:
120
                    self.log.warn("netstat: unknown metric {0}".format(mname))
121
        return [result]
122