bgS dZddlmZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z ddl Z ddl Z ddlZddlmZddlmZddlmZddlmZdd lmZdd lmZdd lmZdd lmZdd lmZddlmZddlmZddlmZddlmZddlmZddlm Z ddlm!Z!ddlm"Z"ddlm#Z#ddlm$Z$ddlm%Z%ddlm&Z&ddlm'Z'ddlm(Z(ddlm)Z)ddlm*Z*ddlm+Z+ddl,m-Z-dd l,m.Z.dd!l,m/Z/dd"l,m0Z0dd#l,m1Z1dd$l,m2Z2e j3d%krddl4Z4ndZ4gd&Z5d'Z6ej78d(ej9zZ:ej78d)ej9zZ;eej?d,Z@ejAZBdaCe jDd-kZEd.ZFe4e jGZHn(e4jId/d0eJe jGiZKeKjHZHe4 dZLdZMd1ZNd2ZOn5Gd3d4e4jIZPeQRePjSejTejUejVejWejXejYejZejZej[ej\ej]ej^d5 Z_ej`ejaejbejcejdejeejfejgejhejiejjd6 Zked7gd8Zled9gd:Zmed;gd<Zned=d>Zoed?eojpd@zZqedAgdBZredCdDdEserjpzZtedFgdGZuedHgdIZvdJZwdKZxdLZye$dMZz ezdNn1#e{$r)e j|edOdPdQdQdQa}YnwxYwdZ~ ddRlm~Z~n1#e$r)ddlZejddSTZe.StructRlimitrlim_currlim_maxN)r@rArBctypes c_longlong_fields_rCrDrE StructRlimitr9s+'):;'):;=rDrrr) r Structurelibcrbyrefrr get_errnoOSErrorrstrerror)pid resource_limitsrcurrentretnewerrno_s rErr8s = = = = =v/ = = =#lnnG~ll3 4g9N9NOO#lnn%ay %ay llFL$5$5v|G7L7LNNaxx)++fbk&&9&9:::$g&67 7rDcdg|]-}|d|+|.S)RLIM) startswithisupper.0rMs rE rQs6FFFqf!5!5F!))++FFFFrDc|d}||ddz} |d}|d}|d}n6#t$r)}td|jdz|cYd}~Sd}~wwxYw t d t z}n#t $r|cYSwxYwd}|5|D]U} | } | d r*|t| d z }V dddn #1swxYwY|tz}||z } ||z} | t| d z |z} | | z } | |t|d z |z z } t| S)axFallback for kernels < 3.14 where /proc/meminfo does not provide "MemAvailable", see: https://blog.famzah.net/2014/09/24/ This code reimplements the algorithm outlined here: https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/ commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773 We use this function also when "MemAvailable" returns 0 (possibly a kernel bug, see: https://github.com/giampaolo/psutil/issues/1915). In that case this routine matches "free" CLI tool result ("available" column). XXX: on recent kernels this calculation may differ by ~1.5% compared to "MemAvailable:", as it's calculated slightly differently. It is still way more realistic than doing (free + cached) though. See: * https://gitlab.com/procps-ng/procps/issues/42 * https://github.com/famzah/linux-memavailable-procfs/issues/2 MemFree:Cached:rs Active(file):sInactive(file): SReclaimable:zY%r is missing from /proc/meminfo; using an approximation for calculating available memoryNz %s/zoneinfoslowrr=g@) getKeyErrorrargsrrIOErrorstriprintrPAGESIZEmin) memsrbfallbacklru_active_filelru_inactive_fileslab_reclaimableerrr watermark_lowlineavail pagecaches rEcalculate_avail_vmemrYs<  Ddhhz1---H/0 !34 01  1368A;? @ @ @  (9(99 : : M 66 6 6D::<a8888 wd7FFD B BBs;A++A/2A/B!B43B48C!C>=C>D E DE !EE EE E  E!E:9E:>F GF-,G-!GGGGGG$$ G32G3 H++IIcLi}tdtz5}|D]7}|}t|ddz||d<8 dddn #1swxYwY |d}|d}n5#t$r(t j\}}}}}}}||z}||z}YnwxYw||z }t||d} td tz}|5dx} } |D]}|d r/t|d dd zdz} nC|d r.t|d dd zdz} | | n#d} tj | tddx} } dddn #1swxYwYnI#t$r<} dt| z} tj | tddx} } Yd} ~ nd} ~ wwxYwtj|||| | | S)zReturn swap memory metrics.rrrrNs SwapTotal:s SwapFree:rz %s/vmstatspswpin r&spswpoutzK'sin' and 'sout' swap memory stats couldn't be determined and were set to 0r=rzP'sin' and 'sout' swap memory stats couldn't be determined and were set to 0 (%s))rrrrrcext linux_sysinforrrrr rstrrsswap)rrrrr^rb_unit_multiplierrar`sinsoutr rs rE swap_memoryrs D \O$5$55 6 64! 4 4DZZ\\F!&)nnt3DOO 4444444444444444 ]#L!    373E3G3G01aE4    4D?t'7E 8 c>a@@@@d%                57:3xx@ c>a8888dddddd 4 =dGS$ ? ??sN;A))A-0A-5B/B87B8G3B9F88F<?F< H 2HH cNt}t|td|z5}|}dddn #1swxYwY|dt t jdz}d|D}t |S)zReturn a named tuple representing the following system-wide CPU times: (user, nice, system, idle, iowait, irq, softirq [steal, [guest, [guest_nice]]]) Last 3 fields may not be available on all Linux kernel versions. rNrc<g|]}t|tz SrCfloat CLOCK_TICKSrs rErzcpu_times..Ts# 5 5 5eAhh$ 5 5 5rD)rrrrrrr_fields)rrrrs rE cpu_timesrHs"##K%%% Y, - -&##%%&&&&&&&&&&&&&&& Ac)+,,q00 1F 5 5f 5 5 5F f s'A##A'*A'ct}t|g}td|z5}||D]y}|drb|}|dt tjdz}d|D}t|}| |z|cdddS#1swxYwYdS)zfReturn a list of namedtuple representing the CPU times for every CPU available on the system. rscpurc<g|]}t|tz SrCrrs rErz!per_cpu_times..fs#AAAQ%(([0AAArDN) rrrrrrrrrr)rcpusrrrrentrys rE per_cpu_timesr#Xs%"##K%%% D Y, - -   # #Dv&& ##i&7"8"81"< <=AA&AAA!6* E"""                  sBCCCcN tjdS#t$rd}tdt z5}|D].}|dr|dz }/ dddn #1swxYwY|dkrtjd}tdt z5}|D]7}| d d}| |r|dz }8 dddn #1swxYwY|dkrYdS|cYSwxYw) z0Return the number of logical CPUs in the system.SC_NPROCESSORS_ONLNr %s/cpuinfos processorrNzcpu\drr) rsysconf ValueErrorrrlowerrrecompilerrmatch)numrrsearchs rEcpu_count_logicalr/lsz/000  (9(99 : : a  ::<<**<881HC                 !88Z))F9'8'8899 !Q!!D::c??1-D||D))!q! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !8844 -s]+D$2B4 D$B D$B ;D$;D ? D$ D D$D  D$D$#D$cPt}d}d}tj|ptj|D]b}t|5}||dddn #1swxYwYct |}|dkr|Si}i}tdtz5}|D]}|}|s& |d||d<n#t$rYnwxYwi}P| dr+| d d \} } t| || < dddn #1swxYwYt|}|pdS) z-Return the number of CPU cores in the system.z9/sys/devices/system/cpu/cpu[0-9]*/topology/core_cpus_listz?/sys/devices/system/cpu/cpu[0-9]*/topology/thread_siblings_listNrr& cpu cores physical id)r2r1s :r)setglobraddreadrrrr)rrrrsumr) lsp1p2rtrresultmapping current_inforkeyvalues rEcpu_count_coresr@s0 B EB JB " .2%%    %! FF16688>>## $ $ $ % % % % % % % % % % % % % % % WWF {{ GL \O$5$55 6 63! 3 3D::<<%%''D 3$\2L899D! ??#ABB3!%FA!6!6JC(+E L% 3333333333333333"!! " "F >TsI :BB B -E8DE8 D$!E8#D$$AE88E<?E<c2tdtz5}d}d}d}|D]}|dr(t|d}ny|dr(t|d}n<|dr't|d}|||ndddn #1swxYwYd}t j||||S)z*Return various CPU stats as a named tuple.rNsctxtrsintrssoftirqr)rrrrrr scpustats)r ctx_switches interruptssoft_interruptsrsyscallss rE cpu_statsrGsR Y!2!22 3 3 q    Dw'' 7"4::<<?33 )) 7 a11 ,, 7"%djjll1o"6"6'O,G".               H  j/8 = ==sC C55C9<C9c Hg}tdtz5}|D]e}|dr<|t |dddf dddn #1swxYwY|S)zzcpu_freq..s&RYy!%<%<%B%B%D%D!E!ErD)r>iscaling_cur_freqNrcpuinfo_cur_freqz!can't find current frequency filescaling_max_freqscaling_min_freq)rJr4sortrrtr enumeraterrNotImplementedErrorrrrscpufreq) cpuinfo_freqspathsrpjoinirtcurrmax_min_s rEcpu_freqr_s .// ID E E C IA B B   EE FFF  '' ; ;GAt5zzS////%Q'$.EE$(:;;dKKK|EE$(:;;dKKK<-;===t99t#DtEE$(:;;<<==DDtEE$(:;;<<==DD JJw'dD99 : : : : rDc2dtDS)z}Alternate implementation using /proc/cpuinfo. min and max frequencies are not available and are set to None. c:g|]}tj|ddS)r)rrWrs rErzcpu_freq..s'MMM B++MMMrD)rJrCrDrEr_r_s NM5J5L5LMMMMrDceZdZdS)_Ipv6UnsupportedErrorN)r@rArBrCrDrErcrcsDrDrccreZdZdZdZdZdZedZed dZ ed dZ d d Z dS) ConnectionsawA wrapper on top of /proc/net/* files, retrieving per-process and system-wide open connections (TCP, UDP, UNIX) similarly to "netstat -an". Note: in case of UNIX sockets we're only able to determine the local endpoint/path, not the one it's connected to. According to [1] it would be possible but not easily. [1] http://serverfault.com/a/417946 c Ldtjtjf}dtjtjf}dtjtjf}dtjtjf}dtjdf}|||||f||f|f|f||f|f|f|f||||f||f||fd |_d|_dS)Ntcptcp6udpudp6unix) allrgtcp4rhriudp4rjrkinetinet4inet6)socketAF_INET SOCK_STREAMAF_INET6 SOCK_DGRAMAF_UNIXtmap _procfs_path)selfrmrhrnrjrks rE__init__zConnections.__init__sv~v'9:);<v~v'89):;-$dD1$<GG$<GGG4t,D\D\    !rDctt}tj|jd|dD]} t |jd|d|}|dr<|dddd}|||t|fq#ttf$rYt$rI}|j tj krYd}~|j tjkrt|Yd}~Ђd}~wwxYw|S)Nr/fd/fd/zsocket:[r)rlistrlistdirryrrrrr r"rerrnoEINVAL ENAMETOOLONGr)rzrinodesruinoders rEget_proc_inodeszConnections.get_proc_inodes,s<T""*4+<+<+ ("10.0.0.5", 22) "0000000000000000FFFF00000100007F:9E49" -> ("::ffff:127.0.0.1", 40521) The IP address portion is a little or big endian four-byte hexadecimal number; that is, the least significant byte is listed first, so we need to reverse the order of the bytes to convert it to an IP address. The port is represented as a two-byte hexadecimal number. Reference: http://linuxdevcenter.com/pub/a/linux/2000/11/16/LinuxAdmin.html :rCasciiNrz>4Iz<4I)rrrencoderrrs LITTLE_ENDIAN inet_ntopbase64 b16decoderustructpackunpackr(rrcraddr)rfamilyipports rEdecode_addresszConnections.decode_addressVs"::c??D4}} 2  $7##B V^ # # D%ff.>r.B.B44R4.HII%ff.>r.B.BCC!"%%B  G) EEFM%,D,DEEEGGBB ) EEFM%,D,DEEEGGB   $//   |B%%%s B E!E<Nc #K|dr!tj|sdSt |5}|t |dD]\}} |dd\ }} } } }}}}}} n&#t$rtd|d|d|wxYw| |vr|| d\} }nd \} }||| krx|tj krt| } n tj} t| |} t| |} n#t"$rYwxYw|||| | | | fV ddddS#1swxYwYdS) z.Parse /proc/net/tcp* and /proc/net/udp* files.6Nrrerror while parsing ; malformed line rrNr)rrrtexistsrrrUrr( RuntimeErrorrrrt TCP_STATUSESr CONN_NONErerrc)filertype_r filter_pidrlinenorrladdrraddrstatusrrrus rE process_inetzConnections.process_inets. ==   bgnnT&:&:  F t__ I JJLLL )!Q I I 1 SbS)BAueVQ1aEE!111&, DD&&&$$01111F??%UmA.GC&GC)jC.?.? 222!-f!5!(!2! + : :5& I I + : :5& I I0!!! !vueUFCHHHHH= I I I I I I I I I I I I I I I I I I IsO*E13'BE1#B>>AE16EE1 EE1EE11E58E5c #"Kt|5}||D]}|} |dd\}}}}}}} n)#t$rd|vrY:t d|d|wxYw| |vr || } ndg} | D]f\} } ||| krt |dkr |d } nd } t jt|}d }t j }| ||| ||| fVg ddddS#1swxYwYdS) zParse /proc/net/unix files.rrrrrNrr) rrrr(rrrsocktype_to_enumrr)rrrrrrtokensrrrpairsrrurtrrs rE process_unixzConnections.process_unixst__! L JJLLL L L)281+/Aq!Qq%%!)))$ &, DD$$())) )F??#5MEE'LE$LLGC!-*2C2C v;;!++#)":DD#%D ' 8U D D!#!(!2!65$vsKKKKKL# L! L! L! L! L! L! L! L! L! L! L! L! L! L! L! L! L! L! Ls6-DADA9!D#A99A=DD Dc ||jvr7td|ddd|jDt|_|||}|sgSn|}t}|j|D]\}}}|jd|}|tj tj fvr| |||||} n| ||||} | D]U\} }}} } } }|rtj| ||| | | }ntj| ||| | | |}||Vt#|S)Nzinvalid z kind argument; choose between rc,g|]}t|SrC)reprrs rErz(Connections.retrieve..s0L0L0LQa0L0L0LrDz/net/)r)rxr(rrryrrr3rrrsrurrrpconnsconnr5r)rzkindrrr proto_namerrrtr8rurrr bound_pidconns rEretrievezConnections.retrieves ty * $dii0L0L$)0L0L0L&M&M&MOPP P+-- ?))#..F   ((**Fee)-4   %J"&"3"3"3ZZ@D&.&/:::&&&%C'AA&&&&S'::FH  BFE5%<"=VUE5)/11DD#=VUE5)/<.read_procfsJs"~(9(99 : : "aKKMME " " " " " " " " " " " " " " " ; ;DZZ\\Fv;;DrzzayF1I69#vad|6L6L4vufmE1itrzzay69#vad|6L6L4 feV]E1iay03C0D0D-vvvKLLLL L}yy !Dt!KLLLu ; ; ; ; ;+ ; ;sAA Ac 3\KtjdD]}tjtjd|D]\}}}d|vr t tj|d5}|}dddn #1swxYwYtj |}tt|dd\ }}} } } } } }}}||| | | | ||| |f VސdS)N /sys/blockstatr) rrwalkrtrrr6rrbasenamerr)blockrootrfilesrrrrrrrrrrrrrs rE read_sysfsz$disk_io_counters..read_sysfsus}Z -- F FE"$'"',,|U*K*K"L"L F Fa&&rw||D&99::6aVVXX^^--3355F666666666666666w''--36sF3B3K3H3H1 feV]E1iUFFFElM9FFFFF F F Fs9C  C C rrzC%s/diskstats nor /sys/block filesystem are available on this system)rrtrrrVrDISK_SECTOR_SIZE)perdiskrr genrr"rrrrrrrrrrrs rEdisk_io_countersrFs-);););V F F F w~~n'8'8899*kmm  % %*jll! &(( )** *GAA(- &ufffeUL 9 066  """"u%}iA  NrDc8eZdZdZddgZdZdZdZdZdZ d S) RootFsDeviceFinderaEdisk_partitions() may return partitions with device == "/dev/root" or "rootfs". This container class uses different strategies to try to obtain the real device path. Resources: https://bootlin.com/blog/find-root-device/ https://www.systutorials.com/how-to-find-the-disk-where-root-is-on-in-bash-on-linux/ majorminorctjdj}tj||_tj||_dSNr)rrst_devrr)rzdevs rEr{zRootFsDeviceFinder.__init__s4gcll!Xc]] Xc]] rDc.tdtz5}|ddD]}|}t |dkr*|drt |dnd}|drt |dnd}|d}||jkr ||jkr|rd|zccdddS ddddS#1swxYwYdS)Nz %s/partitionsr=r&rrr%/dev/%s) rrrrrisdigitrrr)rzrrrrrrs rEask_proc_partitionsz&RootFsDeviceFinder.ask_proc_partitionssq ):):: ; ; 0q abb) 0 0v;;??*0)*;*;*=*=GF1I4*0)*;*;*=*=GF1I4ayDJ&&5DJ+>+>0(4/// 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0sCD ;D  DDc6d|jd|jd}t|5}|D]Y}|drB|dd}|rd|zccdddSZ ddddS#1swxYwYdS)Nz/sys/dev/block/rz/ueventzDEVNAME=r=r)rrrrr rpartition)rzrtrrrs rEask_sys_dev_blockz$RootFsDeviceFinder.ask_sys_dev_blocks04 DJJJG t__ 0 0 0??:..0::<<22:>>qAD0(4///  0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0sAB?BBBc|jd|j}tjd}|D]} t |}|5|}||krOtj tj |}d|zcdddcS dddn #1swxYwY#t$rYwxYwdS)Nrz/sys/class/block/*/devr) rrr4iglobrr6rrrtrdirnamer )rzneedler rrdatars rEask_sys_class_blockz&RootFsDeviceFinder.ask_sys_class_blocksO JJJ 3 344 0 0D 0dOO006688>>++Dv~~!w//0E0EFF(4/ 0000000000%000000000000000%     0 0s#CA-CC C  CCcd}|C |}n-#ttf$r}t|Yd}~nd}~wwxYw|C |}n-#ttf$r}t|Yd}~nd}~wwxYw|C |}n-#ttf$r}t|Yd}~nd}~wwxYw|!t j|r|SdSdSr) rrrrrr#rrtr)rzrtrs rEfindzRootFsDeviceFinder.finds7 < //11W%   c   < --//W%   c   < //11W%   c    t 4 4 K    sDAAA A B 1BB B%%C6C  CN) r@rArBr __slots__r{rrr#r%rCrDrErrsu '"I### 0 0 0000 0 0 0rDrc t}t}|std|z5}|D]}|}|ds(||S|dd}|dkr|d dddn #1swxYwY|dkr?tj dr tj d}n"tj d |z}g}tj |}|D]v} | \} } }} | d krd } | d vr"tp| } |s | d ks||vrDdx} }tj| | || | |}||w|S) z8Return mounted disk partitions as a list of namedtuples.z%s/filesystemsnodev rzfsNrz /etc/mtabz%s/self/mountsnoner)z /dev/rootrootfs)r3rrrrr5rrrtisfilerealpathrdisk_partitionsrr%r sdiskpartr)rlfstypesrrrfstype mounts_pathretlist partitions partitiondevice mountpointoptsmaxfilemaxpathntuples rEr/r/s'eeG!##K  + '+5 6 6 +! + +zz||w//+KK ----"ZZ--a0F E*** + + + + + + + + + + + + + + + +g"'.."="=g&&{33 g&&'7+'EFF G%k22J   +4( FD V  F , , ,'))..00:FF ||vW44  '"6:vt#*G55v NsB C  CCc 8 tjt}tjd}|tjdt t d|D}tjd}tjd}|D]1}| d|}||vr| |2|D]a} |dz}tt|dz }tjtj|d }t#|} n#t&t(t*f$rYwxYwt|d zd } t|d zd } t#|dzd } | & t| dz } n#t*$rd } YnwxYw| & t| dz } n#t*$rd } YnwxYw||  | || | fc|sNtjd}t t |}|D]} tj|d}tt|dz }tj|d}t#|} n3#t&t(t*f$r} t-| Yd } ~ d } ~ wwxYwtj|dz}t d|D}d } d } |D] }tj||dz}t#|d }|dkr3ttj||dzd } n8|dkr2ttj||dzd } | & t| dz } n#t*$rd } YnwxYw| ' t| dz } #t*$rd } YwxYw ||  d|| | ft/|S)aReturn hardware (CPU and others) temperatures as a dict including hardware name, label, current, max and critical temperatures. Implementation notes: - /sys/class/hwmon looks like the most recent interface to retrieve this info, and this implementation relies on it only (old distros will probably use something else) - lm-sensors on Ubuntu 16.04 relies on /sys/class/hwmon - /sys/class/thermal/thermal_zone* is another one but it's more difficult to parse z/sys/class/hwmon/hwmon*/temp*_*z&/sys/class/hwmon/hwmon*/device/temp*_*cDg|]}|ddSrrrrs rErz(sensors_temperatures..0&???AGGCLLO???rDz5/sys/devices/platform/coretemp.*/hwmon/hwmon*/temp*_*z'/sys/devices/platform/coretemp.*/hwmon/z/sys/class/hwmon/_inputg@@r_maxNrP_crit_labelrz /sys/class/thermal/thermal_zone*temptypez /trip_point*cg|]O}dtj|dddPS)rrr%)rrrtrr)rps rErz(sensors_temperatures..psfJJJ9: #xx  ##))#..qs3 5 5JJJrD_typecritical_temphigh) collectionsrrr4extendsortedr3r*r+subrrrrrtrr rrrrr(rdict)r basenames basenames2replraltnamebasertr unit_namerMrKlabelr trip_paths trip_points trip_point trip_types rEsensors_temperaturesr^s  !$ ' 'C ;<>DD ))II*-    H D6MD111w666D8Ob1117799   T{{V+        ??V3     IugtX>???? (AI@AA 3y>>** $ A$ AD w||D&11T ++f4w||D&11IIOO-- Wj1   c  4.#899JJJ>HJJJKKKHD) ( ( w||D*w*>??r22288::  **#BGLLzG7K$L$L-1 3 3 3HH&(( T:3G H H)-///D#$$T{{V3%$$$#$'(#(??V#;%(((#'((  N ! !2wh"? @ @ @ @ 99stBE  E:9E:G!! G0/G06H  HH5BK66L& L!!L&P// P>=P>Q Q'&Q'ctjt}tjd}|stjd}t t d|D}|D]} t t|dz}n-#ttf$r}t|Yd}~Id}~wwxYwttj tj |d}t|dzd }||t%j||t)|S) aReturn hardware fans info (for CPU and other peripherals) as a dict including hardware label and current speed. Implementation notes: - /sys/class/hwmon looks like the most recent interface to retrieve this info, and this implementation relies on it only (old distros will probably use something else) - lm-sensors on Ubuntu 16.04 relies on /sys/class/hwmon z/sys/class/hwmon/hwmon*/fan*_*z%/sys/class/hwmon/hwmon*/device/fan*_*cDg|]}|ddSr?r@rs rErz sensors_fans..rArDrBNrrErrP)rNrrr4rPr3rrrrrrrrtrr rrrsfanrR)rrSrWrrrXrYs rE sensors_fansrbsZ  !$ ' 'C :;;I GIEFF s??Y???@@AAI<< $th//00GG!    #JJJ HHHH  RW__T%:%:FCCDDJJLL D8Ob1117799 Igl5'::;;;; 99s0BB:!B55B:ch t fd}dtjtD}|sdStjtt |d}||dz|dz}||dz|dz}||d z|d z}||d z}|| d |z|z }n;#t$rd }Yn-wxYwtt|dzd}|dkrdSd}|tjtdtjtd} | | dk}nGt|dzd } | dkrd}n| dvrd}|r tj } nj|5|3 t||z dz} nO#t$rtj} Yn7wxYw|%t|dz} | dkr tj} n tj} tj|| |S)aReturn battery information. Implementation note: it appears /sys/class/power_supply/BAT0/ directory structure may vary and provide files with the same meaning but under different names, see: https://github.com/giampaolo/psutil/issues/966 c|D]Q}t|}|kr8 t|cS#t$r|cYcSwxYwRdS)zvAttempt to read the content of multiple files which may not exist. If none of them exist return None. rPN)rrr(r)rYrtrnulls rE multi_bcatz#sensors_battery..multi_bcats ' 'Dtd+++Cd{{'s88OOO!'''99;;&&&&&' ts/AAchg|]/}|dsd|v-|0S)BATbattery)rr)rs rErz#sensors_battery..sG $ $ $! U8K8K $  " "  " " "rDNrz /energy_nowz /charge_nowz /power_nowz /current_nowz /energy_fullz /charge_fullz/time_to_empty_nowgY@rz /capacityrrPz AC0/onlinez AC/onliner/statusr dischargingF)chargingfullTi<)objectrrPOWER_SUPPLY_PATHrtrrPZeroDivisionErrorrrrr)rPOWER_TIME_UNLIMITEDPOWER_TIME_UNKNOWNsbattery) rfbatsr energy_now power_now energy_full time_to_emptyr` power_pluggedonlinersecsleftres @rEsensors_batteryr}s 88D      $ $rz"344 $ $ $D t 7<<)6$<<? ; ;D } }J  | ~I* ~ ~KJt&::;;M:#9 j(;6GG    GGG c$,r:::;; b==4 M Z  & 55  & 4466F! TI%33399;;AACC ] " "!MM + + + M  ./  I$9 2: 1D899HH  2 2 21HHH 2  "}r)** a<<1H-  GX} = ==s$=C CC9GG('G(cg}tj}|D]C}|\}}}}}}|s|dvrd}tj||pd|||} || D|S)z:Return currently connected users as a list of namedtuples.)z:0.0z:0 localhostN)rusersrsuserr) r4rawlistitemrttyhostnametstamp user_processrnts rErrsGjllG  9=6c8V\3   ~ % %"H ]4h D Dr NrDcLdtz}t|5}|D]b}|drKt|d}|a|ccdddSctd|z#1swxYwYdS)zAReturn the system boot time expressed in seconds since the epoch.rsbtimerNzline 'btime' not found in %s)rrrrrr BOOT_TIMEr)rtrrrs rE boot_timer s (( (D T  3a  Dx(( DJJLL..00344  33333333  *T 133 3 3333333333sABBB BcpdtjttDS)z7Returns a list of PIDs currently running on the system.cTg|]%}|t|&SrC)rrrs rErzpids..5s+ L L Lq LCFF L L LrD)rrr#rrCrDrErr3s. L LBJq):):';';<< L L LLrDctj|sdS td|d}t|5}|D]R}|dr;t |d}||kccdddSStd|z#1swxYwYdS#ttf$r|tvcYSwxYw)zcCheck for the existence of a unix PID. Linux TIDs are not supported (always return False). FrrjsTgid:rNz'Tgid' line not found in %s) r pid_existsrrrrrr(EnvironmentErrorr)rrtrrtgids rErr8s[  s # #!u !&5%6%6%6%6!EFFF G G G G G G G G G G!*- ! ! !$&&=  !s;#B0AB# B0B##B''B0*B'+B00!CCci}t}tD]} t|d|d5}|}dddn #1swxYwY|d}||dzd}t |d}|||<#ttf$rYwxYw|S)zsObtain a {pid: ppid, ...} dict for all running processes in one shot. Used to speed up Process.children(). r/statN)r=r) rrrr6rrrr r")rrrrr"rpardsetppids rEppid_maprXs! C!##Kvv   [[[###>?? 1vvxx               ::d##Dq ?((**DtAw<.wrapperrs 3t-d---f-- - 5 5 5tx44 4! 6 6 6$*55 5    7>>T->->->"IJJ :#DHdj999   s BB)) functoolswraps)rrs` rEwrap_exceptionsrns:_S      NrDceZdZdZgdZdZdZeedZ eedZ eedZ dZ d Z ed Zd Zed Zed ZedZejdejzr edZedZedZed7dZedZedZeserHedZee j!de j!de j!dfdZ"dZ#neZ#er edZ$edZ%ee j!dfd Z&ee j!d!fd"Z'ed#Z(ed$Z)ed%Z*e+r)ed&Z,e j!d'fd(Z-ed)Z.e/red*Z0ed+Z1e2 ed7d,Z3ed-Z4ed.Z5ed8d0Z6ed1Z7ed2Z8ee j!d3fd4Z9ee j!d5fd6Z:dS)9ProcesszLinux process implementation.)rr_ppidry_cachecV||_d|_d|_t|_dSr)rrrrry)rzrs rEr{zProcess.__init__s+  +--rDcLtj|jd|jdS)z+Raise NSP if the process disappeared on us.rN)rrryrrzs rE _assert_alivezProcess._assert_alives, 4,,,dhh788888rDct|jd|jd}|d}||ddz|}||dzd}i}||d<|d |d <|d|d <|d |d <|d|d<|d|d<|d|d<|d|d<|d|d<|d|d<|d|d<|S)aYParse /proc/{pid}/stat file and return a dict with various process info. Using "man proc" as a reference: where "man proc" refers to position N always subtract 3 (e.g ppid position 4 in 'man proc' == position 1 in here). The return value is cached in case oneshot() ctx manager is in use. rrr(rr=Nrrrrr&ttynr utime stime children_utimerchildren_stime create_time$cpu_num' blkio_ticks)rryrrr%r)rzr"rrrrs rE_parse_stat_filezProcess._parse_stat_filesD$5$5$5txxx@AAzz$DIIdOOa',-dQhii&&((F q H QiF ayG bzG bzG &r   &r  #BZMI#BZM rDct|jd|jd5}|cdddS#1swxYwYdS)zRead /proc/{pid}/stat file and return its content. The return value is cached in case oneshot() ctx manager is in use. rrjN)rryrr6rzrs rE_read_status_filezProcess._read_status_files4+<+<+>## $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $s&AAAc|j||j||j|dSr)rcache_activaterrrs rE oneshot_enterzProcess.oneshot_entersM ,,T222 --d333 ,,T22222rDc|j||j||j|dSr)rcache_deactivaterrrs rE oneshot_exitzProcess.oneshot_exitsM ..t444 //555 ..t44444rDcf|d}trt|}|S)Nr)rrr)rzrs rErz Process.names0$$&&v.  $<EFU## 6q 6 6D::<??? J8$8$=)>*8$8$  J J J -0Xa[[[%%%"IJJJ JsMB A+B + A85B 7A88B  B B )7C!! D+"D  Dcr|}t|dtz }t|dtz }t|dtz }t|dtz }t|dtz }t|||||S)Nrrrrr)rrrr)rzrrrrrrs rErzProcess.cpu_times2s&&((fWo&&4fWo&&4v&6788;Fv&6788;Fvm,-- ;~~vNNNrDcPt|dS)zWhat CPU the process is on.rrrrs rErzProcess.cpu_num<s#4((**95666rDNcBtj|j||jSr)rwait_pidrr)rztimeouts rEwaitz Process.waitAs 7DJ???rDct|d}tp t}|tz |zS)Nr)rrrrr)rzctimebts rErzProcess.create_timeEs@d++--m<==  %)++ #r))rDc t|jd|jd5}d|ddD\}}}}}}}dddn #1swxYwYt |||||||S)Nrz/statmc<g|]}t|tzSrC)rrrs rErz'Process.memory_info.._s#EEEqQ("EEErDr)rryrrrry) rzrvmsrrgtextlibr"dirtys rE memory_infozProcess.memory_infoPs$*;*;*;TXXXF G G F1EEAJJLL,>,>,@,@!,DEEE 5CfdCu F F F F F F F F F F F F F F FCfdCu===sAA--A14A1cdx}x}} td|j|j5}|D]}|dr.|t |ddzz }E|dr+t |ddz}|dr*t |ddz} dddn #1swxYwYn\#t$rOt|jst|j|j t|j|j |j wxYw|||fS)Nrz{}/{}/smaps_rollupsPrivate_rrPss:Swap:) rformatryrrrrr"rrrrr)rzr{r|r}rrs rE_parse_smaps_rollupzProcess._parse_smaps_rollupds ! C # J !5!>CC!__W55?"%djjll1o"6"6"=CC!__X66?#&tzz||A#7#7$#>D? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?& J J J!$(++J'$*==='$*djIII  J d# #s0-DCD > D DDDDAE/s\nPrivate.*:\s+(\d+)s\nPss\:\s+(\d+)s\nSwap\:\s+(\d+)c|}ttt||dz}ttt||dz}ttt||dz}|||fS)Nr)rr7rrfindall)rz _private_re_pss_re_swap_re smaps_datar{r|r}s rE _parse_smapszProcess._parse_smaps|s,..00Jc#{22:>>??@@4GCc#wz::;;<>EDd# #rDctr|\}}}n|\}}}|}t ||||fzSr)HAS_PROC_SMAPS_ROLLUPrrrrz)rzr{r|r} basic_mems rEmemory_full_infozProcess.memory_full_infosb$ 5!%!9!9!;!;S$$!%!2!2!4!4S$((**IY#sD)99: :rDcd}|}|sgS|d}g}|d}|g}|||D]\}}|dd} |\} } } } } }n#t$r|dgz\} } } } } }YnwxYw|sd}nXtrt |}|}|drt|s |dd }| t | t | || d d| d d| d d| d d| dd| dd| dd| dd| dd| ddf |S)aPReturn process's mapped memory regions as a list of named tuples. Fields are explained in 'man proc'; here is an updated (Apr 2012) version: http://goo.gl/fmebo /proc/{PID}/smaps does not exist on kernels < 2.6.14 or if CONFIG_MMU kernel configuration option is not enabled. c3Ki}|D]}|dd}|dds.||fV||a t |ddz||d<#t $r0|ddrYt d|zwxYw||fVdS)NrrIrrsVmFlags:z#don't know how to interpret line %r)rrpoprrr(r)r current_blockr"rrs rE get_blocksz'Process.memory_maps..get_blockss*!IID!ZZa00F!!9--d33 I,0022D9999%,,T2222I.1&)nnt.CDOO)III%ay33K@@I (&02ACG2H'I'I!I I%((**D111111s)!B  %C2C rNrrz[anon]rrsRss:sSize:rs Shared_Clean:s Shared_Dirty:sPrivate_Clean:sPrivate_Dirty:s Referenced:s Anonymous:r) rrrr(rrrrrrr)rzrr"rr8 first_linerheaderhfieldsrpermsoffsetrrrts rE memory_mapszProcess.memory_mapss+ 2 2 2(((**D  JJu%%EB1J'LM * 5- @ @    ,,tQ//'>BBc t|jd|jdS#ttf$rOt |jst |j|jt|j|j|j wxYw)Nrz/cwd) rryrr r"rrrrrrs rEcwdz Process.cwds F4+<+<+#eAh--U1X?? ?rDsThreads:\t(\d+)cz|}t||dSNr)rrr)rz_num_threads_rer"s rE num_threadszProcess.num_threadss6 %%''?**4003444rDctj|jd|jd}|g}d}|D]'}|jd|jd|d} t |5}|}dddn #1swxYwYn#ttf$rd}Y|wxYw|| ddzd}| d }t|d tz } t|d tz } tjt!|| | } || )|r||S) Nrz/taskFz/task/rTrr=rrr)rrryrrTrr6rr r"r%rrrrpthreadrrr) rz thread_idsr4 hit_enoent thread_idrrstrrrr<s rEthreadszProcess.threads sZ0A0A0A4888 LMM  # # #I!!!4888YYY8E  ''*1))B***************%'9:   "    BGGDMMA%&&'BXXd^^F&*%% 3E&*%% 3E_S^^UEBBF NN6 " " " "  !    s6B&''B B&B B&!B "B&&B<;B<c4tj|jSr)r getpriorityrrs rEnice_getzProcess.nice_get(s%dh///rDc6tj|j|Sr)r setpriorityr)rzr?s rEnice_setzProcess.nice_set1s%dh666rDc4tj|jSr)rr8rrs rEcpu_affinity_getzProcess.cpu_affinity_get8s-dh77 7rDsCpus_allowed_list:\t(\d+)-(\d+)c f|}||}|rRttt |ddt |dddzSttt t S)Nrr)rrrrangerrr#)rz_rer"r,s rE_get_eligible_cpuszProcess._get_eligible_cpus<s))++DKK%%E 9E#eAhqk"2"2Ca 4D4Dq4HIIJJJE#moo"6"677888rDc  tj|j|dS#ttf$r}t |ts|jt jkr|}tttt}|D]4}||vrt d|d|||vrt d|d|5d}~wwxYw)Nzinvalid CPU number z; choose between z CPU number z! is not eligible; choose between ) rproc_cpu_affinity_setrrr(rrrr"tupler rr#)rzr!r eligible_cpusall_cpuscpus rEcpu_affinity_setzProcess.cpu_affinity_setFs *48T:::::Z(   c:.. E#)u|2K2K$($;$;$=$=M$U3}+?+?%@%@AAH#EEh..",*$'CC!8#9#99m33",*03]]!D#E#EE4 sCB+CCctj|j\}}tt |}t j||Sr)rr7renumr?rpionicerzioclassr?s rE ionice_getzProcess.ionice_get\s=!1$(;;NGU$W--?7E22 2rDc|d}|r"|ttfvrtd|z|dks|dkrtdtj|j||S)Nrz%r ioclass accepts no valuerzvalue not in 0-7 range)r+r(r(rproc_ioprio_setrr-s rE ionice_setzProcess.ionice_setcsq} J%68I$JJJ !>!HIIIqyyEAII !9:::''5AA ArDc|jdkrtd |t|j|St|dkrtdt |zt|j||dS#t $rO}|jt jkr4t|jr t|j|j |j d}~wwxYw)Nrz)can't use prlimit() against PID 0 processr=z4second argument must be a (soft, hard) tuple, got %s) rr(rrrrrENOSYSrrrr)rzrrrs rErlimitzProcess.rlimitos x1}} !LMMM >"48Y7776{{a''(%'+F||4555DHi88888   9 ,,DH1E1E,($*djIII  sA=AA== CA CCc|d}tr|}t|dS)Nr?)rrr PROC_STATUSESr)rzletters rErzProcess.statussC&&((2  %]]__F  ---rDc6g}tj|jd|jd}d}|D]}|jd|jd|} t |}|dr,t |r|jd|jd|} t|5}t| d}t| dd} dddn #1swxYwYt| } t|t|t|| | } | | O#ttf$rd}YewxYwk#ttf$rd}Yt $rK} | jt"jkrYd} ~ | jt"jkrt)| Yd} ~ ςd} ~ wwxYw|r||S) Nrr}Fr~z/fdinfo/rrT)rrryrrrrrrrrrrsrr r"rrrrrr) rzr4r rrurrtrposrxrwr<rs rE open_fileszProcess.open_filess ):):):DHHHEFF & /& /B$($5$5$5txxxDD$ /~~&??3''/M$,?,?/)))4888RR9D /(..D!"%ajjll&8&8&:&:1&=">">C$' (:(:(<(  !    saF E:A4D E:D E:#D $E::FFH, H5G;$G;:G;;Hrocnt||j}||Sr)rrrr)rzrrs rE connectionszProcess.connectionss0##D$(33  rDcdttj|jd|jdS)Nrr})rrrryrrs rEnum_fdszProcess.num_fdss.2:D,=,=,=txxxHIIJJJrDcPt|dS)Nrrrs rErz Process.ppids!4((**62333rDsUid:\t(\d+)\t(\d+)\t(\d+)c|}||d\}}}tjt |t |t |Sr )rrrpuidsr)rz_uids_rer"real effectivesaveds rEuidsz Process.uidsX%%''!)!1!1$!7!7!:i}SYYIE CCCrDsGid:\t(\d+)\t(\d+)\t(\d+)c|}||d\}}}tjt |t |t |Sr )rrrpgidsr)rz_gids_rer"rErFrGs rEgidsz Process.gidsrIrDrro);r@rArBrr&r{rrrrrrrrrrrrrrrtrgetpidrrrrrrrHAS_PROC_SMAPSrr*r+rrrrr rrrrHAS_CPU_AFFINITYrr"r)HAS_PROC_IO_PRIORITYr/r2rr5rr<r>r@rrHrMrCrDrErrs}''CCCI... 999 _@_$$_$333 555 _555"_0))_) _ w~~mibikk122J  J J  J<OO_O77_7@@@_@**_*>>_>"A'A'  $ $  $. 'BJ'?@@" #677#$899  $ $ $  $: ; ; ; ;'D A A  A FFF_F#-2:.G#H#H @ @ @_ @*4"*5H*I*I555_5_600_077_7!  8 8  8%"*%HII 9 9 9 9     &B  3 3  3  B B  B      2.._.--_-^_ KK_K44_4&BJ'DEEDDD_D &BJ'DEEDDD_DDDrDrrrN)F)r __future__rrrNrrr4rr*rrrsys tracebackrrrrrrr rr rr r r rrrrrrrrrrrrrrrrr_compatrr r!r"r#r$ version_infor+__extra__all__rprtrrOrPrhasattrrRrQr'r getpagesizerr byteorderrr  AF_PACKETr<IntEnumrr;r(r)r*r+r?globalsr __members__STATUS_RUNNINGSTATUS_SLEEPINGSTATUS_DISK_SLEEPSTATUS_STOPPEDSTATUS_TRACING_STOP STATUS_ZOMBIE STATUS_DEADSTATUS_WAKE_KILL STATUS_WAKING STATUS_IDLE STATUS_PARKEDr8r,r-r.r/r0r1r2r3r4r5r6rr]rirsryrrzr~rrrrrrrr Exception print_excrrresource ImportErrorrCDLLrrOdirrr rrr#r/r@rGrJr_ net_if_addrsrcrerrrr disk_usagerrr/r^rbr}rrrrrrrorrCrDrErss  %$   ######""""""######))))))$$$$$$$$$$$$''''''!!!!!!""""""""""""$$$$$$""""""++++++ ((((((''''''""""""""""""&&&&&&$$$$$$''''''vKKKK D 6 6 6". 0929;; >??'>'LMMwt%67774!899bj&& !: ! # #   ) <GG DL"+SS1A-B-B!CEEM#G<OOT\  GIIZ+,,,     "    $        !         "  "           !         *   KKK L L *    J<<< > > z&>?? :j$,1G"G H H JJJKK J-*?!@!@@ B B j66677 J{""" # # (   $$$& 00 00K!!!!KKKI; ;(:;;CcJJIIIK 8       888MMM 6;tt , , ,DwtY8 8 8 8 88< FFCCIIFFFHHH:::zuBuBuBp2@2@2@t    (:'''T===*7>>;<<'N =>>'NBNNN&      I   kkkkkkkk\{}} '''' """J> \\\\~GGGGGGGGT&&&&\nnnb>Y>Y>Y>B$ 3 3 3&MMM !!!@,*V DV DV DV DV DfV DV DV DV DV Ds$ M))+NNN$$+OO