refactor: fix who and loads, optimize the rest
This commit is contained in:
1 file changed
+71
-44
@@ -1,14 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
import os
|
||||
import misc
|
||||
from misc import bar, gig
|
||||
|
||||
# MISCELLANEOUS
|
||||
|
||||
# SYSTEM IS UP
|
||||
|
||||
# "up <time>\n"
|
||||
uptime = os.popen('uptime -p').read()
|
||||
|
||||
# split to remove first word and newline
|
||||
print("System is up \x1b[34m{}\x1b[0m".format(uptime[3:-1]))
|
||||
|
||||
who = os.popen('echo " 15:13:23 up 10 days, 1:33, 2 users, load average: 1.01, 0.95, 0.98\nUSER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT\nmathias pts/0 10.0.1.4 15:12 1.00s 0.09s 0.00s w\nmathias pts/1 tmux(2498).%0 02Apr20 14:10m 2days 0.00s /bin/bash ./start"').read()
|
||||
|
||||
# N USERS N TERMINALS
|
||||
|
||||
who = os.popen('w').read()
|
||||
|
||||
who = who.split('\n')[2:-1]
|
||||
users = [0, 0]
|
||||
@@ -19,15 +25,18 @@ for line in who:
|
||||
else:
|
||||
users[0] += 1
|
||||
|
||||
print("{} users and {} terminals logged in".format(*users))
|
||||
print("{} users and {} terminals".format(*users))
|
||||
|
||||
# HARDWARE STATS
|
||||
|
||||
loads = os.popen("uptime").read()
|
||||
loads = loads.split()[-3:]
|
||||
# CPU LOADS
|
||||
|
||||
loads = os.popen("cat /proc/loadavg").read()
|
||||
loads = loads.split(' ')[:3]
|
||||
loads[0] = loads[0][:-1]
|
||||
loads[1] = loads[1][:-1]
|
||||
print("CPU: ", end = '')
|
||||
|
||||
print("CPU: ", end='')
|
||||
|
||||
for load in loads:
|
||||
load = float(load)
|
||||
color = '\x1b[32m'
|
||||
@@ -36,58 +45,76 @@ for load in loads:
|
||||
else:
|
||||
if load > 0.5:
|
||||
color = '\x1b[33m'
|
||||
|
||||
print('{}{} '.format(color, load), end = '')
|
||||
print('\x1b[0m\n', end = '')
|
||||
|
||||
free = os.popen("free").read()
|
||||
memory = free.split('\n')[1].split()
|
||||
swap = free.split('\n')[2].split()
|
||||
print("Memory: {:.1f}GB out of {:.1f}GB".format(misc.gig(memory[2]), misc.gig(memory[1])))
|
||||
print(misc.bar(memory[2], memory[1], ['\x1b[93m=', '-']))
|
||||
print('{}{} '.format(color, load), end='')
|
||||
print('\x1b[0m\n')
|
||||
|
||||
print("Swap: {:.1f}GB out of {:.1f}GB".format(misc.gig(swap[2]), misc.gig(swap[1])))
|
||||
print(misc.bar(swap[2], swap[1], ['\x1b[93m=', '-']))
|
||||
|
||||
# MEMORY USAGE
|
||||
|
||||
free = os.popen("free").read().split('\n')
|
||||
|
||||
memory = free[1].split()
|
||||
swap = free[2].split()
|
||||
|
||||
print(
|
||||
"Memory: {:.1f}GB out of {:.1f}GB (Swap {:.1f}/{:.1f})"
|
||||
.format(
|
||||
gig(memory[2]),
|
||||
gig(memory[1]),
|
||||
gig(swap[2]),
|
||||
gig(swap[1])
|
||||
)
|
||||
)
|
||||
|
||||
print(bar(memory[2], memory[1], ['\x1b[93m=', '-']))
|
||||
print()
|
||||
|
||||
# NETWORK ADAPTERS LIST
|
||||
|
||||
ip = os.popen('ip addr').read()
|
||||
ip = ip.split('\n')[:-1]
|
||||
# get all cards
|
||||
ip = os.popen('ip addr').read().split('\n')[:-1]
|
||||
|
||||
# get virtual cards
|
||||
virtual = os.popen(
|
||||
"ls /sys/class/net -l | grep virtual | awk '{print $9}'").read().split('\n')
|
||||
|
||||
links = []
|
||||
skip = False
|
||||
nicNameLen = 0
|
||||
|
||||
for line in ip:
|
||||
line = line.split()
|
||||
line = line.split()
|
||||
|
||||
if ':' in line[0]:
|
||||
if 'UP' in line[2]:
|
||||
eth = line[1][:-1]
|
||||
links.append([eth])
|
||||
if len(eth) > nicNameLen:
|
||||
nicNameLen = len(eth)
|
||||
skip = False
|
||||
else:
|
||||
skip = True
|
||||
if ':' in line[0]:
|
||||
if 'UP' in line[2]:
|
||||
eth = line[1][:-1]
|
||||
links.append([eth])
|
||||
if len(eth) > nicNameLen:
|
||||
nicNameLen = len(eth)
|
||||
skip = False
|
||||
else:
|
||||
skip = True
|
||||
|
||||
if skip: continue
|
||||
if skip:
|
||||
continue
|
||||
|
||||
if 'link' in line[0]:
|
||||
links[-1].append([line[1]])
|
||||
if 'link' in line[0]:
|
||||
links[-1].append([line[1]])
|
||||
|
||||
if 'inet' in line[0]:
|
||||
links[-1][-1].append(line[1].split('/'))
|
||||
if 'inet' in line[0]:
|
||||
links[-1][-1].append(line[1].split('/'))
|
||||
|
||||
for eth in links:
|
||||
if eth[0] == 'lo': continue
|
||||
# skip virtual cards
|
||||
if eth[0] in virtual:
|
||||
continue
|
||||
|
||||
print("{}:{} {} - \x1b[36m{}/{}\x1b[0m".format(
|
||||
eth[0],
|
||||
" "*(nicNameLen-len(eth[0])),
|
||||
eth[1][0],
|
||||
eth[1][1][0],
|
||||
eth[1][1][1]
|
||||
))
|
||||
print()
|
||||
print("{}:{} {} - \x1b[36m{}/{}\x1b[0m".format(
|
||||
eth[0],
|
||||
" "*(nicNameLen-len(eth[0])),
|
||||
eth[1][0],
|
||||
eth[1][1][0],
|
||||
eth[1][1][1]
|
||||
))
|
||||
print()
|
||||
Reference in new issue
Block a user