My name is Vamshi and I am a Master Thesis student at KTH Royal Institute of Technology. I am working on a project for my master thesis related to security detection using Machine Learning. Idea of the project is to monitor a application running in the
Linux environment using psutil. Using this monitored data construct a Machine Learning model to detect the security attacks when the application is under attack.
The application that I am planning to monitor, when it runs on the Linux environment, it starts more than one process. So, I want to monitor all these process at the same time. To construct the ML model, I need monitored data related to all these metrics
at the same time. I am just wondering if it is possible to collect using psutil. I have written a Python code to collect the metrics related to multiple processes that looks something like below:
import datetime def pids_data(multiple_pids): process_list = list() for pid in multiple_pids: for x in range(2): proc_info = None process_infos = {} try: process = psutil.Process(pid) proc_info = {"time": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"), "cpu_percent": process.cpu_percent(interval=2), "memory_percent": process.memory_percent() } except (psutil.ZombieProcess, psutil.AccessDenied, psutil.NoSuchProcess): proc_info = None if proc_info is not None: process_infos[process.name()]=proc_info process_list.append(process_infos) return process_list
The problem with the above code is that it won't collect
the metrics of multiple PIDs
at
the same time. For example, pids_data([9232,
9272])
is called, the function would return:
[{'abc':
{'time': '2019-05-06 12:09:01', 'cpu_percent': 39.5, memory_percent': 9.826277910758757}}, {'abc': {'time': '2019-05-06 12:09:03', 'cpu_percent': 39.4, 'memory_percent': 9.826277910758757,}}, {'def': {'time': '2019-05-06
12:09:07','cpu_percent': 0.5, 'memory_percent': 1.4573088996030759}}, {'def': {'time': '2019-05-06 12:09:09','cpu_percent': 0.5, 'memory_percent': 1.4573088996030759}}]
However, I want my output to be something like:
[{'abc':
{'time': '2019-05-06 12:09:01', 'cpu_percent': 39.5, memory_percent': 9.826277910758757}}, {'abc': {'time': '2019-05-06 12:09:03', 'cpu_percent': 39.4, 'memory_percent': 9.826277910758757,}}, {'def': {'time': '2019-05-06 12:09:01','cpu_percent':
0.5, 'memory_percent': 1.4573088996030759}}, {'def': {'time': '2019-05-06 12:09:03','cpu_percent': 0.5, 'memory_percent': 1.4573088996030759}}]
I am wondering if it possible using psutil to collect the data metrics related to multiple process at the same time. Please
let me know if it is not possible, which tool or what hack I can use to make it work. I will be pleased to hear from you. Waiting for a positive reply.
Thanks,
Best Regards,
Vamshi Pulluri.
for x in range(2):
for pid in multiple_pids: