linux 采集cpu 内存,Linux环境获取(cpu、内存、网卡流量等)系统性能数据
本帖最后由 yuwenge 于 2015-6-22 16:46 編輯
問題導讀
1.如何查看CPU利用率?
2.進程CPU利用率如何查看?
3.內存利用率需要查看哪個文件?
CPU利用率
/proc/stat文件里包含系統cpu使用情況,2-8列對應的CPU消耗含義是用戶態消耗(user)、用戶態nice消耗(nice)、內核態消耗(sys)、空閑時間(idle)、等待IO消耗(iowait)、硬件中斷消耗(hardirq)、軟中斷消耗(softirq)。(man proc)
[mw_shl_code=bash,true]$ cat /proc/stat
cpu??2339504870 2641995 716723851 70316975284 7219566 328971 304057999 0 0
cpu0 211214357 275710 57674236 2778331292 3688175 10421 12279511 0 0
cpu1 140100049 278788 47142039 2857686940 180452 32028 14960093 0 0
...[/mw_shl_code]
要計算t1~t2時間內系統的cpu利用率,需要在t1、t2時刻分別采集cpu使用信息
[mw_shl_code=bash,true]t1_all = t1(user + nice + sys + idle + iowait + hardirq + sortirq)
t1_used = t1(user + nice + sys + iowait + hardirq + sortirq)
t2_all = t2(user + nice + sys + idle + iowait + hardirq + sortirq
t2_used = t2(user + nice + sys + iowait + hardirq + sortirq)
cpu_usage = (t2_used - t1_used) / (t2_all - t1_all)[/mw_shl_code]
進程CPU利用率
/proc/$pid/stat文件里包含某個進程的cpu使用信息,14、15、16、17列對應進程用戶態消耗(user)、內核態消耗(sys)、用戶態等待子進程的消耗(user_child)、內核態等待子進程的消耗(sys_child)。
[mw_shl_code=bash,true]$cat /proc/24076/stat
24076 (redis-server) S 1 24076 24076 0 -1 4202560 5755 147 0 0 1864 3298 0 0 20 0 3 0 70238536 159555584 5145 18446744073709551615 1 1 0 0 0 0 0 4097 17610 18446744073709551615 0 0 17 4 0 0 0 0 0[/mw_shl_code]
t1、t2時刻分別采集/proc/stat、/proc/$pid/stat,獲取總的cpu消耗,以及進程的cpu消耗
[mw_shl_code=bash,true]t1_all = t1(user + nice + sys + idle + iowait + hardirq + sortirq)
t1_pid = t1(user + sys + user_child + sys_child)
t2_all = t2(user + nice + sys + idle + iowait + hardirq + sortirq)
t2_pid = t2(user + sys + user_child + sys_child)
pid_cpu_usage = (t2_pid - t1_pid) / (t2_all - t1_all)[/mw_shl_code]
內存利用率
/proc/meminfo文件里包含系統內存使用信息
[mw_shl_code=bash,true]$ cat /proc/meminfo
MemTotal:? ?? ? 198450624 kB
MemFree:? ?? ???184950332 kB
mem_usage = 1 - MemFree / MemTotal[/mw_shl_code]
系統load
/proc/uptime的前3列包含了最近1min、5min、15min系統的平均load(正在運行或等待IO的進程數,進程state為R或D)
[mw_shl_code=bash,true]$cat /proc/loadavg
0.02 0.09 0.08 1/2362 855[/mw_shl_code]
網卡流量信息
/proc/net/dev包含所有網卡的流量信息,第1、2、10、11列分別代表網卡當前接受字節數(recv_bytes)、接受包個數(recv_packets)、發送字節數(send_bytes)、發送個數(send_packets)。
[mw_shl_code=bash,true]$cat /proc/net/dev
Inter-|? ?Receive? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?|??Transmit
face |bytes? ? packets errs drop fifo frame compressed multicast|bytes? ? packets errs drop fifo colls? ???carrier compressed
lo:25047448261 37947893? ? 0? ? 0? ? 0? ???0? ?? ?? ? 0? ?? ?? ?0 25047448261 37947893? ? 0? ? 0? ? 0? ???0? ?? ? 0? ?? ?? ? 0
eth0:2566932970457 7620547348? ? 0? ? 0? ? 2? ???0? ?? ?? ? 0? ???62732 728662220296 6145797287? ? 0? ? 0? ? 0? ???0? ?? ? 0? ?? ?? ? 0
eth1:2023304550582 7268848253? ? 0? ? 0? ? 0? ???0? ?? ?? ? 0? ???77924 742443231982 6129090809? ? 0? ? 0? ? 0? ???0? ?? ? 0? ?? ?? ? 0[/mw_shl_code]
t1~t2時間內的平均流量為
[mw_shl_code=bash,true]avg_recv_bytes = (t2_recv_bytes - t1_recv_bytes) / (t2 - t1)
avg_send_bytes = (t2_send_bytes - t1_send_bytes) / (t2 - t1)[/mw_shl_code]
磁盤(文件系統)空間信息
通過statfs系統調用可獲取當前文件系統的空間占用情況
[mw_shl_code=bash,true]struct statfs {
long? ? f_type;? ???/* type of file system (see below) */
long? ? f_bsize;? ? /* optimal transfer block size */
long? ? f_blocks;? ?/* total data blocks in file system */
long? ? f_bfree;? ? /* free blocks in fs */
long? ? f_bavail;? ?/* free blocks avail to non-superuser */
...
};
disk_usage = 1 - f_bfree / f_blocks[/mw_shl_code]
磁盤讀寫信息
/proc/diskstats包含磁盤的IO信息,第4、6、8、10、13列分別為當前磁盤讀次數(read),磁盤讀扇區數(read_sec)、磁盤寫次數(write)、磁盤寫扇區數(write_sec)、磁盤IO毫秒數(io_ms)。
[mw_shl_code=bash,true]$cat /proc/diskstats
8? ?? ? 4 sda4 3 0 12 8 0 0 0 0 0 8 8
8? ?? ? 5 sda5 87928 55284 2842138 17349 1230958 7932106 73380432 4931382 0 503343 4947504
8? ?? ?16 sdb 393 35 3418 37 17 2 152 0 0 37 37
8? ?? ?32 sdc 788 114 10690 10132 14550036 226568635 1929026928 623148463 0 4917530 623154797[/mw_shl_code]
t1~t2時間內磁盤平均讀寫次數、扇區數、IO利用率計算如下
[mw_shl_code=bash,true]avg_read = (t2_read - t1_read) / (t2 - t1)
avg_read_sec = (t2_read_sec - t1_read_sec) / (t2 - t1)
avg_write = (t2_write - t1_write) / (t2 - t1)
avg_write_sec = (t2_write_sec - t1_write_sec) / (t2 - t1)
io_util = (t2_io_ms - t1_io_ms) / (t2 - t1) (t1~t2時間段內IO的時間百分比)[/mw_shl_code]
總結
以上是生活随笔為你收集整理的linux 采集cpu 内存,Linux环境获取(cpu、内存、网卡流量等)系统性能数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux二重进程,二叉树递归实现与二重
- 下一篇: Java NIO (十四)NIO 和