Code Duplication    Length = 68-73 lines in 2 locations

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

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

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

@@ 68-135 (lines=68) @@
65
        return plumd.ResultSet(self.check())
66
67
68
    def check(self):
69
        """Return detailed network statitistics proc file net/netstat.
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
        result = plumd.Result("netstat")
77
78
        # read the proc file
79
        dat = None
80
        with open(self.proc_file, 'r') as f:
81
            dat = f.read().strip().split("\n")
82
83
        # timestamp for Differential calculations
84
        ts = time.time()
85
86
        # should always have a header row and value row, ie. divisible by 2
87
        if len(dat) % 2 != 0:
88
            self.log.error("netstat: cannot parse {0}".format(self.proc_file))
89
            return result
90
91
        # split values into lists
92
        dlist = deque()
93
        for entry in dat:
94
            dlist.append(entry.split())
95
96
        # put lists into key: value dict
97
        metrics = {}
98
        while dlist:
99
            headers = dlist.popleft()
100
            values = dlist.popleft()
101
            # { 'IpExt': {'InNoRoutes': 0, ...} } - [:-1] on IpExt: removes :
102
            metrics[headers[0][:-1]] = dict(zip(headers, values))
103
104
        # record gauges
105
        for ext, mnames in self.gauges.items():
106
            if ext not in metrics:
107
                self.log.warn("netstat: unknown extension: {0}".format(ext))
108
                del(self.gauges[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
                    result.add(plumd.Int(mstr, values[mname]))
115
                else:
116
                    self.log.warn("netstat: unknown metric {0}".format(mname))
117
                    del(self.gauges[ext][mname])
118
119
        # record rates
120
        for ext, mnames in self.rates.items():
121
            if ext not in metrics:
122
                self.log.warn("netstat: unknown extension: {0}".format(ext))
123
                del(self.rates[ext])
124
                continue
125
            values = metrics[ext]
126
            for mname in mnames:
127
                if mname in values:
128
                    mstr = "{0}.{1}".format(ext, mname)
129
                    mval = self.calc.per_second(mstr, int(values[mname]), ts)
130
                    result.add(plumd.Float(mstr, mval))
131
                else:
132
                    self.log.warn("netstat: unknown metric {0}".format(mname))
133
                    del(self.rates[ext][mname])
134
135
        return [result]
136