Implement monitoring process network bandwidth under linux

  
        

Well, I’ve been internships in Netease games lately, so it seems that I haven’t come up to write something for a while. . . The main purpose of coming to the NetEase game internship is actually to know what is working in the game company. After all, I have never touched the game company before. . Still curious. . .

I used to think that the game companies are all kinds of tall, and the work they do is interesting. . It seems that it should not be like this now, after all, most of the time should still be writing the logic code of the game. . .

Very strange. . . I feel that although I am an intern at a game company, I still do my own thing. . . Because you want to develop a server monitoring system in python, and there are cases where you want to monitor the network bandwidth occupied by a specified process. . . Ok. . Looking for python has no ready-made things to do this thing. . And there are fewer things that you can use. . .

But I found something similar. . . Nethogs. . . A program specifically designed to monitor the network bandwidth of each process under Linux. . .

So I looked directly at his code implementation. . . The code is not a lot. . . Written in c++. . . And it's very simple. . . This part of the code is not specifically mentioned here. . . Let's talk about the principle of function implementation. . . .

Well, a more important directory: /proc

Well, in fact, it is a pseudo directory, because it does not exist in the real file system, but a run The directory created at the time, and the saved information is also some runtime information, such as process information, network connection information or something. . .

Ok, now suppose we have a process whose PID is 7140, then we can go to the following directory:

/proc/7140/fd

Actually look at the name here It is possible to guess what data is stored in this directory: the file descriptor opened by the current process. . . As shown below:


Well, this should be very familiar. . . A little understanding of Linux programming below should know that in the program, our socket, or open file corresponds to a file descriptor, then the above directory will save the file descriptors in our program. . Then these are actually links, and finally connected to an inode. . . Here we can see that our two sockets correspond to the two inodes of 23045 and 23046. . . .

Then how do we find the specific information of these two sockets? . . ?

Well, enter the following command in the terminal: cat /proc/net/tcp

The output we can get is as follows:

Here is all the current local machine built. Tcp information. . . Ip address, port 啥. . . Finally we can also see a project: inode. . . That is the link to which the inode it is associated with. . .

You should be able to map the file descriptor of the process you saw earlier with its specific link information. . .

Okay. . . Through the above process. . We can get information about all the connections that a process has established. . .

Then the next step is how to calculate its network traffic. . .

Well, the method used by nethogs here is to use libcap to capture packets and then analyze the network packets to calculate the network traffic of each connection. . . (Well, a lot of code is directly used by the nethogs source code)

Well, here is not specific to list the implementation code. .

Using c++ and libcap to write a linux extension library for python. . . The code passed to github. . . The address is as follows:

https://github.com/2225377fjs/processNet

Copyright © Windows knowledge All Rights Reserved