013_针对单个pid的cpu/内存/io的资源占用统计
#!/usr/bin/env python import sys
import os
import subprocess
from decimal import Decimal
from decimal import getcontext def cpu_proc(pid):
'''
Reference:https://stackoverflow.com/questions/16726779/how-do-i-get-the-total-cpu-usage-of-an-application-from-proc-pid-stat
:param pid:
:return:
'''
try:
with open(os.path.join('/proc/', pid, 'stat'), 'r') as pidfile:
proctimes = pidfile.readline()
# get utime from /proc/<pid>/stat, 14 item
utime = proctimes.split(' ')[13]
# get stime from proc/<pid>/stat, 15 item
stime = proctimes.split(' ')[14]
cutime = proctimes.split(' ')[15]
cstime = proctimes.split(' ')[16]
total_time =int(utime) + int(stime) + int(cutime) + int(cstime)
uptime = cput()
starttime = proctimes.split(' ')[21]
Hertz = subprocess.Popen("getconf CLK_TCK", shell=True,stdout=subprocess.PIPE).communicate()[0].strip('\n')
seconds =Decimal(str(uptime)) + Decimal(str(starttime)) +Decimal(str(Hertz))
getcontext().prec = 4
cpu_usag = Decimal('') * (( Decimal(str(total_time)) / Decimal(str(Hertz)) )/ Decimal(str(seconds)))
return cpu_usag
except IOError as e:
print('ERROR: %s' % e)
sys.exit(2) def cpu_top(pid):
try:
proc = subprocess.Popen("top -p %s -b -n 1 | grep -w ezk-agent | awk '{print $9}'" % pid, shell=True, stdout=subprocess.PIPE)
cpu_percentage = proc.communicate()
return cpu_percentage[0].rstrip('\n')
except KeyboardInterrupt:
sys.exit(0) def cput():
try:
with open('/proc/uptime', 'r') as procfile:
cputimes = procfile.readline()
return(float(cputimes.split(' ')[0]))
except IOError as e:
print('ERROR: %s' % e)
sys.exit(3) def mem_usage_calc(pid):
(memkb, err)= subprocess.Popen("pmap -x "+pid+"|grep -i total|awk '{print $3}'", shell=True,
stdout=subprocess.PIPE).communicate()
return memkb.strip('\n') def fd_usage_calc(pid):
(fd_lsof_num, err) = subprocess.Popen("lsof -n|grep " + pid + "| wc -l", shell=True,
stdout=subprocess.PIPE).communicate()
pro_command="ls /proc/"+pid+"/fd|wc -l"
(fd_proc_num, err) = subprocess.Popen(pro_command, shell=True,
stdout=subprocess.PIPE).communicate()
return fd_lsof_num.strip('\n'),fd_proc_num.strip('\n') def main(pid):
print("pid:{}".format(pid))
print("*"*14 + "CPU" + "*"*14)
print "proc_get_value:{}".format(cpu_proc(pid))
print "top_get_value:{}".format(cpu_top(pid)) print("*"*14 + "MEM" + "*"*14)
print "mem_get_value:{}kB".format(mem_usage_calc(pid)) print("*"*10 + "File_Handler" + "*"*10)
print "File_Handler_lsof_get_value:{}".format(fd_usage_calc(pid)[0])
print "File_Handler_fd_get_value:{}".format(fd_usage_calc(pid)[1]) if __name__ == '__main__':
if len(sys.argv) == 2:
pid = sys.argv[1]
else:
try:
(out, err) = subprocess.Popen("ps -ef|grep ezk-agent/ezk-agent|grep -v grep|awk '{print $2}'", shell=True,
stdout=subprocess.PIPE).communicate()
pid = out.strip('\n')
except:
print('No PID specified. Usage: %s <PID>' % os.path.basename(__file__))
sys.exit(1)
main(pid)
013_针对单个pid的cpu/内存/io的资源占用统计的更多相关文章
- centos8平台使用pidstat监控cpu/内存/io
一,安装pidstat: 1,安装 [root@localhost yum.repos.d]# yum install sysstat 2,查看版本: [root@localhost ~]# pids ...
- python glances来监控linux服务器CPU 内存 IO使用
什么是 Glances? Glances 是一个由 Python 编写,使用 psutil 库来从系统抓取信息的基于 curses 开发的跨平台命令行系统监视工具. 通过 Glances,我们可以监视 ...
- Linux 查看CPU、Memory等资源占用情况
linux下获取占用CPU资源最多的10个进程,可以使用如下命令组合: ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3|head linux下获取占用 ...
- winform,WPF 释放内存垃圾,减少资源占用方法
[System.Runtime.InteropServices.DllImport("kernel32.dll")] public static extern boo ...
- Linux 性能监控之CPU&内存&I/O监控Shell脚本1
Linux 性能监控之CPU&内存&I/O监控Shell脚本1 by:授客 QQ:1033553122 #!/bin/bash # 获取要监控的本地服务器IP地址 IP=`if ...
- zabbix的安装(一)监控os资源:内存,cpu,io,负载,带宽
一.Linux下开源监控系统简单介绍1)cacti:存储数据能力强,报警性能差2)nagios:报警性能差,存储数据仅有简单的一段可以判断是否在合理范围内的数据长度,储存在内存中.比如,连续采样数据存 ...
- 统计和分析系统性能【IO CPU 内存】的工具集合
统计和分析系统性能[IO CPU 内存]的工具集合 blktrace http://www.oschina.net/p/blktrace 获取磁盘写入的信息 root@demo:~/install/p ...
- VPS性能测试:CPU内存,硬盘IO读写,带宽速度,UnixBench和压力测试
现在便宜的VPS主机越来越多了,一些美国的VPS主机甚至给出1美元一月的VPS,堪比虚拟主机还要便宜,巨大的价格优势吸引不少人购买和使用,而近些年来国内的主机商也开始意识到便宜的VPS对草根站长的诱惑 ...
- 如何使用 Docker 来限制 CPU、内存和 IO等资源?
如何使用 Docker 来限制 CPU.内存和 IO等资源?http://www.sohu.com/a/165506573_609513
随机推荐
- 关于Django报错django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configure
报错代码:django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but se ...
- CentOS7 使用firewalld打开关闭防火墙与端口
1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...
- r.js合并实践
项目中用到require.js做生产时模块开发,但上线要合并压缩,幸好它配套有r.js.下面就其用法说明一下. 首先建一个目录,里面的结构如下: require.js可以到r.js项目下载 r.js可 ...
- C#运算符的简单使用测试
在代码中看到的代码中|=,有点不太理解故重新学习了下位运算符. 位运算符在 c# 中的测试用例 [TestMethod] public void TestMethod1() { var a = fal ...
- 剑指Spring源码(二)
这是春节后的第一篇博客,我在构思这篇博客的时候,一度想放弃,想想要不要换个东西写,因为毕竟个人水平有限,Spring源码实在博大精深,不是我这个菜的抠脚的菜鸡可以驾驭的,怕误人子弟,还有就是源码分析类 ...
- 【Keras篇】---Keras初始,两种模型构造方法,利用keras实现手写数字体识别
一.前述 Keras 适合快速体验 ,keras的设计是把大量内部运算都隐藏了,用户始终可以用theano或tensorflow的语句来写扩展功能并和keras结合使用. 二.安装 Pip insta ...
- eclipse升级Android SDK Tool版本到25.2.5后运行项目报错Unable to build: the file dx.jar was not loaded from the SDK folder
概述 由于最近通过SDK-Manager更新了build-tools,当要用到dx.jar这个包时,自动调用最新版本Android SDK build-tools中dx.jar,但是运行android ...
- SpringBoot入门教程(二十一)IntelliJ IDEA配置Quartz启动项
本地运行:
- SQL自动生成java实体类POJO
前言 当我们设计完成数据库之后,通常需要创建对应的实体类,有的称为Entity,有的称为DO,都是一个意思,而自己一个个去写非常的麻烦,所以麻烦的时候就需要相应的自动工具类解决这样的麻烦.超级方便~ ...
- RDIFramework.NET V3.3 Web版角色授权管理新增角色对操作权限项、模块起止生效日期的设置
在实际应用在我们可能会有这样的需求,某个操作权限项(按钮)或菜单在某个时间范围内可以让指定角色访问.此时通过我们的角色权限扩展设置就可以办到. 在我们框架V3.3 Web版本全新增加了角色权限扩展设置 ...