fix output

This commit is contained in:
Alexey Avramov 2020-06-28 14:25:51 +09:00
parent 644a063062
commit edf43313d7

View File

@ -53,15 +53,15 @@ def check_permissions():
try: try:
os.path.realpath('/proc/1/exe') os.path.realpath('/proc/1/exe')
except Exception as e: except Exception as e:
print('WARNING: missing CAP_SYS_PTRACE: {}'.format(e)) log('WARNING: missing CAP_SYS_PTRACE: {}'.format(e))
try: try:
os.kill(1, 0) os.kill(1, 0)
except Exception as e: except Exception as e:
print('WARNING: cannot send a signal: {}'.format(e)) log('WARNING: cannot send a signal: {}'.format(e))
try: try:
rline1('/proc/1/oom_score') rline1('/proc/1/oom_score')
except Exception as e: except Exception as e:
print('ERROR: {}'.format(e)) errprint('ERROR: {}'.format(e))
exit(1) exit(1)
@ -75,19 +75,19 @@ def memload():
t0 = monotonic() t0 = monotonic()
inp = input(hi) inp = input(hi)
except KeyboardInterrupt: except KeyboardInterrupt:
print('KeyboardInterrupt\nExit') errprint('KeyboardInterrupt\nExit')
exit(1) exit(1)
try: try:
os.setreuid(1, 1) os.setreuid(1, 1)
except Exception: except Exception:
pass pass
if inp != r: if inp != r:
print('Captcha is not passed ("{}" != "{}")'.format(inp, r)) errprint('Captcha is not passed ("{}" != "{}")'.format(inp, r))
print('memload() is not for robots\nExit') errprint('memload() is not for robots\nExit')
exit(1) exit(1)
if monotonic() - t0 > 30: if monotonic() - t0 > 30:
print('Captcha is not passed (timeout expired)') errprint('Captcha is not passed (timeout expired)')
print('memload() is not for robots\nExit') errprint('memload() is not for robots\nExit')
exit(1) exit(1)
else: else:
print('-' * 68) print('-' * 68)
@ -99,7 +99,7 @@ def memload():
try: try:
mem_available_index = mem_list_names.index('MemAvailable') mem_available_index = mem_list_names.index('MemAvailable')
except ValueError: except ValueError:
print('Your Linux kernel is too old, Linux 3.14+ required\nExit') errprint('Your Linux kernel is too old, Linux 3.14+ required\nExit')
swap_total_index = mem_list_names.index('SwapTotal') swap_total_index = mem_list_names.index('SwapTotal')
swap_free_index = mem_list_names.index('SwapFree') swap_free_index = mem_list_names.index('SwapFree')
@ -132,7 +132,7 @@ def memload():
try: try:
inp = input(hi) inp = input(hi)
except KeyboardInterrupt: except KeyboardInterrupt:
print('KeyboardInterrupt\nExit') errprint('KeyboardInterrupt\nExit')
exit(1) exit(1)
if inp != 'Yes': if inp != 'Yes':
print('Exit') print('Exit')
@ -160,12 +160,12 @@ def memload():
z = monotonic() z = monotonic()
print_mem(mem_available, swap_free) print_mem(mem_available, swap_free)
except KeyboardInterrupt: except KeyboardInterrupt:
print('KeyboardInterrupt') errprint('KeyboardInterrupt')
print('Self terminating by the SIGUSR1 signal') errprint('Self terminating by the SIGUSR1 signal')
os.kill(self_pid, SIGUSR1) os.kill(self_pid, SIGUSR1)
except MemoryError: except MemoryError:
print('MemoryError') errprint('MemoryError')
print('Self terminating by the SIGUSR1 signal') errprint('Self terminating by the SIGUSR1 signal')
os.kill(self_pid, SIGUSR1) os.kill(self_pid, SIGUSR1)
@ -2886,10 +2886,8 @@ def calculate_percent(arg_key):
# {victim_id : {'time': timestamp, 'name': name} # {victim_id : {'time': timestamp, 'name': name}
v_dict = dict() v_dict = dict()
start_time = monotonic() start_time = monotonic()
help_mess = """usage: nohang [-h|--help] [-v|--version] [-m|--memload] help_mess = """usage: nohang [-h|--help] [-v|--version] [-m|--memload]
[-c|--config CONFIG] [--check] [--monitor] [--tasks] [-c|--config CONFIG] [--check] [--monitor] [--tasks]
@ -2962,8 +2960,8 @@ check_config_flag = False
a = argv[1:] a = argv[1:]
la = len(a) la = len(a)
if la == 0: if la == 0:
print('ERROR: invalid input: missing CLI options\n') errprint('ERROR: invalid input: missing CLI options\n')
print(help_mess) errprint(help_mess)
exit(1) exit(1)
if la == 1: if la == 1:
if a[0] == '-h' or a[0] == '--help': if a[0] == '-h' or a[0] == '--help':
@ -2973,12 +2971,12 @@ if la == 1:
print_version() print_version()
if a[0] == '-m' or a[0] == '--memload': if a[0] == '-m' or a[0] == '--memload':
memload() memload()
print('ERROR: invalid input\n') errprint('ERROR: invalid input\n')
print(help_mess) errprint(help_mess)
exit(1) exit(1)
if la == 2: if la == 2:
print('ERROR: invalid input\n') errprint('ERROR: invalid input\n')
print(help_mess) errprint(help_mess)
exit(1) exit(1)
if la == 3: if la == 3:
if '-c' in a or '--config' in a: if '-c' in a or '--config' in a:
@ -2994,29 +2992,29 @@ if la == 3:
try: try:
config = a[aaa + 1] config = a[aaa + 1]
except IndexError: except IndexError:
print('ERROR: invalid input\n') errprint('ERROR: invalid input\n')
print(help_mess) errprint(help_mess)
exit(1) exit(1)
if (config == '--check' or config == '--monitor' or if (config == '--check' or config == '--monitor' or
config == '--tasks:'): config == '--tasks:'):
print('ERROR: invalid input\n') errprint('ERROR: invalid input\n')
print(help_mess) errprint(help_mess)
exit(1) exit(1)
if '--check' in a: if '--check' in a:
check_config_flag = True check_config_flag = True
if '--tasks' in a: if '--tasks' in a:
print_proc_table_flag = True print_proc_table_flag = True
else: else:
print('ERROR: invalid input\n') errprint('ERROR: invalid input\n')
print(help_mess) errprint(help_mess)
exit(1) exit(1)
else: else:
print('ERROR: invalid input\n') errprint('ERROR: invalid input\n')
print(help_mess) errprint(help_mess)
exit(1) exit(1)
if la > 3: if la > 3:
print('ERROR: invalid CLI input: too many options\n') errprint('ERROR: invalid CLI input: too many options\n')
print(help_mess) errprint(help_mess)
exit(1) exit(1)
@ -3076,7 +3074,8 @@ except ValueError:
config = os.path.abspath(config) config = os.path.abspath(config)
log('config: ' + config) print('Starting nohang with the config: {}'.format(config))
separator_in = '>>' + '=' * 75 + '>>' separator_in = '>>' + '=' * 75 + '>>'
separator_out = '<<' + '=' * 75 + '<<' separator_out = '<<' + '=' * 75 + '<<'
@ -3134,7 +3133,7 @@ try:
if key not in config_dict: if key not in config_dict:
config_dict[key] = value config_dict[key] = value
else: else:
log('ERROR: config key duplication: {}'.format(key)) errprint('ERROR: config key duplication: {}'.format(key))
exit(1) exit(1)
if etc: if etc:
@ -3342,13 +3341,16 @@ if separate_log:
pass pass
except PermissionError: except PermissionError:
errprint('ERROR: cannot create {}'.format(log_dir)) errprint('ERROR: cannot create {}'.format(log_dir))
exit(1)
try: try:
os.chmod(log_dir, mode=0o750) os.chmod(log_dir, mode=0o750)
except FileNotFoundError: except FileNotFoundError:
errprint('ERROR: file not found: {}'.format(log_dir)) errprint('ERROR: file not found: {}'.format(log_dir))
exit(1)
except PermissionError: except PermissionError:
errprint('ERROR: permission denied: {}'.format(log_dir)) errprint('ERROR: permission denied: {}'.format(log_dir))
exit(1)
try: try:
logging.basicConfig( logging.basicConfig(
@ -3357,8 +3359,15 @@ if separate_log:
format="%(asctime)s: %(message)s") format="%(asctime)s: %(message)s")
except FileNotFoundError: except FileNotFoundError:
errprint('ERROR: file not found: {}'.format(logfile)) errprint('ERROR: file not found: {}'.format(logfile))
exit(1)
except PermissionError: except PermissionError:
errprint('ERROR: permission denied: {}'.format(logfile)) errprint('ERROR: permission denied: {}'.format(logfile))
exit(1)
if separate_log:
logging.info('Starting nohang with the config: {}'.format(config))
debug_psi = conf_parse_bool('debug_psi') debug_psi = conf_parse_bool('debug_psi')
print_statistics = conf_parse_bool('print_statistics') print_statistics = conf_parse_bool('print_statistics')