python网络监控模块_python2.7系统性能监控psutil模块

系统环境:Centos7.4,系统自带python2.7.5

登录psutil官网,下载psutil的tar包:psutil-5.4.6.tar.gz,并使用命名sha256sum和官网的包进行核对,确保下载的包没有进行篡改。

此次试验是下载到/mnt/下,依次执行如下操作:

tar zxf psutil-5.4.6.tar.gz

cd psutil-5.4.6

python setup.py  install

python

如果安装psutil没有问题,则:

[root@localhost ~]# python

Python 2.7.5 (default, Jul 13 2018, 13:06:57)

[GCC x.x.x xxxxxxxx (Red Hat xxxx-xxx)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import psutil

>>>exit()

此时证明安装psutil模块没有问题,可以直接使用。python脚本需要以.py结尾。文件开头:

#!/usr/bin/python   ##python的安装路径

#-*- coding: utf-8 -*-       ##编码格式的定义

import psutil    ##导入需要使用的模块

为了便于显示效果,以下是在Python命令行执行:

>>> import psutil

>>> psutil.cpu_percent()    ##CPU平均使用率

0.0

>>> psutil.cpu_percent(percpu=True)    ##每颗CPU的使用率

[0.1, 0.3, 0.2, 0.2]

>>> psutil.cpu_count()      ##CPU物理颗数

4

>>> psutil.virtual_memory()    ##内存使用情况

svmem(total=1911857152, available=1534185472, percent=19.8, used=222138368, free=1521319936, active=154025984, inactive=89636864, buffers=16433152, cached=151965696, shared=9072640, slab=64802816)

>>> psutil.swap_memory()    ##swap空间使用情况

sswap(total=21474832384, used=0, free=21474832384, percent=0.0, sin=0, sout=0)

>>> psutil.pids()        ##获取系统所有的进程ID

[1, 2, 3, 5, 7, 8, 9, 10, 11,……]

>>> psutil.Process(pid)    ##根据进程ID,查看进程名

>>> psutil.net_io_counters()    ##查看网络包情况

snetio(bytes_sent=113097, bytes_recv=292916, packets_sent=1228, packets_recv=3817, errin=0, errout=0, dropin=0, dropout=0)

>>> psutil.disk_partitions()         ##查看磁盘挂载情况

[sdiskpart(device='/dev/sda3', mountpoint='/', fstype='ext4', opts='rw,seclabel,relatime,data=ordered'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='ext4', opts='rw,seclabel,relatime,data=ordered'), sdiskpart(device='/dev/mapper/centos-var', mountpoint='/var', fstype='ext4', opts='rw,seclabel,relatime,data=ordered'), sdiskpart(device='/dev/mapper/centos-home', mountpoint='/home', fstype='ext4', opts='rw,seclabel,relatime,data=ordered'), sdiskpart(device='/dev/mapper/centos-data', mountpoint='/data', fstype='ext4', opts='rw,seclabel,relatime,data=ordered')]

>>> psutil.disk_usage("/")    ##具体某个挂载点的使用情况

sdiskusage(total=10186727424, used=1086652416, free=8559009792, percent=11.3)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值