Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.07 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. Awesome dynmaic motd script. Displays a bunch of useful information such as
  4. your public IP, uptime, and CPU/RAM/Disk usages
  5.  
  6. Requirements
  7. install these with `pip install <requirement>`, may need to install
  8. python-pip from system package manage
  9.  
  10. * colorama (ASNI terminal color support)
  11. * psutil (CPU/RAM/Disk usage)
  12. * uptime (system update)
  13.  
  14. Installing
  15. # wget this file to `/usr/local/bin/dynmotd` or wherever else you like
  16. # `chmod +x <file>` to make it executable
  17. # Add `<file>` to `/etc/profile`
  18. # Remove `pam_motd.so` from `/etc/pam.d/sshd` or `/etc/pam.d/system-login`, which ever it is in
  19. """
  20. import math
  21. import platform
  22. import psutil
  23. import socket
  24. import uptime
  25.  
  26. from colorama import init
  27. from colorama import Fore, Back, Style
  28. from datetime import timedelta
  29.  
  30. MAX = 80
  31. init()
  32.  
  33. def print_header(header_text=None):
  34. if header_text is not None:
  35. num_plus = (MAX - (len(header_text) + 4)) / 2
  36. print('{}{}: {}{} {}:{}'.format(
  37. Fore.MAGENTA, '+' * math.floor(num_plus),
  38. Fore.WHITE, header_text,
  39. Fore.MAGENTA, '+' * math.ceil(num_plus)))
  40. else:
  41. print('{}{}'.format(Fore.MAGENTA, '+' * MAX))
  42.  
  43. def print_line(line):
  44. num_space = MAX - len(line) - 3
  45. print('{}+ {}{}{}{}+'.format(
  46. Fore.MAGENTA, Fore.RED, line,
  47. ' ' * num_space, Fore.MAGENTA))
  48.  
  49. def print_kvline(key, value):
  50. num_space = MAX - len(key) - len(value) - 5
  51. print('{}+ {}{}: {}{}{}{}+'.format(
  52. Fore.MAGENTA, Fore.WHITE, key,
  53. Fore.CYAN, value,
  54. ' ' * num_space, Fore.MAGENTA))
  55.  
  56. def total_string(used, total):
  57. mb = math.pow(10, 6)
  58. return '{}MB / {}MB | {:.1f}% free'.format(
  59. math.floor(used/mb),
  60. math.floor(total/mb),
  61. (1-used/total) * 100)
  62.  
  63. cpu = psutil.cpu_percent(interval=0.5, percpu=True)
  64. memory = psutil.virtual_memory()
  65. disk = []
  66. for d in psutil.disk_partitions():
  67. if d.fstype.lower().startswith('ext'):
  68. disk.append({
  69. 'disk': d.mountpoint,
  70. 'usage': psutil.disk_usage(d.mountpoint)})
  71.  
  72.  
  73. print_header('System Information')
  74. ip = socket.gethostbyname(socket.gethostname())
  75. distro = platform.linux_distribution()[0].replace('arch', 'ArchLinux')
  76. kernel = platform.release()
  77. uptime = timedelta(seconds=math.floor(uptime.uptime()))
  78. disk_used = 0
  79. disk_total = 0
  80. for d in disk:
  81. disk_used = disk_used + d['usage'].used
  82. disk_total = disk_total + d['usage'].total
  83.  
  84. print_kvline('Address', ip)
  85. print_kvline('OS', '{} running {}'.format(distro, kernel))
  86. print_kvline('Uptime', str(uptime))
  87. print_kvline('CPU', '% '.join(['{:.1f}'.format(x) for x in cpu]) + '%')
  88. print_kvline('Memory', total_string(memory.total - memory.available, memory.total))
  89. print_kvline('Disk', total_string(disk_used, disk_total))
  90.  
  91.  
  92. print_header('Filesystem')
  93. [print_kvline('{0:10}'.format(x['disk']), total_string(x['usage'].used, x['usage'].total)) for x in disk]
  94.  
  95.  
  96. print_header('Extra Information')
  97. motd = []
  98. with open('/etc/motd') as f:
  99. motd = f.readlines()
  100. [print_line(x.replace('\n', '')) for x in motd]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement