Network bandwidth for monitoring processes under Linux

Source: Internet
Author: User

Well, recently in NetEase games internship, so it seems like a period of time did not come up to write something ... NetEase games to practice the main purpose is to know what is in the game company work is what content, after all, they have not contacted the game company before. Also compare the curiosity ...

Before May think the game company various tall bar, does the work various interesting. Now it should not be like this, after all, most of the time should still be writing the logic of the game code ...

Very strange ... I feel that although in the game company internship, but do things are their own old bank ... Because you want to use Python to develop a server monitoring system, and have to monitor a specified process occupied by the network bandwidth situation ... Well.. Looking for Python there is no ready-made thing to do this thing. And they can use less of the things ...

But I did find something like that ... Nethogs ... A program dedicated to monitoring the network bandwidth of each process under Linux ...

So I looked directly at his code implementation ... The code is not many ... C + + written by ... And very simple ... Here is not specifically said this part of the code ... Just say the principle of functional implementation ....


Well, one of the more important directories:/proc

Well, it is a pseudo-directory, because it does not exist in the real file system, but a run-time created directory, and the information is stored in some runtime information, such as process information, network connection information, etc...

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

/proc/7140/fd

In fact, look at the name here will probably be able to guess what is stored in this directory is what data it: The current process opened the file descriptor ... Such as:



Well, this should be very familiar ... A little bit about Linux programming below should know that in the program, our sockets, or open files are corresponding to a file descriptor, then the above directory is saved in our program running file descriptor situation. And then these are actually links, eventually connected to an inode above ... Here you can see that our two sockets correspond to 23045 and 23046 of the two inode ....


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

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

We can get the output such as:


Here is all the TCP information that is currently created by this machine ... IP address, port what ... Finally we can see a project: Inode ... Which is the inode associated with this link ...

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


All right... Through the process above. We can get a process that has all the connection information it has built ...

So the next step is how to calculate its network traffic situation ...

Well, the nethogs used here is to use the LIBCAP to grab the packet, and then analyze the network packet to calculate the network traffic of each connection. (Well, a lot of code is the direct use of the Nethogs source code)

Well, it's not specific here to list the implementation code.

In C + + and Libcap for Python wrote a Linux extension library ... The code is uploaded to GitHub ... The address is as follows:

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

Click to open link


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.