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 | """Folder plugin.""" |
||
21 | from __future__ import unicode_literals |
||
22 | |||
23 | import numbers |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
24 | |||
25 | from glances.compat import nativestr, n |
||
0 ignored issues
–
show
|
|||
26 | from glances.folder_list import FolderList as glancesFolderList |
||
0 ignored issues
–
show
|
|||
27 | from glances.plugins.glances_plugin import GlancesPlugin |
||
0 ignored issues
–
show
|
|||
28 | from glances.logger import logger |
||
0 ignored issues
–
show
|
|||
29 | |||
30 | |||
31 | class Plugin(GlancesPlugin): |
||
32 | """Glances folder plugin.""" |
||
33 | |||
34 | def __init__(self, args=None, config=None): |
||
35 | """Init the plugin.""" |
||
36 | super(Plugin, self).__init__(args=args, |
||
37 | config=config, |
||
38 | stats_init_value=[]) |
||
39 | self.args = args |
||
40 | self.config = config |
||
41 | |||
42 | # We want to display the stat in the curse interface |
||
43 | self.display_curse = True |
||
44 | |||
45 | # Init stats |
||
46 | self.glances_folders = glancesFolderList(config) |
||
47 | |||
48 | def get_key(self): |
||
49 | """Return the key of the list.""" |
||
50 | return 'path' |
||
51 | |||
52 | @GlancesPlugin._check_decorator |
||
53 | @GlancesPlugin._log_result_decorator |
||
54 | def update(self): |
||
55 | """Update the foldered list.""" |
||
56 | # Init new stats |
||
57 | stats = self.get_init_value() |
||
58 | |||
59 | if self.input_method == 'local': |
||
60 | # Folder list only available in a full Glances environment |
||
61 | # Check if the glances_folder instance is init |
||
62 | if self.glances_folders is None: |
||
63 | return self.stats |
||
64 | |||
65 | # Update the foldered list (result of command) |
||
66 | self.glances_folders.update() |
||
67 | |||
68 | # Put it on the stats var |
||
69 | stats = self.glances_folders.get() |
||
70 | else: |
||
71 | pass |
||
72 | |||
73 | # Update the stats |
||
74 | self.stats = stats |
||
75 | |||
76 | return self.stats |
||
77 | |||
78 | def get_alert(self, stat, header=""): |
||
0 ignored issues
–
show
|
|||
79 | """Manage limits of the folder list.""" |
||
80 | if not isinstance(stat['size'], numbers.Number): |
||
81 | ret = 'DEFAULT' |
||
82 | else: |
||
83 | ret = 'OK' |
||
84 | |||
85 | if stat['critical'] is not None and \ |
||
86 | stat['size'] > int(stat['critical']) * 1000000: |
||
87 | ret = 'CRITICAL' |
||
88 | elif stat['warning'] is not None and \ |
||
89 | stat['size'] > int(stat['warning']) * 1000000: |
||
90 | ret = 'WARNING' |
||
91 | elif stat['careful'] is not None and \ |
||
92 | stat['size'] > int(stat['careful']) * 1000000: |
||
93 | ret = 'CAREFUL' |
||
94 | |||
95 | # Get stat name |
||
96 | stat_name = self.get_stat_name(header=header) |
||
97 | |||
98 | # Manage threshold |
||
99 | self.manage_threshold(stat_name, ret) |
||
100 | |||
101 | # Manage action |
||
102 | self.manage_action(stat_name, |
||
103 | ret.lower(), |
||
104 | header, |
||
105 | stat[self.get_key()]) |
||
106 | |||
107 | return ret |
||
108 | |||
109 | def msg_curse(self, args=None, max_width=None): |
||
110 | """Return the dict to display in the curse interface.""" |
||
111 | # Init the return message |
||
112 | ret = [] |
||
113 | |||
114 | # Only process if stats exist and display plugin enable... |
||
115 | if not self.stats or self.is_disable(): |
||
116 | return ret |
||
117 | |||
118 | # Max size for the interface name |
||
119 | name_max_width = max_width - 7 |
||
120 | |||
121 | # Header |
||
122 | msg = '{:{width}}'.format('FOLDERS', |
||
123 | width=name_max_width) |
||
124 | ret.append(self.curse_add_line(msg, "TITLE")) |
||
125 | |||
126 | # Data |
||
127 | for i in self.stats: |
||
128 | ret.append(self.curse_new_line()) |
||
129 | if len(i['path']) > name_max_width: |
||
130 | # Cut path if it is too long |
||
131 | path = '_' + i['path'][-name_max_width + 1:] |
||
132 | else: |
||
133 | path = i['path'] |
||
134 | msg = '{:{width}}'.format(nativestr(path), |
||
135 | width=name_max_width) |
||
136 | ret.append(self.curse_add_line(msg)) |
||
137 | try: |
||
138 | msg = '{:>9}'.format(self.auto_unit(i['size'])) |
||
139 | except (TypeError, ValueError): |
||
140 | msg = '{:>9}'.format(i['size']) |
||
141 | ret.append(self.curse_add_line(msg, self.get_alert(i, |
||
142 | header='folder_' + i['indice']))) |
||
0 ignored issues
–
show
|
|||
143 | |||
144 | return ret |
||
145 |