3 JZa(@sdZddlZddlZddlZddlZddlZddlmZddlm Z yddlm Z Wnle k rZ zPee jjdrejddkrd Zed 7Zed 7Zed 7ZeenWYddZ [ XnXdd l mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddl mZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddl#m$Z$ddl#m%Z%ddl#m&Z&ddl m'Z'ddl m(Z(dd l m)Z)dd!l m*Z*dd"l m+Z+dd#l m,Z,ej-dkrddl.Z.ndZ.d&d'd(d)d*d+d,d-d.d/g Z/d0Z0d1Z1e2ej3ej4e j5gZ6e2e j7e j8gZ9e.dkrDdZ:ne.j;d2d/diZe j?e j@e jAe jBe jCe jDe jEe jFe jGe jHe jIe jJe jKe jLe jMe jNe jOe jPe jQe jRe jSe0e jTe jUi ZVe.dk rGd3d4d4e.j;ZWeXjYeWjZe[ddd5d$d%d6dd7d8d9d:d;dd?d@dAdBdCdDdEdFZ\edGdHdIdJdKdLgZ]edMdNdOdPdQdRgZ^edSdTdUdVdWdXdYdZd[d\d]d^d_g Z_ed`e_j`dZaedbdcdTgZbedddedfjcebj`ZdedgdhdidjdkdldmgZeedndodpdqZfdrdsZgdtduZhdvdwZie jjZjdxdyZkdzd{Zld|d}Zmd~dZnddZoddZpddZqddZrdddZsddZtddZuddZvddZwdaxddZyddZzdd&Z{dd'Z|Gddde}Z~e jZe jZe jZddZGddde}ZdS)z Windows platform implementation.N) namedtuple)_common)_psutil_windowszdll load failedz3this Windows version is too old (< Windows Vista); z:psutil 3.4.2 is the latest version which supports Windows z92000, XP and 2003 server; it may be possible that psutil z)will work if compiled from sources though) conn_tmap)ENCODING) ENCODING_ERRS) isfile_strict)memoize_when_activated)parse_environ_block)sockfam_to_enum)socktype_to_enum) usage_percent)long) lru_cache)PY3)unicode)xrange) AccessDenied) NoSuchProcess)TimeoutExpired)ABOVE_NORMAL_PRIORITY_CLASS)BELOW_NORMAL_PRIORITY_CLASS)HIGH_PRIORITY_CLASS)IDLE_PRIORITY_CLASS)NORMAL_PRIORITY_CLASS)REALTIME_PRIORITY_CLASSwin_service_iterwin_service_getrrrrrrCONN_DELETE_TCBAF_LINKZ DELETE_TCBi AddressFamilyc@s$eZdZeZeZeZeZeZeZdS)PriorityN) __name__ __module__ __qualname__rrrrrrr)r)"/usr/lib64/python3.6/_pswindows.pyr%os r% ) num_handles ctx_switches user_time kernel_time create_time num_threads io_rcount io_wcount io_rbytes io_wbytesio_count_othersio_bytes_othersnum_page_faults peak_wsetwsetpeak_paged_pool paged_poolpeak_non_paged_poolnon_paged_poolpagefile peak_pagefile mem_private scputimesusersystemidle interruptdpcsvmemtotalZ availablepercentusedfreepmemrssvmsrHrIrJrKrLZpeak_nonpaged_poolZ nonpaged_poolrOrPZprivatepfullmemuss pmmap_groupedpath pmmap_extz addr perms  pioZ read_countZ write_countZ read_bytesZ write_bytes other_countZ other_bytesi)maxsizecCs<dj|jddd}tj|}tjj||t|dS)zConvert paths using native DOS format like: "\Device\HarddiskVolume1\Windows\systemew\file.txt" into: "C:\Windows\systemew\file.txt" \Nr)joinsplitcextZwin32_QueryDosDeviceosrclen)sZrawdriveZ driveletterr)r)r*convert_dos_paths rpcCs(tr|St|tr|S|jttdSdS)zmEncode a unicode string to a byte string by using the default fs encoding + "replace" error handler. )errorsN)r isinstancestrencoderr )ror)r)r* py2_strencodes  ruc CsNtj}|\}}}}}}|}|}|} ||} t|||dd} t||| | | S)z&System virtual memory as a namedtuple.r)_round)rl virtual_memrrX) memZtotphysZ availphysZtotpagefZ availpagefZtotvirtZfreevirtrYZavailr\r[rZr)r)r*virtual_memorysrycCsBtj}|d}|d}||}t||dd}tj||||ddS)z=Swap system memory as a (total, used, free, sin, sout) tuple.r+rr)rvr)rlrwrrZsswap)rxrYr\r[rZr)r)r* swap_memorys rzcCsPtrt|tr|jtdd}tj|\}}||}t||dd}tj ||||S)z'Return disk usage associated with path.strict)rqr)rv) rrrbytesdecoderrl disk_usagerrZ sdiskusage)rcrYr\r[rZr)r)r*r~s r~cCstj|}dd|DS)zReturn disk partitions.cSsg|]}tj|qSr))rZ sdiskpart).0xr)r)r* sz#disk_partitions..)rldisk_partitions)allrawlistr)r)r*rs rcCs<tj\}}}tddttjD}t||||j|jS)z)Return system CPU times as a named tuple.cSsg|] }t|qSr))sum)rnr)r)r*rszcpu_times..)rl cpu_timesrRzip per_cpu_timesrVrW)rSrTrUZ percpu_summedr)r)r*r srcCs>g}x4tjD](\}}}}}t|||||}|j|qW|S)z6Return system per-CPU times as a list of named tuples.)rlrrRappend)retrSrTrUrVrWitemr)r)r*rs rcCstjS)z0Return the number of logical CPUs in the system.)rlcpu_count_logicalr)r)r)r*rsrcCstjS)z1Return the number of physical CPUs in the system.)rlZcpu_count_physr)r)r)r*cpu_count_physical"srcCs$tj\}}}}d}tj||||S)zReturn CPU statistics.r)rl cpu_statsrZ scpustats)r=Z interruptsZdpcsZsyscallsZsoft_interruptsr)r)r*r's rcCs(tj\}}d}tjt||t|gS)zMReturn CPU frequency. On Windows per-cpu frequency is not supported. g)rlcpu_freqrZscpufreqfloat)ZcurrZmax_Zmin_r)r)r*r/s rc Cs|tkr(td|djddtDft|\}}tj|||}t}x|D]}|\}}} } } } } | rrtj| } | rtj| } t| } t |}t | } |dkrtj ||| | | | | }ntj ||| | | | }|j |qNWt|S)zReturn socket connections. If pid == -1 return system-wide connections (as opposed to connections opened by one process only). z+invalid %r kind argument; choose between %sz, cSsg|] }t|qSr))repr)rrr)r)r*rCsz#net_connections..r)r ValueErrorrjrlnet_connectionssetraddr TCP_STATUSESr rZsconnZpconnaddlist)kind_pidZfamiliestypesrrrfdZfamtypeZladdrZraddrstatuspidntr)r)r*r=s(    rcCs|i}tj}xj|jD]^\}}ts@t|ts8tt|t|}|\}}}}t t dr`t j |}t j ||||||<qW|S)z)Get NIC stats (isup, duplex, speed, mtu). NicDuplex) rl net_if_statsitemsrrrrAssertionErrorrruhasattrrrZ snicstats)rZrawdictnamerZisupZduplexZspeedZmtur)r)r*rXs   rcCstj}tdd|jDS)zsReturn network I/O statistics for every network interface installed on the system as a dict of raw tuples. cSsg|]\}}t||fqSr))ru)rkvr)r)r*rlsz#net_io_counters..)rlnet_io_countersdictr)rr)r)r*rgsrcCs<g}x2tjD]&}t|}t|d|d<|j|qW|S)z,Return the addresses associated to each NIC.r)rl net_if_addrsrrur)rrr)r)r*ros rcCsdtj\}}}}|dk}t|d@}t|d@}|r8dS|s@|rHtj}n|dkrVtj}tj|||S)zReturn battery information.rr.Nr)rlsensors_batteryboolrZPOWER_TIME_UNLIMITEDZPOWER_TIME_UNKNOWNZsbattery)Z acline_statusflagsrZZsecsleftZ power_pluggedZ no_batteryZchargingr)r)r*r~s  rcCs,ttj}t|tdkr tS|a|SdS)z:The system boot time expressed in seconds since the epoch.rN)rrl boot_timeabs _last_btime)rr)r)r*rs  rcCsLg}tj}x:|D]2}|\}}}t|}tj|d||d}|j|qW|S)z:Return currently connected users as a list of namedtuples.N)rlusersrurZsuserr)retlistrrrSZhostnameZtstamprr)r)r*rs  rccs.x(tjD]\}}tt|t|Vq WdS)z*Yields a list of WindowsService instances.N)rlZwinservice_enumerateWindowsServiceru)r display_namer)r)r*r scCst|d}|jd|_|S)zBOpen a Windows service and return it as a WindowsService instance.Nr)r _query_config _display_name)rZservicer)r)r*r!s c@seZdZdZddZddZddZdd Zd d Zd d Z ddZ e j ddZ ddZddZddZddZddZddZddZd d!Zd"d#Zd$S)%rz(Represents an installed Windows service.cCs||_||_dS)N)_namer)selfrrr)r)r*__init__szWindowsService.__init__cCs d|j|jf}d|jj|fS)Nz(name=%r, display_name=%r)z%s%s)rr __class__r&)rZdetailsr)r)r*__str__szWindowsService.__str__cCsd|jt|fS)Nz <%s at %s>)rid)rr)r)r*__repr__szWindowsService.__repr__cCst|tstS|j|jkS)N)rrrNotImplementedr)rotherr)r)r*__eq__s zWindowsService.__eq__cCs ||k S)Nr))rrr)r)r*__ne__szWindowsService.__ne__c CsH|jtj|j\}}}}WdQRXtt|t|t|t|dS)N)rbinpathusername start_type)_wrap_exceptionsrlZwinservice_query_configrrru)rrrrrr)r)r*rs zWindowsService._query_configc Cs<|jtj|j\}}WdQRX|dkr0d}t||dS)Nr)rr)rrlZwinservice_query_statusrr)rrrr)r)r* _query_statuss  zWindowsService._query_statusccsy dVWnrtk r|}zV|jtkr>td|jd|jdn.|jtksR|jtkrjtd|jd|jdnWYdd}~XnXdS)z{Ctx manager which translates bare OSError and WindowsError exceptions into NoSuchProcess and AccessDenied. Nz2service %r is not querable (not enough privileges))rrmsgzservice %r does not exist))Z WindowsErrorerrnoACCESS_DENIED_ERRSETrrNO_SUCH_SERVICE_ERRSETZwinerrorr)rerrr)r)r*rs    zWindowsService._wrap_exceptionscCs|jS)zThe service name. This string is how a service is referenced and can be passed to win_service_get() to get a new WindowsService instance. )r)rr)r)r*rszWindowsService.namecCs|jS)z_The service display name. The value is cached when this class is instantiated. )r)rr)r)r*rszWindowsService.display_namecCs |jdS)zwThe fully qualified path to the service binary/exe file as a string, including command line arguments. r)r)rr)r)r*rszWindowsService.binpathcCs |jdS)z,The name of the user that owns this service.r)r)rr)r)r*rszWindowsService.usernamecCs |jdS)zRA string which can either be "automatic", "manual" or "disabled". r)r)rr)r)r*rszWindowsService.start_typecCs |jdS)zzThe process PID, if any, else None. This can be passed to Process class to control the service's process. r)r)rr)r)r*r'szWindowsService.pidcCs |jdS)zService status as a string.r)r)rr)r)r*r-szWindowsService.statuscCsttj|jS)zService long description.)rurlZwinservice_query_descrr)rr)r)r* description1szWindowsService.descriptioncCs>|j}|j|j|j|d<|j|d<|j|d<|S)zUUtility method retrieving all the information above as a dictionary. rrr)rupdaterrrr)rdr)r)r*as_dict7s    zWindowsService.as_dictN)r&r'r(__doc__rrrrrrr contextlibcontextmanagerrrrrrrrrrrr)r)r)r*rs$ rcstjfdd}|S)zqDecorator which translates bare OSError and WindowsError exceptions into NoSuchProcess and AccessDenied. csly|f||Stk rf}z:|jtkr:t|j|j|jtjkrTt|j|jWYdd}~XnXdS)N)OSErrorrrrrrESRCHr)rargskwargsr)funr)r*wrapperxs  z wrap_exceptions..wrapper) functoolswraps)rrr))rr*wrap_exceptionsts rc@seZdZdZdddgZddZddZd d Zed d Z e d dZ e ddZ e ddZ e ddZddZddZe ddZe ddZddZe dd Ze d!d"Ze dPd$d%Ze d&d'Ze d(d)Ze d*d+Ze d,d-Ze d.d/Ze d0d1Ze d2d3Ze d4d5Ze d6d7Ze dQd9d:Z e d;d<Z!e d=d>Z"e#e$d?rve d@dAZ%e dBdCZ&e dDdEZ'e dFdGZ(e dHdIZ)e dJdKZ*e dLdMZ+e dNdOZ,d#S)RProcessz1Wrapper class around underlying C implementation.rr_ppidcCs||_d|_d|_dS)N)rrr)rrr)r)r*rszProcess.__init__cCs|jjdS)N) oneshot_infoZcache_activate)rr)r)r* oneshot_enterszProcess.oneshot_entercCs|jjdS)N)rZcache_deactivate)rr)r)r* oneshot_exitszProcess.oneshot_exitcCs$tj|j}t|ttks t|S)zOReturn multiple information about this process as a raw tuple. )rlZ proc_inforrn pinfo_mapr)rrr)r)r*rs zProcess.oneshot_infoc CsV|jdkrdS|jdkrdSyttjj|jStk rPttj|jSXdS)zbReturn process name, which on Windows is always the final part of the executable. rzSystem Idle ProcessrZSystemN) rrurmrcbasenameexerrlZ proc_name)rr)r)r*rs  z Process.namecCs,|jdkrt|j|jtttj|jS)Nrr)rr)rrrrurprlZproc_exe)rr)r)r*rs z Process.execCs&tj|j}tr|Sdd|DSdS)NcSsg|] }t|qSr))ru)rror)r)r*rsz#Process.cmdline..)rlZ proc_cmdlinerr)rrr)r)r*cmdlines zProcess.cmdlinecCs8tj|j}|r,t r,t|ts,tt|tt |S)N) rlZ proc_environrrrrrrrr ru)rZustrr)r)r*environs  zProcess.environc Cs4y t|jStk r.t|j|jYnXdS)N)ppid_maprKeyErrorrr)rr)r)r*ppids z Process.ppidcCsy tj|jStk r}z|jtkr|j}|td|td|td|td|td|td|td|td|td |td f SWYdd}~XnXdS) NrHrIrJrKrLrMrNrOrPrQ)rlZproc_memory_inforrrrrr)rrinfor)r)r*_get_raw_meminfos            zProcess._get_raw_meminfocCs(|j}|d}|d}t||f|S)Nr+r-)rr])rtr^r_r)r)r* memory_infoszProcess.memory_infocCs"|j}tj|j}t||fS)N)rrlZproc_memory_ussrr`)rZ basic_memrar)r)r*memory_full_infos zProcess.memory_full_infoccsytj|j}WnVtk rf}z:|jtkr:t|j|j|jtjkrTt |j|jWYdd}~XnXXxT|D]L\}}}}t |}t st |t stt|t|}t|}||||fVqnWdS)N)rlZproc_memory_mapsrrrrrrrrrprrrrrrruhex)rrawrrZpermrcr^r)r)r* memory_mapss  zProcess.memory_mapscCs tj|jS)N)rlZ proc_killr)rr)r)r*killsz Process.killcCstj|j|dS)N)rmrr)rZsigr)r)r* send_signalszProcess.send_signalNcCsr|dkrtj}n t|d}xPtj|j|}|tkrDt||j|jt|jrj|dkrZqnt||j|j|SdS)Ni) rlZINFINITEintZ proc_waitr WAIT_TIMEOUTrr pid_exists)rZtimeoutZ cext_timeoutrr)r)r*waits  z Process.waitcCs2|jdkrdStj|j\}}t|dt|S)NrrzNT AUTHORITY\SYSTEMri)rr)rrlZ proc_usernameru)rZdomainrSr)r)r*r(s zProcess.usernamecCs`|jdkrtSy tj|jStk rZ}z"|jtkrH|jtdSWYdd}~XnXdS)Nrrr@)rr) rrrlZproc_create_timerrrrr)rrr)r)r*r@/s   zProcess.create_timecCs|jtdS)NrA)rr)rr)r)r*rA;szProcess.num_threadscCs@tj|j}g}x*|D]"\}}}tj|||}|j|qW|S)N)rlZ proc_threadsrrZpthreadr)rrrZ thread_idutimeZstimentupler)r)r*threads?s  zProcess.threadscCsvytj|j\}}WnPtk rd}z4|jtkrR|j}|td}|td}nWYdd}~XnXtj ||ddS)Nr>r?g) rlZproc_cpu_timesrrrrrrrZ pcputimes)rrSrTrrr)r)r*rHs  zProcess.cpu_timescCs tj|jS)N)rlZ proc_suspendr)rr)r)r*suspendVszProcess.suspendcCs tj|jS)N)rlZ proc_resumer)rr)r)r*resumeZszProcess.resumecCs4|jdkrt|j|jtj|j}ttjj|S)Nrr)rr) rrrrlZproc_cwdrurmrcnormpath)rrcr)r)r*cwd^s  z Process.cwdcCsh|jdkrgSt}tj|j}x>|D]6}t|}t|r&tsFt|}tj |d}|j |q&Wt |S)Nrrr)rrr) rrrlZproc_open_filesrpr rrurZ popenfilerr)rrZraw_file_namesZ_filerr)r)r* open_filesgs    zProcess.open_filesinetcCst||jdS)N)r)rr)rrr)r)r* connectionszszProcess.connectionscCs tj|j}tdk rt|}|S)N)rlZproc_priority_getrenumr%)rvaluer)r)r*nice_get~s zProcess.nice_getcCstj|j|S)N)rlZproc_priority_setr)rrr)r)r*nice_setszProcess.nice_setproc_io_priority_getcCs tj|jS)N)rlr r)rr)r)r* ionice_getszProcess.ionice_getcCs.|r td|dkr td|tj|j|S)Nz@r|qS)rr))ri)rr)r*rszBProcess.cpu_affinity_get..from_bitmask..@)r)rr))rr* from_bitmasksz.Process.cpu_affinity_get..from_bitmask)rlZproc_cpu_affinity_getr)rrbitmaskr)r)r*cpu_affinity_gets zProcess.cpu_affinity_getcCsrdd}tttt}x<|D]4}||kr t|ttfsHtd|q td|q W||}t j |j |dS)NcSs2|std|d}x|D]}|d|O}qW|S)Nzinvalid argument %rrr+)r)loutbr)r)r* to_bitmasks   z,Process.cpu_affinity_set..to_bitmaskz&invalid CPU %r; an integer is requiredzinvalid CPU %r) rrangernrrrrrr rrlZproc_cpu_affinity_setr)rrrZallcpusZcpurr)r)r*cpu_affinity_sets   zProcess.cpu_affinity_setcCsPy tj|jStk rJ}z"|jtkr8|jtdSWYdd}~XnXdS)Nr<)rlZproc_num_handlesrrrrrr)rrr)r)r*r<s   zProcess.num_handlescCs|jtd}tj|dS)Nr=r)rrrZpctxsw)rr=r)r)r*num_ctx_switchesszProcess.num_ctx_switches)N)r)-r&r'r(r __slots__rrrr rrrrrrrrrrrrrrrr@rArrrrrrrrrrrlr r rrrrr<rr)r)r)r*rsR                        r)rrrr)rar)r)rrrrrmsys collectionsrrrrl ImportErrorrrslower startswithZgetwindowsversionr RuntimeErrorrrr r r r r rrZ_compatrrrrr _exceptionsrrrrrrrrr version_inforZ__extra__all__r"r frozensetZEPERMZEACCESZERROR_ACCESS_DENIEDrZERROR_INVALID_NAMEZERROR_SERVICE_DOES_NOT_EXISTrr#IntEnumr$ZMIB_TCP_STATE_ESTABZCONN_ESTABLISHEDZMIB_TCP_STATE_SYN_SENTZ CONN_SYN_SENTZMIB_TCP_STATE_SYN_RCVDZ CONN_SYN_RECVZMIB_TCP_STATE_FIN_WAIT1ZCONN_FIN_WAIT1ZMIB_TCP_STATE_FIN_WAIT2ZCONN_FIN_WAIT2ZMIB_TCP_STATE_TIME_WAITZCONN_TIME_WAITZMIB_TCP_STATE_CLOSEDZ CONN_CLOSEZMIB_TCP_STATE_CLOSE_WAITZCONN_CLOSE_WAITZMIB_TCP_STATE_LAST_ACKZ CONN_LAST_ACKZMIB_TCP_STATE_LISTENZ CONN_LISTENZMIB_TCP_STATE_CLOSINGZ CONN_CLOSINGZMIB_TCP_STATE_DELETE_TCBZPSUTIL_CONN_NONEZ CONN_NONErr%globalsr __members__rrrRrXr]_fieldsr`rbrjrdrfrpruryrzZdisk_io_countersr~rrrrrrrrrrrrrrrr r!objectrZpidsrrrrr)r)r)r*s                                             *