Network bandwidth of monitoring processes in linux

Source: Internet
Author: User

Well, I 've been practicing in NetEase games recently, so it seems that I haven't written anything for a while... The main purpose of Netease game internship is to know what it is about to work in a game company. After all, I have never been in touch with a game company before .. Still curious...

In the past, I may think that gaming companies have all kinds of advantages and they have all kinds of interesting work .. It seems that this is not the case now. After all, the logic code of the game should be written most of the time...

It's strange... I feel that although I am an intern at a game company, I am still doing my own work... Because python is used to develop a monitoring system for a server, and it is suggested to monitor the network bandwidth occupied by a specified process... Well .. There is no ready-made thing in python to do this .. In addition, there are few things that can be used...

But I found something similar... Nethogs... A program that monitors the network bandwidth of various processes in linux...

So I directly read his code implementation... Not a lot of code... Written in c ++... And very easy... This part of the code is not detailed here... Let's talk about the principle of function implementation ....


Well, a more important Directory:/proc

Well, it is actually a pseudo directory, because it does not exist in a real file system, but a directory created at runtime, the saved information is also some running information, such as process information and network connection information...

Well, now let's assume that we have a process whose PID is 7140, we can go to the following directory:

/Proc/7140/fd

In fact, the name can probably guess what data is stored in this directory: The file descriptor opened by the current process... For example:



Well, you should be familiar with this... If you know a little about programming in linux, you should know that in the program, our socket or open files correspond to a file descriptor, the above directory stores the file descriptor in the program running .. Then these are actually links and eventually connected to an inode... Here we can see that the two sockets correspond to the inode 23045 and 23046 ....


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

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

The output is as follows:


Here is the information of all tcp established on the current machine... IP address, port... Finally, we can see a project: inode... That is, the inode associated with this link...

At this point, we should be able to match the file descriptor of the preceding process with its specific link information...


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

The next step is how to calculate the network traffic...

Well, Here nethogs uses libcap to capture packets and analyze the network packets to calculate the network traffic of each connected network... (Well, a lot of code is the source code of nethogs)

Well, the implementation code is not listed here ..

I used c ++ and libcap to write an extension library under linux for python... The code is passed to github... The address is as follows:

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

Click 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.