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
|
|
|
|
32
|
|
|
from glances.logger import logger |
33
|
|
|
|
34
|
|
|
PY3 = sys.version_info[0] == 3 |
35
|
|
|
|
36
|
|
|
if PY3: |
37
|
|
|
import queue |
38
|
|
|
from configparser import ConfigParser, NoOptionError, NoSectionError |
39
|
|
|
from statistics import mean |
40
|
|
|
from xmlrpc.client import Fault, ProtocolError, ServerProxy, Transport, Server |
41
|
|
|
from xmlrpc.server import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer |
42
|
|
|
from urllib.request import urlopen |
43
|
|
|
from urllib.error import HTTPError, URLError |
44
|
|
|
from urllib.parse import urlparse |
45
|
|
|
|
46
|
|
|
input = input |
47
|
|
|
range = range |
48
|
|
|
map = map |
49
|
|
|
|
50
|
|
|
text_type = str |
51
|
|
|
binary_type = bytes |
52
|
|
|
bool_type = bool |
53
|
|
|
long = int |
54
|
|
|
|
55
|
|
|
viewkeys = operator.methodcaller('keys') |
56
|
|
|
viewvalues = operator.methodcaller('values') |
57
|
|
|
viewitems = operator.methodcaller('items') |
58
|
|
|
|
59
|
|
|
def printandflush(string): |
60
|
|
|
"""Print and flush (used by stdout* outputs modules)""" |
61
|
|
|
print(string, flush=True) |
62
|
|
|
|
63
|
|
|
def to_ascii(s): |
64
|
|
|
"""Convert the bytes string to a ASCII string |
65
|
|
|
Usefull to remove accent (diacritics)""" |
66
|
|
|
if isinstance(s, binary_type): |
67
|
|
|
return s.decode() |
68
|
|
|
return s.encode('ascii', 'ignore').decode() |
69
|
|
|
|
70
|
|
|
def listitems(d): |
71
|
|
|
return list(d.items()) |
72
|
|
|
|
73
|
|
|
def listkeys(d): |
74
|
|
|
return list(d.keys()) |
75
|
|
|
|
76
|
|
|
def listvalues(d): |
77
|
|
|
return list(d.values()) |
78
|
|
|
|
79
|
|
|
def iteritems(d): |
80
|
|
|
return iter(d.items()) |
81
|
|
|
|
82
|
|
|
def iterkeys(d): |
83
|
|
|
return iter(d.keys()) |
84
|
|
|
|
85
|
|
|
def itervalues(d): |
86
|
|
|
return iter(d.values()) |
87
|
|
|
|
88
|
|
|
def u(s, errors='replace'): |
89
|
|
|
if isinstance(s, text_type): |
90
|
|
|
return s |
91
|
|
|
return s.decode('utf-8', errors=errors) |
92
|
|
|
|
93
|
|
|
def b(s, errors='replace'): |
94
|
|
|
if isinstance(s, binary_type): |
95
|
|
|
return s |
96
|
|
|
return s.encode('utf-8', errors=errors) |
97
|
|
|
|
98
|
|
|
def n(s): |
99
|
|
|
'''Only in Python 2... |
100
|
|
|
from future.utils import bytes_to_native_str as n |
101
|
|
|
''' |
102
|
|
|
return s |
103
|
|
|
|
104
|
|
|
def nativestr(s, errors='replace'): |
105
|
|
|
if isinstance(s, text_type): |
106
|
|
|
return s |
107
|
|
|
elif isinstance(s, (int, float)): |
108
|
|
|
return s.__str__() |
109
|
|
|
else: |
110
|
|
|
return s.decode('utf-8', errors=errors) |
111
|
|
|
|
112
|
|
|
def system_exec(command): |
113
|
|
|
"""Execute a system command and return the resul as a str""" |
114
|
|
|
try: |
115
|
|
|
res = subprocess.run(command.split(' '), |
116
|
|
|
stdout=subprocess.PIPE).stdout.decode('utf-8') |
117
|
|
|
except Exception as e: |
118
|
|
|
logger.debug('Can not evaluate command {} ({})'.format(command, e)) |
119
|
|
|
res = '' |
120
|
|
|
return res.rstrip() |
121
|
|
|
|
122
|
|
|
else: |
123
|
|
|
from future.utils import bytes_to_native_str as n |
124
|
|
|
import Queue as queue |
125
|
|
|
from itertools import imap as map |
126
|
|
|
from ConfigParser import SafeConfigParser as ConfigParser, NoOptionError, NoSectionError |
127
|
|
|
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer |
128
|
|
|
from xmlrpclib import Fault, ProtocolError, ServerProxy, Transport, Server |
129
|
|
|
from urllib2 import urlopen, HTTPError, URLError |
130
|
|
|
from urlparse import urlparse |
131
|
|
|
|
132
|
|
|
input = raw_input |
|
|
|
|
133
|
|
|
range = xrange |
|
|
|
|
134
|
|
|
ConfigParser.read_file = ConfigParser.readfp |
135
|
|
|
|
136
|
|
|
text_type = unicode |
|
|
|
|
137
|
|
|
binary_type = str |
138
|
|
|
bool_type = types.BooleanType |
139
|
|
|
long = long |
|
|
|
|
140
|
|
|
|
141
|
|
|
viewkeys = operator.methodcaller('viewkeys') |
142
|
|
|
viewvalues = operator.methodcaller('viewvalues') |
143
|
|
|
viewitems = operator.methodcaller('viewitems') |
144
|
|
|
|
145
|
|
|
def printandflush(string): |
146
|
|
|
"""Print and flush (used by stdout* outputs modules)""" |
147
|
|
|
print(string) |
148
|
|
|
sys.stdout.flush() |
149
|
|
|
|
150
|
|
|
def mean(numbers): |
151
|
|
|
return float(sum(numbers)) / max(len(numbers), 1) |
152
|
|
|
|
153
|
|
|
def to_ascii(s): |
154
|
|
|
"""Convert the unicode 's' to a ASCII string |
155
|
|
|
Usefull to remove accent (diacritics)""" |
156
|
|
|
if isinstance(s, binary_type): |
157
|
|
|
return s |
158
|
|
|
return unicodedata.normalize('NFKD', s).encode('ascii', 'ignore') |
159
|
|
|
|
160
|
|
|
def listitems(d): |
161
|
|
|
return d.items() |
162
|
|
|
|
163
|
|
|
def listkeys(d): |
164
|
|
|
return d.keys() |
165
|
|
|
|
166
|
|
|
def listvalues(d): |
167
|
|
|
return d.values() |
168
|
|
|
|
169
|
|
|
def iteritems(d): |
170
|
|
|
return d.iteritems() |
171
|
|
|
|
172
|
|
|
def iterkeys(d): |
173
|
|
|
return d.iterkeys() |
174
|
|
|
|
175
|
|
|
def itervalues(d): |
176
|
|
|
return d.itervalues() |
177
|
|
|
|
178
|
|
|
def u(s, errors='replace'): |
179
|
|
|
if isinstance(s, text_type): |
180
|
|
|
return s.encode('utf-8', errors=errors) |
181
|
|
|
return s.decode('utf-8', errors=errors) |
182
|
|
|
|
183
|
|
|
def b(s, errors='replace'): |
184
|
|
|
if isinstance(s, binary_type): |
185
|
|
|
return s |
186
|
|
|
return s.encode('utf-8', errors=errors) |
187
|
|
|
|
188
|
|
|
def nativestr(s, errors='replace'): |
189
|
|
|
if isinstance(s, binary_type): |
190
|
|
|
return s |
191
|
|
|
elif isinstance(s, (int, float)): |
192
|
|
|
return s.__str__() |
193
|
|
|
else: |
194
|
|
|
return s.encode('utf-8', errors=errors) |
195
|
|
|
|
196
|
|
|
def system_exec(command): |
197
|
|
|
"""Execute a system command and return the resul as a str""" |
198
|
|
|
try: |
199
|
|
|
res = subprocess.check_output(command.split(' ')) |
200
|
|
|
except Exception as e: |
201
|
|
|
logger.debug('Can not execute command {} ({})'.format(command, e)) |
202
|
|
|
res = '' |
203
|
|
|
return res.rstrip() |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
# Globals functions for both Python 2 and 3 |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
def subsample(data, sampling): |
210
|
|
|
"""Compute a simple mean subsampling. |
211
|
|
|
|
212
|
|
|
Data should be a list of numerical itervalues |
213
|
|
|
|
214
|
|
|
Return a subsampled list of sampling lenght |
215
|
|
|
""" |
216
|
|
|
if len(data) <= sampling: |
217
|
|
|
return data |
218
|
|
|
sampling_length = int(round(len(data) / float(sampling))) |
219
|
|
|
return [mean(data[s * sampling_length:(s + 1) * sampling_length]) for s in range(0, sampling)] |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
def time_serie_subsample(data, sampling): |
223
|
|
|
"""Compute a simple mean subsampling. |
224
|
|
|
|
225
|
|
|
Data should be a list of set (time, value) |
226
|
|
|
|
227
|
|
|
Return a subsampled list of sampling lenght |
228
|
|
|
""" |
229
|
|
|
if len(data) <= sampling: |
230
|
|
|
return data |
231
|
|
|
t = [t[0] for t in data] |
232
|
|
|
v = [t[1] for t in data] |
233
|
|
|
sampling_length = int(round(len(data) / float(sampling))) |
234
|
|
|
t_subsampled = [t[s * sampling_length:(s + 1) * sampling_length][0] for s in range(0, sampling)] |
235
|
|
|
v_subsampled = [mean(v[s * sampling_length:(s + 1) * sampling_length]) for s in range(0, sampling)] |
236
|
|
|
return list(zip(t_subsampled, v_subsampled)) |
237
|
|
|
|