python 获取linux的内存信息


1、安装psutil源码

https://pypi.python.org/pypi?:action=display&name=psutil#downloads

找到psutil-5.2.2.tar.gz 点击即可下载

shell # tar xzvf psutil-5.2.2.tar.gz

shell # cd psutil-5.2.2

shell # python setup.py install


2、使用psutil获取内存信息

终端输入python

shell # python

Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 

[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2

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

>>> import psutil

>>> mem = psutil.virtual_memory()

>>> mem

svmem(total=8254787584, available=3015909376, percent=63.5, used=4780199936, free=272994304, active=5022015488, inactive=2267688960, buffers=147845120, cached=3053748224, shared=199192576)

>>>mem.total,mem.used    #总内存,内存使用率

(8254787584, 4780199936)  #然后依次类推

>>> psutil.swap_memory()

sswap(total=4160745472, used=463339520, free=3697405952, percent=11.1, sin=8634368, sout=472358912)    #swap 信息

>>> psutil.swap_memory().used    #swap 使用,后面的可以以此类推

463339520


3、使用psutil获取cpu信息

shell # python

Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) 

[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2

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

>>> import psutil

>>> psutil.cpu_times_percent()

scputimes(user=0.5, nice=0.0, system=0.29999999999999999, idle=93.900000000000006, iowait=5.2000000000000002, irq=0.0, softirq=0.0, steal=0.0, guest=0.0)

>>> psutil.cpu_times_percent().user    #user 占用cpu的时间比,以此类推

>>> psutil.cpu_count()    #获取cpu的逻辑个数

4


4、使用psutil获取磁盘disk信息

>>> psutil.disk_partitions()   

[sdiskpart(device='/dev/mapper/vg_zabbixserver161-lv_root', mountpoint='/', fstype='ext4', opts='rw'), sdiskpart(device='/dev/sda1', mountpoint='/boot', fstype='ext4', opts='rw')]    #获取磁盘分区信息

>>> psutil.disk_partitions()[0]    #获取磁盘第一个分区

sdiskpart(device='/dev/mapper/vg_zabbixserver161-lv_root', mountpoint='/', fstype='ext4', opts='rw')

>>> psutil.disk_partitions()[0].mountpoint    #获取磁盘第一个分区的挂载点

'/'

>>> psutil.disk_usage('/')    #第一个分区的使用信息

sdiskusage(total=37525069824, used=19492302848, free=16119746560, percent=54.700000000000003)

>>> psutil.disk_usage('/').total    #第一个分区的总使用量

37525069824

>>> psutil.disk_io_counters()    #总的磁盘读写

sdiskio(read_count=14272042, write_count=221162728, read_bytes=114464508928, write_bytes=3038042720256, read_time=90401135, write_time=3063482316, read_merged_count=19088, write_merged_count=53414985, busy_time=2101696856)

>>> psutil.disk_io_counter(perdisk=true)    #每个磁盘的IO信息

{'dm-1': sdiskio(read_count=2405, write_count=117472, read_bytes=9850880, write_bytes=481165312, read_time=29109, write_time=9174865, read_merged_count=0, write_merged_count=0, busy_time=81047), 'sda2': sdiskio(read_count=7126122, write_count=83871599, read_bytes=57226652160, write_bytes=1518993299456, read_time=45019892, write_time=21754212, read_merged_count=18833, write_merged_count=53377640, busy_time=1050439643), 'dm-0': sdiskio(read_count=7142858, write_count=137133182, read_bytes=57216399872, write_bytes=1518512134144, read_time=45351656, write_time=3030824832, read_merged_count=0, write_merged_count=0, busy_time=1050531804), 'sda1': sdiskio(read_count=673, write_count=41897, read_bytes=11671552, write_bytes=81393664, read_time=598, write_time=1807846, read_merged_count=255, write_merged_count=37583, busy_time=656567)}


5、网络信息

>>> psutil.net_io_counters()    #获取总的网络信息

snetio(bytes_sent=29464674146, bytes_recv=33920851012, packets_sent=110329213, packets_recv=111418651, errin=0, errout=0, dropin=0, dropout=0)

>>> psutil.net_io_counters(pernic=True)       #获取单个出口的网络I/O信息

{'lo': snetio(bytes_sent=23965934128, bytes_recv=23965934128, packets_sent=70801554, packets_recv=70801554, errin=0, errout=0, dropin=0, dropout=0), 'eth1': snetio(bytes_sent=5499003974, bytes_recv=9955206326, packets_sent=39528890, packets_recv=40618352, errin=0, errout=0, dropin=0, dropout=0)}


6、其他网络信息

获取用户信息

>>> psutil.users()    #获取当前用户

[suser(name='root', terminal='pts/0', host='10.0.2.11', started=1498550912.0)]

获取开机时间

>>> psutil.boot_time()    #获取机器开机时间,返回unix时间戳

1494645282.0

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

'2017-05-13 11:14:42'    #unix时间转换自然时间格式


7、系统进程管理办法

>>> import psutil

>>> psutil.pids()    #列出所有系统进程

[1, 2, 3, 4, 5, 6, 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, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 78, 79, 80, 81, 82, 84, 85, 86, 117, 118, 203, 204, 211, 212, 346, 348, 365, 366, 441, 558, 737, 738, 774, 978, 979, 999, 1106, 1164, 1169, 1179, 1180, 1195, 1211, 2067, 2101, 2413, 2502, 2539, 2553, 2554, 2555, 2556, 2578, 2613, 2630, 2632, 2634, 2636, 2638, 2641, 2643, 2644, 2645, 2646, 2647, 2648, 2649, 2650, 2651, 2652, 2653, 2654, 2655, 2656, 2657, 2658, 2659, 2660, 2661, 2662, 2663, 2664, 2665, 2666, 2667, 2668, 2669, 2670, 2671, 2672, 2673, 2713, 2852, 7203, 7204, 7205, 7206, 7207, 7208, 7209, 7210, 7211, 7212, 7213, 7214, 7215, 7216, 7217, 7218, 7219, 7220, 7221, 7222, 7223, 7391, 7392, 7516, 8776, 8777, 9326, 11598, 11605, 11620, 11624, 11660, 11704, 12231, 12232, 13601, 13803, 13804, 15504, 16481, 16496, 16511, 16518, 16557, 16566, 16875, 17638, 17639, 17905, 18919, 18959, 18961, 18983, 19326, 21931, 24430, 24442, 24453, 24473, 24481, 24569, 27130, 27132, 27133, 27134, 27135, 27142, 27150, 27390, 30190, 30192, 30193, 30194, 30195, 30196, 30197, 30198, 30199, 30200, 30201, 31825]

>>> p = psutil.Process(2554)

>>> p.name()    #进程的名字

'nginx'

>>> p.exe()    #进程bin路径

'/usr/sbin/nginx'

>>> p.cwd()    #进程工作目录绝对路径

'/'

>>> p.status()    #进程状态

'sleeping'

>>> p.create_time()    #进程创建时间,时间戳格式

1494645304.8499999

>>> p.uids()    #进程uid

puids(real=0, effective=0, saved=0)

>>> p.gids()    #进程pid

pgids(real=0, effective=0, saved=0)

>>> p.cpu_times()    #进程cpu信息

pcputimes(user=0.0, system=0.0, children_user=0.0, children_system=0.0)

>>> p.memory_percent()    #进程内存使用率

0.016870694561533128

>>> p.memory_info()    #进程内存信息

pmem(rss=1392640, vms=45932544, shared=405504, text=864256, lib=0, data=835584, dirty=0)

p.io_counters()    #进程的I/O信息

pio(read_count=0, write_count=1, read_bytes=4096, write_bytes=4096, read_chars=0, write_chars=5)

>>> p.connections()    #返回打开进程的socket列表

[pconn(fd=8, family=2, type=1, laddr=('0.0.0.0', 80), raddr=(), status='LISTEN'), pconn(fd=9, family=2, type=1, laddr=('0.0.0.0', 81), raddr=(), status='LISTEN')]

>>> p.num_threads()    #进程开启的线程数

1