Conditions | 12 |
Total Lines | 66 |
Lines | 65 |
Ratio | 98.48 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like NetSnmp.check() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | # -*- coding: utf-8 -*- |
||
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 |