Obtain the cpu, memory, and network data usage in the proc file system (with the source code for detecting the network speed)

Source: Internet
Author: User

1) processor usage (2) memory usage (3) inbound and outbound data packets (4) the overall network load data should be extracted from the/proc/stat,/proc/meminfo,/proc/net/dev files, in fact, in Embedded linux, vlan division interfaces are generally used. The traffic of a specific interface can be seen in the/proc/net/vlan/interface name file. If you have any problems or are not clear about the data to be extracted, you can use man proc to view the online manual of the proc file system. (1) processor usage here we need to extract four pieces of data from/proc/stat: user mode, nice mode, and system mode) and idle processor time (idle ). They are located in the first line of the/proc/stat file. The CPU usage is calculated using the following formula. CPU usage = 100 * (user + nice + system)/(user + nice + system + idle) CPU usage = 100 * (idle system time idle)/(total system time) my previous blog has an explanation of the stat file for getting source code for cpu usage http://blog.csdn.net/jk110333/article/details/8683478 (2) memory usage here two data needs to be extracted from the/proc/meminfo file, current memory usage (cmem) and total memory (amem ). Memory usage percentage = 100 * (cmem/umem) (3) network utilization in order to obtain the data related to network utilization, two data needs to be obtained from the/proc/net/dev file: number of data packets output from the local machine and the number of data packets flowing into the local machine. Both are located in the fourth row of the file. The performance collection program starts to record the initial values of the two data values. After each value is obtained, the initial value is subtracted from the data packets passed through the current node starting from the cluster. The above data is used to calculate the average network load. The method is as follows: average network load = (output packet + incoming packet) /2 obtain the function through/proc/net/vlan/eth2.2 as follows [cpp] // obtain the network speed level long int last_bytes = 0; # define file_name "/proc/net/vlan/eth2.2" # define CHECK_SPEED 5 // time interval for detecting the network speed int check_speed_new (void) {FILE * fp; int nl = 0, ifl = 0; char buf [128]; long int data = 0; char character [10], ch2 [10], ch3 [10]; fp = fopen (file_name, "r"); while (nl <2) {if (fgets (buf, sizeof (buf), fp) = NULL) re Turn-1; nl ++;} if (fgets (buf, sizeof (buf), fp )! = NULL) {if (sscanf (buf, "% s % d", buckets, ch2, ch3, & data )! = 4) {fclose (fp); return-1 ;}} fclose (fp); int bytes; // printf ("data = % d \ n", data ); bytes = (data-last_bytes)/1024)/CHECK_SPEED; last_bytes = data; if (bytes> 50 & bytes <251) return 2; else if (bytes> = 0 & bytes <51) return 1; else if (bytes> 250 & bytes <500) return 3; else if (bytes> 500) return 4; else return-1 ;}

Related Article

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.