executable_sensors.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env python3
  2. import sys, datetime, psutil
  3. from time import sleep, time
  4. class Time:
  5. @staticmethod
  6. def date():
  7. return datetime.datetime.now().strftime(f"%a %b %-e %I:%M:%S %p")
  8. class CPU:
  9. @staticmethod
  10. def coretemp():
  11. return str(
  12. round(psutil.sensors_temperatures(fahrenheit=False)["k10temp"][0][1], 1)
  13. )
  14. @staticmethod
  15. def freq():
  16. return str(round(int(psutil.cpu_freq(percpu=False)[0]) / 1000, 1))
  17. @staticmethod
  18. def usage():
  19. u = psutil.cpu_percent(interval=0.1, percpu=False)
  20. return f"{u}%"
  21. class DISK:
  22. @staticmethod
  23. def disk_total(mnt):
  24. return f"{round(psutil.disk_usage(mnt).total/1000**3, 1)}"
  25. def disk_used(mnt):
  26. return f"{round(psutil.disk_usage(mnt).used/1000**3, 1)}"
  27. class RAM:
  28. @staticmethod
  29. def ram_total():
  30. return f"{round(psutil.virtual_memory().total/1000**3, 1)}"
  31. @staticmethod
  32. def ram_used():
  33. return f"{round(psutil.virtual_memory().used/1000**3, 1)}"
  34. @staticmethod
  35. def swap_total():
  36. return f"{round(psutil.swap_memory().total/1000**3, 2)}"
  37. @staticmethod
  38. def swap_used():
  39. return f"{round(psutil.swap_memory().used/1000**3, 2)}"
  40. class Network:
  41. time_down = 0
  42. time_up = 0
  43. bytes_recv = 0
  44. bytes_sent = 0
  45. @staticmethod
  46. def get_bytes_down():
  47. bytes_delta = psutil.net_io_counters().bytes_recv - Network.bytes_recv
  48. time_delta = time() - Network.time_down
  49. download_speed = (bytes_delta / time_delta) // 1000
  50. Network.time_down = time()
  51. Network.bytes_recv = psutil.net_io_counters().bytes_recv
  52. return download_speed
  53. @staticmethod
  54. def get_bytes_up():
  55. bytes_delta = psutil.net_io_counters().bytes_sent - Network.bytes_sent
  56. time_delta = time() - Network.time_up
  57. upload_speed = (bytes_delta / time_delta) // 1000
  58. Network.time_up = time()
  59. Network.bytes_sent = psutil.net_io_counters().bytes_sent
  60. return upload_speed
  61. class Battery:
  62. @staticmethod
  63. def percent():
  64. return int(psutil.sensors_battery().percent)
  65. while True:
  66. sys.stdout.write(
  67. f"Network: up: {Network.get_bytes_up()}KB/s down: {Network.get_bytes_down()}KB/s CPU: {CPU.freq()} GHz {CPU.coretemp()} C /: {DISK.disk_used('/')} GB/{DISK.disk_total('/')} GB /srv: {DISK.disk_used('/srv')} GB/{DISK.disk_total('/srv')} GB RAM: {RAM.ram_used()} GB/{RAM.ram_total()} GB SWAP: {RAM.swap_used()} GB/{RAM.swap_total()} GB Time: {datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S')} "
  68. )
  69. sys.stdout.flush()
  70. sleep(0.75)