@@ 65-123 (lines=59) @@ | ||
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 | try: |
|
83 | dat = f.read().strip().split("\n") |
|
84 | except Exception as e: |
|
85 | self.log.error("failed to read {0}".format(self.proc_file)) |
|
86 | return result |
|
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("netstat: 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 | # now, we have a list of items to record, just need to record them |
|
110 | for ext, mnames in record.items(): |
|
111 | if ext not in metrics: |
|
112 | self.log.warn("netstat: unknown extension: {0}".format(ext)) |
|
113 | continue |
|
114 | values = metrics[ext] |
|
115 | for mname in mnames: |
|
116 | if mname in values: |
|
117 | mstr = "{0}.{1}".format(ext, mname) |
|
118 | mval = self.calc.per_second(mstr, int(values[mname]), ts) |
|
119 | result.add(plumd.Int(mstr, mval)) |
|
120 | else: |
|
121 | self.log.warn("netstat: unknown metric {0}".format(mname)) |
|
122 | ||
123 | return [result] |
|
124 |
@@ 68-125 (lines=58) @@ | ||
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 | try: |
|
86 | dat = f.read().strip().split("\n") |
|
87 | except Exception as e: |
|
88 | self.log.error("failed to read {0}".format(self.proc_file)) |
|
89 | return result |
|
90 | ||
91 | # timestamp for Differential calculations |
|
92 | ts = time.time() |
|
93 | ||
94 | # should always have a header row and value row, ie. divisible by 2 |
|
95 | if len(dat) % 2 != 0: |
|
96 | self.log.error("netsnmp: cannot parse {0}".format(self.proc_file)) |
|
97 | return result |
|
98 | ||
99 | # split values into lists |
|
100 | dlist = deque() |
|
101 | for entry in dat: |
|
102 | dlist.append(entry.split()) |
|
103 | ||
104 | # put lists into key: value dict |
|
105 | metrics = {} |
|
106 | while dlist: |
|
107 | headers = dlist.popleft() |
|
108 | values = dlist.popleft() |
|
109 | # { 'IpExt': {'InNoRoutes': 0, ...} } - [:-1] on IpExt: removes : |
|
110 | metrics[headers[0][:-1]] = dict(zip(headers, values)) |
|
111 | ||
112 | # now, we have a list of items to record, just need to record them |
|
113 | for proto, mnames in record.items(): |
|
114 | if proto not in metrics: |
|
115 | self.log.warn("netsnmp: unknown protocol: {0}".format(proto)) |
|
116 | continue |
|
117 | values = metrics[proto] |
|
118 | for mname in mnames: |
|
119 | if mname in values: |
|
120 | mstr = "{0}.{1}".format(proto, mname) |
|
121 | mval = self.calc.per_second(mstr, int(values[mname]), ts) |
|
122 | result.add(plumd.Int(mstr, mval)) |
|
123 | else: |
|
124 | self.log.warn("netstat: unknown metric {0}".format(mname)) |
|
125 | return [result] |
|
126 |