fix GUI notifications

This commit is contained in:
Alexey Avramov 2019-03-24 23:54:51 +09:00
parent bb1016896f
commit af7ff8fcd0
4 changed files with 65 additions and 30 deletions

58
nohang
View File

@ -277,12 +277,6 @@ def check_mem():
return int(rline1('/proc/meminfo').split(':')[1][:-4])
def write(text):
file = '/dev/shm/nohang_notify'
with open(file, 'w') as f:
f.write(text)
def check_mem_and_swap():
"""find mem_available, swap_total, swap_free"""
with open('/proc/meminfo') as f:
@ -616,13 +610,14 @@ def send_notify_warn():
t0 = time()
title = 'Low memoy'
title = 'Low memory'
body = 'MemAvail: {}%\nSwapFree: {}%'.format(
round(mem_available / mem_total * 100),
round(swap_free / (swap_total + 0.1) * 100)
)
write('{}\x00{}'.format(title, body))
os.system(notify_helper_path + ' &')
send_notification(title, body)
t1 = time()
print('t:', t1 - t0)
@ -647,8 +642,7 @@ def send_notify(signal, name, pid):
)
)
write('{}\x00{}'.format(title, body))
os.system(notify_helper_path + ' &')
send_notification(title, body)
'''
if root:
@ -669,16 +663,14 @@ def send_notify_etc(pid, name, command):
pid: str process pid
"""
title = 'Freeze prevention'
body = 'Victim is {}[{}]\nExecute the command:\n<b>{}</b>'.format(
name.replace('&', '*'),
body = '<b>Victim is</b> [{}] <b>{}</b>\nExecute the command:\n<b>{}</b>'.format(
pid,
name.replace('&', '*'),
command.replace('&', '*')
)
# write() -> wrinte_notify(title, body)
send_notification(title, body)
write('{}\x00{}'.format(title, body))
os.system(notify_helper_path + ' &')
'''
if root:
# send notification to all active users with notify-send
@ -689,6 +681,28 @@ def send_notify_etc(pid, name, command):
'''
def send_notification(title, body):
"""
"""
split_by = '#' * 16
t000 = time()
path_to_cache = '/dev/shm/nohang_notify_cache_uid{}_time{}'.format(
str(self_uid), t000
)
text = '{}{}{}'.format(title, split_by, body)
with open(path_to_cache, 'w') as f:
f.write(text)
os.chmod(path_to_cache, 0o600)
cmd = '{} --uid {} --time {} &'.format(notify_helper_path, self_uid, t000)
os.system(cmd)
def sleep_after_send_signal(signal):
"""
Sleeping after signal was sent.
@ -1176,14 +1190,14 @@ def implement_corrective_action(signal):
y = dick['v']
print(y[3] - x[3])
#print(y[3] - x[3])
if x[0] == y[0] and x[1] == y[1] and x[2] == y[2]:
print('совпадение имени, пид, сигнала')
#print('совпадение имени, пид, сигнала')
# сохр в словаре первре совпавшее время
dt = y[3] - x[3]
print(dt, 'dt')
#print(dt, 'dt')
if dt < delay_after_same_notify:
notif = False
@ -2132,7 +2146,8 @@ while True:
mem_info = 'PSI avg value ({}) > sigkill_psi_threshold ({})'.format(
psi_avg_value, sigkill_psi_threshold)
implement_corrective_action(SIGKILL)
# implement_corrective_action(SIGKILL)
print('implement!')
psi_t0 = time()
continue
@ -2142,7 +2157,8 @@ while True:
mem_info = 'PSI avg value ({}) > sigterm_psi_threshold ({})'.format(
psi_avg_value, sigterm_psi_threshold)
implement_corrective_action(SIGTERM)
# implement_corrective_action(SIGTERM)
print('implement!')
psi_t0 = time()
continue

View File

@ -384,7 +384,6 @@ max_ancestry_depth = 1
separate_log = False
psi_debug = False
#####################################################################

View File

@ -7,7 +7,7 @@ Documentation=man:nohang(1) https://github.com/hakavlad/nohang
ExecStart=/usr/sbin/nohang --config /etc/nohang/nohang.conf
Slice=nohang.slice
Restart=always
MemoryMax=50M
MemoryMax=60M
TasksMax=20
OOMScoreAdjust=-5
Nice=-20

View File

@ -1,7 +1,21 @@
#!/usr/bin/env python3
from os import listdir, path
from os import listdir, path, remove
from subprocess import Popen, TimeoutExpired
from sys import argv
# print('Starting nohang_notify_helper')
# print(argv)
# print(len(argv))
split_by = '#' * 16
uid = argv[2]
t000 = argv[4]
wait_time = 10
@ -23,7 +37,13 @@ def rfile(path):
return f.read()
a1, a2 = rfile('/dev/shm/nohang_notify').split('\x00')
path_to_cache = '/dev/shm/nohang_notify_cache_uid{}_time{}'.format(
uid, t000
)
title, body = rfile(path_to_cache).split(split_by)
remove(path_to_cache)
def re_pid_environ(pid):
@ -109,7 +129,7 @@ list_len = len(list_with_envs)
if list_len > 0:
for i in list_with_envs:
print('Send GUI notification:', a1, a2, i)
print('Send GUI notification:', title, body, i)
# iterating over logged-in users
for i in list_with_envs:
@ -127,8 +147,8 @@ if list_len > 0:
'DBUS_SESSION_BUS_ADDRESS=' + dbus_value,
'notify-send',
'--icon=dialog-warning',
a1,
a2
title,
body
]) as proc:
try:
proc.wait(timeout=wait_time)
@ -140,6 +160,6 @@ if list_len > 0:
else:
print(
'Not send GUI notification: [',
a1,
a2,
title,
body,
']. Nobody logged-in with GUI. Nothing to do.')