process_monitor.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import psutil
  2. from datetime import datetime
  3. import pandas as pd
  4. import time
  5. import os
  6. sort_by = "memory_usage"
  7. columns = [
  8. "name",
  9. "cpu_usage",
  10. "memory_usage",
  11. "read_bytes",
  12. "write_bytes",
  13. "status",
  14. "create_time",
  15. "nice",
  16. "n_threads",
  17. "cores",
  18. "username",
  19. ]
  20. name_filter = ["pwrplay.exe", "httpd.exe", "impadmin.exe", "trnsfrmr.exe"]
  21. def get_size(bytes):
  22. """Returns size of bytes in a nice format"""
  23. for unit in ["", "K", "M", "G", "T", "P"]:
  24. if bytes < 1024:
  25. return f"{bytes:.2f}{unit}B"
  26. bytes /= 1024
  27. def get_processes_info(name_filter):
  28. processes = []
  29. for process in psutil.process_iter():
  30. with process.oneshot():
  31. pid = process.pid
  32. if pid == 0:
  33. continue
  34. name = process.name().lower()
  35. if name not in name_filter:
  36. continue
  37. try:
  38. create_time = datetime.fromtimestamp(process.create_time())
  39. except OSError:
  40. create_time = datetime.fromtimestamp(psutil.boot_time())
  41. try:
  42. cores = len(process.cpu_affinity())
  43. except psutil.AccessDenied:
  44. cores = 0
  45. cpu_usage = process.cpu_percent()
  46. status = process.status()
  47. try:
  48. nice = int(process.nice())
  49. except psutil.AccessDenied:
  50. nice = 0
  51. try:
  52. memory_usage = process.memory_full_info().uss
  53. except psutil.AccessDenied:
  54. memory_usage = 0
  55. io_counters = process.io_counters()
  56. read_bytes = io_counters.read_bytes
  57. write_bytes = io_counters.write_bytes
  58. n_threads = process.num_threads()
  59. try:
  60. username = process.username()
  61. except psutil.AccessDenied:
  62. username = "N/A"
  63. processes.append(
  64. {
  65. "pid": pid,
  66. "name": name,
  67. "create_time": create_time,
  68. "cores": cores,
  69. "cpu_usage": cpu_usage,
  70. "status": status,
  71. "nice": nice,
  72. "memory_usage": memory_usage,
  73. "read_bytes": read_bytes,
  74. "write_bytes": write_bytes,
  75. "n_threads": n_threads,
  76. "username": username,
  77. }
  78. )
  79. return processes
  80. def construct_dataframe(processes):
  81. df = pd.DataFrame(processes)
  82. df.set_index("pid", inplace=True)
  83. df.sort_values(sort_by, inplace=True)
  84. # pretty printing bytes
  85. df["memory_usage"] = df["memory_usage"].apply(get_size)
  86. df["write_bytes"] = df["write_bytes"].apply(get_size)
  87. df["read_bytes"] = df["read_bytes"].apply(get_size)
  88. # convert to proper date format
  89. df["create_time"] = df["create_time"].apply(datetime.strftime, args=("%Y-%m-%d %H:%M:%S",))
  90. # reorder and define used columns
  91. df = df[columns]
  92. return df
  93. def main():
  94. while True:
  95. processes = get_processes_info(name_filter)
  96. if len(processes) > 0:
  97. df = construct_dataframe(processes)
  98. os.system("cls") if "nt" in os.name else os.system("clear")
  99. print(df.to_string())
  100. else:
  101. print("No active processes.")
  102. time.sleep(10)
  103. if __name__ == "__main__":
  104. main()