@@ 67-132 (lines=66) @@ | ||
64 | :rtype: ResultSet |
|
65 | """ |
|
66 | return plumd.ResultSet(self.check()) |
|
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 | result = plumd.Result("netsnmp") |
|
80 | ||
81 | # read the proc file |
|
82 | dat = [] |
|
83 | with open(self.proc_file, 'r') as pfd: |
|
84 | dat = pfd.read().strip().split("\n") |
|
85 | ||
86 | # timestamp for Differential calculations |
|
87 | times = time.time() |
|
88 | ||
89 | # split values into lists |
|
90 | dlist = deque([entry.split() for entry in dat]) |
|
91 | ||
92 | # put lists into key: value dict |
|
93 | metrics = {} |
|
94 | while dlist: |
|
95 | headers = dlist.popleft() |
|
96 | values = dlist.popleft() |
|
97 | # { 'IpExt': {'InNoRoutes': 0, ...} } - [:-1] on IpExt: removes : |
|
98 | metrics[headers[0][:-1]] = dict(zip(headers, values)) |
|
99 | ||
100 | # record gauges |
|
101 | for proto, mnames in self.gauges.items(): |
|
102 | if proto not in metrics: |
|
103 | self.log.warn("netsnmp: unknown protocol: {0}".format(proto)) |
|
104 | del self.gauges[proto] |
|
105 | continue |
|
106 | values = metrics[proto] |
|
107 | for mname in mnames: |
|
108 | if mname in values: |
|
109 | mstr = "{0}.{1}".format(proto, mname) |
|
110 | result.add(plumd.Int(mstr, values[mname])) |
|
111 | else: |
|
112 | self.log.warn("netstat: unknown metric {0}".format(mname)) |
|
113 | self.gauges[proto].remove(mname) |
|
114 | continue |
|
115 | ||
116 | # record rates |
|
117 | for proto, mnames in self.rates.items(): |
|
118 | if proto not in metrics: |
|
119 | self.log.warn("netsnmp: unknown protocol: {0}".format(proto)) |
|
120 | del self.gauges[proto] |
|
121 | continue |
|
122 | values = metrics[proto] |
|
123 | for mname in mnames: |
|
124 | if mname in values: |
|
125 | mstr = "{0}.{1}".format(proto, mname) |
|
126 | mval = self.calc.per_second(mstr, int(values[mname]), times) |
|
127 | result.add(plumd.Int(mstr, mval)) |
|
128 | else: |
|
129 | self.log.warn("netstat: unknown metric {0}".format(mname)) |
|
130 | self.rates[proto].remove(mname) |
|
131 | continue |
|
132 | ||
133 | return [result] |
|
134 |
@@ 65-125 (lines=61) @@ | ||
62 | :rtype: ResultSet |
|
63 | """ |
|
64 | return plumd.ResultSet(self.check()) |
|
65 | ||
66 | def check(self): |
|
67 | """Return detailed network statitistics proc file net/netstat. |
|
68 | ||
69 | Note: ECT1Pkts and ECT0Pkts relate to ECT congestion notifications. |
|
70 | ||
71 | :rtype: plumd.Result |
|
72 | """ |
|
73 | # what metrics do we want to record? |
|
74 | result = plumd.Result("netstat") |
|
75 | ||
76 | # read the proc file |
|
77 | dat = None |
|
78 | with open(self.proc_file, 'r') as f: |
|
79 | dat = f.read().strip().split("\n") |
|
80 | ||
81 | # timestamp for Differential calculations |
|
82 | ts = time.time() |
|
83 | ||
84 | # split values into lists |
|
85 | dlist = deque([entry.split() for entry in dat]) |
|
86 | ||
87 | # put lists into key: value dict |
|
88 | metrics = {} |
|
89 | while dlist: |
|
90 | headers = dlist.popleft() |
|
91 | values = dlist.popleft() |
|
92 | # { 'IpExt': {'InNoRoutes': 0, ...} } - [:-1] on IpExt: removes : |
|
93 | metrics[headers[0][:-1]] = dict(zip(headers, values)) |
|
94 | ||
95 | # record gauges |
|
96 | for ext, mnames in self.gauges.items(): |
|
97 | if ext not in metrics: |
|
98 | self.log.warn("netstat: unknown extension: {0}".format(ext)) |
|
99 | del self.gauges[ext] |
|
100 | continue |
|
101 | values = metrics[ext] |
|
102 | for mname in mnames: |
|
103 | if mname in values: |
|
104 | mstr = "{0}.{1}".format(ext, mname) |
|
105 | result.add(plumd.Int(mstr, values[mname])) |
|
106 | else: |
|
107 | self.log.warn("netstat: unknown metric {0}".format(mname)) |
|
108 | self.gauges[ext].remove(mname) |
|
109 | ||
110 | # record rates |
|
111 | for ext, mnames in self.rates.items(): |
|
112 | if ext not in metrics: |
|
113 | self.log.warn("netstat: unknown extension: {0}".format(ext)) |
|
114 | del self.rates[ext] |
|
115 | continue |
|
116 | values = metrics[ext] |
|
117 | for mname in mnames: |
|
118 | if mname in values: |
|
119 | mstr = "{0}.{1}".format(ext, mname) |
|
120 | mval = self.calc.per_second(mstr, float(values[mname]), ts) |
|
121 | result.add(plumd.Float(mstr, mval)) |
|
122 | else: |
|
123 | self.log.warn("netstat: unknown metric {0}".format(mname)) |
|
124 | self.rates[ext].remove(mname) |
|
125 | ||
126 | return [result] |
|
127 |