|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# |
|
3
|
|
|
# This file is part of Glances. |
|
4
|
|
|
# |
|
5
|
|
|
# Copyright (C) 2015 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
|
|
|
"""Manage the folder list.""" |
|
21
|
|
|
|
|
22
|
|
|
import os |
|
23
|
|
|
|
|
24
|
|
|
from glances.compat import range |
|
25
|
|
|
from glances.logger import logger |
|
26
|
|
|
|
|
27
|
|
|
# Use the built-in version of scandir/walk if possible, otherwise |
|
28
|
|
|
# use the scandir module version |
|
29
|
|
|
scandir_tag = True |
|
30
|
|
|
try: |
|
31
|
|
|
# For Python 3.5 or higher |
|
32
|
|
|
from os import scandir |
|
33
|
|
|
except ImportError: |
|
34
|
|
|
# For others... |
|
35
|
|
|
try: |
|
36
|
|
|
from scandir import scandir |
|
37
|
|
|
except ImportError: |
|
38
|
|
|
scandir_tag = False |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
class FolderList(object): |
|
42
|
|
|
|
|
43
|
|
|
"""This class describes the optional monitored folder list. |
|
44
|
|
|
|
|
45
|
|
|
The folder list is a list of 'important' folder to monitor. |
|
46
|
|
|
|
|
47
|
|
|
The list (Python list) is composed of items (Python dict). |
|
48
|
|
|
An item is defined (dict keys): |
|
49
|
|
|
* path: Path to the folder |
|
50
|
|
|
* careful: optional careful threshold (in MB) |
|
51
|
|
|
* warning: optional warning threshold (in MB) |
|
52
|
|
|
* critical: optional critical threshold (in MB) |
|
53
|
|
|
""" |
|
54
|
|
|
|
|
55
|
|
|
# Maximum number of items in the list |
|
56
|
|
|
__folder_list_max_size = 10 |
|
57
|
|
|
# The folder list |
|
58
|
|
|
__folder_list = [] |
|
59
|
|
|
|
|
60
|
|
|
def __init__(self, config): |
|
61
|
|
|
"""Init the folder list from the configuration file, if it exists.""" |
|
62
|
|
|
self.config = config |
|
63
|
|
|
|
|
64
|
|
|
if self.config is not None and self.config.has_section('folders'): |
|
65
|
|
|
if scandir_tag: |
|
66
|
|
|
# Process monitoring list |
|
67
|
|
|
logger.debug("Folder list configuration detected") |
|
68
|
|
|
self.__set_folder_list('folders') |
|
69
|
|
|
else: |
|
70
|
|
|
logger.error('Scandir not found. Please use Python 3.5+ or install the scandir lib') |
|
71
|
|
|
else: |
|
72
|
|
|
self.__folder_list = [] |
|
73
|
|
|
|
|
74
|
|
|
def __set_folder_list(self, section): |
|
75
|
|
|
"""Init the monitored folder list. |
|
76
|
|
|
|
|
77
|
|
|
The list is defined in the Glances configuration file. |
|
78
|
|
|
""" |
|
79
|
|
|
for l in range(1, self.__folder_list_max_size + 1): |
|
80
|
|
|
value = {} |
|
81
|
|
|
key = 'folder_' + str(l) + '_' |
|
82
|
|
|
|
|
83
|
|
|
# Path is mandatory |
|
84
|
|
|
try: |
|
85
|
|
|
value['path'] = self.config.get_value(section, key + 'path') |
|
86
|
|
|
except Exception as e: |
|
87
|
|
|
logger.error("Cannot read folder list: {0}".format(e)) |
|
88
|
|
|
continue |
|
89
|
|
|
if value['path'] is None: |
|
90
|
|
|
continue |
|
91
|
|
|
|
|
92
|
|
|
# Optional conf keys |
|
93
|
|
|
for i in ['careful', 'warning', 'critical']: |
|
94
|
|
|
try: |
|
95
|
|
|
value[i] = self.config.get_value(section, key + i) |
|
96
|
|
|
except: |
|
97
|
|
|
value[i] = None |
|
98
|
|
|
logger.debug("No {0} threshold for folder {1}".format(i, value["path"])) |
|
99
|
|
|
|
|
100
|
|
|
# Add the item to the list |
|
101
|
|
|
self.__folder_list.append(value) |
|
102
|
|
|
|
|
103
|
|
|
def __str__(self): |
|
104
|
|
|
return str(self.__folder_list) |
|
105
|
|
|
|
|
106
|
|
|
def __repr__(self): |
|
107
|
|
|
return self.__folder_list |
|
108
|
|
|
|
|
109
|
|
|
def __getitem__(self, item): |
|
110
|
|
|
return self.__folder_list[item] |
|
111
|
|
|
|
|
112
|
|
|
def __len__(self): |
|
113
|
|
|
return len(self.__folder_list) |
|
114
|
|
|
|
|
115
|
|
|
def __get__(self, item, key): |
|
116
|
|
|
"""Meta function to return key value of item. |
|
117
|
|
|
|
|
118
|
|
|
Return None if not defined or item > len(list) |
|
119
|
|
|
""" |
|
120
|
|
|
if item < len(self.__folder_list): |
|
121
|
|
|
try: |
|
122
|
|
|
return self.__folder_list[item][key] |
|
123
|
|
|
except Exception: |
|
124
|
|
|
return None |
|
125
|
|
|
else: |
|
126
|
|
|
return None |
|
127
|
|
|
|
|
128
|
|
|
def __folder_size(self, path): |
|
129
|
|
|
"""Return the size of the directory given by path |
|
130
|
|
|
|
|
131
|
|
|
path: <string>""" |
|
132
|
|
|
|
|
133
|
|
|
ret = 0 |
|
134
|
|
|
for f in scandir(path): |
|
135
|
|
|
if f.is_dir() and (f.name != '.' or f.name != '..'): |
|
136
|
|
|
ret += self.__folder_size(os.path.join(path, f.name)) |
|
137
|
|
|
else: |
|
138
|
|
|
try: |
|
139
|
|
|
ret += f.stat().st_size |
|
140
|
|
|
except OSError: |
|
141
|
|
|
pass |
|
142
|
|
|
|
|
143
|
|
|
return ret |
|
144
|
|
|
|
|
145
|
|
|
def update(self): |
|
146
|
|
|
"""Update the command result attributed.""" |
|
147
|
|
|
# Only continue if monitor list is not empty |
|
148
|
|
|
if len(self.__folder_list) == 0: |
|
149
|
|
|
return self.__folder_list |
|
150
|
|
|
|
|
151
|
|
|
# Iter upon the folder list |
|
152
|
|
|
for i in range(len(self.get())): |
|
153
|
|
|
# Update folder size |
|
154
|
|
|
try: |
|
155
|
|
|
self.__folder_list[i]['size'] = self.__folder_size(self.path(i)) |
|
156
|
|
|
except Exception as e: |
|
157
|
|
|
self.__folder_list[i]['size'] = None |
|
158
|
|
|
logger.debug('Can get folder size ({0}). Error: {1}'.format(self.path(i), e)) |
|
159
|
|
|
|
|
160
|
|
|
return self.__folder_list |
|
161
|
|
|
|
|
162
|
|
|
def get(self): |
|
163
|
|
|
"""Return the monitored list (list of dict).""" |
|
164
|
|
|
return self.__folder_list |
|
165
|
|
|
|
|
166
|
|
|
def set(self, newlist): |
|
167
|
|
|
"""Set the monitored list (list of dict).""" |
|
168
|
|
|
self.__folder_list = newlist |
|
169
|
|
|
|
|
170
|
|
|
def getAll(self): |
|
171
|
|
|
# Deprecated: use get() |
|
172
|
|
|
return self.get() |
|
173
|
|
|
|
|
174
|
|
|
def setAll(self, newlist): |
|
175
|
|
|
# Deprecated: use set() |
|
176
|
|
|
self.set(newlist) |
|
177
|
|
|
|
|
178
|
|
|
def path(self, item): |
|
179
|
|
|
"""Return the path of the item number (item).""" |
|
180
|
|
|
return self.__get__(item, "path") |
|
181
|
|
|
|
|
182
|
|
|
def careful(self, item): |
|
183
|
|
|
"""Return the careful threshold of the item number (item).""" |
|
184
|
|
|
return self.__get__(item, "careful") |
|
185
|
|
|
|
|
186
|
|
|
def warning(self, item): |
|
187
|
|
|
"""Return the warning threshold of the item number (item).""" |
|
188
|
|
|
return self.__get__(item, "warning") |
|
189
|
|
|
|
|
190
|
|
|
def critical(self, item): |
|
191
|
|
|
"""Return the critical threshold of the item number (item).""" |
|
192
|
|
|
return self.__get__(item, "critical") |
|
193
|
|
|
|