Python对Linux系统的操作模块
1、pip安装(pip 是“A tool for installing and managing Python packages.”,也就是说pip是python的软件安装工具)
yum -y install epel-release
yum -y install python-pip
yum -y install python-devel
yum -y install gcc*
2、下载psutil(或使用 pip install psutil 安装)
wget https://pypi.python.org/packages/source/p/psutil/psutil-3.2.2.tar.gz
tar zxvf psutil-3.2.2.tar.gz
cd psutil-3.2.2/
python setup.py install
3、清除缓存
yum clean all
4、python进入python命令行
[root@python ~]# python
Python 2.7.5 (default, Sep 15 2016, 22:37:39)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
导入psutil
>>> import psutil
5、cpu完整信息(命令top -bn 1 -i -c、vmstat等)
psutil.cpu_times()
scputimes(user=471.21, nice=104.58, system=276.85, idle=28935.3, iowait=391.02, irq=0.0, softirq=16.35, steal=0.0, guest=0.0, guest_nice=0.0
>>> psutil.cpu_times(percpu=True)
[scputimes(user=249.03, nice=61.25, system=156.09, idle=20729.97, iowait=195.16, irq=0.0, softirq=7.25, steal=0.0, guest=0.0, guest_nice=0.0),
scputimes(user=231.55, nice=43.33, system=140.27, idle=20633.5, iowait=196.1, irq=0.0, softirq=9.24, steal=0.0, guest=0.0, guest_nice=0.0)]
获取单个数据,如用户的cpu时间
>>> psutil.cpu_times().user
471.25
获取cpu物理和逻辑个数,默认logical值为True
>>> psutil.cpu_count()
2
>>> psutil.cpu_count(logical=False)
2
6、内存信息
>>> mem = psutil.virtual_memory()
获取内存完整信息
>>> mem
svmem(total=2082361344L, available=1130319872L, percent=45.7, used=1805217792L, free=277143552L, active=664281088, inactive=797175808, buffers=0L, cached=853176320)
获取内存总数
>>> mem.total
2082361344L
获取空闲存数
>>> mem.free
277143552L
获取swap分区信息
>>> psutil.swap_memory()
sswap(total=2147479552L, used=8335360L, free=2139144192L, percent=0.4, sin=3416064, sout=11354112)
可以换算一下单位
>>> print (mem.total/8/1024)
127874
>>> print (mem.total/1024/1024)
其中percent表示实际已经使用的内存占比,即(2082361344-1130319872)/2082361344*100% 。available表示还可以使用的内存。
7、磁盘信息
磁盘信息主要有两部分,一个是磁盘的利用率,一个是io,他们分别可以通过disk_usage和disk_io_counters方法获取。
如下先获取分区信息,然后看下根分区的使用情况。
获取磁盘完整信息
>>> psutil.disk_partitions()
[sdiskpart(device='/dev/mapper/centos-root', mountpoint='/', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota'),
sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='xfs', opts='rw,seclabel,relatime,attr2,inode64,noquota')]
获取分区使用情况
>>> psutil.disk_usage('/')
sdiskusage(total=18746441728, used=5844000768, free=12902440960, percent=31.2)
获取总的IO个数、读写信息
>>> psutil.disk_io_counters()
sdiskio(read_count=146151, write_count=203807, read_bytes=4112552448, write_bytes=11684216320, read_time=2269317, write_time=39627462)
perdisk=Ture获取单个分区IO个数、读写信息
>>> psutil.disk_io_counters(perdisk=True)
{'fd0': sdiskio(read_count=0, write_count=0, read_bytes=0, write_bytes=0, read_time=0, write_time=0),
'sr0': sdiskio(read_count=0, write_count=0, read_bytes=0, write_bytes=0, read_time=0, write_time=0),
'sda2': sdiskio(read_count=71699, write_count=82805, read_bytes=2054048768, write_bytes=5806939648,
read_time=1124424, write_time=11573392), 'sda1': sdiskio(read_count=1952, write_count=2255, read_bytes=5081600,
write_bytes=70337024, read_time=10686, write_time=27267), 'dm-0': sdiskio(read_count=71452, write_count=115975,
read_bytes=2048183296, write_bytes=5795585536, read_time=1129014, write_time=25763350),
'dm-1': sdiskio(read_count=1048, write_count=2772, read_bytes=5238784, write_bytes=11354112, read_time=5193, write_time=2263453)}
8、网络信息
获得网络总的IO信息,默认pernic=False
>>> psutil.net_io_counters()
snetio(bytes_sent=17640593, bytes_recv=837836495, packets_sent=227041, packets_recv=594110, errin=0, errout=0, dropin=0, dropout=0)
pernic=True输出每个网络接口的IO信息
>>> psutil.net_io_counters(pernic=True)
{'eno16777736': snetio(bytes_sent=17641429, bytes_recv=837839462, packets_sent=227046, packets_recv=594142, errin=0, errout=0, dropin=0, dropout=0),
'virbr0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0),
'virbr0-nic': 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=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)}
9、其他系统信息
查看当前登录用户
>>> psutil.users()
[suser(name='wolf', terminal=':0', host='localhost', started=1477147392.0),
suser(name='wolf', terminal='pts/0', host='localhost', started=1477147520.0),
suser(name='wolf', terminal='pts/4', host='192.168.3.26', started=1477190016.0),
suser(name='wolf', terminal='pts/6', host='192.168.3.26', started=1477190144.0)]
>>> import psutil, datetime
查看时间(linux格式时间戳)
>>> psutil.boot_time()
1477177417.0
linux时间戳转换成自然时间格式
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
'2016-10-23 07:03:37'
10、系统进程管理
列出所有进程PID
>>> psutil.pids()
[[1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, ....]
实例化一个process对象,参数为一进程PID
>>> p=psutil.Process(1)
进程名
>>> p.name()
'systemd'
>>> p=psutil.Process(76242)
>>> p.name()
'pickup'
进程bin路径
>>> p.exe()
'/usr/libexec/postfix/pickup'
进程工种目录绝对路径
>>> p.cwd()
'/var/spool/postfix'
进程状态
>>> p.status()
'sleeping'
进程创建时间
>>> p.create_time()
1477208086.18
进程uid
>>> p.uids()
puids(real=89, effective=89, saved=89)
进程gid
>>> p.gids()
pgids(real=89, effective=89, saved=89)
进程cpu时间信息,包括user、system两个cpu时间
>>> p.cpu_times()
pcputimes(user=0.02, system=0.02)
get进程CPU亲和度,如果设置进程CPU亲和度,将CPU号作为时间参数即可
>>> p.cpu_affinity()
[0, 1]
进程内存利用率
>>> p.memory_percent()
0.19276577581340273
进程内存css,vms信息
>>> p.memory_info()
pmem(rss=4014080, vms=95567872)
进程IO信息,包括读写IO数及字节数
>>> p.io_counters()
pio(read_count=149, write_count=160, read_bytes=0, write_bytes=0)
返回打开进程socker的namedutples列表,包含fs、family、laddr等信息
>>> p.connections()
[]
进程开启的线程数
>>> p.num_threads()
1
popen类作用:获取用户启动的应用程序进程信息,以便跟踪程序进程的运行状态。
>>> from subprocess import PIPE
通过psutil的Popen方法启动的应用程序,可以跟踪该程序的所有相关信息
>>> p = psutil.Popen(["/usr/bin/python","-c","print('hello')"],stdout=PIPE)
>>> p.name()
'python'
>>> p.username()
'root'
>>> p.communicate()
('hello\n', None)
>>> psutil.cpu_times()
scputimes(user=499.47, nice=104.58, system=335.63, idle=69964.99, iowait=391.57, irq=0.0, softirq=16.75, steal=0.0, guest=0.0, guest_nice=0.0)