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
|
|
|
# flake8: noqa |
21
|
|
|
# pylint: skip-file |
22
|
|
|
"""Python 2/3 compatibility shims.""" |
23
|
|
|
|
24
|
|
|
from __future__ import print_function, unicode_literals |
25
|
|
|
|
26
|
|
|
import operator |
27
|
|
|
import sys |
28
|
|
|
import unicodedata |
29
|
|
|
import types |
30
|
|
|
import subprocess |
31
|
|
|
import os |
32
|
|
|
|
33
|
|
|
from glances.logger import logger |
34
|
|
|
|
35
|
|
|
PY3 = sys.version_info[0] == 3 |
36
|
|
|
|
37
|
|
|
if PY3: |
38
|
|
|
import queue |
39
|
|
|
from configparser import ConfigParser, NoOptionError, NoSectionError |
40
|
|
|
from statistics import mean |
41
|
|
|
from xmlrpc.client import Fault, ProtocolError, ServerProxy, Transport, Server |
42
|
|
|
from xmlrpc.server import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer |
43
|
|
|
from urllib.request import urlopen |
44
|
|
|
from urllib.error import HTTPError, URLError |
45
|
|
|
from urllib.parse import urlparse |
46
|
|
|
|
47
|
|
|
input = input |
48
|
|
|
range = range |
49
|
|
|
map = map |
50
|
|
|
|
51
|
|
|
text_type = str |
52
|
|
|
binary_type = bytes |
53
|
|
|
bool_type = bool |
54
|
|
|
long = int |
55
|
|
|
|
56
|
|
|
viewkeys = operator.methodcaller('keys') |
57
|
|
|
viewvalues = operator.methodcaller('values') |
58
|
|
|
viewitems = operator.methodcaller('items') |
59
|
|
|
|
60
|
|
|
def printandflush(string): |
61
|
|
|
"""Print and flush (used by stdout* outputs modules)""" |
62
|
|
|
print(string, flush=True) |
63
|
|
|
|
64
|
|
|
def to_ascii(s): |
65
|
|
|
"""Convert the bytes string to a ASCII string |
66
|
|
|
Usefull to remove accent (diacritics)""" |
67
|
|
|
if isinstance(s, binary_type): |
68
|
|
|
return s.decode() |
69
|
|
|
return s.encode('ascii', 'ignore').decode() |
70
|
|
|
|
71
|
|
|
def listitems(d): |
72
|
|
|
return list(d.items()) |
73
|
|
|
|
74
|
|
|
def listkeys(d): |
75
|
|
|
return list(d.keys()) |
76
|
|
|
|
77
|
|
|
def listvalues(d): |
78
|
|
|
return list(d.values()) |
79
|
|
|
|
80
|
|
|
def iteritems(d): |
81
|
|
|
return iter(d.items()) |
82
|
|
|
|
83
|
|
|
def iterkeys(d): |
84
|
|
|
return iter(d.keys()) |
85
|
|
|
|
86
|
|
|
def itervalues(d): |
87
|
|
|
return iter(d.values()) |
88
|
|
|
|
89
|
|
|
def u(s, errors='replace'): |
90
|
|
|
if isinstance(s, text_type): |
91
|
|
|
return s |
92
|
|
|
return s.decode('utf-8', errors=errors) |
93
|
|
|
|
94
|
|
|
def b(s, errors='replace'): |
95
|
|
|
if isinstance(s, binary_type): |
96
|
|
|
return s |
97
|
|
|
return s.encode('utf-8', errors=errors) |
98
|
|
|
|
99
|
|
|
def n(s): |
100
|
|
|
'''Only in Python 2... |
101
|
|
|
from future.utils import bytes_to_native_str as n |
102
|
|
|
''' |
103
|
|
|
return s |
104
|
|
|
|
105
|
|
|
def nativestr(s, errors='replace'): |
106
|
|
|
if isinstance(s, text_type): |
107
|
|
|
return s |
108
|
|
|
elif isinstance(s, (int, float)): |
109
|
|
|
return s.__str__() |
110
|
|
|
else: |
111
|
|
|
return s.decode('utf-8', errors=errors) |
112
|
|
|
|
113
|
|
|
def system_exec(command): |
114
|
|
|
"""Execute a system command and return the resul as a str""" |
115
|
|
|
try: |
116
|
|
|
res = subprocess.run(command.split(' '), |
117
|
|
|
stdout=subprocess.PIPE).stdout.decode('utf-8') |
118
|
|
|
except Exception as e: |
119
|
|
|
logger.debug('Can not evaluate command {} ({})'.format(command, e)) |
120
|
|
|
res = '' |
121
|
|
|
return res.rstrip() |
122
|
|
|
|
123
|
|
|
else: |
124
|
|
|
from future.utils import bytes_to_native_str as n |
125
|
|
|
import Queue as queue |
126
|
|
|
from itertools import imap as map |
127
|
|
|
from ConfigParser import SafeConfigParser as ConfigParser, NoOptionError, NoSectionError |
128
|
|
|
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer |
129
|
|
|
from xmlrpclib import Fault, ProtocolError, ServerProxy, Transport, Server |
130
|
|
|
from urllib2 import urlopen, HTTPError, URLError |
131
|
|
|
from urlparse import urlparse |
132
|
|
|
|
133
|
|
|
input = raw_input |
|
|
|
|
134
|
|
|
range = xrange |
|
|
|
|
135
|
|
|
ConfigParser.read_file = ConfigParser.readfp |
136
|
|
|
|
137
|
|
|
text_type = unicode |
|
|
|
|
138
|
|
|
binary_type = str |
139
|
|
|
bool_type = types.BooleanType |
140
|
|
|
long = long |
|
|
|
|
141
|
|
|
|
142
|
|
|
viewkeys = operator.methodcaller('viewkeys') |
143
|
|
|
viewvalues = operator.methodcaller('viewvalues') |
144
|
|
|
viewitems = operator.methodcaller('viewitems') |
145
|
|
|
|
146
|
|
|
def printandflush(string): |
147
|
|
|
"""Print and flush (used by stdout* outputs modules)""" |
148
|
|
|
print(string) |
149
|
|
|
sys.stdout.flush() |
150
|
|
|
|
151
|
|
|
def mean(numbers): |
152
|
|
|
return float(sum(numbers)) / max(len(numbers), 1) |
153
|
|
|
|
154
|
|
|
def to_ascii(s): |
155
|
|
|
"""Convert the unicode 's' to a ASCII string |
156
|
|
|
Usefull to remove accent (diacritics)""" |
157
|
|
|
if isinstance(s, binary_type): |
158
|
|
|
return s |
159
|
|
|
return unicodedata.normalize('NFKD', s).encode('ascii', 'ignore') |
160
|
|
|
|
161
|
|
|
def listitems(d): |
162
|
|
|
return d.items() |
163
|
|
|
|
164
|
|
|
def listkeys(d): |
165
|
|
|
return d.keys() |
166
|
|
|
|
167
|
|
|
def listvalues(d): |
168
|
|
|
return d.values() |
169
|
|
|
|
170
|
|
|
def iteritems(d): |
171
|
|
|
return d.iteritems() |
172
|
|
|
|
173
|
|
|
def iterkeys(d): |
174
|
|
|
return d.iterkeys() |
175
|
|
|
|
176
|
|
|
def itervalues(d): |
177
|
|
|
return d.itervalues() |
178
|
|
|
|
179
|
|
|
def u(s, errors='replace'): |
180
|
|
|
if isinstance(s, text_type): |
181
|
|
|
return s.encode('utf-8', errors=errors) |
182
|
|
|
return s.decode('utf-8', errors=errors) |
183
|
|
|
|
184
|
|
|
def b(s, errors='replace'): |
185
|
|
|
if isinstance(s, binary_type): |
186
|
|
|
return s |
187
|
|
|
return s.encode('utf-8', errors=errors) |
188
|
|
|
|
189
|
|
|
def nativestr(s, errors='replace'): |
190
|
|
|
if isinstance(s, binary_type): |
191
|
|
|
return s |
192
|
|
|
elif isinstance(s, (int, float)): |
193
|
|
|
return s.__str__() |
194
|
|
|
else: |
195
|
|
|
return s.encode('utf-8', errors=errors) |
196
|
|
|
|
197
|
|
|
def system_exec(command): |
198
|
|
|
"""Execute a system command and return the resul as a str""" |
199
|
|
|
try: |
200
|
|
|
res = subprocess.check_output(command.split(' ')) |
201
|
|
|
except Exception as e: |
202
|
|
|
logger.debug('Can not execute command {} ({})'.format(command, e)) |
203
|
|
|
res = '' |
204
|
|
|
return res.rstrip() |
205
|
|
|
|
206
|
|
|
|
207
|
|
|
# Globals functions for both Python 2 and 3 |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
def subsample(data, sampling): |
211
|
|
|
"""Compute a simple mean subsampling. |
212
|
|
|
|
213
|
|
|
Data should be a list of numerical itervalues |
214
|
|
|
|
215
|
|
|
Return a subsampled list of sampling lenght |
216
|
|
|
""" |
217
|
|
|
if len(data) <= sampling: |
218
|
|
|
return data |
219
|
|
|
sampling_length = int(round(len(data) / float(sampling))) |
220
|
|
|
return [mean(data[s * sampling_length:(s + 1) * sampling_length]) for s in range(0, sampling)] |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
def time_serie_subsample(data, sampling): |
224
|
|
|
"""Compute a simple mean subsampling. |
225
|
|
|
|
226
|
|
|
Data should be a list of set (time, value) |
227
|
|
|
|
228
|
|
|
Return a subsampled list of sampling lenght |
229
|
|
|
""" |
230
|
|
|
if len(data) <= sampling: |
231
|
|
|
return data |
232
|
|
|
t = [t[0] for t in data] |
233
|
|
|
v = [t[1] for t in data] |
234
|
|
|
sampling_length = int(round(len(data) / float(sampling))) |
235
|
|
|
t_subsampled = [t[s * sampling_length:(s + 1) * sampling_length][0] for s in range(0, sampling)] |
236
|
|
|
v_subsampled = [mean(v[s * sampling_length:(s + 1) * sampling_length]) for s in range(0, sampling)] |
237
|
|
|
return list(zip(t_subsampled, v_subsampled)) |
238
|
|
|
|
239
|
|
|
|
240
|
|
|
def to_fahrenheit(celsius): |
241
|
|
|
"""Convert Celsius to Fahrenheit.""" |
242
|
|
|
return celsius * 1.8 + 32 |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
def is_admin(): |
246
|
|
|
""" |
247
|
|
|
https://stackoverflow.com/a/19719292 |
248
|
|
|
@return: True if the current user is an 'Admin' whatever that |
249
|
|
|
means (root on Unix), otherwise False. |
250
|
|
|
Warning: The inner function fails unless you have Windows XP SP2 or |
251
|
|
|
higher. The failure causes a traceback to be printed and this |
252
|
|
|
function to return False. |
253
|
|
|
""" |
254
|
|
|
|
255
|
|
|
if os.name == 'nt': |
256
|
|
|
import ctypes |
257
|
|
|
import traceback |
258
|
|
|
# WARNING: requires Windows XP SP2 or higher! |
259
|
|
|
try: |
260
|
|
|
return ctypes.windll.shell32.IsUserAnAdmin() |
261
|
|
|
except Exception as e: |
262
|
|
|
traceback.print_exc() |
263
|
|
|
return False |
264
|
|
|
else: |
265
|
|
|
# Check for root on Posix |
266
|
|
|
return os.getuid() == 0 |
267
|
|
|
|