Yc@sddlZddlZddlZdZdZdd'dYZdd(dYZdd)d YZd d*d YZd d+d YZ dd,dYZ dd-dYZ dd.dYZ dd/dYZ dd0dYZdd1dYZedkrddlZe Zx-ej jD]Zdeej efGHq5We Zx-ejjD]ZdeejefGHqnWe ZdejGHx'ejD]ZdeeefGHqWddd GHe eejd!Zx:eejD])Zd"ejej ejej!fGHqWdd GHx'ej"ejd#D]Z#e#d$GHqPWeZ$e$d!GHeZ%xOe&rej'd!e%j(x#e%D]Zd%ej!ej)fGHqWd2GHqWndS(3iNs0.3cCs0|dr$td|djS|ddS(s Returns the process command line, if available in the given `process' class, if not available, falls back to using the comm (short process name) in its pidstat key. tcmdlinecSs |d|S(Ns %s((tatb((s1/usr/lib/python2.7/site-packages/procfs/procfs.pytststattcomm(treducetstrip(tpid_info((s1/usr/lib/python2.7/site-packages/procfs/procfs.pytprocess_cmdlines tpidstatc+BseZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$d Z%d Z&d!d"d#d$d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIdJdKg+Z'dLdMZ(dNZ)dOZ*dPZ+dQZ,dRZ-dSZ.dLdTZ/dUZ0dVZ1RS(WsProvides a dictionary to access the fields in the per process /proc/PID/stat files. One can obtain the available fields asking for the keys of the dictionary, e.g.: >>> p = procfs.pidstat(1) >>> print p.keys() ['majflt', 'rss', 'cnswap', 'cstime', 'pid', 'session', 'startstack', 'startcode', 'cmajflt', 'blocked', 'exit_signal', 'minflt', 'nswap', 'environ', 'priority', 'state', 'delayacct_blkio_ticks', 'policy', 'rt_priority', 'ppid', 'nice', 'cutime', 'endcode', 'wchan', 'num_threads', 'sigcatch', 'comm', 'stime', 'sigignore', 'tty_nr', 'kstkeip', 'utime', 'tpgid', 'itrealvalue', 'kstkesp', 'rlim', 'signal', 'pgrp', 'flags', 'starttime', 'cminflt', 'vsize', 'processor'] And then access the various process properties using it as a dictionary: >>> print p['comm'] systemd >>> print p['priority'] 20 >>> print p['state'] S Please refer to the 'procfs(5)' man page, by using: $ man 5 procfs To see information for each of the above fields, it is part of the 'man-pages' RPM package. iiiiii i@iiiiiii i@iiiiiii i@iiiiiii i@ItpidRtstatetppidtpgrptsessionttty_nrttpgidtflagstminflttcminflttmajflttcmajflttutimetstimetcutimetcstimetprioritytnicet num_threadst itrealvaluet starttimetvsizetrsstrlimt startcodetendcodet startstacktkstkesptkstkeiptsignaltblockedt sigignoretsigcatchtwchantnswaptcnswapt exit_signalt processort rt_prioritytpolicytdelayacct_blkio_tickstenvirons/proccCs||_|j|dS(N(R tload(tselfR tbasedir((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyt__init__rs cCs |j|S(N(tfields(R6t fieldname((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyt __getitem__vscCs |jjS(N(R9tkeys(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR<yscCs |jjS(N(R9tvalues(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR=|scCs|jj|S(N(R9thas_key(R6R:((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR>scCs|jS(N(R9(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pytitemsscCs ||jkS(N(R9(R6R:((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyt __contains__scCs td||jf}|jjjd}|j|djd|dj}i|_tt|t|j }x~t |D]p}|j |}||}|dkr|jd|jdR?R@R5RRRY(((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR #st                t pidstatuscBs\eZdZddZdZdZdZdZdZdZ dd Z RS( s Provides a dictionary to access the fields in the per process /proc/PID/status files. This provides additional information about processes and threads to what can be obtained with the procfs.pidstat() class. One can obtain the available fields asking for the keys of the dictionary, e.g.: >>> import procfs >>> p = procfs.pidstatus(1) >>> print p.keys() ['VmExe', 'CapBnd', 'NSpgid', 'Tgid', 'NSpid', 'VmSize', 'VmPMD', 'ShdPnd', 'State', 'Gid', 'nonvoluntary_ctxt_switches', 'SigIgn', 'VmStk', 'VmData', 'SigCgt', 'CapEff', 'VmPTE', 'Groups', 'NStgid', 'Threads', 'PPid', 'VmHWM', 'NSsid', 'VmSwap', 'Name', 'SigBlk', 'Mems_allowed_list', 'VmPeak', 'Ngid', 'VmLck', 'SigQ', 'VmPin', 'Mems_allowed', 'CapPrm', 'Seccomp', 'VmLib', 'Cpus_allowed', 'Uid', 'SigPnd', 'Pid', 'Cpus_allowed_list', 'TracerPid', 'CapInh', 'voluntary_ctxt_switches', 'VmRSS', 'FDSize'] >>> print p["Pid"] 1 >>> print p["Threads"] 1 >>> print p["VmExe"] 1248 kB >>> print p["Cpus_allowed"] f >>> print p["SigQ"] 0/30698 >>> print p["VmPeak"] 320300 kB >>> Please refer to the 'procfs(5)' man page, by using: $ man 5 procfs To see information for each of the above fields, it is part of the 'man-pages' RPM package. In the man page there will be references to further documentation, like referring to the "getrlimit(2)" man page when explaining the "SigQ" line/field. s/proccCs||_|j|dS(N(R R5(R6R R7((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR8s cCs |j|S(N(R9(R6R:((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR;scCs |jjS(N(R9R<(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR<scCs |jjS(N(R9R=(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR=scCs|jj|S(N(R9R>(R6R:((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR>scCs|jS(N(R9(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR?scCs ||jkS(N(R9(R6R:((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR@ scCstd||jf}i|_x|jD]u}|jd}t|dkr\q/n|d}|dj}yt||j|R?R@R5(((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyRs$       tprocesscBsYeZdZddZdZdZdZdZdZdZ d Z RS( s Information about a process with a given pid, provides a dictionary with two entries, instances of different wrappers for /proc/ process related meta files: "stat" and "status", see the documentation for procfs.pidstat and procfs.pidstatus for further info about those classes. s/proccCs||_||_dS(N(R R7(R6R R7((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR8$s cCst||s|dkrX|dkr0t}nt}t||||j|jq|dkrq|jq|dkr|jq|dkr|jq|dkr|j qnt ||S(NRtstatusRtthreadstcgroupsR4(sstatR( thasattrR RtsetattrR R7t load_cmdlinet load_threadst load_cgroupst load_environRU(R6RXtsclass((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR;(s   "       cCs t||S(N(R(R6RX((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR><scCs t||S(N(R(R6RX((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR@?scCsCtd|j}|jjjdd |_|jdS(Ns/proc/%d/cmdlineti(tfileR RBRRCRRD(R6RJ((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyRBs"cCs'td|j|_|j|j=dS(Ns/proc/%d/task/(tpidstatsR R(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyRGscCs~td|j}d|_xQt|jD]=}t|jr_|jd|d |_q/|d |_q/W|jdS(Ns/proc/%d/cgrouptt,i(RR RtreversedRRFRD(R6RJR((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyRLs cCsi|_td|j}xV|jjdD]?}t|dkr2|jd}|d|j|d>> all_processes = procfs.pidstats() >>> firefox_pid = all_processes.find_by_name("firefox") >>> firefox_process = all_processes[firefox_pid[0]] >>> print firefox_process["environ"]["PWD"] /home/acme >>> print len(firefox_process.environ.keys()) 66 >>> print firefox_process["environ"]["SHELL"] /bin/bash >>> print firefox_process["environ"]["USERNAME"] acme >>> print firefox_process["environ"]["HOME"] /home/acme >>> print firefox_process["environ"]["MAIL"] /var/spool/mail/acme >>> s/proc/%d/environRit=iN(R4RR RBRCRFRD(R6RJtxty((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyRVs ( RZR[R\R8R;R>R@RRRR(((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyRs       RcBseZdZddZdZdZdZdZdZdZ d Z d Z d Z d Z d ZdZdZdZdZRS(s Provides access to all the processes in the system, to get a picture of how many processes there are at any given moment. The entries can be accessed as a dictionary, keyed by pid. Also there are methods to find processes that match a given COMM or regular expression. s/proccCs ||_i|_|jdS(N(R7t processestreload(R6R7((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR8s  cCs |j|S(N(R(R6tkey((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR;scCsy|j|=WnnXdS(N(R(R6R((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyt __delitem__scCs |jjS(N(RR<(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR<scCs |jjS(N(RR=(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR=scCs|jj|S(N(RR>(R6R((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR>scCs|jS(N(R(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR?scCs ||jkS(N(R(R6R((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR@scCsl|`i|_tj|j}xD|D]<}yt|}Wn q(nXt||j|j|R?R@RRRRRRRRR(((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyRxs"             t interruptscBszeZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d ZRS( s4 Information about IRQs in the system. A dictionary keyed by IRQ number will have as its value another dictionary with "cpu", "type" and "users" keys, with the SMP affinity mask, type of IRQ and the drivers associated with each interrupt. The information comes from the /proc/interrupts file, documented in 'man procfs(5)', for instance, the 'cpu' dict is an array with one entry per CPU present in the sistem, each value being the number of interrupts that took place per CPU. E.g.: >>> import procfs >>> interrupts = procfs.interrupts() >>> thunderbolt_irq = interrupts.find_by_user("thunderbolt") >>> print thunderbolt_irq 34 >>> thunderbolt = interrupts[thunderbolt_irq] >>> print thunderbolt {'affinity': [0, 1, 2, 3], 'type': 'PCI-MSI', 'cpu': [3495, 0, 81, 0], 'users': ['thunderbolt']} >>> cCsi|_|jdS(N(RR(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR84s cCs|jt|S(N(Rtstr(R6R((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR;8scCs |jjS(N(RR<(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR<;scCs |jjS(N(RR=(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR=>scCs|jjt|S(N(RR>R(R6R((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR>AscCs|jS(N(R(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR?DscCst||jkS(N(RR(R6R((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR@GscCs|`i|_td}x|jD]}|j}|j}|dd dkrot||_q(n|djd}i|j|<|j|d||j|>> import procfs >>> interrupts = procfs.interrupts() >>> thunderbolt_irq = interrupts.find_by_user("thunderbolt") >>> print thunderbolt_irq 34 >>> thunderbolt = interrupts[thunderbolt_irq] >>> print thunderbolt {'affinity': [0, 1, 2, 3], 'type': 'PCI-MSI', 'cpu': [3495, 0, 81, 0], 'users': ['thunderbolt']} >>> RN(RR<R>tNone(R6tuserRL((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyt find_by_userys cCs|g}xo|jjD]^}|j|jds8qnx9|j|dD]&}|j|rJ|j|PqJqJWqW|S(s Looks up a interrupt number by a regex that matches names of its users" E.g.: >>> import procfs >>> import re >>> interrupts = procfs.interrupts() >>> usb_controllers = interrupts.find_by_user_regex(re.compile(".*hcd")) >>> print usb_controllers ['22', '23', '31'] >>> print [ interrupts[irq]["users"] for irq in usb_controllers ] [['ehci_hcd:usb4'], ['ehci_hcd:usb3'], ['xhci_hcd']] >>> R(RR<R>RRV(R6RtirqsRLR((s1/usr/lib/python2.7/site-packages/procfs/procfs.pytfind_by_user_regexs  (RZR[R\R8R;R<R=R>R?R@RRRRR(((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyRs          RcBsDeZdZdZdZdZdZdZdZRS(s Parses the kernel command line (/proc/cmdline), turning it into a dictionary." Useful to figure out if some kernel boolean knob has been turned on, as well as to find the value associated to other kernel knobs. It can also be used to find out about parameters passed to the init process, such as 'BOOT_IMAGE', etc. E.g.: >>> import procfs >>> kcmd = procfs.cmdline() >>> print kcmd.keys() ['LANG', 'BOOT_IMAGE', 'quiet', 'rhgb', 'rd.lvm.lv', 'ro', 'root'] >>> print kcmd["BOOT_IMAGE"] /vmlinuz-4.3.0-rc1+ >>> cCsi|_|jdS(N(toptionstparse(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR8s cCstd}xj|jjjD]P}|jd}t|dkr`t|j|d>> cpus = procfs.cpuinfo() >>> print cpus.nr_cpus 4 It is also possible to figure out aspects of the CPU topology, such as how many CPU physical sockets exists, i.e. groups of CPUs sharing components such as CPU memory caches: >>> print len(cpus.sockets) 1 Additionally dictionary with information common to all CPUs in the system is available: >>> print cpus["model name"] Intel(R) Core(TM) i7-3667U CPU @ 2.00GHz >>> print cpus["cache size"] 4096 KB >>> s /proc/cpuinfocCs,i|_d|_g|_|j|dS(Ni(ttagsRtsocketsR(R6tfilename((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR8s   cCs|j|jS(N(Rtlower(R6R((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR;scCs |jjS(N(RR<(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR<scCs |jjS(N(RR=(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR=scCs|jS(N(R(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR?scCs~t|}x|jD]}|j}t|dkrCqn|jd}|djj}|dkr|jd7_qn|dkrqn|dj|j|<|dkr|j|}||jkr|jj |qqqW|j |jrt|jpB|jd|jkr>t |jdpAd|_ d|jkrjt |jdpmd|j |_ dS( NiRR0iscore ids physical idtsiblingss cpu cores(RRRRFRCRRRRRVRDRIt nr_socketstnr_cores(R6RRJRR9ttagnamet socket_id((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR s*       2( RZR[R\R8R;R<R=R?R(((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyRs     t smaps_libcBs;eZdZdZdZdZdZdZRS(s^ Representation of an mmap in place for a process. Can be used to figure out which processes have an library mapped, etc. The 'perm' member can be used to figure out executable mmaps, i.e. libraries. The 'vm_start' and 'vm_end' in turn can be used when trying to resolve processor instruction pointer addresses to a symbol name in a library. cCs/|dj}td|djd\|_|_|d|_t|dd|_|djd\|_|_t|d |_ t |d kr|d |_ n d|_ i|_ xd|dD]X}|j}|dd j}yt|d|j |>> import procfs >>> processes = procfs.pidstats() >>> sshd = processes.find_by_name("sshd") >>> sshd_maps = procfs.smaps(sshd[0]) >>> for i in range(len(sshd_maps)): ... if 'x' in sshd_maps[i].perms: ... print "%s: %s" % (sshd_maps[i].name, sshd_maps[i].perms) ... /usr/sbin/sshd: r-xp /usr/lib64/libnss_files-2.20.so: r-xp /usr/lib64/librt-2.20.so: r-xp /usr/lib64/libkeyutils.so.1.5: r-xp /usr/lib64/libkrb5support.so.0.1: r-xp /usr/lib64/libfreebl3.so: r-xp /usr/lib64/libpthread-2.20.so: r-xp ... cCs ||_g|_|jdS(N(R tentriesR(R6R ((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR8fs  cCsg}|s!|jj}n|s+dS|j|xWtr|j}|sWPn|j}|jdddkr|j|q;Pq;W|jjt||S(NiiR(RBRRVRPRCRR(R6RJRR((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyRks     cCs t|jS(N(RFR(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyt__len__~scCs |j|S(N(R(R6tindex((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR;scCsbtd|j}d}x&trA|j||}|sPqqW|jt|j|_dS(Ns/proc/%d/smaps( RR RRPRRDRFRt nr_entries(R6RJR((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyRs  cCsjg}x]t|jD]L}|j|jr|j|jj|dkr|j|j|qqW|S(Ni(RHRRRtfindRV(R6tfragmenttresultRL((s1/usr/lib/python2.7/site-packages/procfs/procfs.pytfind_by_name_fragments ( RZR[R\R8RRR;RR(((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyRMs     tcpustatcBs eZdZdZdZRS(s CPU statistics, obtained from a line in the '/proc/stat' file, Please refer to 'man procfs(5)' for further information about the '/proc/stat' file, that is the source of the information provided by this class. cCs|d|_g|dd!D]}t|^q\|_|_|_|_|_|_|_t |dkrt|d|_ t |dkrt|d|_ qndS(Niiii( RRIRRtsystemtidletiowaitRtsoftirqRFtstealtguest(R6R9RL((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR8s  PcCsd|j|j|j|j|j|j|jf}t|drW|d|j7}nt|drz|d|j 7}n|dS(NsL< user: %s, nice: %s, system: %s, idle: %s, iowait: %s, irq: %s, softirq: %sRs , steal: %dRs , guest: %dt>( RRRRRRRRRR(R6ts((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyt__repr__s1(RZR[R\R8R(((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyRs t cpusstatscBsYeZdZddZdZdZdZdZdZdZ d Z RS( s< Dictionary with information about CPUs in the system. First entry in the dictionary gives an aggregate view of all CPUs, each other entry is about separate CPUs. Please refer to 'man procfs(5)' for further information about the '/proc/stat' file, that is the source of the information provided by this class. s /proc/statcCs;i|_d|_tjd|_||_|jdS(Ni(RRttimeRtsysconfthertzRR(R6R((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR8s    cCs t|jS(N(titerR(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyt__iter__scCs |j|S(N(R(R6R((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR;scCst|jjS(N(RFRR<(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyRscCs |jjS(N(RR<(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR<scCs |jjS(N(RR=(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR=scCs|jS(N(R(R6((s1/usr/lib/python2.7/site-packages/procfs/procfs.pyR?scCs|j}i|_t|j}x|jD]}|jj}|dd jdkrfq.nt|}|jdkrd}nt |jdd}||j|sR$ K\/I,JB       '