1 | # -*- coding: utf-8 -*- |
||
2 | # |
||
3 | # This file is part of Glances. |
||
4 | # |
||
5 | # Copyright (C) 2019 Nicolargo <[email protected]> |
||
6 | # |
||
7 | # Glances is free software; you can redistribute it and/or modify |
||
8 | # it under the terms of the GNU Lesser General Public License as published by |
||
9 | # the Free Software Foundation, either version 3 of the License, or |
||
10 | # (at your option) any later version. |
||
11 | # |
||
12 | # Glances is distributed in the hope that it will be useful, |
||
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
15 | # GNU Lesser General Public License for more details. |
||
16 | # |
||
17 | # You should have received a copy of the GNU Lesser General Public License |
||
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
19 | |||
20 | """Per-CPU plugin.""" |
||
21 | |||
22 | from glances.logger import logger |
||
23 | from glances.cpu_percent import cpu_percent |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
24 | from glances.plugins.glances_plugin import GlancesPlugin |
||
0 ignored issues
–
show
|
|||
25 | |||
26 | # Define the history items list |
||
27 | items_history_list = [{'name': 'user', |
||
28 | 'description': 'User CPU usage', |
||
29 | 'y_unit': '%'}, |
||
30 | {'name': 'system', |
||
31 | 'description': 'System CPU usage', |
||
32 | 'y_unit': '%'}] |
||
33 | |||
34 | |||
35 | class Plugin(GlancesPlugin): |
||
36 | """Glances per-CPU plugin. |
||
37 | |||
38 | 'stats' is a list of dictionaries that contain the utilization percentages |
||
39 | for each CPU. |
||
40 | """ |
||
41 | |||
42 | def __init__(self, args=None, config=None): |
||
43 | """Init the plugin.""" |
||
44 | super(Plugin, self).__init__(args=args, |
||
45 | config=config, |
||
46 | items_history_list=items_history_list, |
||
47 | stats_init_value=[]) |
||
48 | |||
49 | # We want to display the stat in the curse interface |
||
50 | self.display_curse = True |
||
51 | |||
52 | def get_key(self): |
||
53 | """Return the key of the list.""" |
||
54 | return 'cpu_number' |
||
55 | |||
56 | @GlancesPlugin._check_decorator |
||
57 | @GlancesPlugin._log_result_decorator |
||
58 | def update(self): |
||
59 | """Update per-CPU stats using the input method.""" |
||
60 | # Init new stats |
||
61 | stats = self.get_init_value() |
||
62 | |||
63 | # Grab per-CPU stats using psutil's cpu_percent(percpu=True) and |
||
64 | # cpu_times_percent(percpu=True) methods |
||
65 | if self.input_method == 'local': |
||
66 | stats = cpu_percent.get(percpu=True) |
||
67 | else: |
||
68 | # Update stats using SNMP |
||
69 | pass |
||
70 | |||
71 | # Update the stats |
||
72 | self.stats = stats |
||
73 | |||
74 | return self.stats |
||
75 | |||
76 | def msg_curse(self, args=None, max_width=None): |
||
77 | """Return the dict to display in the curse interface.""" |
||
78 | # Init the return message |
||
79 | ret = [] |
||
80 | |||
81 | # Only process if stats exist... |
||
82 | if not self.stats or not self.args.percpu or self.is_disable(): |
||
83 | return ret |
||
84 | |||
85 | # Build the string message |
||
86 | if self.is_disable('quicklook'): |
||
87 | msg = '{:7}'.format('PER CPU') |
||
88 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
89 | |||
90 | # Per CPU stats displayed per line |
||
91 | for stat in ['user', 'system', 'idle', 'iowait', 'steal']: |
||
92 | if stat not in self.stats[0]: |
||
93 | continue |
||
94 | msg = '{:>7}'.format(stat) |
||
95 | ret.append(self.curse_add_line(msg)) |
||
96 | |||
97 | # Per CPU stats displayed per column |
||
98 | for cpu in self.stats: |
||
99 | ret.append(self.curse_new_line()) |
||
100 | if self.is_disable('quicklook'): |
||
101 | try: |
||
102 | msg = '{:6.1f}%'.format(cpu['total']) |
||
103 | except TypeError: |
||
104 | # TypeError: string indices must be integers (issue #1027) |
||
105 | msg = '{:>6}%'.format('?') |
||
106 | ret.append(self.curse_add_line(msg)) |
||
107 | for stat in ['user', 'system', 'idle', 'iowait', 'steal']: |
||
108 | if stat not in self.stats[0]: |
||
109 | continue |
||
110 | try: |
||
111 | msg = '{:6.1f}%'.format(cpu[stat]) |
||
112 | except TypeError: |
||
113 | msg = '{:>6}%'.format('?') |
||
114 | ret.append(self.curse_add_line(msg, |
||
115 | self.get_alert(cpu[stat], |
||
116 | header=stat))) |
||
117 | |||
118 | return ret |
||
119 |