Using Python scripts to monitor network card traffic for Linux servers

Source: Internet
Author: User

* This article already has the relevant code on the net, in order to deepen the impression, I made the related comment, hoped to be helpful to the friend

How it works: Based on the/proc file system

The Linux system provides an excellent way for administrators to change the kernel while the system is running, without rebooting the kernel system, which is achieved through the/proc virtual file system. The/proc file virtual system is a mechanism that the kernel and kernel modules use to send information to the process (so called "/proc"), a pseudo-file system that allows interaction with the kernel's internal data structures to obtain useful information about the process, in operation (on the Fly) to change the settings (by changing the kernel parameters). Unlike other file systems,/proc exists in memory and not on the hard disk. The information provided by the proc file system is as follows:

1: Process information: Any process in the system, in the proc subdirectory has a process ID of the same name, you can find CmdLine,

Mem, Root, Stat, STATM, and status. Some information is only visible to the superuser, such as the process root directory. Each individual containing a

A process with existing process information has some specialized links available, and any process in the system has a separate self-linking point to the process

Information, which is useful for getting command-line information from the process.
2: System Information: If you need to know the entire system information can also be obtained from the/proc/stat, including CPU usage, disk

Space, memory swap, interrupt, and so on.
3:CPU information: Use the/proc/cpuinfo file to obtain the current accurate information of the CPU.
Payload information: The/proc/loadavg file contains system payload information.
4: System memory Information: The/proc/meminfo file contains details of the system memory, which shows the number of physical memory, the available swap null

And the amount of free memory, among other things.

Notes from http://www.ibm.com/developerworks/cn/linux/1312_caojh_pythonlinux/index.html

Using Python scripts to monitor Linux servers
#!/usr/bin/env python# _*_coding:utf-8_*_# file:get_net_infor.py# Author li ning# Date 2015.11.30 00:41:35 
Import sysimport Time "" "Principle: Linux system based on the principle of all documents, we can put any information as a string read into the analysis" "#若没有提供参数, here The default is the network card is eth0, according to their actual situation to connect parameters. Eg: (python get_net_infor.py eth1) If Len (sys.argv) > 1:interface = sys.argv[1]else:interface = "eth0" print "currently viewed NIC is:", Interfa Cestats = [] #定义接收流量函数def rx (): #open read file is very large, it is very time-consuming to read into memory, it is recommended to use ReadLine instead of readlines ifstat = open ("/proc/net/dev"). R            Eadlines () #遍历list the string in the Ifstat for interface in Ifstat: #判断eth0是否在字符串中 if interface in interface: #默认空格分裂字符串获取需要的流量包值 stat = float (Interface.split () [1]) #将值复制给STATS stats[0:] = [St At] #定义发送流量函数def TX (): Ifstat = open ("/proc/net/dev"). ReadLines () to interface in Ifstat:if interface in int Erface:stat = float (Interface.split () [9]) stats[1:] = [stat]if __name__ = = ' __main__ ': print ' in         "," >>>> "," Out "Rx () TX () #死循环 Time to monitor network card traffic while True:time.sleep (1) sta_0 = List (STATS) Rx () TX () RX = float (stats[0]) rx_0 = sta_0[0] TX = float (stats[0]) tx_0 = sta_0[1] #round函数转换, retain three-bit RX after decimal point _info = Round ((rx-rx_0)/1024/1024,3) Tx_info = Round ((tx-tx_0)/1024/1024,3) print rx_info, "MB", Tx_info, "mb "
The following is a local run result

Using Python scripts to monitor network card traffic for Linux servers

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.