将设为首页浏览此站
开启辅助访问 天气与日历 收藏本站联系我们切换到窄版

易陆发现论坛

 找回密码
 开始注册
查看: 2689|回复: 2
收起左侧

psutil 的功能python实现一个服务器信息的简单监控

[复制链接]
发表于 2017-12-23 20:59:48 | 显示全部楼层 |阅读模式
购买主题 本主题需向作者支付 6 金钱 才能浏览
 楼主| 发表于 2017-12-23 21:01:22 | 显示全部楼层
Example usages1 e; [9 t& m# W* t7 J
% S( i0 y' e. u$ ?2 R1 z9 {9 N2 P
CPU" W# R9 W6 }+ ~7 X3 a! K
>>> import psutil5 F' l+ P& @, F
>>> psutil.cpu_times()% U  b% h: b2 ]! f  ?" z
scputimes(user=3961.46, nice=169.729, system=2150.659, idle=16900.540, iowait=629.59, irq=0.0, softirq=19.42, steal=0.0, guest=0, nice=0.0)5 q+ U: Z$ M, j/ E" F/ D
>>>
! X% x3 E2 P* t( V& A>>> for x in range(3):
  F7 g+ r, b; j7 j/ R' Q...     psutil.cpu_percent(interval=1)
7 b; d# Q4 x3 z$ _: ~$ |3 X...9 B- W5 m6 |" N5 r
4.05 C' X6 N1 ]& u
5.9
* c5 V' \4 q9 ]! M1 |- ?3.8
' n5 Q1 R/ T4 K3 C$ A$ ]) x3 Z>>>0 g( u- s0 T9 H' H/ c
>>> for x in range(3):  ]4 ?9 T# ~! h! g  [
...     psutil.cpu_percent(interval=1, percpu=True)
' B9 c' t0 |( u" d...
: R9 d9 }2 n1 [[4.0, 6.9, 3.7, 9.2]- W2 H& G) m9 r2 Z
[7.0, 8.5, 2.4, 2.1]1 S! y/ n# `4 y# f
[1.2, 9.0, 9.9, 7.2]
3 W$ ]% V, h: M7 f1 Q3 O! a% T4 ?( [>>>
. Q) P, l- @0 f/ ^% T! F, K$ z, ~% ]& X>>> for x in range(3):; L: S2 ]; }% ^( S
...     psutil.cpu_times_percent(interval=1, percpu=False)
9 F* B; m2 O' c2 e) M4 k...
, v0 ]1 c; F6 }0 q% a% |( \! H# b2 sscputimes(user=1.5, nice=0.0, system=0.5, idle=96.5, iowait=1.5, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
1 N: D% e5 m( P: _5 A4 u  |scputimes(user=1.0, nice=0.0, system=0.0, idle=99.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0). h2 M/ @! T9 @8 D$ g* r, w
scputimes(user=2.0, nice=0.0, system=0.0, idle=98.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)" i/ _% |8 Q  ], A
>>>5 S8 `( C! m& M
>>> psutil.cpu_count()
- R  y# P- j6 A9 U2 Q9 V1 Z0 x4
: g+ C, t1 r6 k; R, J>>> psutil.cpu_count(logical=False)% E9 w0 }) }( y; d
2
2 b& _0 _" H! i! S' N) k- U>>>3 }* w! ^8 s7 R$ x8 D  k& M/ j
>>> psutil.cpu_stats()# s4 ?% F" B7 _) G5 i
scpustats(ctx_switches=20455687, interrupts=6598984, soft_interrupts=2134212, syscalls=0)
' ?1 Q  @" f0 {' |2 [8 a- R* c>>>
/ _& L* X5 p/ A. K>>> psutil.cpu_freq()
- h: d% R) ^- C; g' Bscpufreq(current=931.42925, min=800.0, max=3500.0)1 O9 g, \& I. x4 F5 B, o
>>>  X& R9 m6 @4 {7 I/ w0 D; y
Memory
* `0 G/ W* ~4 Q>>> import psutil
& M) G7 A/ I: T" ]; C>>> psutil.virtual_memory()
, k  b' Q* k. n( `" L+ p0 n0 K% {& asvmem(total=10367352832, available=6472179712, percent=37.6, used=8186245120, free=2181107712, active=4748992512, inactive=2758115328, buffers=790724608, cached=3500347392, shared=787554304)$ b, ~- e, g0 {1 o5 \& m
>>> psutil.swap_memory(); d* r$ G+ j7 f, B. Q: m1 A3 p! ?
sswap(total=2097147904, used=296128512, free=1801019392, percent=14.1, sin=304193536, sout=677842944)
& Q. o4 c7 ^, f$ N; X& {$ t/ X' m>>>! {# m% N& p) c. E# G4 }2 P) f
Disks9 k( q: V: Z+ T4 r' `, S" g
>>> import psutil' m2 i! v& A* n* g* n2 ^# Z; r) ~
>>> psutil.disk_partitions()
2 X8 r+ k4 p7 i* c% f: W4 e[sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,nosuid'),3 p8 @2 G5 c/ c9 e" H/ f
sdiskpart(device='/dev/sda2', mountpoint='/home', fstype='ext, opts='rw')]% Y: Q, c2 Q% Q  j0 X+ m. F0 J
>>>- N& D/ G( k: O4 N( ~" h/ F; h
>>> psutil.disk_usage('/')/ d% E* M. b+ w# `, \- h
sdiskusage(total=21378641920, used=4809781248, free=15482871808, percent=22.5)
! D7 y. T& v5 u/ T/ I& V) y% \7 v$ M>>>4 z, J  e0 F: X6 h
>>> psutil.disk_io_counters(perdisk=False)
( V) _* ~( B, g4 N$ Wsdiskio(read_count=719566, write_count=1082197, read_bytes=18626220032, write_bytes=24081764352, read_time=5023392, write_time=63199568, read_merged_count=619166, write_merged_count=812396, busy_time=4523412)6 K- V, R) h  w7 u. y+ R
>>>
7 C, P3 B6 V5 ?! Q% F( }  |  K1 YNetwork3 N' o5 k. e5 d6 M; l- t. X
>>> import psutil9 x& l* k4 j; n- C3 E
>>> psutil.net_io_counters(pernic=True)( D! s' }% ]( ~# \+ z2 b! d$ Y
{'eth0': netio(bytes_sent=485291293, bytes_recv=6004858642, packets_sent=3251564, packets_recv=4787798, errin=0, errout=0, dropin=0, dropout=0),- N9 c4 Y" `: B! Y5 V  G, C
'lo': netio(bytes_sent=2838627, bytes_recv=2838627, packets_sent=30567, packets_recv=30567, errin=0, errout=0, dropin=0, dropout=0)}
" m$ P+ g( ], T4 K5 P2 d! U+ n>>>
& w7 V( F: |" C# f1 s; H>>> psutil.net_connections()
4 I4 H& L4 f2 ^& A* m[sconn(fd=115, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=48776), raddr=addr(ip='93.186.135.91', port=80), status='ESTABLISHED', pid=1254),
; S& A' e* v( e- N5 ] sconn(fd=117, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=43761), raddr=addr(ip='72.14.234.100', port=80), status='CLOSING', pid=2987),
4 l8 U* x8 h* L0 x% I& b sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=60759), raddr=addr(ip='72.14.234.104', port=80), status='ESTABLISHED', pid=None),
6 \$ S$ S' a0 h/ V sconn(fd=-1, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=51314), raddr=addr(ip='72.14.234.83', port=443), status='SYN_SENT', pid=None)
0 Z8 z* E* U4 X: F. b/ \( M. p ...]$ z- p1 a& a" u) r, _
>>>
) ~9 O  H, H* T7 ]>>> psutil.net_if_addrs()) I3 t( O1 D4 D5 R2 t9 H3 H
{'lo': [snic(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast='127.0.0.1', ptp=None),: z% m; O. P1 E4 H% g
        snic(family=<AddressFamily.AF_INET6: 10>, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None, ptp=None),- Z& f, F) i" @* a4 q- v
        snic(family=<AddressFamily.AF_LINK: 17>, address='00:00:00:00:00:00', netmask=None, broadcast='00:00:00:00:00:00', ptp=None)],
3 H' ]# f1 t* l; l* G' ]5 B 'wlan0': [snic(family=<AddressFamily.AF_INET: 2>, address='192.168.1.3', netmask='255.255.255.0', broadcast='192.168.1.255', ptp=None),0 t* Y- P1 Y  v9 i
           snic(family=<AddressFamily.AF_INET6: 10>, address='fe80::c685:8ff:fe45:641%wlan0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None),
  y" K% M/ @; b           snic(family=<AddressFamily.AF_LINK: 17>, address='c4:85:08:45:06:41', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]}
) O6 v% o3 x- V! \$ C6 m>>>' A/ z/ P5 g, N$ g4 r
>>> psutil.net_if_stats()& E, ?: T* R/ V2 W
{'eth0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=100, mtu=1500),
) E9 i0 O: _, U2 M( V$ K* e 'lo': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=65536)}: f4 n$ i  i  Y1 `$ i6 v) A
>>>
$ D; ^9 u; P$ n9 g  B0 ESensors
' q; c$ J) R/ E  f2 E6 t; i>>> import psutil; L0 g/ J. \+ P! E* ^% x
>>> psutil.sensors_temperatures()9 K5 ?1 p9 J6 N8 g" L
{'acpitz': [shwtemp(label='', current=47.0, high=103.0, critical=103.0)],: V) @% ]5 z# v2 N. a& o- v" F
'asus': [shwtemp(label='', current=47.0, high=None, critical=None)],+ S2 U, h0 x+ V: G( @
'coretemp': [shwtemp(label='Physical id 0', current=52.0, high=100.0, critical=100.0),
- L. I2 V8 n: U' d& {              shwtemp(label='Core 0', current=45.0, high=100.0, critical=100.0),+ T; j* b. {/ O( J4 F
              shwtemp(label='Core 1', current=52.0, high=100.0, critical=100.0),
) P, ]: L0 m0 p- |4 |% i              shwtemp(label='Core 2', current=45.0, high=100.0, critical=100.0),: M' {3 b  G" ]/ h$ ~# K+ H) v6 g
              shwtemp(label='Core 3', current=47.0, high=100.0, critical=100.0)]}
# B0 J" e% g9 k1 p) u0 X>>>) d0 l- ?  I/ L' V! b9 ?
>>> psutil.sensors_fans()
+ E3 b. V# P& D" J8 M- F) }8 j{'asus': [sfan(label='cpu_fan', current=3200)]}  W" ?' g' x) Z/ N, C* [$ [$ B+ }% r
>>>: |3 v; M, a; C+ E' H2 p8 {' t
>>> psutil.sensors_battery()+ H. U6 S! H* E4 k0 I( b
sbattery(percent=93, secsleft=16628, power_plugged=False)
* q) [6 r3 p1 a8 J/ ]% L$ P>>>- c7 c' n) o8 v, l
Other system info1 D! i; g# z. Q" V' n" A) R4 K
>>> import psutil
/ T  g. q5 l! M1 m9 N5 D>>> psutil.users()
! c$ a; T/ G6 Z0 b, C" L9 f[suser(name='giampaolo', terminal='pts/2', host='localhost', started=1340737536.0, pid=1352),* L) ^; h1 K0 @' x
suser(name='giampaolo', terminal='pts/3', host='localhost', started=1340737792.0, pid=1788)]
- i- ^$ \3 C% N- C>>>" ]$ U+ e7 g0 \3 F
>>> psutil.boot_time()
# W8 [7 i( x+ F4 h8 m1365519115.0( I  L8 X6 f6 B7 P' Y
>>>5 M) s6 ^9 s- _4 m
Process management
; j! S2 v6 p+ l4 h. y>>> import psutil4 J' f5 P8 \" x9 Y
>>> psutil.pids()% A( C( n$ v1 F0 C. ^" @
[1, 2, 3, 4, 5, 6, 7, 46, 48, 50, 51, 178, 182, 222, 223, 224, 268, 1215, 1216, 1220, 1221, 1243, 1244,
3 B% h' p- i, a, T, @ 1301, 1601, 2237, 2355, 2637, 2774, 3932, 4176, 4177, 4185, 4187, 4189, 4225, 4243, 4245, 4263, 4282,
% `  N+ u4 _8 i, K: q 4306, 4311, 4312, 4313, 4314, 4337, 4339, 4357, 4358, 4363, 4383, 4395, 4408, 4433, 4443, 4445, 4446,) c3 j1 [0 Q- A( x
5167, 5234, 5235, 5252, 5318, 5424, 5644, 6987, 7054, 7055, 7071]
+ L) z6 j/ X+ M; z# @6 k! a>>>7 {4 o1 Q$ K1 H8 ?$ W
>>> p = psutil.Process(7055)
* U* R; s/ }8 k( o; {>>> p.name()
# D' i( v$ u9 f- ['python'
2 Y& m1 x" j  ~- c2 M& H+ e1 n>>> p.exe()3 x* _5 M. P4 {, I0 Q
'/usr/bin/python') t+ _6 Q# F- U
>>> p.cwd()
* X: I+ p1 k6 h% N" Z'/home/giampaolo'! C" e9 D) D/ J3 T1 W$ {
>>> p.cmdline()
/ `" y4 X) A9 s  \['/usr/bin/python', 'main.py']
2 o3 {  w: y7 }- g>>>
8 U# k8 T) o& I! w4 o' G- s>>> p.pid
" c2 m, `7 @/ b+ Y  M# k2 l7055: O; P' \: i6 P" t! k& _( e- y
>>> p.ppid()
, F5 r  w9 [! A% O' @6 @7054
7 c+ g5 L# b! F$ Y: N2 @>>> p.parent()
+ h) e# Y. o6 g& R/ [) H<psutil.Process(pid=7054, name='bash') at 140008329539408>
3 B: D9 I, N( d: J2 Q>>> p.children()
$ a" w& d9 i0 c0 N4 }[<psutil.Process(pid=8031, name='python') at 14020832451977>,
! @! x; Q3 ^6 t, \; R1 [) D# G7 w# Z+ [4 h <psutil.Process(pid=8044, name='python') at 19229444921932>]
0 s1 v1 @2 o6 ?+ F>>>2 x# A2 F. L; S
>>> p.status()
! K* ]2 }+ {3 \9 m'running'# L6 ?% c. f  E  ~( e5 c8 [! n7 h
>>> p.username()
3 J, ~* _, R6 L, @+ F6 n( a- x) ['giampaolo'
& M. h* n! c/ Z+ Y>>> p.create_time()
! E+ v9 I7 P. |' F1267551141.5019531( W" J& I7 ]3 r( f2 D! V
>>> p.terminal()
+ Y& ~1 B1 @+ ]) G$ k+ ^( w'/dev/pts/0'* L) }& I) ?5 {5 [- t) R8 [
>>>
1 i/ e; |6 O$ ]4 W; Z4 J>>> p.uids()
7 ?4 c  E( }$ Y" |2 R1 Jpuids(real=1000, effective=1000, saved=1000); r0 t% w& j9 R7 K0 T; x' S" G+ Z, q
>>> p.gids()
: |* R( f+ A( @7 O% m( R. ]pgids(real=1000, effective=1000, saved=1000)
4 t& O$ h) U- o>>>4 {( _9 a( T4 t7 p# e6 ^  `
>>> p.cpu_times(), U' y/ F$ F+ c+ I) X3 ~
pcputimes(user=1.02, system=0.31, children_user=0.32, children_system=0.1)
# o2 \" [* |+ Z0 H  G5 d* g>>> p.cpu_percent(interval=1.0); o2 J, q+ s5 X/ t
12.1) R* W0 Y, n( G  l( F5 `% ^7 p- V
>>> p.cpu_affinity()
" l5 z# @4 f  s6 P5 k/ M[0, 1, 2, 3]
; R9 O; H+ k' }; ]6 e>>> p.cpu_affinity([0, 1])  # set
1 k; V9 L) O4 ?: n' K: ]>>> p.cpu_num()
4 s9 e/ m+ v6 s* T1
" @, G! L! i# K>>>! g9 }2 [5 l. L9 N) F+ u
>>> p.memory_info()6 ?& B9 j$ t- I
pmem(rss=10915840, vms=67608576, shared=3313664, text=2310144, lib=0, data=7262208, dirty=0)7 }) k0 ^' ~2 q* [
>>> p.memory_full_info()  # "real" USS memory usage (Linux, OSX, Win only)
3 ^4 f  a+ p" U9 |/ r/ o1 Kpfullmem(rss=10199040, vms=52133888, shared=3887104, text=2867200, lib=0, data=5967872, dirty=0, uss=6545408, pss=6872064, swap=0)) \% l+ W9 `! q3 j! @0 Z
>>> p.memory_percent()
# O' D, M6 |( ~/ T# r# r0.7823
" B2 B8 u# N9 h' N0 j0 U: d>>> p.memory_maps(), A  Z9 |1 y8 J7 n8 w$ s" W  _
[pmmap_grouped(path='/lib/x8664-linux-gnu/libutil-2.15.so', rss=32768, size=2125824, pss=32768, shared_clean=0, shared_dirty=0, private_clean=20480, private_dirty=12288, referenced=32768, anonymous=12288, swap=0),( ^# r) }6 U, C2 b2 U7 }7 `* ~% }
pmmap_grouped(path='/lib/x8664-linux-gnu/libc-2.15.so', rss=3821568, size=3842048, pss=3821568, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=3821568, referenced=3575808, anonymous=3821568, swap=0),
5 h1 Z1 ?: g. g' I( [ pmmap_grouped(path='/lib/x8664-linux-gnu/libcrypto.so.0.1', rss=34124, rss=32768, size=2134016, pss=15360, shared_clean=24576, shared_dirty=0, private_clean=0, private_dirty=8192, referenced=24576, anonymous=8192, swap=0),3 e5 m; D' [7 @  y. |9 a
pmmap_grouped(path='[heap]',  rss=32768, size=139264, pss=32768, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=32768, referenced=32768, anonymous=32768, swap=0),
' H/ f- M9 R) L pmmap_grouped(path='[stack]', rss=2465792, size=2494464, pss=2465792, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=2465792, referenced=2277376, anonymous=2465792, swap=0),/ N  h) Y1 R8 v/ ^; B5 l$ ^: r
...]
3 j3 H& D  G5 c! x; ?7 d, `0 z! V8 x>>>
5 k, k! A( t& T3 E3 ~>>> p.io_counters()
5 X- ?$ l5 x' rpio(read_count=478001, write_count=59371, read_bytes=700416, write_bytes=69632, read_chars=456232, write_chars=517543)9 c$ u% }& H) k# O  T2 [
>>># h2 U% p. m8 ^* Q
>>> p.open_files()
( Z8 M- k% R8 W! h[popenfile(path='/home/giampaolo/svn/psutil/setup.py', fd=3, position=0, mode='r', flags=32768),/ D; [" I( e/ v* x( F+ E6 T
popenfile(path='/var/log/monitd', fd=4, position=235542, mode='a', flags=33793)]! F% N" i* K7 W  E+ R
>>>
. R& q3 |% n, b/ F>>> p.connections()
2 q8 k) P5 i3 B% W  |- v[pconn(fd=115, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=48776), raddr=addr(ip='93.186.135.91', port=80), status='ESTABLISHED'),
  U+ S: B) r' r4 H1 S0 q9 w" E pconn(fd=117, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=43761), raddr=addr(ip='72.14.234.100', port=80), status='CLOSING')," x( |/ ?1 W$ z# E0 i* z$ Y: w
pconn(fd=119, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=60759), raddr=addr(ip='72.14.234.104', port=80), status='ESTABLISHED'),
/ b* w7 f7 G1 Q9 x8 T; p pconn(fd=123, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=51314), raddr=addr(ip='72.14.234.83', port=443), status='SYN_SENT')]
& T9 d  `* Z! S+ f9 G, n8 K>>>
1 i; a7 L  w) {>>> p.num_threads()
  R1 r7 b6 }# G" B0 E" j% m: t: z* ?4- ]' L& d2 b7 ]4 r
>>> p.num_fds()' w  G' z& K! L& O& ]
8
% J5 ^; e! ]5 L8 k, G! {: ]. ~>>> p.threads()
2 Y1 [$ y2 G- }3 E& f[pthread(id=5234, user_time=22.5, system_time=9.2891),
/ o2 m8 ^' z9 ]6 I2 A( O  z pthread(id=5235, user_time=0.0, system_time=0.0),; S: Q" r! m$ Q  s( ]
pthread(id=5236, user_time=0.0, system_time=0.0),3 ^. c+ \1 t# N
pthread(id=5237, user_time=0.0707, system_time=1.1)]0 Z* X# L& Y! |
>>>- ~* k9 N6 v( g/ o
>>> p.num_ctx_switches()
! y4 h* g% a$ Z3 p8 h0 W: Fpctxsw(voluntary=78, involuntary=19)- L9 Q2 K% H* J
>>>
6 r3 z4 e6 z" G% q>>> p.nice()
: w) e+ Z5 ]( o+ j. d/ e0
6 z9 A( Z! G8 y! n2 ~>>> p.nice(10)  # set# k9 H$ O4 s: @; M0 v0 u  h6 d
>>>
: K& l( p: o7 k>>> p.ionice(psutil.IOPRIO_CLASS_IDLE)  # IO priority (Win and Linux only)
0 _+ d5 e8 p5 n5 |9 N- T" F>>> p.ionice()
% G7 y' P* F; ^4 C' ?% I6 j+ |pionice(ioclass=<IOPriority.IOPRIO_CLASS_IDLE: 3>, value=0)
0 ]5 \  ?4 t9 U" \& M>>>
. B3 V) i' P# u- U" G( y8 c, A$ y>>> p.rlimit(psutil.RLIMIT_NOFILE, (5, 5))  # set resource limits (Linux only)
. g8 r1 Q5 w4 P>>> p.rlimit(psutil.RLIMIT_NOFILE)# u, X5 J# q8 [! O! [2 h
(5, 5)
! b& V) y2 n+ ^: u- z+ `9 \>>>
: z  |& J7 V. u>>> p.environ()! h5 |1 W- ?5 \. ]; u
{'LC_PAPER': 'it_IT.UTF-8', 'SHELL': '/bin/bash', 'GREP_OPTIONS': '--color=auto',% m: a* C. }4 y( f2 |% s3 {
'XDG_CONFIG_DIRS': '/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg', 'COLORTERM': 'gnome-terminal',6 l# H) ^/ i$ Y8 B) c" D
...}
; d8 q4 T* G" m6 W/ s, M$ D>>>
3 N- t( Q4 _9 u0 B6 F  x* T- g>>> p.as_dict()9 x5 h. ~2 X+ f3 W
{'status': 'running', 'num_ctx_switches': pctxsw(voluntary=63, involuntary=1), 'pid': 5457, ...}
5 W# T" M4 H% g& l; ^* T) i. ]>>> p.is_running()
$ o, T2 A4 l1 m8 ~6 W0 N. |6 \True  F5 R6 {* M0 d; I7 c, n
>>> p.suspend(): K, i: K8 e7 Z3 I
>>> p.resume()$ c! O2 x4 m; D/ R* F7 X) P
>>>
5 s+ @. T8 ?6 y. L  y+ ~* C$ V>>> p.terminate()0 v+ _1 e" s4 x3 A
>>> p.wait(timeout=3)
1 S: x# e3 n3 P) V, }3 h0" R. J. l$ u$ G4 P( g
>>>7 `7 e2 ?7 v* L7 _5 |$ f2 {
>>> psutil.test()0 ]* \  i* \2 O! l' r. u
USER         PID %CPU %MEM     VSZ     RSS TTY        START    TIME  COMMAND
2 X' b0 N( F3 \( ]' rroot           1  0.0  0.0   24584    2240            Jun17   00:00  init
( b* z! N* H# m7 d+ P3 L; ~root           2  0.0  0.0       0       0            Jun17   00:00  kthreadd
4 y+ V4 x. n! r. `3 d' t3 T9 ~root           3  0.0  0.0       0       0            Jun17   00:05  ksoftirqd/0( j- h( d) j, `$ n
...
+ }( t. B  z/ `) A6 i9 X9 Hgiampaolo  31475  0.0  0.0   20760    3024 /dev/pts/0 Jun19   00:00  python2.4
# D9 X: }+ N6 i2 L/ f0 vgiampaolo  31721  0.0  2.2  773060  181896            00:04   10:30  chrome
8 U7 R! n8 ]$ _3 Groot       31763  0.0  0.0       0       0            00:05   00:00  kworker/0:1
& E- `  e% K$ P  b" U>>>
, W+ y2 d' O# U* W4 EFurther process APIs
' u# L4 }0 ?. n/ `5 L/ v4 U  `>>> import psutil- k3 J( a6 ~$ V# D
>>> for proc in psutil.process_iter(attrs=['pid', 'name']):
, m) {0 |, S& U+ f5 U! B...     print(proc.info)
1 r( [# G- b/ @, w.... G2 K' Q: c  o& M% r
{'pid': 1, 'name': 'systemd'}' V" {) z: [2 ?* v
{'pid': 2, 'name': 'kthreadd'}* D9 F) T) R& }4 J. H* g+ S
{'pid': 3, 'name': 'ksoftirqd/0'}
* x  j  |0 X# Q% J...
+ c$ V1 V3 g6 F: d* t>>>
* Q4 o+ e$ s& q- L/ V- w: i, ~: [>>> psutil.pid_exists(3)
" Q/ o7 O5 K. F1 T. K$ ]* i) LTrue
/ n$ Y5 `5 m3 L0 ?/ z>>>
* P( F/ i) o- ?, W( a0 J0 _>>> def on_terminate(proc):- U0 `  v! J- U
...     print("process {} terminated".format(proc))- L" Q% L& M, A
...6 W; V% w" X& y& x2 o
>>> # waits for multiple processes to terminate4 S; O7 c% j" p$ y! D6 H
>>> gone, alive = psutil.wait_procs(procs_list, timeout=3, callback=on_terminate)
7 g  ^: @0 \) e8 \  j>>>8 H$ E5 }2 t, G/ G% C* c/ k) i" U
Popen wrapper:
+ S/ ~6 g" w2 f0 R( i& w: |; Y/ S7 e( r6 x+ B
>>> import psutil2 r% l; y6 w$ k5 Z' K
>>> from subprocess import PIPE
; p1 `! X6 `4 a>>> p = psutil.Popen(["/usr/bin/python", "-c", "print('hello')"], stdout=PIPE)
+ ?9 m* |8 S2 {9 M) s>>> p.name()
" m6 l- a9 A% V8 @% ^$ |* l7 ]'python'% V& l1 ~9 ~( [
>>> p.username()2 m* u' \  b7 T  ]2 o7 Y; o
'giampaolo'
, J0 V0 N4 x3 _>>> p.communicate()- q8 M# e2 l) o" y" D
('hello\n', None)* l0 x; q7 ~3 S3 h; _
>>> p.wait(timeout=2). j9 c. v, A: N. [. p. v4 z% X8 D. e
0. p  D6 n$ C  Q( G) u) U2 B
>>>+ G2 {7 ?2 E& y/ w+ h
Windows services
5 T" Z4 u+ O8 z3 k/ |# ~>>> list(psutil.win_service_iter()). y6 p7 f! ~( w. p" j
[<WindowsService(name='AeLookupSvc', display_name='Application Experience') at 38850096>,
% c, d; K0 O$ ~5 i$ M: F, h1 r <WindowsService(name='ALG', display_name='Application Layer Gateway Service') at 38850128>,# J: L0 o4 x4 _% d
<WindowsService(name='APNMCP', display_name='Ask Update Service') at 38850160>,  i. z9 S5 ?0 A/ L9 {* Z! C
<WindowsService(name='AppIDSvc', display_name='Application Identity') at 38850192>,
( ]8 `3 E' X4 V ...]
3 t' @5 u$ g% G8 ~4 e>>> s = psutil.win_service_get('alg')/ d5 L8 t& R! f
>>> s.as_dict()
. z9 i3 A! M- m4 A/ L; G{'binpath': 'C:\\Windows\\System32\\alg.exe',' _6 S" k$ S0 s3 @- F) [/ U. [
'description': 'Provides support for 3rd party protocol plug-ins for Internet Connection Sharing',. C; m" Y% F% c' e$ ^5 P: \5 ^
'display_name': 'Application Layer Gateway Service',
, c/ x# b: H* b  h1 w6 u 'name': 'alg',6 C2 q' V4 D5 g3 m3 l
'pid': None,
  R8 _* f( C+ X1 o7 Y' I& E' q 'start_type': 'manual',6 a, L- S- R! g& g6 ^2 z$ n0 p1 u
'status': 'stopped',- n# Z- t- Z) M9 X( |6 B" C. ^. B# ]* F6 e' s
'username': 'NT AUTHORITY\\LocalService'}# M0 P9 C3 z* a$ Q  \" y2 t
Other samples
 楼主| 发表于 2017-12-23 21:09:49 | 显示全部楼层
Installing using pip on UNIX% X( Y- r8 o& ]! O

+ j! X# R; S; [The easiest way to install psutil on UNIX is by using pip (but first you might need to install python header files; see later). First install pip:- a" y7 e4 F& A  I
" S0 N* q# S9 k; Y" X5 D
$ wget [url]https://bootstrap.pypa.io/get-pip.py[/url]  l/ P$ s$ Q" C( t' _% [# D7 g
$ python get-pip.py0 S; N  d& U1 X+ c
...then run:5 @* @6 {4 o5 O# Z, B) j
1 l) L0 h* k7 v" A. q) E: t! l9 }
$ pip install psutil
您需要登录后才可以回帖 登录 | 开始注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

如有购买积分卡请联系497906712

QQ|返回首页|Archiver|手机版|小黑屋|易陆发现 点击这里给我发消息

GMT+8, 2021-10-18 05:45 , Processed in 0.055496 second(s), 28 queries .

Powered by LR.LINUX.cloud bbs168x X3.2 Licensed

© 2012-2022 Comsenz Inc.

快速回复 返回顶部 返回列表