python 獲取linux的性能信息

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章