3 JZ$v@sldZddlZddlZddlZddlZddljjZddl m Z ddl m Z ddl mZddl mZddl mZdd l mZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlmZddlmZddl m!Z!ddl m"Z"ddl m#Z#gZ$erhej%ej&ej'ej(ej)ej*ej+ej,ej-ej.ej/ej0ej1ej2iZ3nesterej%ej&ej)ej*ej+ej,ej4ej.ej-ej.ej'ej5ej6ej(iZ3nBerej%ej&ej7ej(ej8ej.ej+ej,ej-ej.ej4ej9ej:ej;iZ3ejej?ej@ejAejBejCejDejEejFejGejHejIejJejKejLejMejNejOejPejQejRejSi ZTerjejUdZVn ejUdZVejWZWeXddddddddd d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1ZYe d2d3d4d5d6d7d8d9d:d;dd?d@dAdBdCgZ[e dDdEdFdGdHdIgZ\e\Z]e dJd?dAdKdLgZ^e dMdNZ_e dOdPZ`erFe dQdRdSdTdUdVdWdXgZane dQdRdSdTdUgZadYdZZbd[d\Zcd]d^Zdeeed_rd`d_Zfndad_Zfdbef_gdcddZheserdedfZindgdfZidhdiZjddjdkZkejlZlejmZmejnZnejoZodldmZpdndoZqerdpdqZrdrdsZsdtduZtedvdwZudxdyZves2errr zgroup/children/group/cpu) rfZcpu_count_physrfindETZ fromstringlenfindallclearrv)rtsindexrootrmrmrnrys    c Cstrtj\}}}}}nttrrtj\}}}}}}}tdd.}x&|D]}|jdrDt|jd}qDWWdQRXntrtj\}}}}}}}t j ||||S)z*Return various CPU stats as a named tuple.z /proc/statrdsintrrN) r rf cpu_statsrrgrhrirjrrZ scpustats) ZctxswZintrsZ soft_intrsZsyscallsZtrapsZfaultsZforksrkrlrmrmrnrs     rc CsDg}tj}x2|D]*}|\}}}}tj||||}|j|qW|S)zReturn mounted disk partitions as a list of namedtuples. 'all' argument is ignored, see: https://github.com/giampaolo/psutil/issues/906 )rfdisk_partitionsrZ sdiskpartrs) allretlistZ partitions partitionZdeviceZ mountpointZfstypeZoptsntuplermrmrnr<s  rcCsjtj}i}xV|D]N}tj|}tj|}tj|\}}ttdrNtj|}tj ||||||<qW|S)z)Get NIC stats (isup, duplex, speed, mtu). NicDuplex) net_io_counterskeys cext_posixZ net_if_mtuZ net_if_flagsZnet_if_duplex_speedhasattrrrZ snicstats)namesrtrHZmtuZisupZduplexZspeedrmrmrn net_if_statsWs      rcCstrzg}xltD]b}yt|j|}Wnttfk r@wYqXx.|D]&}t|}|j||jtj |qHWqW|S|tj krt d|dj ddt Dft |\}}t }trtjd}ntj}x|D]}|\} } } } } }}| |ko| |kry t|}Wn tk r&ttj}YnX| ttfkrV| rFtj| } | rVtj| } t| } t| } tj | | | | | ||}|j|qWt|S)z System-wide network connections.z+invalid %r kind argument; choose between %sz, cSsg|] }t|qSrm)repr).0xrmrmrn wsz#net_connections..rrz)rpidsProcess connectionsrrlistrsrZsconnr ValueErrorjoinsetrrfnet_connections TCP_STATUSESKeyErrorPSUTIL_CONN_NONErr addrrradd)kindrtpidZconsZconnfamiliestypesrawlistrufdfamtypeladdrraddrr1ntrmrmrnresJ          rc Csbytj\}}}Wntk r&dSX|dk}|r.wrapper) functoolswraps)rrrm)rrnwrap_exceptionssrccsy dVWntk r}zf|jtjtjfkrZt|jsHt|j|jnt|j|j|j |jtj tj fkrzt |j|jWYdd}~XnXdS)z8Same as above, for routines relying on reading /proc fs.N) EnvironmentErrorrENOENTrrrrrrrrrr)Zinstrrmrmrnwrap_exceptions_procfs s  rc@seZdZdZdddgZddZeddZd d Zd d Z e d dZ e ddZ e ddZ e ddZe ddZe ddZe ddZe ddZere ddZe dd ZeZe d!d"Ze d#d$Ze d%d&Ze d'd(Ze dJd*d+Ze dKd-d.Ze d/d0Ze d1d2Ze d3d4Ze d5d6Z e d7d8Z!e"d9d:Z#e"d9d;Z$drxe d?d@Z(ne%Z(e&e'dAre dBdCZ)ne%Z)ere dDdEZ*e dFdGZ+e dHdIZ,d,S)Lrz1Wrapper class around underlying C implementation.rrrcCs||_d|_d|_dS)N)rrr)rrrmrmrn__init__"szProcess.__init__cCs$tj|j}t|ttks t|S)z;Retrieves multiple process info in one shot as a raw tuple.)rfZproc_oneshot_inforr}kinfo_proc_mapr)rrtrmrmrnoneshot's zProcess.oneshotcCs|jjdS)N)rZcache_activate)rrmrmrn oneshot_enter.szProcess.oneshot_entercCs|jjdS)N)rZcache_deactivate)rrmrmrn oneshot_exit1szProcess.oneshot_exitcCs(|jtd}|dk r|Stj|jS)NrH)rrrfZ proc_namer)rrHrmrmrnrH4sz Process.namec Csdtrtj|jStrD|jdkr"dSt|tjd|jSQRXn|j}|r\t |dSdSdS)Nrz /proc/%s/exe) r rfZproc_exerrrosreadlinkcmdliner)rrrmrmrnexe9s    z Process.execCstr|jdkrgStry tj|jStk r}zB|jtjkrnt|jsZt |j|j qpt |j|j |j nWYdd}~XqXn tj|jSdS)Nr) rrrrfZ proc_cmdlinerrEINVALrrrrr)rrrmrmrnrOs   zProcess.cmdlinec Cs:|jtd}tj}y||Stk r4dSXdS)Nr8)rrrZget_terminal_mapr)rZtty_nrZtmaprmrmrnterminales zProcess.terminalcCs|jtd|_|jS)Nr0)rrr)rrmrmrnr0nsz Process.ppidcCs.|j}tj|td|td|tdS)Nr2r3r4)rrZpuidsr)rrawtuplermrmrnuidsss   z Process.uidscCs.|j}tj|td|td|tdS)Nr5r6r7)rrZpgidsr)rrrmrmrngids{s   z Process.gidscCs8|j}tj|td|td|td|tdS)Nr>r?r@rA)rrr^r)rrrmrmrnrqs    zProcess.cpu_timescCs|jtdS)NrG)rr)rrmrmrncpu_numszProcess.cpu_numcCs@|j}t|td|td|td|td|tdS)NrBrCrDrErF)rrZr)rrrmrmrn memory_infos    zProcess.memory_infocCs|jtdS)Nr9)rr)rrmrmrnr9szProcess.create_timecCs&ttdrtj|jSt|jSdS)Nproc_num_threads)rrfrrr}threads)rrmrmrn num_threadss  zProcess.num_threadscCs$|j}tj|td|tdS)Nr:r;)rrZpctxswr)rrrmrmrnnum_ctx_switchess zProcess.num_ctx_switchescCsLtj|j}g}x*|D]"\}}}tj|||}|j|qWtrH|j|S)N)rfZ proc_threadsrrZpthreadrsrrH)rrrZ thread_idutimeZstimerrmrmrnrs zProcess.threadsinetcCs|tkr(td|djddtDftrt|\}}t}tj|j}x|D]}|\}}} } } } } | |jksvt||ko| |krRy t | } Wnt k rt tj } YnX|t t fkr| rtj| } | rtj| } t|}t| } tj||| | | | }|j|qRW|jt|St|\}}tj|j||}g}x|D]z}|\}}} } } } |t t fkr| rttj| } | rtj| } t|}t| } t | } tj||| | | | }|j|qBWtr|j|S)Nz+invalid %r kind argument; choose between %sz, cSsg|] }t|qSrm)r)rrrmrmrnrsz'Process.connections..)r rrrrrfrrrrrrrr rrrrZpconnrrHrZproc_connectionsrsr)rrrrrtrrurrrrrr1rrrmrmrnrsX           zProcess.connectionsNcCstj|j||jS)N)rZwait_pidrr)rZtimeoutrmrmrnwaitsz Process.waitcCs tj|jS)N)r getpriorityr)rrmrmrnnice_getszProcess.nice_getcCstj|j|S)N)r setpriorityr)rvaluermrmrnnice_setszProcess.nice_setcCs|jtd}tj|dS)Nr1?)rr PROC_STATUSESget)rcodermrmrnr1szProcess.statuscCs(|j}tj|td|tdddS)Nr<r=rrzrz)rrZpior)rrrmrmrn io_counterss   zProcess.io_countersc Csftr|jdkrdStr8t|tjd|jSQRXn*ttdrRtj|jpPdSt t r\dnddS)z)Return process current working directory.rNz /proc/%s/cwdproc_open_filesz&supported only starting from FreeBSD 8r) rrrrrrrrfZproc_cwdrxr )rrmrmrncwds  z Process.cwdZmmapz*path rss, private, ref_count, shadow_countz6addr, perms path rss, private, ref_count, shadow_countcCstdS)N)rx)rrmrmrn_not_implemented)szProcess._not_implementedrcCstj|j}dd|DS)z8Return files opened by process as a list of namedtuples.cSsg|]\}}tj||qSrm)rZ popenfile)rpathrrmrmrnr3sz&Process.open_files..)rfrr)rrrmrmrn open_files/s zProcess.open_files proc_num_fdscCstj|j}tr|j|S)z=Return the number of file descriptors opened by this process.)rfrrrrH)rrtrmrmrnnum_fds:s zProcess.num_fdscCs tj|jS)N)rfZproc_cpu_affinity_getr)rrmrmrncpu_affinity_getJszProcess.cpu_affinity_getcCstttt}x$|D]}||krtd||fqWytj|j|Wn\tk r}z@|j t j t j fkrx$|D]}||krttd||fqtWWYdd}~XnXdS)Nz#invalid CPU #%i (choose between %s)) tupleranger}rrrrfZproc_cpu_affinity_setrrrrZEDEADLK)rZcpusZallcpusZcpurrmrmrncpu_affinity_setNs  zProcess.cpu_affinity_setcCs tj|jS)N)rfZproc_memory_mapsr)rrmrmrn memory_mapsgszProcess.memory_maps)r)N)-__name__ __module__ __qualname____doc__ __slots__rr rrrrrHrrrr0rrrqr rrZmemory_full_infor9rrrrrrrr1rrrZnt_mmap_groupedZ nt_mmap_extrrrfrrrrrrmrmrmrnrs\                6          r)F)}r contextlibrrrZxml.etree.ElementTreeZetreeZ ElementTreer| collectionsrZsocketrrrrrrfrrr r r r r rrrrrZ_compatr _exceptionsrrrZ__extra__all__ZSIDLZ STATUS_IDLEZSRUNZSTATUS_RUNNINGZSSLEEPZSTATUS_SLEEPINGZSSTOPZSTATUS_STOPPEDZSZOMBZ STATUS_ZOMBIEZSWAITZSTATUS_WAITINGZSLOCKZ STATUS_LOCKEDrZSDEADZ STATUS_WAKINGZSONPROCZSACTIVEZSDYINGZ STATUS_DEADZ SSUSPENDEDZSTATUS_SUSPENDEDZTCPS_ESTABLISHEDZCONN_ESTABLISHEDZ TCPS_SYN_SENTZ CONN_SYN_SENTZTCPS_SYN_RECEIVEDZ CONN_SYN_RECVZTCPS_FIN_WAIT_1ZCONN_FIN_WAIT1ZTCPS_FIN_WAIT_2ZCONN_FIN_WAIT2ZTCPS_TIME_WAITZCONN_TIME_WAITZ TCPS_CLOSEDZ CONN_CLOSEZTCPS_CLOSE_WAITZCONN_CLOSE_WAITZ TCPS_LAST_ACKZ CONN_LAST_ACKZ TCPS_LISTENZ CONN_LISTENZ TCPS_CLOSINGZ CONN_CLOSINGrZ CONN_NONErsysconfZPAGESIZEZAF_LINKdictrrIrTrZZpfullmemr^rarbrcrorprqrrrrwrvryrrZ disk_usageZdisk_io_countersrZ net_if_addrsrrrrrrrrrcontextmanagerrobjectrrmrmrmrns                                    # 6