123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- import psutil
- from datetime import datetime
- import pandas as pd
- import time
- import os
- sort_by = "memory_usage"
- columns = [
- "name",
- "cpu_usage",
- "memory_usage",
- "read_bytes",
- "write_bytes",
- "status",
- "create_time",
- "nice",
- "n_threads",
- "cores",
- "username",
- ]
- name_filter = ["pwrplay.exe", "httpd.exe", "impadmin.exe", "trnsfrmr.exe"]
- def get_size(bytes):
- """Returns size of bytes in a nice format"""
- for unit in ["", "K", "M", "G", "T", "P"]:
- if bytes < 1024:
- return f"{bytes:.2f}{unit}B"
- bytes /= 1024
- def get_processes_info(name_filter):
- processes = []
- for process in psutil.process_iter():
- with process.oneshot():
- pid = process.pid
- if pid == 0:
- continue
- name = process.name().lower()
- if name not in name_filter:
- continue
- try:
- create_time = datetime.fromtimestamp(process.create_time())
- except OSError:
- create_time = datetime.fromtimestamp(psutil.boot_time())
- try:
- cores = len(process.cpu_affinity())
- except psutil.AccessDenied:
- cores = 0
- cpu_usage = process.cpu_percent()
- status = process.status()
- try:
- nice = int(process.nice())
- except psutil.AccessDenied:
- nice = 0
- try:
- memory_usage = process.memory_full_info().uss
- except psutil.AccessDenied:
- memory_usage = 0
- io_counters = process.io_counters()
- read_bytes = io_counters.read_bytes
- write_bytes = io_counters.write_bytes
- n_threads = process.num_threads()
- try:
- username = process.username()
- except psutil.AccessDenied:
- username = "N/A"
- processes.append(
- {
- "pid": pid,
- "name": name,
- "create_time": create_time,
- "cores": cores,
- "cpu_usage": cpu_usage,
- "status": status,
- "nice": nice,
- "memory_usage": memory_usage,
- "read_bytes": read_bytes,
- "write_bytes": write_bytes,
- "n_threads": n_threads,
- "username": username,
- }
- )
- return processes
- def construct_dataframe(processes):
- df = pd.DataFrame(processes)
- df.set_index("pid", inplace=True)
- df.sort_values(sort_by, inplace=True)
- # pretty printing bytes
- df["memory_usage"] = df["memory_usage"].apply(get_size)
- df["write_bytes"] = df["write_bytes"].apply(get_size)
- df["read_bytes"] = df["read_bytes"].apply(get_size)
- # convert to proper date format
- df["create_time"] = df["create_time"].apply(datetime.strftime, args=("%Y-%m-%d %H:%M:%S",))
- # reorder and define used columns
- df = df[columns]
- return df
- def main():
- while True:
- processes = get_processes_info(name_filter)
- if len(processes) > 0:
- df = construct_dataframe(processes)
- os.system("cls") if "nt" in os.name else os.system("clear")
- print(df.to_string())
- else:
- print("No active processes.")
- time.sleep(10)
- if __name__ == "__main__":
- main()
|