##// END OF EJS Templates
system-info: optimized code based on linting
super-admin -
r4938:fd6cd0ae default
parent child Browse files
Show More
@@ -24,6 +24,7 b' import sys'
24 import time
24 import time
25 import platform
25 import platform
26 import collections
26 import collections
27 import psutil
27 from functools import wraps
28 from functools import wraps
28
29
29 import pkg_resources
30 import pkg_resources
@@ -35,16 +36,8 b' import configparser'
35 log = logging.getLogger(__name__)
36 log = logging.getLogger(__name__)
36
37
37
38
38 psutil = None
39
40 try:
41 # cygwin cannot have yet psutil support.
42 import psutil as psutil
43 except ImportError:
44 pass
45
46
47 _NA = 'NOT AVAILABLE'
39 _NA = 'NOT AVAILABLE'
40 _NA_FLOAT = 0.0
48
41
49 STATE_OK = 'ok'
42 STATE_OK = 'ok'
50 STATE_ERR = 'error'
43 STATE_ERR = 'error'
@@ -74,7 +67,7 b' def register_sysinfo(func):'
74
67
75
68
76 # HELPERS
69 # HELPERS
77 def percentage(part, whole):
70 def percentage(part: (int, float), whole: (int, float)):
78 whole = float(whole)
71 whole = float(whole)
79 if whole > 0:
72 if whole > 0:
80 return round(100 * float(part) / whole, 1)
73 return round(100 * float(part) / whole, 1)
@@ -160,7 +153,7 b' class SysInfo(object):'
160 # SysInfo functions
153 # SysInfo functions
161 @register_sysinfo
154 @register_sysinfo
162 def python_info():
155 def python_info():
163 value = dict(version=' '.join(platform._sys_version()),
156 value = dict(version=f'{platform.python_version()}:{platform.python_implementation()}',
164 executable=sys.executable)
157 executable=sys.executable)
165 return SysInfoRes(value=value)
158 return SysInfoRes(value=value)
166
159
@@ -232,8 +225,6 b' def uptime():'
232
225
233 value = dict(boot_time=0, uptime=0, text='')
226 value = dict(boot_time=0, uptime=0, text='')
234 state = STATE_OK_DEFAULT
227 state = STATE_OK_DEFAULT
235 if not psutil:
236 return SysInfoRes(value=value, state=state)
237
228
238 boot_time = psutil.boot_time()
229 boot_time = psutil.boot_time()
239 value['boot_time'] = boot_time
230 value['boot_time'] = boot_time
@@ -259,8 +250,6 b' def memory():'
259 total=0, buffers=0, text='')
250 total=0, buffers=0, text='')
260
251
261 state = STATE_OK_DEFAULT
252 state = STATE_OK_DEFAULT
262 if not psutil:
263 return SysInfoRes(value=value, state=state)
264
253
265 value.update(dict(psutil.virtual_memory()._asdict()))
254 value.update(dict(psutil.virtual_memory()._asdict()))
266 value['used_real'] = value['total'] - value['available']
255 value['used_real'] = value['total'] - value['available']
@@ -273,7 +262,7 b' def memory():'
273 format_byte_size_binary(value['total']),
262 format_byte_size_binary(value['total']),
274 value['percent_used'],)
263 value['percent_used'],)
275
264
276 keys = value.keys()[::]
265 keys = list(value.keys())[::]
277 keys.pop(keys.index('percent'))
266 keys.pop(keys.index('percent'))
278 keys.pop(keys.index('percent_used'))
267 keys.pop(keys.index('percent_used'))
279 keys.pop(keys.index('text'))
268 keys.pop(keys.index('text'))
@@ -293,21 +282,20 b' def memory():'
293
282
294 @register_sysinfo
283 @register_sysinfo
295 def machine_load():
284 def machine_load():
296 value = {'1_min': _NA, '5_min': _NA, '15_min': _NA, 'text': ''}
285 value = {'1_min': _NA_FLOAT, '5_min': _NA_FLOAT, '15_min': _NA_FLOAT, 'text': ''}
297 state = STATE_OK_DEFAULT
286 state = STATE_OK_DEFAULT
298 if not psutil:
299 return SysInfoRes(value=value, state=state)
300
287
301 # load averages
288 # load averages
302 if hasattr(psutil.os, 'getloadavg'):
289 if hasattr(psutil.os, 'getloadavg'):
303 value.update(dict(
290 value.update(dict(
304 zip(['1_min', '5_min', '15_min'], psutil.os.getloadavg())))
291 zip(['1_min', '5_min', '15_min'], psutil.os.getloadavg())
292 ))
305
293
306 human_value = value.copy()
294 human_value = value.copy()
307 human_value['text'] = '1min: {}, 5min: {}, 15min: {}'.format(
295 human_value['text'] = '1min: {}, 5min: {}, 15min: {}'.format(
308 value['1_min'], value['5_min'], value['15_min'])
296 value['1_min'], value['5_min'], value['15_min'])
309
297
310 if state['type'] == STATE_OK and value['15_min'] > 5:
298 if state['type'] == STATE_OK and value['15_min'] > 5.0:
311 msg = 'Warning: your machine load is very high.'
299 msg = 'Warning: your machine load is very high.'
312 state = {'message': msg, 'type': STATE_WARN}
300 state = {'message': msg, 'type': STATE_WARN}
313
301
@@ -319,9 +307,6 b' def cpu():'
319 value = {'cpu': 0, 'cpu_count': 0, 'cpu_usage': []}
307 value = {'cpu': 0, 'cpu_count': 0, 'cpu_usage': []}
320 state = STATE_OK_DEFAULT
308 state = STATE_OK_DEFAULT
321
309
322 if not psutil:
323 return SysInfoRes(value=value, state=state)
324
325 value['cpu'] = psutil.cpu_percent(0.5)
310 value['cpu'] = psutil.cpu_percent(0.5)
326 value['cpu_usage'] = psutil.cpu_percent(0.5, percpu=True)
311 value['cpu_usage'] = psutil.cpu_percent(0.5, percpu=True)
327 value['cpu_count'] = psutil.cpu_count()
312 value['cpu_count'] = psutil.cpu_count()
@@ -341,8 +326,6 b' def storage():'
341
326
342 value = dict(percent=0, used=0, total=0, path=path, text='')
327 value = dict(percent=0, used=0, total=0, path=path, text='')
343 state = STATE_OK_DEFAULT
328 state = STATE_OK_DEFAULT
344 if not psutil:
345 return SysInfoRes(value=value, state=state)
346
329
347 try:
330 try:
348 value.update(dict(psutil.disk_usage(path)._asdict()))
331 value.update(dict(psutil.disk_usage(path)._asdict()))
@@ -374,10 +357,8 b' def storage_inodes():'
374 from rhodecode.model.settings import VcsSettingsModel
357 from rhodecode.model.settings import VcsSettingsModel
375 path = VcsSettingsModel().get_repos_location()
358 path = VcsSettingsModel().get_repos_location()
376
359
377 value = dict(percent=0, free=0, used=0, total=0, path=path, text='')
360 value = dict(percent=0.0, free=0, used=0, total=0, path=path, text='')
378 state = STATE_OK_DEFAULT
361 state = STATE_OK_DEFAULT
379 if not psutil:
380 return SysInfoRes(value=value, state=state)
381
362
382 try:
363 try:
383 i_stat = os.statvfs(path)
364 i_stat = os.statvfs(path)
General Comments 0
You need to be logged in to leave comments. Login now