Proc file system: queries CPU usage, memory usage,

Source: Internet
Author: User

"The proc file system is a pseudo file system, which only exists in the memory and does not occupy the external storage space. It provides interfaces for accessing system kernel data in the form of a file system. Users and applications can obtain system information through proc and change certain kernel parameters. "

This section describes how to obtain some firewall-related performance parameters from the/proc file system and how to modify the Kernel configuration through the/proc file system.

1. Obtain relevant performance parameters from the/proc file system
CPU usage:/proc/STAT
Memory usage:/proc/meminfo
Network load information:/proc/NET/dev

Calculation method: (from: proc file system, see references)
(1) processor usage
(2) memory usage
(3) inbound and outbound data packets
(4) overall network load
These data must be extracted from the/proc/STAT,/proc/NET/dev, And/proc/meminfo files respectively. 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)

C predictions are implemented as follows:

Int get_cpu_usage (void)
{
Int total;
Int usage;
Int user;
Int nice;
INT system;
Int idle;
Unsigned char CPU [8];
Unsigned char text [256];
File * FP;
Fp = fopen ("/proc/STAT", "R ");
Fread (text, 1, sizeof (text), FP );
If (strstr (text, "CPU "))
{
Sscanf (text, "% S % d", CPU, & user, & nice, & system, & idle );
}
 
Printf ("% d/N", user, nice, system, idle );
 
Fclose (FP );
Total = (User + nice + system + idle );
Usage = (User + nice + System) * 100)/Total;
Return usage;
}

(2) memory usage
Here we need to extract two data from the/proc/meminfo file, the current memory usage (cmem) and the total memory (amem ).
Memory usage percentage = 100 * (cmem/umem)
(3) Network Utilization
To obtain data about network utilization, You need to obtain two pieces of data from the/proc/NET/dev file: the 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.
Calculate the average network load by using the above data:
Average network load = (output packet + incoming packet)/2

2. Adjust the related Kernel configuration through the/proc file system
Allow IP Forwarding/proc/sys/NET/IPv4/ip_forward
Disable Ping/proc/sys/NET/IPv4/icmp_echo_ignore_all

You can directly write "1" to the above two "Files" under the command line to implement related configuration. If "0" is written, the related configuration will be canceled. However, after the system is restarted, these configurations will be restored to the default settings. If you want these modifications to take effect, you can directly write the following configuration into the/etc/profile file, or other program files executed as the system starts.

Echo 1>/proc/sys/NET/IPv4/ip_forward
Echo 1>/proc/sys/NET/IPv4/icmp_echo_ignore_all

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.