fix output and logging

This commit is contained in:
Alexey Avramov 2019-04-23 01:07:10 +09:00
parent 448a60c3f0
commit 9e6aafce89
2 changed files with 32 additions and 36 deletions

56
nohang
View File

@ -143,7 +143,6 @@ def log(*msg):
print(*msg) print(*msg)
except OSError: except OSError:
sleep(0.01) sleep(0.01)
pass
# print('OSError in print(*msg)') # print('OSError in print(*msg)')
if separate_log: if separate_log:
@ -154,7 +153,6 @@ def log(*msg):
info(*msg) info(*msg)
except OSError: except OSError:
sleep(0.01) sleep(0.01)
pass
# print('OSError in info(*msg)') # print('OSError in info(*msg)')
@ -626,7 +624,7 @@ def update_stat_dict_and_print(key):
for i in stat_dict: for i in stat_dict:
stats_msg += '\n {}: {}'.format(i, stat_dict[i]) stats_msg += '\n {}: {}'.format(i, stat_dict[i])
print(stats_msg) log(stats_msg)
def find_psi_metrics_value(psi_path, psi_metrics): def find_psi_metrics_value(psi_path, psi_metrics):
@ -895,7 +893,7 @@ def send_notify_warn():
notify_send_wait(title, body) notify_send_wait(title, body)
''' '''
print('Warning threshold exceeded') log('Warning threshold exceeded')
if check_warning_exe: if check_warning_exe:
exe(warning_exe) exe(warning_exe)
@ -991,7 +989,7 @@ def sleep_after_send_signal(signal):
""" """
if signal is SIGKILL: if signal is SIGKILL:
if print_sleep_periods: if print_sleep_periods:
print(' sleep', min_delay_after_sigkill) log(' sleep {}'.format(min_delay_after_sigkill))
sleep(min_delay_after_sigkill) sleep(min_delay_after_sigkill)
else: else:
if print_sleep_periods: if print_sleep_periods:
@ -1120,7 +1118,7 @@ def find_victim(_print_proc_table):
elif extra_table_info == 'All': elif extra_table_info == 'All':
extra_table_line = '[CG: {}] [CL: {}] [RP: {}]'.format( extra_table_line = '[CG: {}] [CL: {}] [RP: {}]'.format(
pid_to_cgroup(pid), pid_to_cgroup_v1(pid),
pid_to_cmdline(pid), pid_to_cmdline(pid),
pid_to_realpath(pid) pid_to_realpath(pid)
) )
@ -1164,7 +1162,7 @@ def find_victim(_print_proc_table):
if _print_proc_table: if _print_proc_table:
log(hr) log(hr)
log('Found {} processes with exists realpath'.format(real_proc_num)) log('Found {} processes with existing realpaths'.format(real_proc_num))
log( log(
'Process with highest badness (found in {} ms):\n PID: {}, Na' 'Process with highest badness (found in {} ms):\n PID: {}, Na'
@ -1237,12 +1235,12 @@ def find_victim_info(pid, victim_badness, name):
oom_score_adj = rline1('/proc/' + pid + '/oom_score_adj') oom_score_adj = rline1('/proc/' + pid + '/oom_score_adj')
except FileNotFoundError: except FileNotFoundError:
print('The victim died in the search process: FileNotFoundError') log('The victim died in the search process: FileNotFoundError')
update_stat_dict_and_print( update_stat_dict_and_print(
'The victim died in the search process: FileNotFoundError') 'The victim died in the search process: FileNotFoundError')
return None return None
except ProcessLookupError: except ProcessLookupError:
print('The victim died in the search process: ProcessLookupError') log('The victim died in the search process: ProcessLookupError')
update_stat_dict_and_print( update_stat_dict_and_print(
'The victim died in the search process: ProcessLookupError') 'The victim died in the search process: ProcessLookupError')
return None return None
@ -1292,22 +1290,22 @@ def find_victim_info(pid, victim_badness, name):
oom_score_adj = rline1('/proc/' + pid + '/oom_score_adj') oom_score_adj = rline1('/proc/' + pid + '/oom_score_adj')
except IndexError: except IndexError:
print('The victim died in the search process: IndexError') log('The victim died in the search process: IndexError')
update_stat_dict_and_print( update_stat_dict_and_print(
'The victim died in the search process: IndexError') 'The victim died in the search process: IndexError')
return None return None
except ValueError: except ValueError:
print('The victim died in the search process: ValueError') log('The victim died in the search process: ValueError')
update_stat_dict_and_print( update_stat_dict_and_print(
'The victim died in the search process: ValueError') 'The victim died in the search process: ValueError')
return None return None
except FileNotFoundError: except FileNotFoundError:
print('The victim died in the search process: FileNotFoundError') log('The victim died in the search process: FileNotFoundError')
update_stat_dict_and_print( update_stat_dict_and_print(
'The victim died in the search process: FileNotFoundError') 'The victim died in the search process: FileNotFoundError')
return None return None
except ProcessLookupError: except ProcessLookupError:
print('The victim died in the search process: ProcessLookupError') log('The victim died in the search process: ProcessLookupError')
update_stat_dict_and_print( update_stat_dict_and_print(
'The victim died in the search process: ProcessLookupError') 'The victim died in the search process: ProcessLookupError')
return None return None
@ -1428,11 +1426,11 @@ def implement_corrective_action(signal):
cgroup_v1 = pid_to_cgroup_v1(pid) cgroup_v1 = pid_to_cgroup_v1(pid)
service = '' service = ''
cgroup_v1_tail = cgroup_v1.rpartition('/')[2] cgroup_v1_tail = cgroup_v1.rpartition('/')[2]
print(cgroup_v1_tail) # log(cgroup_v1_tail)
if cgroup_v1_tail.endswith('.service'): if cgroup_v1_tail.endswith('.service'):
service = cgroup_v1_tail service = cgroup_v1_tail
print('$SERVICE:', [service]) # print('$SERVICE:', [service])
print('ИЩЕМ СОВПАДЕНИЯ ДЛЯ МЯГКИХ ДЕЙСТВИЙ') # print('ИЩЕМ СОВПАДЕНИЯ ДЛЯ МЯГКИХ ДЕЙСТВИЙ')
# итерируемся по списку кортежей # итерируемся по списку кортежей
for i in soft_actions_list: for i in soft_actions_list:
unit = i[0] unit = i[0]
@ -1442,9 +1440,10 @@ def implement_corrective_action(signal):
u = cgroup_v1 u = cgroup_v1
regexp = i[1] regexp = i[1]
command = i[2] command = i[2]
print([u, regexp, command]) # print([u, regexp, command])
if search(regexp, u) is not None: if search(regexp, u) is not None:
print('СОВПАДЕНИЕ НАЙДЕНО') log("Regexp '{}' matches with {} '{}'".format(regexp, unit, u))
# print('СОВПАДЕНИЕ НАЙДЕНО')
soft_match = True soft_match = True
break break
@ -1483,9 +1482,9 @@ def implement_corrective_action(signal):
exit_status, exit_status,
round(response_time * 1000)) round(response_time * 1000))
print(etc_info) log(etc_info)
key = "Run the command '{}'".format(command) key = "Run the command '{}'".format(cmd)
update_stat_dict_and_print(key) update_stat_dict_and_print(key)
if gui_notifications: if gui_notifications:
@ -1591,7 +1590,7 @@ def implement_corrective_action(signal):
min_badness, min_badness,
round(response_time * 1000)) round(response_time * 1000))
print(victim_badness_is_too_small) log(victim_badness_is_too_small)
# update stat_dict # update stat_dict
key = 'victim badness < min_badness' key = 'victim badness < min_badness'
@ -1642,7 +1641,7 @@ def sleep_after_check_mem():
if print_sleep_periods: if print_sleep_periods:
print( log(
'Sleep {} sec (t_mem={}, t_swap={}, t_zram={})'.format( 'Sleep {} sec (t_mem={}, t_swap={}, t_zram={})'.format(
round(t, 2), round(t, 2),
round(t_mem, 2), round(t_mem, 2),
@ -1711,12 +1710,12 @@ def calculate_percent(arg_key):
mem_min_percent = mem_min_kb / mem_total * 100 mem_min_percent = mem_min_kb / mem_total * 100
else: else:
print('Invalid {} units in config.\n Exit'.format(arg_key)) log('Invalid {} units in config.\n Exit'.format(arg_key))
exit(1) exit(1)
mem_min_percent = None mem_min_percent = None
else: else:
print('{} not in config\nExit'.format(arg_key)) log('{} not in config\nExit'.format(arg_key))
exit(1) exit(1)
mem_min_percent = None mem_min_percent = None
@ -1818,8 +1817,7 @@ except ValueError:
########################################################################## ##########################################################################
print('Config:', config) log('Config: ' + config)
# todo: log it
########################################################################## ##########################################################################
@ -1870,7 +1868,7 @@ try:
if key not in config_dict: if key not in config_dict:
config_dict[key] = value config_dict[key] = value
else: else:
print('ERROR: config key duplication: {}'.format(key)) log('ERROR: config key duplication: {}'.format(key))
exit(1) exit(1)
if etc: if etc:
@ -2037,14 +2035,14 @@ else:
# print(cgroup_v2_re_list) # print(cgroup_v2_re_list)
print(soft_actions_list) # print(soft_actions_list)
if soft_actions_list == []: if soft_actions_list == []:
soft_actions = False soft_actions = False
else: else:
soft_actions = True soft_actions = True
print('soft_actions:', soft_actions) # print('soft_actions:', soft_actions)
########################################################################## ##########################################################################

View File

@ -185,8 +185,6 @@ oom_score_adj_max = 20
Example: Example:
@PROCESSNAME_RE 1000 /// ^Xorg$
@PROCESSNAME_RE -500 /// ^sshd$ @PROCESSNAME_RE -500 /// ^sshd$
5.2 Matching cmdlines with RE patterns 5.2 Matching cmdlines with RE patterns
@ -249,7 +247,7 @@ oom_score_adj_max = 20
- OOM prevention results and - OOM prevention results and
- low memory warnings - low memory warnings
gui_notifications = True gui_notifications = False
Enable GUI notifications about the low level of available memory. Enable GUI notifications about the low level of available memory.
Valid values are True and False. Valid values are True and False.
@ -273,7 +271,7 @@ zram_max_warnings = 40 %
Valid values are floating-point numbers from the range [1; 300]. Valid values are floating-point numbers from the range [1; 300].
min_time_between_warnings = 15 min_time_between_warnings = 60
Ampersands (&) will be replaced with asterisks (*) in process Ampersands (&) will be replaced with asterisks (*) in process
names and in commands. names and in commands.
@ -290,9 +288,9 @@ print_config = False
Print memory check results. Print memory check results.
Valid values are True and False. Valid values are True and False.
print_mem_check_results = True print_mem_check_results = False
min_mem_report_interval = 30 min_mem_report_interval = 60
Print sleep periods between memory checks. Print sleep periods between memory checks.
Valid values are True and False. Valid values are True and False.