1、psutil系统性能信息模块
下载地址
https://pypi.python.org/packages/source/p/psutil/
安装
tar zxvf psutil-2.0.0.tar.gz
cd psutil-2.0.0
python setup.py install
1.1 - 获取内存
>>> import psutil
>>> mem=psutil.virtual_memory()
>>> mem.total,mem.used
(1050415104L, 159047680L)
>>> mem.free
891367424L
>>> psutil.swap_memory()
sswap(total=2113921024L, used=0L, free=2113921024L, percent=0.0, sin=0, sout=0)
>>>
1.2 - 获取cpu信息
>>> psutil.cpu_times()
scputimes(user=9.13, nice=0.05, system=7.04, idle=563.88, iowait=16.04, irq=0.06, softirq=0.75, steal=0.0)
获取cpu单项数据信息,user的cpu时间比
>>> psutil.cpu_times().user
9.14
获取cpu的逻辑个数和物理个数
>>> psutil.cpu_count()
1
>>> psutil.cpu_count(logical=False)
1.3 - 获取硬盘信息
获取磁盘完整信息
>>> psutil.disk_partitions()
[sdiskpart(device='/dev/mapper/VolGroup00-LogVol00', mountpoint='/', fstype='ext3', opts='rw'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='ext3', opts='rw')]
磁盘利用率
>>> psutil.disk_usage('/')
sdiskusage(total=29025366016, used=1982386176, free=25544777728, percent=6.8)
获取磁盘IO个数
>>> psutil.disk_io_counters(perdisk=True)
{'dm-1': sdiskio(read_count=112, write_count=0, read_bytes=458752, write_bytes=0, read_time=1253, write_time=0), 'sda2': sdiskio(read_count=3412, write_count=2354, read_bytes=71059968, write_bytes=20303872, read_time=20905, write_time=24267), 'dm-0': sdiskio(read_count=4259, write_count=4957, read_bytes=70456320, write_bytes=20303872, read_time=23871, write_time=56374), 'sda1': sdiskio(read_count=274, write_count=2, read_bytes=1102848, write_bytes=2048, read_time=752, write_time=42)}
1.4 - 网络信息
>>> psutil.net_io_counters() 获取网络总的IO信息
snetio(bytes_sent=143241, bytes_recv=362052, packets_sent=1355, packets_recv=1575, errin=0, errout=0, dropin=0, dropout=0)
>>> psutil.net_io_counters(pernic=True) 获取每个网络接口的IO信息
{'sit0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'lo': snetio(bytes_sent=11575, bytes_recv=11575, packets_sent=151, packets_recv=151, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=135758, bytes_recv=355793, packets_sent=1242, packets_recv=1485, errin=0, errout=0, dropin=0, dropout=0)}
>>>
1.5 - 获取当前登录系统的用户信息
>>> psutil.users()
[suser(name='root', terminal='pts/0', host='182.168.8.2', started=1417608064.0)]
>>>
1。6 - psutil.boot_time()获取开机时间,以linux时间戳格式返回,用datetime转换成自然格式
>>> import psutil,datetime
>>> psutil.boot_time()
1417607909.0
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y%m%d %H:%M:%S")
'20141203 19:58:29'
>>>
1.7 - 进程管理
>>> psutil.pids() 列出所有进程PID
[1, 2, 3, 4, 5, 14, 18, 19, 185, 188, 190, 256, 257, 258, 259, 466, 495, 496, 499, 500, 505, 514, 525, 555, 588, 1367, 1925, 1926, 1949, 2120, 2328, 2341, 2344, 2358, 2394, 2395, 2405, 2416, 2425, 2442, 2516, 2522, 2575, 2588, 2604, 2629, 2643, 2644, 2650, 2651, 2652, 2653, 2654, 2655, 2709, 2711, 2761]
>>> p=psutil.Process(2522) 实例化一个Process对象
>>> p.name() 进程名字
'snmpmagt'
>>> p.exe() 进程bin路径
'/opt/bmc/Patrol3/Linux-2-6-x86-64-nptl/bin/snmpmagt'
>>> p.cwd() 进程工作目录绝对路径
'/opt/bmc/Patrol3/Linux-2-6-x86-64-nptl/bin'
>>> p.status() 进程状态
'sleeping'
>>> p.create_time() 进程创建时间,时间戳格式
1417607977.06
>>> p.uids() 进程uid信息
puids(real=500, effective=0, saved=0)
>>> p.gids() 进程gid信息
pgids(real=500, effective=500, saved=500)
>>> p.cpu_times() 进程cpu信息
pcputimes(user=0.0, system=0.0)
>>> p.cpu_affinity() get进程cpu亲和度
[0]
>>> p.memory_percent() 进程内存利用率
0.1052840915737632
>>> p.memory_info() 进程内存rss、vms信息
pmem(rss=1105920, vms=34746368)
>>> p.io_counters() 进程IO信息
pio(read_count=37, write_count=0, read_bytes=176128, write_bytes=0)
>>> p.connections() 返回打开进程socket的namedutples列表
[pconn(fd=4, family=2, type=1, laddr=('0.0.0.0', 199), raddr=(), status='LISTEN'), pconn(fd=5, family=2, type=1, laddr=('182.168.8.131', 52549), raddr=('182.168.8.131', 199), status='ESTABLISHED'), pconn(fd=6, family=2, type=1, laddr=('182.168.8.131', 199), raddr=('182.168.8.131', 52549), status='ESTABLISHED'), pconn(fd=7, family=2, type=1, laddr=('127.0.0.1', 199), raddr=('127.0.0.1', 44749), status='ESTABLISHED'), pconn(fd=3, family=2, type=2, laddr=('0.0.0.0', 8161), raddr=(), status='NONE')]
>>> p.num_threads() 进程开启的线程数
1
>>>
1.8 - psutil提供popen类用于获取用户启动应用程序进程信息,以便跟踪程序的运行状态
>>> import psutil
>>> from subprocess import PIPE
>>> p=psutil.Popen(["/usr/bin/python","-c","print('hello')"],stdout=PIPE)
>>> p.name()
'python'
>>> p.username()
'root'
>>> p.communicate()
('hello\n', None)
下载地址
https://pypi.python.org/packages/source/p/psutil/
安装
tar zxvf psutil-2.0.0.tar.gz
cd psutil-2.0.0
python setup.py install
1.1 - 获取内存
>>> import psutil
>>> mem=psutil.virtual_memory()
>>> mem.total,mem.used
(1050415104L, 159047680L)
>>> mem.free
891367424L
>>> psutil.swap_memory()
sswap(total=2113921024L, used=0L, free=2113921024L, percent=0.0, sin=0, sout=0)
>>>
1.2 - 获取cpu信息
>>> psutil.cpu_times()
scputimes(user=9.13, nice=0.05, system=7.04, idle=563.88, iowait=16.04, irq=0.06, softirq=0.75, steal=0.0)
获取cpu单项数据信息,user的cpu时间比
>>> psutil.cpu_times().user
9.14
获取cpu的逻辑个数和物理个数
>>> psutil.cpu_count()
1
>>> psutil.cpu_count(logical=False)
1.3 - 获取硬盘信息
获取磁盘完整信息
>>> psutil.disk_partitions()
[sdiskpart(device='/dev/mapper/VolGroup00-LogVol00', mountpoint='/', fstype='ext3', opts='rw'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='ext3', opts='rw')]
磁盘利用率
>>> psutil.disk_usage('/')
sdiskusage(total=29025366016, used=1982386176, free=25544777728, percent=6.8)
获取磁盘IO个数
>>> psutil.disk_io_counters(perdisk=True)
{'dm-1': sdiskio(read_count=112, write_count=0, read_bytes=458752, write_bytes=0, read_time=1253, write_time=0), 'sda2': sdiskio(read_count=3412, write_count=2354, read_bytes=71059968, write_bytes=20303872, read_time=20905, write_time=24267), 'dm-0': sdiskio(read_count=4259, write_count=4957, read_bytes=70456320, write_bytes=20303872, read_time=23871, write_time=56374), 'sda1': sdiskio(read_count=274, write_count=2, read_bytes=1102848, write_bytes=2048, read_time=752, write_time=42)}
1.4 - 网络信息
>>> psutil.net_io_counters() 获取网络总的IO信息
snetio(bytes_sent=143241, bytes_recv=362052, packets_sent=1355, packets_recv=1575, errin=0, errout=0, dropin=0, dropout=0)
>>> psutil.net_io_counters(pernic=True) 获取每个网络接口的IO信息
{'sit0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'lo': snetio(bytes_sent=11575, bytes_recv=11575, packets_sent=151, packets_recv=151, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=135758, bytes_recv=355793, packets_sent=1242, packets_recv=1485, errin=0, errout=0, dropin=0, dropout=0)}
>>>
1.5 - 获取当前登录系统的用户信息
>>> psutil.users()
[suser(name='root', terminal='pts/0', host='182.168.8.2', started=1417608064.0)]
>>>
1。6 - psutil.boot_time()获取开机时间,以linux时间戳格式返回,用datetime转换成自然格式
>>> import psutil,datetime
>>> psutil.boot_time()
1417607909.0
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y%m%d %H:%M:%S")
'20141203 19:58:29'
>>>
1.7 - 进程管理
>>> psutil.pids() 列出所有进程PID
[1, 2, 3, 4, 5, 14, 18, 19, 185, 188, 190, 256, 257, 258, 259, 466, 495, 496, 499, 500, 505, 514, 525, 555, 588, 1367, 1925, 1926, 1949, 2120, 2328, 2341, 2344, 2358, 2394, 2395, 2405, 2416, 2425, 2442, 2516, 2522, 2575, 2588, 2604, 2629, 2643, 2644, 2650, 2651, 2652, 2653, 2654, 2655, 2709, 2711, 2761]
>>> p=psutil.Process(2522) 实例化一个Process对象
>>> p.name() 进程名字
'snmpmagt'
>>> p.exe() 进程bin路径
'/opt/bmc/Patrol3/Linux-2-6-x86-64-nptl/bin/snmpmagt'
>>> p.cwd() 进程工作目录绝对路径
'/opt/bmc/Patrol3/Linux-2-6-x86-64-nptl/bin'
>>> p.status() 进程状态
'sleeping'
>>> p.create_time() 进程创建时间,时间戳格式
1417607977.06
>>> p.uids() 进程uid信息
puids(real=500, effective=0, saved=0)
>>> p.gids() 进程gid信息
pgids(real=500, effective=500, saved=500)
>>> p.cpu_times() 进程cpu信息
pcputimes(user=0.0, system=0.0)
>>> p.cpu_affinity() get进程cpu亲和度
[0]
>>> p.memory_percent() 进程内存利用率
0.1052840915737632
>>> p.memory_info() 进程内存rss、vms信息
pmem(rss=1105920, vms=34746368)
>>> p.io_counters() 进程IO信息
pio(read_count=37, write_count=0, read_bytes=176128, write_bytes=0)
>>> p.connections() 返回打开进程socket的namedutples列表
[pconn(fd=4, family=2, type=1, laddr=('0.0.0.0', 199), raddr=(), status='LISTEN'), pconn(fd=5, family=2, type=1, laddr=('182.168.8.131', 52549), raddr=('182.168.8.131', 199), status='ESTABLISHED'), pconn(fd=6, family=2, type=1, laddr=('182.168.8.131', 199), raddr=('182.168.8.131', 52549), status='ESTABLISHED'), pconn(fd=7, family=2, type=1, laddr=('127.0.0.1', 199), raddr=('127.0.0.1', 44749), status='ESTABLISHED'), pconn(fd=3, family=2, type=2, laddr=('0.0.0.0', 8161), raddr=(), status='NONE')]
>>> p.num_threads() 进程开启的线程数
1
>>>
1.8 - psutil提供popen类用于获取用户启动应用程序进程信息,以便跟踪程序的运行状态
>>> import psutil
>>> from subprocess import PIPE
>>> p=psutil.Popen(["/usr/bin/python","-c","print('hello')"],stdout=PIPE)
>>> p.name()
'python'
>>> p.username()
'root'
>>> p.communicate()
('hello\n', None)