autoiop.blogg.se

Python linux process monitor
Python linux process monitor







python linux process monitor

This function accepts several parameters, one of the important ones is the callback that is called whenever a packet is captured. If you're not familiar with Scapy, then to be able to sniff packets, we have to use the sniff() function provided by this library.

python linux process monitor python linux process monitor

  • is_program_running is simply a boolean that is when set to False, the program will stop and exit.
  • global_df is a Pandas dataframe that is used to store the previous traffic data (so we can calculate the usage).
  • pid2traffic is another dictionary that maps each process ID (PID) to a list of two values representing the upload and download traffic.
  • connection2pid is a Python dictionary that maps each connection (represented as the source and destination ports on the TCP/UDP layer).
  • all_macs is a Python set that contains the MAC addresses of all network interfaces in our machine.
  • # global boolean for status of the programĪfter we import the necessary libraries, we initialize our global variables that will be used in our upcoming functions: # the global Pandas DataFrame that's used to track previous traffic stats # A dictionary to map each process ID (PID) to total Upload (0) and Download (1) traffic # A dictionary to map each connection to its correponding process ID (PID) # print the total download/upload along with current speeds Us, ds = io_2.bytes_sent - bytes_sent, io_2.bytes_recv - bytes_recv

    python linux process monitor

    Now let's enter the loop that gets the same stats but after a delay so we can calculate the download and upload speed: while True: # extract the total bytes sent and receivedīytes_sent, bytes_recv = io.bytes_sent, io.bytes_recv Next, we will use _io_counters() function that returns the network input and output statistics: # get the network I/O stats from psutil Starting with the simplest program Let's import psutil and make a function that prints the bytes in a nice format: import psutilįor unit in : Psutil is a cross-platform library for retrieving information on running processes and system and hardware information in Python, we will be using it for retrieving network statistics as well as established connections. To get started, let's install the required libraries: $ pip install psutil scapy pandas Have you ever wanted to make a program that monitors the network usage of your machine? In this tutorial, we will make three Python scripts that monitor total network usage, network usage per network interface, and network usage per system process:









    Python linux process monitor