3 JZ2d@s dZddlZddlZddlZddlZddlZddlmZddlmZddl m Z ddl m Z ddl m Z dd l mZdd l mZdd l mZdd l mZdd l mZddl mZddl mZddlmZddlmZddlmZddlmZddlmZdddgZejdZe j Z ej!d^kZ"dZ#dZ$ej%e j&ej'e j(ej)e j*ej+e j,ej-e j.ej/e j(ej0e j1iZ2ej3e j4ej5e j6ej7e j8ej9e j:ej;e jej?e j@ejAe jBejCe jDejEe jFejGe jHejIe jJejKe#ejLe$iZMeNddddddd d!d"ZOed#d$d%d&d'gZPed(d$d%d)d*gZQed+d,d-d.d/d0gZRed1d2d3gZSeSZTed4d5d2d6d7gZUed8d9d:jVeUjWZXd;d<ZYd=d>ZZd?d@Z[dAdBZ\dCdDZ]dEdFZ^dGdHZ_dIdJZ`ejaZae jbZbd_dLdMZcejdZde jeZedadNdOZfdPdQZgdRdSZhdTdUZidVdWZjdXdYZkdZd[ZlGd\d]d]emZndS)bz'Sun OS Solaris platform implementation.N) namedtuple)AF_INET)_common)_psposix) _psutil_posix) _psutil_sunos)AF_INET6) isfile_strict)memoize_when_activated)sockfam_to_enum)socktype_to_enum) usage_percent)b)PY3) AccessDenied) NoSuchProcess) ZombieProcess CONN_IDLE CONN_BOUND PROCFS_PATH SC_PAGE_SIZE ZIDLEZBOUND)ppidrssvms create_timenice num_threadsstatusttynr scputimesusersystemZidleZiowait pcputimes children_userchildren_systemsvmemtotalZ availablepercentusedfreepmemr r! pmmap_groupedpathZ anonymouslocked pmmap_extz addr perms  cCs tjdjS)z+Return updated psutil.PROCFS_PATH constant.Zpsutil)sysmodulesrr:r: /usr/lib64/python3.6/_pssunos.pyget_procfs_pathqsr<cCsFtjdt}tjdt}}||}t||dd}t|||||S)zReport virtual memory metrics. SC_PHYS_PAGESSC_AVPHYS_PAGESr)_round)ossysconf PAGE_SIZErr-)r.r1Zavailr0r/r:r:r;virtual_memory{s rCc Cstj\}}tjddtjdddgtjd}|j\}}trL|j t j j }|j dkrdtd|j |jjd d d }|std d}}xL|D]D}|j}|dd \} } |tt| d7}|tt| d7}qW||} t| |d d} tj|| || |t|tS)zReport swap memory metrics.z /usr/bin/envzPATH=/usr/sbin:/sbin:%sPATHZswapz-l)stdoutrz'swap -l' failed (retcode=%s) rNzno swap device(s) configuredri)r?)cextZswap_mem subprocessPopenr@environPIPE communicaterdecoder8rEencoding returncode RuntimeErrorstripsplitintrrZsswaprB) ZsinZsoutprEstderrlinesr.r1linetfr0r/r:r:r; swap_memorys,      r[cCstj}tddt|DS)z-Return system-wide CPU times as a named tuplecSsg|] }t|qSr:)sum).0xr:r:r; szcpu_times..)rH per_cpu_timesr'zip)retr:r:r; cpu_timessrccCstj}dd|DS)z5Return system per-CPU times as a list of named tuplescSsg|] }t|qSr:)r')r]r^r:r:r;r_sz!per_cpu_times..)rHr`)rbr:r:r;r`sr`c Cs$y tjdStk rdSXdS)z0Return the number of logical CPUs in the system.SC_NPROCESSORS_ONLNN)r@rA ValueErrorr:r:r:r;cpu_count_logicals rfcCstjS)z1Return the number of physical CPUs in the system.)rHZcpu_count_physr:r:r:r;cpu_count_physicalsrgcCs$tj\}}}}d}tj||||S)z*Return various CPU stats as a named tuple.r)rH cpu_statsrZ scpustats)Z ctx_switchesZ interruptsZsyscallsZtrapsZsoft_interruptsr:r:r;rhs rhFc Cs`g}tj}xN|D]F}|\}}}}|dkr.d}|s>t|js>qtj||||}|j|qW|S)zReturn system disk partitions.Znone)rHdisk_partitions disk_usager.rZ sdiskpartappend) allretlistZ partitions partitionZdeviceZ mountpointZfstypeZoptsZntupler:r:r;rjs   rjc Cstjj}|dkr|jdd||krFtd|djdd|Dftj|\}}tj|}t}x|D]}|\}} } } } } }| |krqj| |krqj| t t fkr| rtj | } | rtj | } t | } t | } t| } |d krtj|| | | | | |}ntj|| | | | | }|j|qjWt|S) zReturn socket connections. If pid == -1 return system-wide connections (as opposed to connections opened by one process only). Only INET sockets are returned (UNIX are not). runixrz+invalid %r kind argument; choose between %sz, cSsg|] }t|qSr:)repr)r]r^r:r:r;r_sz#net_connections..rr)rZ conn_tmapcopypoprejoinrHnet_connectionssetrr addr TCP_STATUSESr r Zsconnpconnaddlist)kind_pidZcmapZfamiliestypesrawlistrbitemfdZfamZtype_ZladdrZraddrr%pidntr:r:r;rvs8       rvcCsVtj}xH|jD]<\}}|\}}}}ttdr:tj|}tj||||||<qW|S)z)Get NIC stats (isup, duplex, speed, mtu). NicDuplex)rH net_if_statsitemshasattrrrZ snicstats)rbnamerZisupZduplexZspeedZmtur:r:r;rs   rcCstjS)z:The system boot time expressed in seconds since the epoch.)rH boot_timer:r:r:r;r)src Cs`g}tj}d}xJ|D]B}|\}}}}}} |s0q||kr.)r@listdirrr<r:r:r:r;pidsFsrcCs tj|S)z&Check for the existence of a unix pid.)r pid_exists)rr:r:r;rKsrcsfdd}|S)zCall callable into a try/except clause and translate ENOENT, EACCES and EPERM in NoSuchProcess or AccessDenied exceptions. csy|f||Stk r}z|jdkrHdtkrFt|j|jn|jtjtjfkrt|jstt |j|jnt |j|j|j |jtj tj fkrt|j|jWYdd}~XnXdS)Nr)EnvironmentErrorrrr_nameerrnoENOENTESRCHrrr_ppidZEPERMZEACCES)selfargskwargserr)funr:r;wrapperUs   z wrap_exceptions..wrapperr:)rrr:)rr;wrap_exceptionsPs rc@seZdZdZddddgZddZdd Zd d Zed d Z eddZ eddZ e ddZ e ddZe ddZe ddZe ddZe ddZe ddZe d d!Ze d"d#Ze d$d%Ze d&d'Ze d(d)Ze d*d+Ze d,d-Ze d.d/Ze d0d1ZeZe d2d3Ze d4d5Ze d6d7Z d8d9Z!e dId;d<Z"e#d=d>Z$e#d=d?Z%e d@dAZ&e dBdCZ'e dDdEZ(e dJdGdHZ)dFS)KProcessz1Wrapper class around underlying C implementation.rrr _procfs_pathcCs||_d|_d|_t|_dS)N)rrrr<r)rrr:r:r;__init__qszProcess.__init__cCs"|jj|jj|jjdS)N)_proc_name_and_argsZcache_activate_proc_basic_info _proc_cred)rr:r:r; oneshot_enterws  zProcess.oneshot_entercCs"|jj|jj|jjdS)N)rZcache_deactivaterr)rr:r:r; oneshot_exit|s  zProcess.oneshot_exitcCstj|j|jS)N)rHZproc_name_and_argsrr)rr:r:r;rszProcess._proc_name_and_argscCstj|j|j}|S)N)rHZproc_basic_inforr)rrbr:r:r;rszProcess._proc_basic_infocCstj|j|jS)N)rHZ proc_credrr)rr:r:r;rszProcess._proc_credcCs |jdS)Nr)r)rr:r:r;rsz Process.namec Cs8ytjd|j|jfStk r*YnX|jdS)Nz%s/%s/path/a.outri)r@readlinkrrOSErrorcmdline)rr:r:r;exesz Process.execCs|jdjdS)Nrr7)rrS)rr:r:r;rszProcess.cmdlinecCstj|j|jS)N)rHZ proc_environrr)rr:r:r;rKszProcess.environcCs|jtdS)Nr")r proc_info_map)rr:r:r;r"szProcess.create_timecCs|jtdS)Nr$)rr)rr:r:r;r$szProcess.num_threadscCsby tj|jStk r\}z4|jtjtjdfkrJt|jrJt|j|j WYdd}~XnXdS)N0) cext_posix getpriorityrrrrrrrr)rrr:r:r;nice_gets  zProcess.nice_getcCs&|jdkrt|j|jtj|j|S)Nrr)rr)rrrr setpriority)rvaluer:r:r;nice_sets zProcess.nice_setcCs|jtd|_|jS)Nr)rrr)rr:r:r;rsz Process.ppidcCs"|j\}}}}}}tj|||S)N)rrpuids)rreal effectivesaved_r:r:r;uidssz Process.uidscCs"|j\}}}}}}tj|||S)N)rrr)rrrrrr:r:r;gidssz Process.gidscCs\ytj|j|j}Wn<tk rP}z |jtjkr>t r>d}nWYdd}~XnXtj |S)N)rrrr) rHZproc_cpu_timesrrrr EOVERFLOW IS_64_BITrr*)rtimesrr:r:r;rcszProcess.cpu_timescCstj|j|jS)N)rHZ proc_cpu_numrr)rr:r:r;cpu_numszProcess.cpu_numcCs|j}d}t|jtd}|tjkrxZd D]R}ytjd||j|fSt k r~}z|j t j krld}w.WYdd}~Xq.Xq.W|rtj d ||jfdS) NFr&rrrz %s/%d/path/%dTz%s/%s)rrrr) rrrrrHZPRNODEVr@rrrrrstat)r procfs_path hit_enoentrr^rr:r:r;terminals    zProcess.terminalcCsh|j}ytjd||jfStk rb}z,|jtjkrPtjd||jfdSWYdd}~XnXdS)Nz%s/%s/path/cwdz%s/%s)rr@rrrrrr)rrrr:r:r;cwd s z Process.cwdcCs2|j}|tdd}|tdd}t||S)Nr ir!)rrr2)rrbr r!r:r:r; memory_infoszProcess.memory_infocCs|jtd}tj|dS)Nr%?)rr PROC_STATUSESget)rcoder:r:r;r%$szProcess.statusc Cs|j}g}tjd||jf}d}x|D]}t|}ytj|j||\}}WnJtk r}z.|jtj krrt rrw(|jtj krd}w(WYdd}~Xq(Xt j |||} |j| q(W|rtjd||jf|S)Nz %s/%d/lwpFTz%s/%s)rr@rrrTrHZquery_process_threadrrrrrrZpthreadrlr) rrrbZtidsrtidutimeZstimerrr:r:r;threads*s*  zProcess.threadsc Csg}d}|j}d||jf}xtjd||jfD]}tjj||}tjj|r2ytj|}Wn6tk r}z|j t j krd}w2WYdd}~Xq2Xt |r2|j t j|t|q2W|rtjd||jf|S)NFz %s/%d/pathz%s/%d/fdTz%s/%s)rrr@rr4ruislinkrrrrr rlrZ popenfilerTr) rrnrrZpathdirrr4filerr:r:r; open_filesLs&  zProcess.open_filesc cs,d|}tj|dtjtjd}|j\}}trDdd||fD\}}|jdkrd|jkrht|j|j d|jkrt |j|j t d ||f|j d d d }xt |D]v\}}|j}|jd r|j dd d } ||d j} | dkrtj} n| dkr tj} nd} dtj| | dtjfVqWd S)z.rzpermission deniedzno such processz%r command error %srFrNzsockname: AF_UNIXr7 SOCK_STREAM SOCK_DGRAMrrirrrr)rIrJrLrMrrPlowerrrrrrQrS enumeratelstrip startswithrRsocketrrZAF_UNIXr CONN_NONE) rrcmdrUrErVrWirXr4typer:r:r;_get_unix_socketses2        zProcess._get_unix_socketsinetcCsPt||jd}|s(tjd|j|jf|dkrL|jdd|j|jD|S)N)r~z%s/%srmrpcSsg|]}tj|qSr:)rrz)r]Zconnr:r:r;r_sz'Process.connections..)rmrp)rvrr@rrextendr)rr}rbr:r:r; connectionss zProcess.connectionsZmmapzpath rss anon lockedzaddr perms path rss anon lockedc$Cs.dd}|j}g}ytj|j|}Wn:tk r^}z|jtjkrLt rLgSWYdd}~XnXd}x|D]}|\}} } } } } }||| }| jdsyt j d||j| f} WnFtk r}z*|jtj krd||j| f} d}nWYdd}~XnX|j || | | | |fqjW|r*t j d||jf|S)NcSs0dt|ddjdt|ddjdfS)Nz%s-%srL)hexrR)startendr:r:r;toaddrsz#Process.memory_maps..toaddrF[z %s/%s/path/%sTz%s/%s)rrHZproc_memory_mapsrrrrrrr@rrrlr)rrrrnrrrrrxZaddrsizeZpermrr Zanonr5r:r:r; memory_mapss4    zProcess.memory_mapscCsttjd|j|jfS)Nz%s/%s/fd)lenr@rrr)rr:r:r;num_fdsszProcess.num_fdscCstjtj|j|jS)N)rZpctxswrHZproc_num_ctx_switchesrr)rr:r:r;num_ctx_switchesszProcess.num_ctx_switchesNcCstj|j||jS)N)rZwait_pidrr)rZtimeoutr:r:r;waitsz Process.wait)r)N)*__name__ __module__ __qualname____doc__ __slots__rrrr rrrrrrrrKr"r$rrrrrrcrrrrZmemory_full_infor%rrrrrZnt_mmap_groupedZ nt_mmap_extrrrrr:r:r:r;rlsL                    "      0  rl)Frr)rr)orrr@rrIr8 collectionsrrrirrrrrrHr r r r r rZ_compatrr _exceptionsrrrZ__extra__all__rArBZAF_LINKmaxsizerrrZSSLEEPZSTATUS_SLEEPINGZSRUNZSTATUS_RUNNINGZSZOMBZ STATUS_ZOMBIEZSSTOPZSTATUS_STOPPEDZSIDLZ STATUS_IDLEZSONPROCZSWAITZSTATUS_WAITINGrZTCPS_ESTABLISHEDZCONN_ESTABLISHEDZ TCPS_SYN_SENTZ CONN_SYN_SENTZ TCPS_SYN_RCVDZ 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_CLOSINGZPSUTIL_CONN_NONErZ TCPS_IDLEZ TCPS_BOUNDrydictrr'r*r-r2Zpfullmemr3ru_fieldsr6r<rCr[rcr`rfrgrhZdisk_io_countersrkrjZnet_io_countersZ net_if_addrsrvrrrrrrobjectrr:r:r:r;s                         &    $