已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
获取CPU,内存使用率,磁盘相关信息 cpp view plaincopyprint?1.cpp view plaincopyprint?1. CPU信息 cpp view plaincopyprint?1. bool get_cpuoccupy(CPU_OCCUPY *cpust) /对无类型get函数含有一个形参结构体类弄的指针O 2. 3. FILE *fd; 4. char buff256; 5. CPU_OCCUPY *cpu_occupy; 6. cpu_occupy = cpust; 7.8. if (fd = fopen(/proc/stat, r) name, &cpu_occupy-user, 14. &cpu_occupy-nice, &cpu_occupy-system, &cpu_occupy-idle); 15.16. fclose(fd); 17. return true; 18. 19.20. /获取CPU频率 MHZ 21. bool get_cpu_info(unsigned int *total) 22. 23. FILE *fd; 24. char buff256; 25. char name128, Hz128, tmp128; 26. char *p; 27.28. *total = 0; 29. if (fd = fopen(/proc/cpuinfo, r) = 0) 30. return false; 31. memset(buff, 0, 256); 32. for (int i = 0; i user + o-nice + o-system + o-idle);/第一次(用户+优先级+系统+空闲)的时间再赋给od 8. newtotal = (unsigned long) (n-user + n-nice + n-system + n-idle);/第二次(用户+优先级+系统+空闲)的时间再赋给od 9.10. id = (unsigned long) (n-user - o-user); /用户第一次和第二次的时间之差再赋给id 11. nd = (unsigned long) (n-nice - o-nice); 12. sd = (unsigned long) (n-system - o-system);/系统第一次和第二次的时间之差再赋给sd 13. if (newtotal - oldtatal) != 0) 14. cpu_use = (int) (sd + id + nd) * 10000) / (newtotal - oldtatal); /(用户+系统)乖100)除(第一次和第二次的时间差)再赋给g_cpu_used 15. else 16. cpu_use = 0; 17. return cpu_use; 18. 内存信息cpp view plaincopyprint?1. bool get_memoccupy(MEM_OCCUPY *mem) /对无类型get函数含有一个形参结构体类弄的指针O 2. 3. FILE *fd; 4. char buff256, tmp20; 5. MEM_OCCUPY *m; 6. m = mem; 7.8. if (fd = fopen(/proc/meminfo, r) name, &m-total, tmp); 16.17. memset(buff, 0, 256); 18. fgets(buff, sizeof(buff), fd); /从fd文件中读取长度为buff的字符串再存到起始地址为buff这个空间里 19. sscanf(buff, %s %lu %s, m-name2, &m-free, tmp); 20.21. fclose(fd); /关闭文件fd 22. return true; 23. 调用函数cpp view plaincopyprint?1. bool qury_sys_resource(char *buf) 2. 3. CPU_OCCUPY cpu_stat1; 4. CPU_OCCUPY cpu_stat2; 5. MEM_OCCUPY mem_stat; 6. unsigned int cpu_MHZ; 7. char xml1024; 8. int cpu_used, memused; 9. float cpu_use_per = 0; 10.11. memset(&cpu_stat1, 0, sizeof(cpu_stat1); 12. memset(&cpu_stat2, 0, sizeof(cpu_stat2); 13. memset(&mem_stat, 0, sizeof(mem_stat); 14. memset(xml, 0, 1024); 15. /第一次获取cpu使用情况 16. if (!get_cpuoccupy(CPU_OCCUPY *) &cpu_stat1) 17. 18. if (debug_level) 19. 20. cout get cpu stat err! endl; 21. 22. sprintf( 23. buf, 24. HTTP/1.1 500 Server err nServer:Brainaire nConnection: close nContent-Type:text/html; charset=UTF-8 n n); 25. return false; 26. 27.28. /获取内存 29. if (!get_memoccupy(MEM_OCCUPY *) &mem_stat) 30. 31. if (debug_level) 32. 33. cout get memery stat err! endl; 34. 35. sprintf( 36. buf, 37. HTTP/1.1 500 Server err nServer:Brainaire nConnection: close nContent-Type:text/html; charset=UTF-8 n n); 38. return false; 39. 40.41. if (!get_cpu_info(&cpu_MHZ) 42. 43. if (debug_level) 44. 45. cout get cpu info err! endl; 46. 47. sprintf( 48. buf, 49. HTTP/1.1 500 Server err nServer:Brainaire nConnection: close nContent-Type:text/html; charset=UTF-8 n n); 50. return false; 51. 52. sleep(1); 53.54. /第二次获取cpu使用情况 55. if (!get_cpuoccupy(CPU_OCCUPY *) &cpu_stat2) 56. 57. if (debug_level) 58. 59. cout get cpu stat err! endl; 60. 61. sprintf( 62. buf, 63. HTTP/1.1 500 Server err nServer:Brainaire nConnection: close nContent-Type:text/html; charset=UTF-8 n n); 64. return false; 65. 66.67. /计算cpu使用率 68. cpu_used = cal_cpuoccupy(CPU_OCCUPY *) &cpu_stat1, 69. (CPU_OCCUPY *) &cpu_stat2); 70. cpu_use_per = (float) (cpu_used / 100.0); 71. /memery used 72. if (mem_stat.total - mem_stat.free) = 0) 73. 74. memused = 100; 75. 76. else 77. 78. memused = (int) (mem_stat.total - mem_stat.free) * 100 79. / mem_stat.total); 80. 81. sprintf( 82. xml, 83. n n n, 84. cpu_MHZ, cpu_use_per, mem_stat.total / 1024, memused); 85. sprintf( 86. buf, 87. HTTP/1.1 200 OK nServer:Brainaire nConnection: close nContent-type: xml/text; nContent-Length:%d n n%s, 88. strlen(xml), xml); 89. if (debug_level) 90. 91. cout memery total: mem_stat.total / 1024 , used: 92. memused endl; 93. cout cpu used = cpu_use_per % endl; 94. cout buf; 95. 96. return true; 97. 获取磁盘分区情况cpp view plaincopyprint?1. int get_disk_rw(const char *disk_name) 2. 3. FILE *fd; 4. char tmp256, name256, a256, flag256; 5. if (fd = fopen(/etc/mtab, r) = 0) 6. 7. if (debug_level) 8. 9. perror(/etc/mtab); 10. 11. return 0; 12. 13. while (fgets(tmp, sizeof(tmp), fd) 14. 15. memset(flag, 0, sizeof(flag); 16. sscanf(tmp, %s %s %s %s, name, a, a, flag); 17. if (strcmp(name, disk_name) = 0) 18. 19. / coutflagendl; 20. for (int i = 0; i 256; i+) 21. 22. if (flagi = r & (flagi + 1 = o) | flagi + 1 23. = , | flagi + 1 = 0) 24. 25. return 1; 26. 27. if (flagi + 1 = 0) 28. 29. return 0; 30. 31. 32. 33. 34. return 0; 35. 36.37. void qury_single_disk_info(const char *disk_path, char *buf) 38. 39. FILE *fd; 40. char tmp256, name256; 41. unsigned long long total, used, avail; 42. float ftotal, favail; 43. if (!(fd = popen(df, r) 44. 45. sprintf( 46. buf, 47. HTTP/1.1 500 Server err nServer:Brainaire nConnection: close nContent-Type:text/html; charset=UTF-8 n n); 48. return; 49. 50. while (fgets(tmp, sizeof(tmp), fd) 51. 52. sscanf(tmp, %s %llu %llu %llu, name, &total, &used, &avail); 53. if (strcmp(disk_path, name) = 0) 54. 55. ftotal = (float) (total / 1000000.0); 56. favail = (float) (avail / 1000000.0); 57. sprintf( 58. tmp, 59. n n n n, 60. disk_path, get_disk_rw(disk_path), ftotal, favail); 61. sprintf( 62. buf, 63. HTTP/1.1 200 OKnServer:BrainairenConnection: closenContent-type: xml/text; nContent-Length:%dnn%s, 64. strlen(tmp), tmp); 65. if (debug_level) 66. cout buf endl; 67. return; 68. 69. 70. sprintf(tmp, n n n); 71. sprintf( 72. buf, 73. HTTP/1.1 200 OK nServer:Brainaire nConnection: close nContent-type: xml/text; nContent-Length:%d n n%s, 74. strlen(tmp), tmp); 75. return; 76. 77.78. void qury_all_disk_info(char *buf) 79. 80. FILE *fd; 81. char tmp256, name256, res_bufMAX_BUF_SIZE; 82. unsigned long long total, used, avail; 83. float ftotal, favail; 84. memset(res_buf, 0, MAX_BUF_SIZE); 85. if (!(fd = popen(df, r) 86. 87. sprintf( 88. buf, 89. HTTP/1.1 500 Server err nServer:Brainaire nConnection: close nContent-Type:text/html;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电子绝缘材料试制工操作规程水平考核试卷含答案
- 公司氯乙烯装置操作工岗位合规化技术规程
- 公司彩灯艺术设计师岗位安全技术规程
- 医疗机构医学影像科委托协议书
- 航空安全考试题库与答案
- 硕士之路介绍
- 硕士考试成功之道
- 部编版五年级语文上册《圆明园的毁灭》同步练习(含答案)
- 贵州国企招聘2025贵州仟山农林开发(集团)有限公司招聘14人笔试历年参考题库附带答案详解
- 2025四川长虹电子控股集团有限公司招聘13人笔试历年参考题库附带答案详解
- 《HSK标准教程2》第6课课件
- 心理咨询师模拟题库与答案
- 排除危石工程施工合同
- 就业指导课专题之简历制作
- GB/T 9124.2-2019钢制管法兰第2部分:Class系列
- GB/T 19949.2-2005道路车辆安全气囊部件第2部分:安全气囊模块试验
- GA/T 1674-2019法庭科学痕迹检验形态特征比对方法确认规范
- FZ/T 64012-2013卫生用水刺法非织造布
- 奥的斯DCSS5门机调试说明
- R32装置操作规程课件
- 地产公司适老化产品设计指引操作手册
评论
0/150
提交评论