bundles/sshmon: fix occasional KeyErrors in check_cpu_stats
All checks were successful
bundlewrap/pipeline/head This commit looks good

This commit is contained in:
Franzi 2021-06-06 17:59:58 +02:00
parent 94dba9139b
commit 455d4d7551
Signed by: kunsi
GPG key ID: 12E3D2136B818350

View file

@ -5,7 +5,16 @@ from subprocess import check_output
from sys import exit from sys import exit
try: try:
top_output = check_output("top -b -n1 -d1 | grep -i '^\%cpu'", shell=True).decode('UTF-8').split(': ', 2)[1].strip() top_output = None
for line in check_output(['top', '-b', '-n1', '-d1']).decode('UTF-8').splitlines():
if line.lower().strip().startswith('%cpu'):
top_output = line.lower().split(':', 2)[1]
break
if not top_output:
print('%cpu not found in top output')
exit(3)
cpu_usage = {} cpu_usage = {}
for value, identifier in findall('([0-9\.\,]{3,5}) ([a-z]{2})', top_output): for value, identifier in findall('([0-9\.\,]{3,5}) ([a-z]{2})', top_output):