进去linux主机
安装IPy模块
cd /usr/local/src/python
git clone https://github.com/haypo/python-ipy.git
cd python-ipy
python setup.py install
安装psutil模块,可以通过python获取系统的cpu,磁盘,内存,进程,网络等相关信息
下载地址https://pypi.python.org/pypi/psutil/
cd /usr/local/src/python
wget https://pypi.python.org/packages/source/p/psutil/psutil-2.1.3.tar.gz
tar -zxvf psutil-2.1.3.tar.gz
cd psutil-2.1.3
python setup.py install
安装是出现报错
error: command 'gcc' failed with exit status 1
解决办法
yum install gcc python-devel
重新安装
python setup.py install
安装成功
运行的一些实例
CPU信息;
>>> import psutil
>>> psutil.cpu_times()
scputimes(user=19207.82, nice=88.010000000000005, system=31732.639999999999, idle=20690363.170000002, iowait=1414.04, irq=543.19000000000005, softirq=463.97000000000003, steal=0.0, guest=0.0)
>>> psutil.cpu_times().user
123.61
>>> psutil.cpu_times().user #获取单项数据信息,用户的CPU时间比
125.83
>>> psutil.cpu_count() #获取CPU逻辑的个数
4
>>> psutil.cpu_times().user
19208.419999999998
>>> psutil.cpu_count(logical=False) #获取CPU的物理个数
内存信息:
buffers():缓冲使用数, cache:缓存使用数
>>> mem=psutil.virtual_memory()
>>> mem
svmem(total=8256098304L, available=2370256896L, percent=71.299999999999997, used=8012435456L, free=243662848L, active=1199607808, inactive=1128972288, buffers=274292736L, cached=1852301312)
>>> mem.total
8256098304L
>>> mem.free
243662848L
>>> psutil.swap_memory()
sswap(total=2147479552L, used=20070400L, free=2127409152L, percent=0.9, sin=262144, sout=20078592)
磁盘信息:
>>> psutil.disk_partitions()
[sdiskpart(device='/dev/mapper/centos_node5-root', mountpoint='/', fstype='xfs', opts='rw,relatime,attr2,inode64,noquota'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='xfs', opts='rw,relatime,attr2,inode64,noquota')]
>>> psutil.disk_usage('/')
sdiskusage(total=40217083904, used=21070389248, free=19146694656, percent=52.4)
>>> psutil.disk_usage('/boot')
sdiskusage(total=520794112, used=112791552, free=408002560, percent=21.7)
>>> psutil.disk_io_counters() #磁盘IO统计
sdiskio(read_count=58286, write_count=678680, read_bytes=1032004096, write_bytes=15667899392, read_time=787291, write_time=3830070)
>>> psutil.disk_io_counters(perdisk=True) #每个分区的IO个数
{'sdb1': sdiskio(read_count=228, write_count=361, read_bytes=1000448, write_bytes=3636224, read_time=2676, write_time=13287), 'fd0': sdiskio(read_count=0, write_count=0, read_bytes=0, write_bytes=0, read_time=0, write_time=0), 'sda2': sdiskio(read_count=27469, write_count=332451, read_bytes=513177088, write_bytes=7841338880, read_time=386776, write_time=1826302), 'sda1': sdiskio(read_count=2090, write_count=2049, read_bytes=4648448, write_bytes=2097152, read_time=1616, write_time=358), 'dm-0': sdiskio(read_count=28189, write_count=340088, read_bytes=511908352, write_bytes=7824896512, read_time=393955, write_time=1941704), 'dm-1': sdiskio(read_count=310, write_count=4902, read_bytes=1269760, write_bytes=20078592, read_time=2268, write_time=52154)}
网络信息:
>>> psutil.net_io_counters() #所有网络接口的总的IO信息
snetio(bytes_sent=6465060850, bytes_recv=6504957828, packets_sent=16940891, packets_recv=17434189, errin=0, errout=0, dropin=6588, dropout=0)
>>> psutil.net_io_counters(pernic=True) #输入每个网络接口的IO信息
{'lo': snetio(bytes_sent=6462165218, bytes_recv=6462165218, packets_sent=16908867, packets_recv=16908867, errin=0, errout=0, dropin=0, dropout=0), 'eno16777728': snetio(bytes_sent=3418868, bytes_recv=43397505, packets_sent=34218, packets_recv=528623, errin=0, errout=0, dropin=6605, 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), 'virbr0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0)}
用户信息:
>>> psutil.users() #获取用户信息
[suser(name='root', terminal='pts/0', host='192.168.2.1', started=1441585792.0), suser(name='root', terminal='pts/1', host='', started=1441585792.0), suser(name='root', terminal='pts/2', host='192.168.2.1', started=1441586944.0), suser(name='root', terminal='pts/3', host='', started=1441586944.0)]
>>> psutil.boot_time() #获取开机时间
1441585701.0
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
'2015-09-07 08:28:21'
进程信息
>>> psutil.pids() #获取所有的进程
[1, 2, 3, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167]
>>> p=psutil.process(1018)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'ModuleWrapper' object has no attribute 'process'
>>> p=psutil.Process(1018)
>>> p.name()
'NetworkManager'
>>> p.exe()
'/usr/sbin/NetworkManager'
>>> p.cwd()
'/'
>>> p.status()
'sleeping'
>>> p.create_time()
1441585714.56
>>> datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
File "<stdin>", line 1
datetime.datetime.fromtimestamp(psutil.boot_time()).strftime("%Y-%m-%d %H:%M:%S")
^
IndentationError: unexpected indent
>>> datetime.datetime.fromtimestamp(p.create_time()).strftime("%Y-%m-%d %H:%M:%S")
'2015-09-07 08:28:34'
>>> p.uids()
puids(real=0, effective=0, saved=0)
>>> p.gids()
pgids(real=0, effective=0, saved=0)
>>> p.cpu_times()
pcputimes(user=1.14, system=0.36)
>>> p.cpu_affinity()
[0]
>>> p.memory_percent()
0.5439293871385189
>>> p.memory_info()
pmem(rss=4321280, vms=448077824)
>>> p.io_counters() #进程IO信息
pio(read_count=32847, write_count=39257, read_bytes=6279168, write_bytes=16384)
>>> p.connections()
[]
>>> p=psutil.Process(997)
>>> p.exe()
'/usr/bin/python2.7'
>>> p.status()
'sleeping'
>>> p.connections()
[]
>>> p.num_threads() #进程开启的线程数
5
>>> 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()
2017-01-07
Python-模块-psutil
评论
发表评论
姓 名: