[PEP8] Format code to comply with PEP8 style
This commit is contained in:
parent
e0daa984e2
commit
52dd97fb1e
112
nohang
112
nohang
@ -1,7 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""A daemon that prevents OOM in Linux systems."""
|
"""A daemon that prevents OOM in Linux systems."""
|
||||||
from time import sleep, time
|
from time import sleep, time
|
||||||
start_time = time()
|
|
||||||
|
|
||||||
import os
|
import os
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
@ -12,6 +11,8 @@ from argparse import ArgumentParser
|
|||||||
from sys import stdout
|
from sys import stdout
|
||||||
from signal import SIGKILL, SIGTERM
|
from signal import SIGKILL, SIGTERM
|
||||||
|
|
||||||
|
start_time = time()
|
||||||
|
|
||||||
sig_dict = {SIGKILL: 'SIGKILL',
|
sig_dict = {SIGKILL: 'SIGKILL',
|
||||||
SIGTERM: 'SIGTERM'}
|
SIGTERM: 'SIGTERM'}
|
||||||
|
|
||||||
@ -94,14 +95,6 @@ def check_zram():
|
|||||||
return (mem_used_total_sum + disksize_sum * ZRAM_DISKSIZE_FACTOR) / 1024.0
|
return (mem_used_total_sum + disksize_sum * ZRAM_DISKSIZE_FACTOR) / 1024.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def format_time(t):
|
def format_time(t):
|
||||||
t = int(t)
|
t = int(t)
|
||||||
if t < 60:
|
if t < 60:
|
||||||
@ -365,10 +358,8 @@ def send_notify_warn():
|
|||||||
if root: # If nohang was started by root
|
if root: # If nohang was started by root
|
||||||
# send notification to all active users with special script
|
# send notification to all active users with special script
|
||||||
|
|
||||||
|
|
||||||
# теперь можно напрямую уведомлять из кэша если он не устарел
|
# теперь можно напрямую уведомлять из кэша если он не устарел
|
||||||
|
|
||||||
|
|
||||||
Popen([
|
Popen([
|
||||||
'/usr/bin/nohang_notify_low_mem',
|
'/usr/bin/nohang_notify_low_mem',
|
||||||
'--mem', low_mem_percent,
|
'--mem', low_mem_percent,
|
||||||
@ -376,7 +367,6 @@ def send_notify_warn():
|
|||||||
'--name', name
|
'--name', name
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
else: # Or by regular user
|
else: # Or by regular user
|
||||||
# send notification to user that runs this nohang
|
# send notification to user that runs this nohang
|
||||||
try:
|
try:
|
||||||
@ -420,6 +410,7 @@ def send_notify(signal, name, pid):
|
|||||||
except TimeoutExpired:
|
except TimeoutExpired:
|
||||||
print('TimeoutExpired: ' + 'notify send signal')
|
print('TimeoutExpired: ' + 'notify send signal')
|
||||||
|
|
||||||
|
|
||||||
def send_notify_etc(pid, name, command):
|
def send_notify_etc(pid, name, command):
|
||||||
"""
|
"""
|
||||||
Notificate about OOM Preventing.
|
Notificate about OOM Preventing.
|
||||||
@ -555,7 +546,6 @@ def find_victim_and_send_signal(signal):
|
|||||||
uid = line.split('\t')[1]
|
uid = line.split('\t')[1]
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|
||||||
if n is vm_size_index:
|
if n is vm_size_index:
|
||||||
vm_size = kib_to_mib(int(line.split('\t')[1][:-4]))
|
vm_size = kib_to_mib(int(line.split('\t')[1][:-4]))
|
||||||
continue
|
continue
|
||||||
@ -599,7 +589,8 @@ def find_victim_and_send_signal(signal):
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
oom_score = rline1('/proc/' + pid + '/oom_score') # тут может быть FileNotFoundError!
|
# тут может быть FileNotFoundError!
|
||||||
|
oom_score = rline1('/proc/' + pid + '/oom_score')
|
||||||
oom_score_adj = rline1('/proc/' + pid + '/oom_score_adj')
|
oom_score_adj = rline1('/proc/' + pid + '/oom_score_adj')
|
||||||
|
|
||||||
len_vm = len(str(vm_size))
|
len_vm = len(str(vm_size))
|
||||||
@ -656,7 +647,6 @@ def find_victim_and_send_signal(signal):
|
|||||||
victim_info, command, exit_status,
|
victim_info, command, exit_status,
|
||||||
round(response_time * 1000))
|
round(response_time * 1000))
|
||||||
|
|
||||||
|
|
||||||
# update stat_dict
|
# update stat_dict
|
||||||
key = "Run the command '\033[35m{}\033[0m'".format(command)
|
key = "Run the command '\033[35m{}\033[0m'".format(command)
|
||||||
if key not in stat_dict:
|
if key not in stat_dict:
|
||||||
@ -665,7 +655,6 @@ def find_victim_and_send_signal(signal):
|
|||||||
new_value = stat_dict[key] + 1
|
new_value = stat_dict[key] + 1
|
||||||
stat_dict.update({key: new_value})
|
stat_dict.update({key: new_value})
|
||||||
|
|
||||||
|
|
||||||
print(mem_info)
|
print(mem_info)
|
||||||
print(etc_info)
|
print(etc_info)
|
||||||
if gui_notifications:
|
if gui_notifications:
|
||||||
@ -680,7 +669,8 @@ def find_victim_and_send_signal(signal):
|
|||||||
round(response_time * 1000))
|
round(response_time * 1000))
|
||||||
|
|
||||||
# update stat_dict
|
# update stat_dict
|
||||||
key = 'Send \033[35m{}\033[0m to \033[35m{}\033[0m'.format(sig_dict[signal], name)
|
key = 'Send \033[35m{}\033[0m to \033[35m{}\033[0m'.format(
|
||||||
|
sig_dict[signal], name)
|
||||||
if key not in stat_dict:
|
if key not in stat_dict:
|
||||||
stat_dict.update({key: 1})
|
stat_dict.update({key: 1})
|
||||||
else:
|
else:
|
||||||
@ -738,12 +728,10 @@ def sleep_after_check_mem():
|
|||||||
else:
|
else:
|
||||||
t = t_mem_zram
|
t = t_mem_zram
|
||||||
|
|
||||||
|
|
||||||
max_sleep_time = 1
|
max_sleep_time = 1
|
||||||
if t > max_sleep_time:
|
if t > max_sleep_time:
|
||||||
t = 1
|
t = 1
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if print_sleep_periods:
|
if print_sleep_periods:
|
||||||
print('sleep', round(t, 2),
|
print('sleep', round(t, 2),
|
||||||
@ -968,14 +956,15 @@ try:
|
|||||||
exit()
|
exit()
|
||||||
etc_dict[etc_name] = etc_command
|
etc_dict[etc_name] = etc_command
|
||||||
|
|
||||||
|
|
||||||
# NEED VALIDATION!
|
# NEED VALIDATION!
|
||||||
if line.startswith('@PROCESSNAME_RE'):
|
if line.startswith('@PROCESSNAME_RE'):
|
||||||
a = line.partition('@PROCESSNAME_RE')[2].strip(' \n').partition('///')
|
a = line.partition('@PROCESSNAME_RE')[
|
||||||
|
2].strip(' \n').partition('///')
|
||||||
processname_re_list.append((a[0].strip(' '), a[2].strip(' ')))
|
processname_re_list.append((a[0].strip(' '), a[2].strip(' ')))
|
||||||
|
|
||||||
if line.startswith('@CMDLINE_RE'):
|
if line.startswith('@CMDLINE_RE'):
|
||||||
a = line.partition('@CMDLINE_RE')[2].strip(' \n').partition('///')
|
a = line.partition('@CMDLINE_RE')[2].strip(
|
||||||
|
' \n').partition('///')
|
||||||
cmdline_re_list.append((a[0].strip(' '), a[2].strip(' ')))
|
cmdline_re_list.append((a[0].strip(' '), a[2].strip(' ')))
|
||||||
|
|
||||||
if line.startswith('@UID_RE'):
|
if line.startswith('@UID_RE'):
|
||||||
@ -1020,8 +1009,6 @@ execute_the_command = conf_parse_bool('execute_the_command')
|
|||||||
ignore_psi = conf_parse_bool('ignore_psi')
|
ignore_psi = conf_parse_bool('ignore_psi')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
regex_matching = conf_parse_bool('regex_matching')
|
regex_matching = conf_parse_bool('regex_matching')
|
||||||
|
|
||||||
re_match_cmdline = conf_parse_bool('re_match_cmdline')
|
re_match_cmdline = conf_parse_bool('re_match_cmdline')
|
||||||
@ -1163,10 +1150,6 @@ else:
|
|||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if 'min_delay_after_sigkill' in config_dict:
|
if 'min_delay_after_sigkill' in config_dict:
|
||||||
min_delay_after_sigkill = string_to_float_convert_test(
|
min_delay_after_sigkill = string_to_float_convert_test(
|
||||||
config_dict['min_delay_after_sigkill'])
|
config_dict['min_delay_after_sigkill'])
|
||||||
@ -1181,12 +1164,6 @@ else:
|
|||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if 'psi_avg10_sleep_time' in config_dict:
|
if 'psi_avg10_sleep_time' in config_dict:
|
||||||
psi_avg10_sleep_time = string_to_float_convert_test(
|
psi_avg10_sleep_time = string_to_float_convert_test(
|
||||||
config_dict['psi_avg10_sleep_time'])
|
config_dict['psi_avg10_sleep_time'])
|
||||||
@ -1201,9 +1178,6 @@ else:
|
|||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if 'sigkill_psi_avg10' in config_dict:
|
if 'sigkill_psi_avg10' in config_dict:
|
||||||
sigkill_psi_avg10 = string_to_float_convert_test(
|
sigkill_psi_avg10 = string_to_float_convert_test(
|
||||||
config_dict['sigkill_psi_avg10'])
|
config_dict['sigkill_psi_avg10'])
|
||||||
@ -1218,8 +1192,6 @@ else:
|
|||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if 'sigterm_psi_avg10' in config_dict:
|
if 'sigterm_psi_avg10' in config_dict:
|
||||||
sigterm_psi_avg10 = string_to_float_convert_test(
|
sigterm_psi_avg10 = string_to_float_convert_test(
|
||||||
config_dict['sigterm_psi_avg10'])
|
config_dict['sigterm_psi_avg10'])
|
||||||
@ -1234,14 +1206,6 @@ else:
|
|||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if 'min_badness' in config_dict:
|
if 'min_badness' in config_dict:
|
||||||
min_badness = string_to_int_convert_test(
|
min_badness = string_to_int_convert_test(
|
||||||
config_dict['min_badness'])
|
config_dict['min_badness'])
|
||||||
@ -1378,9 +1342,6 @@ else:
|
|||||||
mla_res = ''
|
mla_res = ''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if self_uid == 0:
|
if self_uid == 0:
|
||||||
root = True
|
root = True
|
||||||
decrease_res = 'OK'
|
decrease_res = 'OK'
|
||||||
@ -1431,7 +1392,7 @@ if print_config:
|
|||||||
if decrease_oom_score_adj:
|
if decrease_oom_score_adj:
|
||||||
print('oom_score_adj_max: {}'.format(oom_score_adj_max))
|
print('oom_score_adj_max: {}'.format(oom_score_adj_max))
|
||||||
|
|
||||||
print('\n4. Impact on the badness of processes via matching their' \
|
print('\n4. Impact on the badness of processes via matching their'
|
||||||
' names, cmdlines ir UIDs with regular expressions\n')
|
' names, cmdlines ir UIDs with regular expressions\n')
|
||||||
|
|
||||||
print('regex_matching: {}'.format(regex_matching))
|
print('regex_matching: {}'.format(regex_matching))
|
||||||
@ -1527,18 +1488,6 @@ print('The duration of startup:',
|
|||||||
print('Monitoring started!')
|
print('Monitoring started!')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def save_env_cache():
|
def save_env_cache():
|
||||||
z = '{}\n'.format(int(time()))
|
z = '{}\n'.format(int(time()))
|
||||||
a = root_notify_env()
|
a = root_notify_env()
|
||||||
@ -1574,13 +1523,13 @@ def root_env_cache():
|
|||||||
return save_env_cache()
|
return save_env_cache()
|
||||||
delta_t = time() - int(cache[0])
|
delta_t = time() - int(cache[0])
|
||||||
if delta_t > cache_time:
|
if delta_t > cache_time:
|
||||||
print('cache time: {}, delta: {}, ' \
|
print('cache time: {}, delta: {}, '
|
||||||
'get new env and cache it'.format(
|
'get new env and cache it'.format(
|
||||||
cache_time, round(delta_t)))
|
cache_time, round(delta_t)))
|
||||||
save_env_cache()
|
save_env_cache()
|
||||||
return root_notify_env()
|
return root_notify_env()
|
||||||
else:
|
else:
|
||||||
print('cache time: {}, delta: {}, ' \
|
print('cache time: {}, delta: {}, '
|
||||||
'get cached env'.format(
|
'get cached env'.format(
|
||||||
cache_time, round(delta_t)))
|
cache_time, round(delta_t)))
|
||||||
return cache[1]
|
return cache[1]
|
||||||
@ -1592,25 +1541,11 @@ t2 = time()
|
|||||||
# print(t2 - t1)
|
# print(t2 - t1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
stdout.flush()
|
stdout.flush()
|
||||||
|
|
||||||
# exit()
|
# exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
sigterm_psi = sigterm_psi_avg10
|
sigterm_psi = sigterm_psi_avg10
|
||||||
sigkill_psi = sigkill_psi_avg10
|
sigkill_psi = sigkill_psi_avg10
|
||||||
# avg_min_time = 4
|
# avg_min_time = 4
|
||||||
@ -1653,7 +1588,6 @@ while True:
|
|||||||
# print('PSI is OK or psi_min_sleep_time_after_action did not pass')
|
# print('PSI is OK or psi_min_sleep_time_after_action did not pass')
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
'''
|
'''
|
||||||
if psi_support:
|
if psi_support:
|
||||||
|
|
||||||
@ -1685,8 +1619,6 @@ while True:
|
|||||||
print('PSI is OK or psi_min_sleep_time_after_action did not pass')
|
print('PSI is OK or psi_min_sleep_time_after_action did not pass')
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
mem_available, swap_total, swap_free = check_mem_and_swap()
|
mem_available, swap_total, swap_free = check_mem_and_swap()
|
||||||
|
|
||||||
# if swap_min_sigkill is set in percent
|
# if swap_min_sigkill is set in percent
|
||||||
@ -1699,12 +1631,8 @@ while True:
|
|||||||
if swap_warn_is_percent:
|
if swap_warn_is_percent:
|
||||||
swap_min_warnings_kb = swap_total * swap_min_warnings_percent / 100.0
|
swap_min_warnings_kb = swap_total * swap_min_warnings_percent / 100.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
mem_used_zram = check_zram()
|
mem_used_zram = check_zram()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if print_mem_check_results:
|
if print_mem_check_results:
|
||||||
|
|
||||||
# Calculate 'swap-column' width
|
# Calculate 'swap-column' width
|
||||||
@ -1736,16 +1664,12 @@ while True:
|
|||||||
human(mem_used_zram, mem_len),
|
human(mem_used_zram, mem_len),
|
||||||
just_percent_mem(mem_used_zram / mem_total)))
|
just_percent_mem(mem_used_zram / mem_total)))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# если swap_min_sigkill задан в абсолютной величине и Swap_total = 0
|
# если swap_min_sigkill задан в абсолютной величине и Swap_total = 0
|
||||||
if swap_total > swap_min_sigkill_kb: # If swap_min_sigkill is absolute
|
if swap_total > swap_min_sigkill_kb: # If swap_min_sigkill is absolute
|
||||||
swap_sigkill_pc = percent(swap_min_sigkill_kb / (swap_total + 0.1))
|
swap_sigkill_pc = percent(swap_min_sigkill_kb / (swap_total + 0.1))
|
||||||
else:
|
else:
|
||||||
swap_sigkill_pc = '-'
|
swap_sigkill_pc = '-'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if swap_total > swap_min_sigterm_kb:
|
if swap_total > swap_min_sigterm_kb:
|
||||||
swap_sigterm_pc = percent(swap_min_sigterm_kb / (swap_total + 0.1))
|
swap_sigterm_pc = percent(swap_min_sigterm_kb / (swap_total + 0.1))
|
||||||
else:
|
else:
|
||||||
@ -1753,12 +1677,8 @@ while True:
|
|||||||
# печатать так: SwapTotal = 0, ignore swapspace
|
# печатать так: SwapTotal = 0, ignore swapspace
|
||||||
swap_sigterm_pc = '-'
|
swap_sigterm_pc = '-'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# далее пошло ветвление
|
# далее пошло ветвление
|
||||||
|
|
||||||
|
|
||||||
# MEM SWAP KILL
|
# MEM SWAP KILL
|
||||||
if mem_available <= mem_min_sigkill_kb and \
|
if mem_available <= mem_min_sigkill_kb and \
|
||||||
swap_free <= swap_min_sigkill_kb:
|
swap_free <= swap_min_sigkill_kb:
|
||||||
@ -1856,7 +1776,3 @@ while True:
|
|||||||
# SLEEP BETWEEN MEM CHECKS
|
# SLEEP BETWEEN MEM CHECKS
|
||||||
else:
|
else:
|
||||||
sleep_after_check_mem()
|
sleep_after_check_mem()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user