Last active
April 7, 2021 16:46
-
-
Save AngellusMortis/192965a2c0936a0e7c84 to your computer and use it in GitHub Desktop.
Dynamic MOTD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Awesome dynmaic motd script. Displays a bunch of useful information such as | |
your public IP, uptime, and CPU/RAM/Disk usages | |
Requirements | |
install these with `pip install <requirement>`, may need to install | |
python-pip from system package manage | |
* colorama (ASNI terminal color support) | |
* psutil (CPU/RAM/Disk usage) | |
* uptime (system update) | |
Installing | |
# wget this file to `/usr/local/bin/dynmotd` or wherever else you like | |
# `chmod +x <file>` to make it executable | |
# Add `<file>` to `/etc/profile` | |
# Remove `pam_motd.so` from `/etc/pam.d/sshd` or `/etc/pam.d/system-login`, which ever it is in | |
""" | |
import math | |
import platform | |
import psutil | |
import socket | |
import uptime | |
from colorama import init | |
from colorama import Fore, Back, Style | |
from datetime import timedelta | |
MAX = 80 | |
init() | |
def print_header(header_text=None): | |
if header_text is not None: | |
num_plus = (MAX - (len(header_text) + 4)) / 2 | |
print('{}{}: {}{} {}:{}'.format( | |
Fore.MAGENTA, '+' * math.floor(num_plus), | |
Fore.WHITE, header_text, | |
Fore.MAGENTA, '+' * math.ceil(num_plus))) | |
else: | |
print('{}{}'.format(Fore.MAGENTA, '+' * MAX)) | |
def print_line(line): | |
num_space = MAX - len(line) - 3 | |
print('{}+ {}{}{}{}+'.format( | |
Fore.MAGENTA, Fore.RED, line, | |
' ' * num_space, Fore.MAGENTA)) | |
def print_kvline(key, value): | |
num_space = MAX - len(key) - len(value) - 5 | |
print('{}+ {}{}: {}{}{}{}+'.format( | |
Fore.MAGENTA, Fore.WHITE, key, | |
Fore.CYAN, value, | |
' ' * num_space, Fore.MAGENTA)) | |
def total_string(used, total): | |
mb = math.pow(10, 6) | |
return '{}MB / {}MB | {:.1f}% free'.format( | |
math.floor(used/mb), | |
math.floor(total/mb), | |
(1-used/total) * 100) | |
cpu = psutil.cpu_percent(interval=0.5, percpu=True) | |
memory = psutil.virtual_memory() | |
disk = [] | |
for d in psutil.disk_partitions(): | |
if d.fstype.lower().startswith('ext'): | |
disk.append({ | |
'disk': d.mountpoint, | |
'usage': psutil.disk_usage(d.mountpoint)}) | |
print_header('System Information') | |
ip = socket.gethostbyname(socket.gethostname()) | |
distro = platform.linux_distribution()[0].replace('arch', 'ArchLinux') | |
kernel = platform.release() | |
uptime = timedelta(seconds=math.floor(uptime.uptime())) | |
disk_used = 0 | |
disk_total = 0 | |
for d in disk: | |
disk_used = disk_used + d['usage'].used | |
disk_total = disk_total + d['usage'].total | |
print_kvline('Address', ip) | |
print_kvline('OS', '{} running {}'.format(distro, kernel)) | |
print_kvline('Uptime', str(uptime)) | |
print_kvline('CPU', '% '.join(['{:.1f}'.format(x) for x in cpu]) + '%') | |
print_kvline('Memory', total_string(memory.total - memory.available, memory.total)) | |
print_kvline('Disk', total_string(disk_used, disk_total)) | |
print_header('Filesystem') | |
[print_kvline('{0:10}'.format(x['disk']), total_string(x['usage'].used, x['usage'].total)) for x in disk] | |
print_header('Extra Information') | |
motd = [] | |
with open('/etc/motd') as f: | |
motd = f.readlines() | |
[print_line(x.replace('\n', '')) for x in motd] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment