Article Title: how to obtain system performance monitoring parameters in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
The current project needs to easily monitor the Linux system: CPU load, memory consumption, disk space in several specified directories, disk I/O, swap, and network traffic.
There are actually many performance testing tools in Linux.
Mrtg (http://people.ee.ethz.ch /~ Oetiker/webtools/mrtg/) is a good choice. However, mrtg is required to install sysstat, apache, snmp, perl, and other things. It may take several steps to install the SDK. I also wanted to directly call the commands such as sar and vmstat, And the parse would just calculate the result. Which knows that the results of linux commands of different versions are also different. Since the results of version parse are required, it is better to calculate them directly in the system. So I studied the source code of sysstat (http://freshmeat.net/projects/sysstat/) and gkrellm (http://gkrellm.net), and found the data of monitoring performance.
1. CPU
The file "/proc/stat" contains CPU information. Every CPU tick is used in this file. The following numbers indicate user, nice, sys, idle, and iowait. Some versions of kernel do not include iowait. These values indicate where each tick of the CPU is used from the boot to the present. For example:
Cpu0 256279030 0 11832528 1637168262
That is, 256279030 tick is used for user consumption since CPU 0 is started, and 11832528 is used for sys consumption. So if you want to calculate the CPU load per unit time (for example, 1 s), you only need to calculate the difference between the values before and after 1 second by the number of tick per second. Gkrellm is implemented in this way: (200 * (v2-v1)/CPU_TICKS_PER_SECOND) + 1)/2
For example, if you read/proc/stat for the first time, the user value is 256279030; if you read it again after one second, the value is 256289030, the CPU user consumption in this second is: (200 * (256289030-256279030)/CPU_TICKS_PER_SECOND) + 1)/2 = (10000*200/1000000) + 1) /2 = 1%.
2. memory consumption
The file "/proc/meminfo" contains the memory information and swap information. For example:
$ cat /proc/meminfototal: used: free: shared: buffers: cached:Mem: 1057009664 851668992 205340672 0 67616768 367820800Swap: 2146787328 164429824 1982357504MemTotal: 1032236 kBMemFree: 200528 kBMemShared: 0 kB ……
|
However, from the source code of gkrellm, some versions do not have the statistics of the previous two rows, but can only collect data based on the following types of Key: Value.
3. Disk Space
From the source code of gkrellm, this is a very complex data. Disk partition data may be distributed in:/proc/mounts,/proc/diskstats,/proc/partitions, and so on. In addition, if you want to check a specific path, you also need the help of commands such as mount and df. To reduce the trouble, I can directly use the statfs function to obtain this data.
Int statfs (const char * path, struct statfs * buf );
This function only needs to enter the path name to be checked to return the space usage of the partition where the path is located:
Total Space: buf. f_bsize * buf. f_blocks
Free Space: buf. f_bsize * buf. f_bavail
4. Disk I/O
Disk I/O data is also complex. For some versions, see/proc/diskstats, for some versions, see/proc/partitions, and for some versions, I still don't know where to view them ...... However, we can see that the data version is also the same as the CPU, which requires a value every other time. The difference between the two values is the traffic.
5. Network Traffic
Network traffic is also varied, but it can basically be obtained in/proc/net/dev. It also requires two values to take the difference as the traffic value.