Total Complexity | 16 |
Total Lines | 103 |
Duplicated Lines | 40.78 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | # -*- coding: utf-8 -*- |
||
18 | class Stat(plumd.plugins.Reader): |
||
19 | """Class to read metrics from /proc/stat""" |
||
20 | defaults = { |
||
21 | 'cpu_metrics': ["user", "nice", "system", "idle", "iowait", "irq", |
||
22 | "softirq","steal", "guest", "guest_nice"], |
||
23 | 'per_cpu': False, |
||
24 | } |
||
25 | |||
26 | def __init__(self, log, config): |
||
27 | """Plugin to measure various kernel metrics from /proc/stat |
||
28 | |||
29 | :param log: A logger |
||
30 | :type log: logging.RootLogger |
||
31 | :param config: a plumd.config.Conf configuration helper instance. |
||
32 | :type config: plumd.config.Conf |
||
33 | """ |
||
34 | super(Stat, self).__init__(log, config) |
||
35 | config.defaults(Stat.defaults) |
||
36 | self.calc = Differential() |
||
37 | self.proc_file = "{0}/stat".format(config.get('proc_path')) |
||
38 | self.per_cpu = config.get('per_cpu') |
||
39 | self.cpu_metrics = config.get('cpu_metrics') |
||
40 | |||
41 | |||
42 | def poll(self): |
||
43 | """Return cpu utilization and process metrics from proc file stat. |
||
44 | |||
45 | :rtype: plumd.ResultSet |
||
46 | """ |
||
47 | return plumd.ResultSet(self.check()) |
||
48 | |||
49 | |||
50 | def check(self): |
||
51 | """Return cpu utilization and process metrics from proc file stat. |
||
52 | |||
53 | :rtype: collections.deque |
||
54 | """ |
||
55 | per_cpu = self.per_cpu |
||
56 | results = deque() |
||
57 | result = plumd.Result("stat") |
||
58 | dat = get_file_map(self.proc_file, 0, 0) |
||
59 | ts = time.time() |
||
60 | # parse |
||
61 | for key, val in dat.items(): |
||
62 | # cpu and btime are the only special metrics |
||
63 | if key == "btime": |
||
64 | result.add(plumd.Int(key, val[0])) |
||
65 | elif key == "cpu": |
||
66 | results.append(self.proc_stat_cpu_percent(key, val, ts)) |
||
67 | elif per_cpu and key.startswith("cpu"): |
||
68 | results.append(self.proc_stat_cpu_percent(key, val, ts)) |
||
69 | else: |
||
70 | # val will be a list, take the first item from it |
||
71 | mval = self.calc.per_second(key, float(val[0]), ts) |
||
72 | result.add(plumd.Int(key, mval)) |
||
73 | results.append(result) |
||
74 | return results |
||
75 | |||
76 | |||
77 | View Code Duplication | def proc_stat_cpu_percent(self, key, val, ts): |
|
1 ignored issue
–
show
|
|||
78 | """Return cpu utilization metrics in percentage. |
||
79 | |||
80 | :param key: The metric name (eg. cpu, cpu0, cpu1, etc) |
||
81 | :type key: str |
||
82 | :param val: A deque populated with the metric values from stat |
||
83 | :type val: deque |
||
84 | :rtype: list |
||
85 | """ |
||
86 | result = plumd.Result("cpu") |
||
87 | total = sum([ float(i) for i in val]) |
||
88 | cpu = self.config.get('cpu_metrics') |
||
89 | for map_val in cpu: |
||
90 | if len(val) < 1: |
||
91 | break |
||
92 | metric_val = float(val.popleft()) |
||
93 | mstr = "{0}_{1}".format(key, map_val) |
||
94 | percent_val = metric_val / total * 100.00 |
||
95 | result.add(plumd.Float(mstr, percent_val)) |
||
96 | return result |
||
97 | |||
98 | |||
99 | View Code Duplication | def proc_stat_cpu(self, key, val, ts): |
|
1 ignored issue
–
show
|
|||
100 | """Return cpu utilization metrics in USER_HZ or Jiffies |
||
101 | (most likely units of 100Hz intervals ie. 100ms intervals). |
||
102 | |||
103 | :param key: The metric name (eg. cpu, cpu0, cpu1, etc) |
||
104 | :type key: str |
||
105 | :param val: A deque populated with the metric values from stat |
||
106 | :type val: deque |
||
107 | :rtype: list |
||
108 | """ |
||
109 | result = plumd.Result("cpus") |
||
110 | total = sum([ float(i) for i in val]) |
||
111 | cpu = self.config.get('cpu_metrics') |
||
112 | for map_val in cpu: |
||
113 | if len(val) < 1: |
||
114 | break |
||
115 | metric_val = float(val.popleft()) |
||
116 | mstr = "{0}_{1}".format(key, map_val) |
||
117 | percent_val = float(metric_val / total) * 100.00 |
||
118 | mval = self.calc.per_second(key, percent_val, ts) |
||
119 | result.add(plumd.Float(mstr, mval)) |
||
120 | return result |
||
121 |