您的位置:首页 > 编程语言 > Python开发

python自动化运维 第1章系统基础信息详解 第一节

2016-01-28 16:40 691 查看
第1章 系统基础信息详解

环境配置

1、RHEL6.4 CLIENT 10.10.10.130

1、系统性能信息模块psutil 安装

[root@python ~]# tar zxvf psutil-2.1.3.tar.gz

[root@python ~]# cd psutil-2.1.3

[root@python psutil-2.1.3]# python setup.py install

unable to execute gcc: No such file or directory

error: command 'gcc' failed with exit status 1

以上这个错误是没有安装gcc包和python-devel包,使用yum进行安装

2、使用shell和python分别查看内存空间

1) Shell查看

物理内存总内存值(total)

[root@python psutil-2.1.3]# free -m |grep Mem|awk '{print $2}'

981

物流内存使用值(used)

[root@python psutil-2.1.3]# free -m |grep Mem|awk '{print $3}'

733

2) Python交互式查看

[root@python psutil-2.1.3]# python

Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48)

[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2

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

>>> import psutil
---插入psutil模块

>>> mem = psutil.virtual_memory()

---设置变量mem的值为psutil模块下的方法

>>> mem.total,mem.used

(1028718592L, 773910528L)
----内存值为字节

3) 通过使用bc计数器计算值是否是正确

[root@python psutil-2.1.3]# bc

bc 1.06.95

Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.

This is free software with ABSOLUTELY NO WARRANTY.

For details type `warranty'.

1028718592/1024/1024
--字节转换

981 --跟shell算出一致

773910528/1024/1024

738

4) 使用swap_memory()方法查看虚拟内存信息

>>> psutil.swap_memory()

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

3、获取系统性能信息

1) cpu用户使用的比例

[root@python psutil-2.1.3]# python

Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48)

[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2

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

>>> import psutil

>>> cuptime=psutil.cpu_times()

>>> cuptime.user

32.740000000000002

2) 查看cpu所有的信息

>>> import psutil

>>> psutil.cpu_times()

scputimes(user=32.93, nice=0.0, system=37.359999999999999, idle=18363.98, iowait=55.799999999999997, irq=0.12, softirq=6.0099999999999998, steal=0.0, guest=0.0)

3) 查询cpu逻辑个数和物理个数

>>> psutil.cpu_count()

>>> psutil.cpu_count(logical=False)

4、磁盘信息

1)查看磁盘总体信息

>>> psutil.disk_partitions()

[sdiskpart(device='/dev/sda3', mountpoint='/', fstype='ext4', opts='rw'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='ext4', opts='rw')]

以上显示device设备是/dev/sda3 挂载到根目录 文件格式是ext4 具有读写权限

2)查看分区使用情况

>>> psutil.disk_io_counters()

sdiskio(read_count=10559, write_count=5672, read_bytes=314326016, write_bytes=185157632, read_time=360900, write_time=96689)

以上显示:

读的I/O数、写的I/O数、读的字节数、写的字节数、磁盘读的时间、磁盘写的时间

3)查看单个分区I/O个数

>>> psutil.disk_io_counters(perdisk=True)

{'sda2': sdiskio(read_count=363, write_count=0, read_bytes=1486848, write_bytes=0, read_time=891, write_time=0), 'sda3': sdiskio(read_count=9764, write_count=5703, read_bytes=311120896, write_bytes=185360384, read_time=357713, write_time=96972), 'sda1': sdiskio(read_count=432, write_count=7, read_bytes=1718272, write_bytes=14336, read_time=2296, write_time=17)}

5、网络信息

1) 查看网络整体信息

>>> psutil.net_io_counters()

snetio(bytes_sent=1131515, bytes_recv=40189615, packets_sent=11510, packets_recv=39685, errin=0, errout=0, dropin=0, dropout=0)

以上是发送字节数、接收字节数、发送数据包、接收数据包的值

6、其他系统信息

1)系统用户登录信息

>>> psutil.users()

[suser(name='root', terminal='tty1', host=':0', started=1453985792.0), suser(name='root', terminal='pts/0', host='10.10.10.1', started=1453986432.0)]

以上是使用用户root 远程终端pts/0 远程登录源侦听地址,系统开机时间

2) 查看开机时间的间隔时间

>>> import psutil,datetime

>>> psutil.boot_time()

1453985538.0

3) 将时间转换为自然格式

>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")

'2016-01-28 07:52:18'

7、系统中进程管理方法

1) 使用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)

>>> p.cpu_times() ---得到进程运行的CPU时间
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: