使用/proc/stat文件,用php计算CPU使用率、内存使用率

来源:互联网 发布:新速特软件站下载 编辑:程序博客网 时间:2024/05/16 06:35

Linux 的/proc/stat文件包含很多信息,但是看起来有些杂乱,到底都是些什么内容呢,今天仔细研究一下,先看一下stat文件内容:

  1. [root@localhost ~]# cat /proc/stat
  2. cpu 7543 0 6902 10332516 11903 2770 28485 0
  3. cpu0 7543 0 6902 10332516 11903 2770 28485 0
  4. intr 104942647 104103305 1274 0 3 3 0 5 0 3 0 0 0 2400 0 0 324441 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45657 0 0 0 0 0 0 0 118 0 0 0 0 0 0 0 465438 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  5. ctxt 1919702
  6. btime 1323717585
  7. processes 21449
  8. procs_running 1
  9. procs_blocked 0

/proc/stat 每列第一行表示项目内容,其后每列为对应的信息

  1. cpu           - 整体 CPU 各项使用时间
  2. cpu0/cpu1...  - 个别 CPU 各项使用时间
  3. ctxt          - 显示系统经历过的 context switch 次数。
  4. btime         - 计算机开机的时间,以由 epoch (1970 年 1 月 1 日) 至开机时间的秒数表示。
  5. processes     - 开机后 fork 的次数
  6. procs_running - 在可运行 (runnable) 状态的进程数目,Linux 2.5.45 开始支持
  7. procs_blocked - 被阻截 (blocked) 直至输入/输出完成的进程数目,Linux 2.5.45 开始支持

CPU 各项时间 (cpu/cpu0/cpu1...)
显示 CPU 各项使用时间,单位为为 USER_HZ (大部份架构为百分之一秒),一般会有 4 至 8 个时间,分别代表:
  1. user   - CPU 花在用户模式的时间,即运行应用程序花费的时间
  2. nice   - CPU 花在 nice 值大于一般值 0 (即有较低优先级别) 进程的时间。
  3. system - CPU 花在系统模式即在内核空间 (kernel space) 的时间,即在运行内核工作的时间
  4. idle   - CPU 闲置的时间,其值一定为 /proc/uptime 中第二个项目乘 USER_HZ
  5. iowait - CPU 花在等候输入/输出的时间,Linux 2.5.41 开始才开始支援
  6. irq    - CPU 花在处理硬件中断 (hardware interrupt) 的时间,Linux 2.6.0-test4 开始支持
  7. softirq- CPU 花在处理 softirq 软件中断的时间,Linux 2.6.0-test4 开始支持
  8. steal_time  - 在虚拟环境下 CPU 花在处理其他作业系统的时间,Linux 2.6.11 开始支持
  9. guest  - 在 Linux 内核控制下 CPU 为 guest 作业系统运行虚拟 CPU 的时间,Linux 2.6.24 开始支持(本例

基本要素搞清楚了,怎么计算CPU使用率呢?我们继续:
  1. CPU总时间:total=user+nice+system+idle+iowait+softirq+steal_time+guest
  2. CPU繁忙时间:time=user+nice+system+iowait+softirq+steal_time+guest
  3. 间隔1秒钟2次获取CPU总时间:total1、total2
  4. 和CPU繁忙时间:time1、time2
  5. CPU使用率=(time2-time1)/(total2-total1)
计算方法有了,具体用什么语言实现呢?
  1. 我们使用PHP的形式来在页面上对CPU使用率进行展现,以下是用到的代码:
  2. cat index.php
  3. <?php
  4. $mode = "/(cpu)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)/";
  5. $string=shell_exec("more /proc/stat");
  6. preg_match_all($mode,$string,$arr);
  7. //print_r($arr);
  8. $total1=$arr[2][0]+$arr[3][0]+$arr[4][0]+$arr[5][0]+$arr[6][0]+$arr[7][0]+$arr[8][0]+$arr[9][0];
  9. $time1=$arr[2][0]+$arr[3][0]+$arr[4][0]+$arr[6][0]+$arr[7][0]+$arr[8][0]+$arr[9][0];

  10. sleep(1);
  11. $string=shell_exec("more /proc/stat");
  12. preg_match_all($mode,$string,$arr);
  13. $total2=$arr[2][0]+$arr[3][0]+$arr[4][0]+$arr[5][0]+$arr[6][0]+$arr[7][0]+$arr[8][0]+$arr[9][0];
  14. $time2=$arr[2][0]+$arr[3][0]+$arr[4][0]+$arr[6][0]+$arr[7][0]+$arr[8][0]+$arr[9][0];
  15. $time=$time2-$time1;
  16. $total=$total2-$total1;
  17. //echo "CPU amount is: ".$num;
  18. $percent=bcdiv($time,$total,3);
  19. $percent=$percent*100;
  20. echo "CPU Usage is: ".$percent."%";
  21. ?>
继续实现针对内存的监控
  1. $str=shell_exec("more /proc/meminfo");
  2. $mode="/(.+):\s*([0-9]+)/";
  3. preg_match_all($mode,$str,$arr);
  4. $pr=bcdiv($arr[2][1],$arr[2][0],3);
  5. $pr=1-$pr;
  6. $pr=$pr*100;
  7. echo $pr."%";




0 0
原创粉丝点击