2019-07-18 16:45:46 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
colors = {
|
2020-04-09 16:02:57 +00:00
|
|
|
'us': '#FDD835', # us, user : time running un-niced user processes
|
|
|
|
'sy': '#23A8F1', # sy, system : time running kernel processes
|
2019-07-18 16:45:46 +00:00
|
|
|
'ni': '#FFFFFF', # ni, nice : time running niced user processes
|
|
|
|
'wa': '#E53935', # wa, IO-wait : time waiting for I/O completion
|
|
|
|
'hi': '#66CDAA', # hi : time spent servicing hardware interrupts
|
|
|
|
'si': '#7FFFD4', # si : time spent servicing software interrupts
|
|
|
|
'st': '#7B2B4E', # st : time stolen from this vm by the hypervisor
|
|
|
|
'id': '#555555'
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def show_cpu_usage():
|
|
|
|
new_env = dict(os.environ)
|
|
|
|
new_env['LANG'] = 'en_US.UTF-8' # ensure that the decimal separator is a point
|
|
|
|
top = subprocess.run(["top", "-bn1"], capture_output=True, env=new_env)
|
|
|
|
|
|
|
|
raw_cpu_usages = top.stdout.decode("utf-8").split('\n')[2]
|
|
|
|
cpu_usage = str.replace(raw_cpu_usages, '%Cpu(s):', '')
|
|
|
|
|
|
|
|
bar = ''
|
|
|
|
global_count = 0
|
|
|
|
for usage in cpu_usage.split(','):
|
2023-06-30 17:48:49 +00:00
|
|
|
values = str.lstrip(usage).split(' ')
|
|
|
|
count = round(float(values[0]) / 10)
|
|
|
|
if count > 0 and values[1] != 'id':
|
2019-07-18 16:45:46 +00:00
|
|
|
global_count += count
|
2023-06-30 17:48:49 +00:00
|
|
|
bar += '%{F' + colors[values[1]] + '}' + '_' * count + '%{F-}'
|
2019-07-18 16:45:46 +00:00
|
|
|
|
|
|
|
padding = ''
|
|
|
|
if global_count < 10:
|
|
|
|
padding = '%{F' + colors['id'] + '}' + '_' * (10 - global_count) + '%{F-}'
|
|
|
|
|
2022-03-05 12:00:21 +00:00
|
|
|
print("%{A1:$TERMINAL_CMD 'top -o %CPU' &:}%{T5}" + bar + padding + "%{T-}%{A}")
|
2019-07-18 16:45:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
show_cpu_usage()
|