Cross-platform lib for process and system monitoring in Python

Overview



Home    Install    Documentation    Download    Forum    Blog    Funding    What's new   

Summary

psutil (process and system utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network, sensors) in Python. It is useful mainly for system monitoring, profiling and limiting process resources and management of running processes. It implements many functionalities offered by classic UNIX command line tools such as ps, top, iotop, lsof, netstat, ifconfig, free and others. psutil currently supports the following platforms:

  • Linux
  • Windows
  • macOS
  • FreeBSD, OpenBSD, NetBSD
  • Sun Solaris
  • AIX

Supported Python versions are 2.6, 2.7, 3.4+ and PyPy.

Funding

While psutil is free software and will always be, the project would benefit immensely from some funding. Keeping up with bug reports and maintenance has become hardly sustainable for me alone in terms of time. If you're a company that's making significant use of psutil you can consider becoming a sponsor via GitHub Sponsors, Open Collective or PayPal and have your logo displayed in here and psutil doc.

Sponsors

add your logo

Supporters

add your avatar

Contributing

See contributing guidelines.

Example usages

This represents pretty much the whole psutil API.

CPU

>>> import psutil
>>>
>>> psutil.cpu_times()
scputimes(user=3961.46, nice=169.729, system=2150.659, idle=16900.540, iowait=629.59, irq=0.0, softirq=19.42, steal=0.0, guest=0, nice=0.0)
>>>
>>> for x in range(3):
...     psutil.cpu_percent(interval=1)
...
4.0
5.9
3.8
>>>
>>> for x in range(3):
...     psutil.cpu_percent(interval=1, percpu=True)
...
[4.0, 6.9, 3.7, 9.2]
[7.0, 8.5, 2.4, 2.1]
[1.2, 9.0, 9.9, 7.2]
>>>
>>> for x in range(3):
...     psutil.cpu_times_percent(interval=1, percpu=False)
...
scputimes(user=1.5, nice=0.0, system=0.5, idle=96.5, iowait=1.5, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
scputimes(user=1.0, nice=0.0, system=0.0, idle=99.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
scputimes(user=2.0, nice=0.0, system=0.0, idle=98.0, iowait=0.0, irq=0.0, softirq=0.0, steal=0.0, guest=0.0, guest_nice=0.0)
>>>
>>> psutil.cpu_count()
4
>>> psutil.cpu_count(logical=False)
2
>>>
>>> psutil.cpu_stats()
scpustats(ctx_switches=20455687, interrupts=6598984, soft_interrupts=2134212, syscalls=0)
>>>
>>> psutil.cpu_freq()
scpufreq(current=931.42925, min=800.0, max=3500.0)
>>>
>>> psutil.getloadavg()  # also on Windows (emulated)
(3.14, 3.89, 4.67)

Memory

>>> psutil.virtual_memory()
svmem(total=10367352832, available=6472179712, percent=37.6, used=8186245120, free=2181107712, active=4748992512, inactive=2758115328, buffers=790724608, cached=3500347392, shared=787554304)
>>> psutil.swap_memory()
sswap(total=2097147904, used=296128512, free=1801019392, percent=14.1, sin=304193536, sout=677842944)
>>>

Disks

>>> psutil.disk_partitions()
[sdiskpart(device='/dev/sda1', mountpoint='/', fstype='ext4', opts='rw,nosuid', maxfile=255, maxpath=4096),
 sdiskpart(device='/dev/sda2', mountpoint='/home', fstype='ext, opts='rw', maxfile=255, maxpath=4096)]
>>>
>>> psutil.disk_usage('/')
sdiskusage(total=21378641920, used=4809781248, free=15482871808, percent=22.5)
>>>
>>> psutil.disk_io_counters(perdisk=False)
sdiskio(read_count=719566, write_count=1082197, read_bytes=18626220032, write_bytes=24081764352, read_time=5023392, write_time=63199568, read_merged_count=619166, write_merged_count=812396, busy_time=4523412)
>>>

Network

>>> psutil.net_io_counters(pernic=True)
{'eth0': netio(bytes_sent=485291293, bytes_recv=6004858642, packets_sent=3251564, packets_recv=4787798, errin=0, errout=0, dropin=0, dropout=0),
 'lo': netio(bytes_sent=2838627, bytes_recv=2838627, packets_sent=30567, packets_recv=30567, errin=0, errout=0, dropin=0, dropout=0)}
>>>
>>> psutil.net_connections(kind='tcp')
[sconn(fd=115, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=48776), raddr=addr(ip='93.186.135.91', port=80), status='ESTABLISHED', pid=1254),
 sconn(fd=117, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=43761), raddr=addr(ip='72.14.234.100', port=80), status='CLOSING', pid=2987),
 ...]
>>>
>>> psutil.net_if_addrs()
{'lo': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast='127.0.0.1', ptp=None),
        snicaddr(family=<AddressFamily.AF_INET6: 10>, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None, ptp=None),
        snicaddr(family=<AddressFamily.AF_LINK: 17>, address='00:00:00:00:00:00', netmask=None, broadcast='00:00:00:00:00:00', ptp=None)],
 'wlan0': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='192.168.1.3', netmask='255.255.255.0', broadcast='192.168.1.255', ptp=None),
           snicaddr(family=<AddressFamily.AF_INET6: 10>, address='fe80::c685:8ff:fe45:641%wlan0', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None),
           snicaddr(family=<AddressFamily.AF_LINK: 17>, address='c4:85:08:45:06:41', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]}
>>>
>>> psutil.net_if_stats()
{'lo': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=65536),
 'wlan0': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_FULL: 2>, speed=100, mtu=1500)}
>>>

Sensors

>>> import psutil
>>> psutil.sensors_temperatures()
{'acpitz': [shwtemp(label='', current=47.0, high=103.0, critical=103.0)],
 'asus': [shwtemp(label='', current=47.0, high=None, critical=None)],
 'coretemp': [shwtemp(label='Physical id 0', current=52.0, high=100.0, critical=100.0),
              shwtemp(label='Core 0', current=45.0, high=100.0, critical=100.0)]}
>>>
>>> psutil.sensors_fans()
{'asus': [sfan(label='cpu_fan', current=3200)]}
>>>
>>> psutil.sensors_battery()
sbattery(percent=93, secsleft=16628, power_plugged=False)
>>>

Other system info

>>> import psutil
>>> psutil.users()
[suser(name='giampaolo', terminal='pts/2', host='localhost', started=1340737536.0, pid=1352),
 suser(name='giampaolo', terminal='pts/3', host='localhost', started=1340737792.0, pid=1788)]
>>>
>>> psutil.boot_time()
1365519115.0
>>>

Process management

>>> import psutil
>>> psutil.pids()
[1, 2, 3, 4, 5, 6, 7, 46, 48, 50, 51, 178, 182, 222, 223, 224, 268, 1215,
 1216, 1220, 1221, 1243, 1244, 1301, 1601, 2237, 2355, 2637, 2774, 3932,
 4176, 4177, 4185, 4187, 4189, 4225, 4243, 4245, 4263, 4282, 4306, 4311,
 4312, 4313, 4314, 4337, 4339, 4357, 4358, 4363, 4383, 4395, 4408, 4433,
 4443, 4445, 4446, 5167, 5234, 5235, 5252, 5318, 5424, 5644, 6987, 7054,
 7055, 7071]
>>>
>>> p = psutil.Process(7055)
>>> p
psutil.Process(pid=7055, name='python3', status='running', started='09:04:44')
>>> p.name()
'python'
>>> p.exe()
'/usr/bin/python'
>>> p.cwd()
'/home/giampaolo'
>>> p.cmdline()
['/usr/bin/python', 'main.py']
>>>
>>> p.pid
7055
>>> p.ppid()
7054
>>> p.children(recursive=True)
[psutil.Process(pid=29835, name='python3', status='sleeping', started='11:45:38'),
 psutil.Process(pid=29836, name='python3', status='waking', started='11:43:39')]
>>>
>>> p.parent()
psutil.Process(pid=4699, name='bash', status='sleeping', started='09:06:44')
>>> p.parents()
[psutil.Process(pid=4699, name='bash', started='09:06:44'),
 psutil.Process(pid=4689, name='gnome-terminal-server', status='sleeping', started='0:06:44'),
 psutil.Process(pid=1, name='systemd', status='sleeping', started='05:56:55')]
>>>
>>> p.status()
'running'
>>> p.username()
'giampaolo'
>>> p.create_time()
1267551141.5019531
>>> p.terminal()
'/dev/pts/0'
>>>
>>> p.uids()
puids(real=1000, effective=1000, saved=1000)
>>> p.gids()
pgids(real=1000, effective=1000, saved=1000)
>>>
>>> p.cpu_times()
pcputimes(user=1.02, system=0.31, children_user=0.32, children_system=0.1, iowait=0.0)
>>> p.cpu_percent(interval=1.0)
12.1
>>> p.cpu_affinity()
[0, 1, 2, 3]
>>> p.cpu_affinity([0, 1])  # set
>>> p.cpu_num()
1
>>>
>>> p.memory_info()
pmem(rss=10915840, vms=67608576, shared=3313664, text=2310144, lib=0, data=7262208, dirty=0)
>>> p.memory_full_info()  # "real" USS memory usage (Linux, macOS, Win only)
pfullmem(rss=10199040, vms=52133888, shared=3887104, text=2867200, lib=0, data=5967872, dirty=0, uss=6545408, pss=6872064, swap=0)
>>> p.memory_percent()
0.7823
>>> p.memory_maps()
[pmmap_grouped(path='/lib/x8664-linux-gnu/libutil-2.15.so', rss=32768, size=2125824, pss=32768, shared_clean=0, shared_dirty=0, private_clean=20480, private_dirty=12288, referenced=32768, anonymous=12288, swap=0),
 pmmap_grouped(path='/lib/x8664-linux-gnu/libc-2.15.so', rss=3821568, size=3842048, pss=3821568, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=3821568, referenced=3575808, anonymous=3821568, swap=0),
 pmmap_grouped(path='[heap]',  rss=32768, size=139264, pss=32768, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=32768, referenced=32768, anonymous=32768, swap=0),
 pmmap_grouped(path='[stack]', rss=2465792, size=2494464, pss=2465792, shared_clean=0, shared_dirty=0, private_clean=0, private_dirty=2465792, referenced=2277376, anonymous=2465792, swap=0),
 ...]
>>>
>>> p.io_counters()
pio(read_count=478001, write_count=59371, read_bytes=700416, write_bytes=69632, read_chars=456232, write_chars=517543)
>>>
>>> p.open_files()
[popenfile(path='/home/giampaolo/monit.py', fd=3, position=0, mode='r', flags=32768),
 popenfile(path='/var/log/monit.log', fd=4, position=235542, mode='a', flags=33793)]
>>>
>>> p.connections(kind='tcp')
[pconn(fd=115, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=48776), raddr=addr(ip='93.186.135.91', port=80), status='ESTABLISHED'),
 pconn(fd=117, family=<AddressFamily.AF_INET: 2>, type=<SocketType.SOCK_STREAM: 1>, laddr=addr(ip='10.0.0.1', port=43761), raddr=addr(ip='72.14.234.100', port=80), status='CLOSING')]
>>>
>>> p.num_threads()
4
>>> p.num_fds()
8
>>> p.threads()
[pthread(id=5234, user_time=22.5, system_time=9.2891),
 pthread(id=5237, user_time=0.0707, system_time=1.1)]
>>>
>>> p.num_ctx_switches()
pctxsw(voluntary=78, involuntary=19)
>>>
>>> p.nice()
0
>>> p.nice(10)  # set
>>>
>>> p.ionice(psutil.IOPRIO_CLASS_IDLE)  # IO priority (Win and Linux only)
>>> p.ionice()
pionice(ioclass=<IOPriority.IOPRIO_CLASS_IDLE: 3>, value=0)
>>>
>>> p.rlimit(psutil.RLIMIT_NOFILE, (5, 5))  # set resource limits (Linux only)
>>> p.rlimit(psutil.RLIMIT_NOFILE)
(5, 5)
>>>
>>> p.environ()
{'LC_PAPER': 'it_IT.UTF-8', 'SHELL': '/bin/bash', 'GREP_OPTIONS': '--color=auto',
'XDG_CONFIG_DIRS': '/etc/xdg/xdg-ubuntu:/usr/share/upstart/xdg:/etc/xdg',
 ...}
>>>
>>> p.as_dict()
{'status': 'running', 'num_ctx_switches': pctxsw(voluntary=63, involuntary=1), 'pid': 5457, ...}
>>> p.is_running()
True
>>> p.suspend()
>>> p.resume()
>>>
>>> p.terminate()
>>> p.kill()
>>> p.wait(timeout=3)
<Exitcode.EX_OK: 0>
>>>
>>> psutil.test()
USER         PID %CPU %MEM     VSZ     RSS TTY        START    TIME  COMMAND
root           1  0.0  0.0   24584    2240            Jun17   00:00  init
root           2  0.0  0.0       0       0            Jun17   00:00  kthreadd
...
giampaolo  31475  0.0  0.0   20760    3024 /dev/pts/0 Jun19   00:00  python2.4
giampaolo  31721  0.0  2.2  773060  181896            00:04   10:30  chrome
root       31763  0.0  0.0       0       0            00:05   00:00  kworker/0:1
>>>

Further process APIs

>>> import psutil
>>> for proc in psutil.process_iter(['pid', 'name']):
...     print(proc.info)
...
{'pid': 1, 'name': 'systemd'}
{'pid': 2, 'name': 'kthreadd'}
{'pid': 3, 'name': 'ksoftirqd/0'}
...
>>>
>>> psutil.pid_exists(3)
True
>>>
>>> def on_terminate(proc):
...     print("process {} terminated".format(proc))
...
>>> # waits for multiple processes to terminate
>>> gone, alive = psutil.wait_procs(procs_list, timeout=3, callback=on_terminate)
>>>

Popen wrapper:

>>> import psutil
>>> from subprocess import PIPE
>>> p = psutil.Popen(["/usr/bin/python", "-c", "print('hello')"], stdout=PIPE)
>>> p.name()
'python'
>>> p.username()
'giampaolo'
>>> p.communicate()
('hello\n', None)
>>> p.wait(timeout=2)
0
>>>

Windows services

>>> list(psutil.win_service_iter())
[<WindowsService(name='AeLookupSvc', display_name='Application Experience') at 38850096>,
 <WindowsService(name='ALG', display_name='Application Layer Gateway Service') at 38850128>,
 <WindowsService(name='APNMCP', display_name='Ask Update Service') at 38850160>,
 <WindowsService(name='AppIDSvc', display_name='Application Identity') at 38850192>,
 ...]
>>> s = psutil.win_service_get('alg')
>>> s.as_dict()
{'binpath': 'C:\\Windows\\System32\\alg.exe',
 'description': 'Provides support for 3rd party protocol plug-ins for Internet Connection Sharing',
 'display_name': 'Application Layer Gateway Service',
 'name': 'alg',
 'pid': None,
 'start_type': 'manual',
 'status': 'stopped',
 'username': 'NT AUTHORITY\\LocalService'}

Projects using psutil

Here's some I find particularly interesting:

Portings

Comments
  • Provide wheel for Linux and OSX [was: Install psutil without gcc?]

    Provide wheel for Linux and OSX [was: Install psutil without gcc?]

    This is a question: Is it possible to install psutil without gcc?

    How much work would it be to port the code to ctypes (or other way to run without gcc)?

    linux macos wheels 
    opened by guettli 70
  • Osx temps

    Osx temps

    This addresses the discussion #371, This is mainly based on the work of @wiggin15, I have mainly changed it to work similarly to the current API in Linux. I had a chance to test this on 2 machines. I have also tested how this will integrate with my own project s-tui-#49 Please leave feedback so I can do the work to integrate this while I have access to hardware.

    macos 
    opened by amanusk 62
  • get_open_files might hang forever

    get_open_files might hang forever

    From [email protected] on November 24, 2012 15:39:51

    What steps will reproduce the problem?  
    
    
    1. Create a process_iter()
    2. Call get_open_files on each of the processes 
    
    
    
    What is the expected output?  
    The list of open files for each process. 
    
    
    
    What do you see instead?  
    The program hangs forever.
    
    This has already been encountered, there are some exception in the code for 
    exactly this problem. I have found on more access mask where the problem arises 
    - please add it to the list of exceptions. Patch is attached.
    

    Attachment: patch

    Original issue: http://code.google.com/p/psutil/issues/detail?id=340

    bug imported windows critical 
    opened by giampaolo 56
  • Import of psutil errors with

    Import of psutil errors with "import _psutil_mswindows .. DLL load failed"

    From [email protected] on January 02, 2013 02:56:28

    What steps will reproduce the problem?  
    1. Install python-3.3.0 and psutil 0.6.1.win32 on windows-XP, 32bit
    2. start python cli
    3. import psutil 
    
    What is the expected output?  
    succesful import of psutil 
    
    What do you see instead?  
    >python
    Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (In
    tel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import psutil
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "c:\Python33\lib\site-packages\psutil\__init__.py", line 76, in <module>
        import psutil._psmswindows as _psplatform
      File "c:\Python33\lib\site-packages\psutil\_psmswindows.py", line 16, in <modu
    le>
        import _psutil_mswindows
    ImportError: DLL load failed: The specified procedure could not be found.
    >>> 
    
    What version of psutil are you using? What Python version?  
    psutil 0.6.1
    python 3.3 
    
    On what operating system? Is it 32bit or 64bit version?  
    Windows XP, 32bit, SP-3 
    
    Please provide any additional information below.  
    I have installed same python 3.3 and psutil 0.6.1 on a windows 7 systems and it works.
    

    Original issue: http://code.google.com/p/psutil/issues/detail?id=348

    bug imported windows critical 
    opened by giampaolo 50
  • Solaris support

    Solaris support

    From [email protected] on February 23, 2009 14:24:10

    psutil support for Solaris OS
    

    Original issue: http://code.google.com/p/psutil/issues/detail?id=18

    enhancement imported sunos 
    opened by giampaolo 48
  • Hardware temperatures (sensors_temperatures())

    Hardware temperatures (sensors_temperatures())

    From g.rodola on April 24, 2013 14:59:32

    Proposal

    I borrowed this idea from glances: https://github.com/nicolargo/glances ...which internally uses pysensors: https://pypi.python.org/pypi/PySensors/ API may look like this:

    >>> psutil.cpu_temperature()
    [cputemp(name='cpu1', temp=35, high=70, critical=90), 
     cputemp(name='cpu2', temp=30, high=70, critical=90,
     ...]
    

    psutil.cpu_temperature(celsius=False) could be used to express temperature in fahrenheit. The order in which CPUs are returned should be the same as other cpu_* functions and be consistent across calls so that:

    >>> psutil.cpu_temperature()[0]
    

    ...and:

    >>> psutil.cpu_times(percpu=True)[0]
    

    ...will refer to the same CPU.

    FreeBSD: http://superuser.com/questions/344498/get-temperature-of-cpu-intel-core-i5-in-freebsd

    Original issue: http://code.google.com/p/psutil/issues/detail?id=371

    enhancement imported linux 
    opened by giampaolo 45
  • Windows: run Process.open_files() in a separate thread

    Windows: run Process.open_files() in a separate thread

    This PR fixes issue #340 and supersedes PR #596.

    This will spawn a new thread on every call to get_open_files() and time it out after 1 second.

    Based off the skeleton reverse engineered code from Sysinternal's Handles here: http://forum.sysinternals.com/discussion-howto-enumerate-handles_topic19403_post99587.html#99587

    Tested on Python 2.7.2 and Python 3.4.2 on Windows 7 x64.

    All tests pass and the only memory leak test failures are related to net_if_addrs as seen in #596 as well.

    windows 
    opened by mrjefftang 44
  • Real time network IO counters

    Real time network IO counters

    From [email protected] on February 18, 2011 07:04:31

    What steps will reproduce the problem?  
    1. if can show the Process's IO, i means: psutil support a function
    like 
    def get_disk_io(pid=None):
        return (write_speed, read_speed)
    
    def get_net_io(pid=None):
        return (recv_speed, send_speed) 2. 3. 
    
    What is the expected output?  
    
    
    What do you see instead?  
    
    
    What version of psutil are you using? What Python version?  
    
    
    On what operating system? Is it 32bit or 64bit version?  
    Please provide any additional information below.
    

    Original issue: http://code.google.com/p/psutil/issues/detail?id=150

    enhancement imported 
    opened by giampaolo 44
  • Build wheel2

    Build wheel2

    Next part of job. I xfail rlimit test on linux. Add python 2.7 build to test group

    When on macos test_contracts.test_all (test_open_file) fails nondeterministic. looks like catch temporary file which is deleted in mid time:

    Traceback (most recent call last):
      File "/Users/runner/runners/2.262.1/work/psutil/psutil/psutil/tests/test_contracts.py", line 423, in test_all
        raise self.fail(''.join(failures))
    AssertionError:     
        ======================================================================
        FAIL: test_open_files pid=991, ret=[popenfile(path='/Users/runner/Library/Caches/com.apple.helpd/Cache.db', fd=3), popenfile(path='/Users/runner/Library/Caches/com.apple.helpd/Cache.db-wal', fd=4), popenfile(path='/Users/runner/Library/Caches/com.apple.helpd/Cache.db-shm', fd=5), popenfile(path='/Users/runner/Library/Caches/com.apple.helpd/Generated/com.apple.iBooksX.help*2.4/.dat.nosync03df.luRX63', fd=12)]
        ----------------------------------------------------------------------
        Traceback (most recent call last):
          File "/Users/runner/runners/2.262.1/work/psutil/psutil/psutil/tests/test_contracts.py", line 409, in test_all
            meth(value, info)
          File "/Users/runner/runners/2.262.1/work/psutil/psutil/psutil/tests/test_contracts.py", line 604, in open_files
            assert isfile_strict(f.path), f
        AssertionError: popenfile(path='/Users/runner/Library/Caches/com.apple.helpd/Generated/com.apple.iBooksX.help*2.4/.dat.nosync03df.luRX63', fd=12)
    
    wheels 
    opened by Czaki 43
  • Process.cmdline() wrong results, parameters not correctly splitted

    Process.cmdline() wrong results, parameters not correctly splitted

    psutil version 5.4.1 python 3.6.1 Linux Kernel 4.13.0.16 inside an LXC container (if that matters)

    proc=psutil.Process(xxx)   # some PID Number
    proc.cmdline()    # does not give correctly splitted results 
    
    

    for instance:

    # WRONG RESULT
    ['/opt/python3/bin/python3 /opt/rotek-apps/bin/rpyc_server/rpyc_server.py '
     '--[app_startup]b_daemon=False']
    
    # or sometimes WRONG RESULT 
    ['/opt/python3/bin/python3 /opt/rotek-apps/bin/rpyc_server/rpyc_server.py --[app_startup]b_daemon=False']
    
    # CORRECT RESULT 
    ['/opt/python3/bin/python3', '/opt/rotek-apps/bin/rpyc_server/rpyc_server.py'
     '--[app_startup]b_daemon=False']
    

    so I needed to concentate and shlex to split correctly. Randomly wrong results, maybe because some of those processes were started via systemd service ? No idea ...

    yours sincerely Robert

    opened by bitranox 38
  • Real time disk IO counters

    Real time disk IO counters

    From g.rodola on September 15, 2011 00:18:13

    Similarly to network_io_counters (see issue 150 ) it would be good for psutil 
    to provide a disk_io_counters() function returning the number of bytes read and 
    written from/to disk.
    With this we'd have the possibility to write tools such as iotop [1].
    
    >>> psutil.disk_io_counters()
    iostat(read_bytes=2334342, write_bytes=12353453)
    
    As an extra, if the OS provides the necessary hooks, it might be good to 
    provide also the number of read()/write() calls, as per Process.get_io_counters 
    method (see issue 64 ).
    
    I haven't looked into any implementation yet so I'm still not sure what the 
    implications are and whether this can be done on all platforms.
    
    [1] http://guichaz.free.fr/iotop/
    

    Original issue: http://code.google.com/p/psutil/issues/detail?id=206

    enhancement imported 
    opened by giampaolo 38
  • [macOS] cmdline() raises NoSuchProcess on EINVAL

    [macOS] cmdline() raises NoSuchProcess on EINVAL

    Recreated the problem I was facing:

    process_hash = "login"  
    for proc in psutil.process_iter():
        if process_hash in proc.name():
            server_process_running = True
            print(proc)
            print(str(proc.as_dict(['cmdline'])))  # this is where the problem rises 
    
    psutil.Process(pid=3629, name='login', status='running', started='09:16:35')
    Traceback (most recent call last):
      File "/opt/anaconda3/envs/trust/lib/python3.10/site-packages/psutil/_psosx.py", line 346, in wrapper
        return fun(self, *args, **kwargs)
      File "/opt/anaconda3/envs/trust/lib/python3.10/site-packages/psutil/_psosx.py", line 404, in cmdline
        return cext.proc_cmdline(self.pid)
    ProcessLookupError: [Errno 3] assume no such process (originated from sysctl(KERN_PROCARGS2) -> EINVAL)
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/test.py", line 14, in <module>
        print(str(proc.as_dict(['cmdline'])))
      File "/opt/anaconda3/envs/trust/lib/python3.10/site-packages/psutil/__init__.py", line 528, in as_dict
        ret = meth()
      File "/opt/anaconda3/envs/trust/lib/python3.10/site-packages/psutil/__init__.py", line 681, in cmdline
        return self._proc.cmdline()
      File "/opt/anaconda3/envs/trust/lib/python3.10/site-packages/psutil/_psosx.py", line 351, in wrapper
        raise NoSuchProcess(self.pid, self._name)
    psutil.NoSuchProcess: process no longer exists (pid=3629, name='login')
    

    When I looked online about similar errors, some suggested it was caused by a zombie process. So I tried to kill the process and the terminal session closed.

    I'm not sure if this is a bug or a feature, but this only happened when I updated psutil from 5.6.7 to 5.9.4

    bug macos 
    opened by tamruta 8
  • [Linux] psutil.disk_partitions(all=False) print non physical disk

    [Linux] psutil.disk_partitions(all=False) print non physical disk

    Summary

    • OS: { Linux version 3.10.0-957 (Red Hat 4.8.5-36) }
    • Architecture: { 64bit,}
    • Psutil version: { 5.9.4 }
    • Python version: { Python 3.6.8 }
    • Type: { performance }

    Description

    When I use the following code to output a physical disk info, but the results include non physical disks. I don't want this result evice='/dev/sda', mountpoint='/ftp/share/test. Is that a bug? If not, what should I do?

    for part in psutil.disk_partitions(all=False):
        print("------------part------------")
        print(part)
    

    output:

    ------------part------------
    sdiskpart(device='/dev/mapper/centos-root', mountpoint='/', fstype='xfs', opts='rw,relatime,attr2,inode64,noquota', maxfile=255, maxpath=4096)
    ------------part------------
    sdiskpart(device='/dev/sdb2', mountpoint='/boot', fstype='xfs', opts='rw,relatime,attr2,inode64,noquota', maxfile=255, maxpath=4096)
    ------------part------------
    sdiskpart(device='/dev/sdb1', mountpoint='/boot/efi', fstype='vfat', opts='rw,relatime,fmask=0077,dmask=0077,codepage=437,iocharset=ascii,shortname=winnt,errors=remount-ro', maxfile=1530, maxpath=4096)
    ------------part------------
    sdiskpart(device='/dev/mapper/centos-home', mountpoint='/home', fstype='xfs', opts='rw,relatime,attr2,inode64,noquota', maxfile=255, maxpath=4096)
    ------------part------------
    sdiskpart(device='/dev/mapper/centos-root', mountpoint='/ftp/share/myshare', fstype='xfs', opts='rw,relatime,attr2,inode64,noquota', maxfile=255, maxpath=4096)
    ------------part------------
    sdiskpart(device='/dev/mapper/centos-root', mountpoint='/ftp/anonymous/myshare', fstype='xfs', opts='rw,relatime,attr2,inode64,noquota', maxfile=255, maxpath=4096)
    ------------part------------
    sdiskpart(device='/dev/sda', mountpoint='/data/3a9eaef9-66a8-423f-b136-53cd66c85086', fstype='xfs', opts='rw,relatime,attr2,inode64,logbsize=256k,sunit=512,swidth=512,prjquota', maxfile=255, maxpath=4096)
    ------------part------------
    sdiskpart(device='/dev/sda', mountpoint='/test', fstype='xfs', opts='rw,relatime,attr2,inode64,logbsize=256k,sunit=512,swidth=512,prjquota', maxfile=255, maxpath=4096)
    ------------part------------
    sdiskpart(device='/dev/sda', mountpoint='/ftp/anonymous/test', fstype='xfs', opts='rw,relatime,attr2,inode64,logbsize=256k,sunit=512,swidth=512,prjquota', maxfile=255, maxpath=4096)
    ------------part------------
    sdiskpart(device='/dev/sda', mountpoint='/ftp/share/test', fstype='xfs', opts='rw,relatime,attr2,inode64,logbsize=256k,sunit=512,swidth=512,prjquota', maxfile=255, maxpath=4096)
    
    
    bug linux performance 
    opened by L-Super 0
  • [freeBSD] net_connections do not see NFS connections

    [freeBSD] net_connections do not see NFS connections

    Summary

    • OS: { FreeBSD 12.2-RELEASE-p11 }
    • Architecture: { 64bit }
    • Psutil version: { 5.8.0 }
    • Python version: { Python 3.8.12 }
    • Type: { core }

    Description

    Running net_connections do not retrieve ESTABLISHED NFS connections (port 2049) on freeBSD Some NFS connections are ESTABLISHED:

    root@serv #netstat -an | grep 2049
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.748       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.913       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.895       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.805       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.688       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.959       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.784       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.687       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.1005      ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.931       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.774       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.667       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.929       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.976       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.711       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.992       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.674       ESTABLISHED
    tcp6       0      0 *.2049                 *.*          LISTEN     
    tcp4       0      0 *.2049                 *.*          LISTEN     
    udp6       0      0 *.2049                 *.*                    
    udp4       0      0 *.2049                 *.*   
    

    But on psutil side, none are visible:

    >>> pprint([ (sconn.laddr, sconn.radrr) for sconn in psutil.net_connections() if sconn.status == "ESTABLISHED" and sconn.laddr.port == 2049])
    []
    >>> pprint([ sconn for sconn in psutil.net_connections() if sconn.laddr.port == 2049])
    [
        sconn(fd=5, family=<AddressFamily.AF_INET: 2>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='0.0.0.0', port=2049), raddr=(), status='LISTEN', pid=42281),
        sconn(fd=6, family=<AddressFamily.AF_INET6: 28>, type=<SocketKind.SOCK_STREAM: 1>, laddr=addr(ip='::', port=2049), raddr=(), status='LISTEN', pid=42281)
    ]
    

    However, using subprocess to run a netstat command, the NFS connections are listed

    root@serv #cat test-netstat.py 
    #!/usr/bin/env python3
    # coding:utf-8
    import subprocess
    p = subprocess.Popen(["netstat", "-an"], stdout=subprocess.PIPE)
    out = p.stdout.read().decode("utf8").split("\n")
    nfs_conn = [line for line in out if ".2049" in line]
    for line in nfs_conn:
        print(line)
    
    root@serv # ./test-netstat.py 
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.748       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.913       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.895       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.805       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.688       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.959       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.784       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.687       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.1005      ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.931       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.774       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.667       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.929       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.976       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.711       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.992       ESTABLISHED
    tcp4       0      0 x.x.x.x.2049      x.x.x.x.674       ESTABLISHED
    tcp6       0      0 *.2049                 *.*          LISTEN     
    tcp4       0      0 *.2049                 *.*          LISTEN     
    udp6       0      0 *.2049                 *.*                    
    udp4       0      0 *.2049                 *.*   
    
    bug freebsd 
    opened by fzindovh 3
  • [ubuntu-20.04] build problem

    [ubuntu-20.04] build problem

    Summary

    • OS: ubuntu-20.04.5, with nix package manager
    • Architecture: 64bit
    • Psutil version: psutil-5.9.4
    • Python version: python3.11
    • Type: tests, build problem

    Description

    I am not using psutil directly. It's a dependency and it needs to be built from sources. I am using nix package manager to setup the environment.

    The problem is that a build process for psutil fails during test phase, see the error output below. It looks like it is expecting something regarding the swap status. I have started to troubleshoot the problem. It failed to build twice in a row, but it was OK on the third run with no obvious reason. So I don't really have a reproducible problem. I've captured the output when it failed.

    I am not sure if there is some problem with my swap. However, it does not look right to fail the test in this case. It is more like a precondition of the test not being met, rater than a test error. It prevents me to build/install the package. Maybe it would be better to skip this test step if something like this is detected?

    Build trace:

    $ nix-shell
    ...
    
    ============================= test session starts ==============================
    platform linux -- Python 3.11.1, pytest-7.1.3, pluggy-1.0.0
    rootdir: /nix/store/vwflfg5a5kmmz63xann8gzba4hxks250-python3.11-psutil-5.9.4/lib/python3.11/site-packages/psutil/tests
    collected 44 items / 13 deselected / 31 selected
    
    ../../nix/store/vwflfg5a5kmmz63xann8gzba4hxks250-python3.11-psutil-5.9.4/lib/python3.11/site-packages/psutil/tests/test_system.py . [  3%]
    ........F...........s.........                                           [100%]
    
    =================================== FAILURES ===================================
    _______________________ TestMemoryAPIs.test_swap_memory ________________________
    
    self = <psutil.tests.test_system.TestMemoryAPIs testMethod=test_swap_memory>
    
        def test_swap_memory(self):
            mem = psutil.swap_memory()
            self.assertEqual(
                mem._fields, ('total', 'used', 'free', 'percent', 'sin', 'sout'))
    
            assert mem.total >= 0, mem
            assert mem.used >= 0, mem
            if mem.total > 0:
                # likely a system with no swap partition
    >           assert mem.free > 0, mem
    E           AssertionError: sswap(total=1023406080, used=1023406080, free=0, percent=100.0, sin=709828608, sout=2367311872)
    E           assert 0 > 0
    E            +  where 0 = sswap(total=1023406080, used=1023406080, free=0, percent=100.0, sin=709828608, sout=2367311872).free
    
    /nix/store/vwflfg5a5kmmz63xann8gzba4hxks250-python3.11-psutil-5.9.4/lib/python3.11/site-packages/psutil/tests/test_system.py:298: AssertionError
    =========================== short test summary info ============================
    FAILED ../../nix/store/vwflfg5a5kmmz63xann8gzba4hxks250-python3.11-psutil-5.9.4/lib/python3.11/site-packages/psutil/tests/test_system.py::TestMemoryAPIs::test_swap_memory
    ============ 1 failed, 29 passed, 1 skipped, 13 deselected in 0.50s ============
    error: builder for '/nix/store/3sb80p5fhy6sppcxnvrif3755k9icfiw-python3.11-psutil-5.9.4.drv' failed with exit code 1
    error: 1 dependencies of derivation '/nix/store/hns020xb3sbvvrwk1sz958gpiys3wz42-python3.11-pytest-xdist-2.5.0.drv' failed to build
    error: 1 dependencies of derivation '/nix/store/ydp3ra3winrawf19cv4k1fcx6sp6zs8f-python3.11-hypothesis-6.54.5.drv' failed to build
    error: 1 dependencies of derivation '/nix/store/dk5n5nsm2i7ifrhhkhvkz3kc7wbm7isd-python3-3.11.1-env.drv' failed to build
    

    Memory status:

    $ free -h
                  total        used        free      shared  buff/cache   available
    Mem:           15Gi       9,3Gi       3,1Gi       499Mi       3,1Gi       5,5Gi
    Swap:         975Mi       975Mi       0,0Ki
    

    This is the simplified version of the shell.nix file in use, but I can not reproduce the problem easily.

    { pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/0e50858afba8396af18f2a2681208a1e5835a83c.tar.gz") {}
    }:
    
    pkgs.stdenv.mkDerivation {
      name = "env";
      buildInputs = [pkgs.python311Packages.psutil];
    }
    
    bug tests linux 
    opened by zoranbosnjak 0
  • [Windows] - psutil does not report the correct path via `Process.exe()` if a mounted volume is in the path.

    [Windows] - psutil does not report the correct path via `Process.exe()` if a mounted volume is in the path.

    Summary

    • OS: Windows Server 2019
    • Architecture: 64 Bit
    • Psutil version: 5.9.4
    • Python version: 3.11.0
    • Type: core

    Description

    psutil.Process.exe() is not reporting the correct path to a process.

    The situation is the following: There are multiple processes running in C:\test\my-program\tomcat\bin\tomcat.exe. Moreover, there are multiple versions of my-program (e.g. my-program1.0, my-program1.1, ...). Additionally, test is a mounted volume and provides the following information:

     Directory of C:\
    04/12/2022  07:04 PM    <DIR>               home
    03/23/2022  01:50 PM    <JUNCTION>          test [\??\Volume{e4a9240d-9878-4c6c-8594-00fea2f7af11}\]
    11/16/2022  02:02 PM    <DIR>               Program Files
    04/12/2022  03:30 PM    <DIR>               Program Files (x86)
    ...
    

    In my script, I am checking if processes of my-program are running. For that, I am interested in the PID and the location of the Process (psutil.Process.exe()). Gathering this information is done via psutil.process_iter(). The processes are listed correctly (PID). However, the problem is psutil.Process.exe().

    When calling psutil.Process.exe() I would expect, that the returned path to the executable is C:\test\my-program1.0\tomcat\bin\tomcat.exe, which it is if test is not a mounted volume. However, if test is a mounted volume, it returns \my-program1.0\tomcat\bin\tomcat.exe. Meaning that the part C:\test is missing.

    I guess this problem is somewhat related to #775, but might be slightly different.

    Additional Info on the Mounted Volume

    According to Windows, the drive is of Type Mounted Volume. However, the type switches to Local Disk if I look at the properties of the Volume. The File System inside the Volume is NTFS.

    bug windows 
    opened by mratzenb 0
  • Use GitHub Actions concurrency control

    Use GitHub Actions concurrency control

    Summary

    • OS: n/a
    • Bug fix: no
    • Type: wheels
    • Fixes:

    Description

    https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#concurrency

    GitHub Actions offers their own built in concurrency control. This is just an offer in case you are interested in switching over. It includes the commit hash in the 'concurrency group' if the run is for the default branch so that the default branch builds do not get cancelled.

    opened by altendky 0
Owner
Giampaolo Rodola
Python enthusiast and core developer, author of psutil and pyftpdlib python libs
Giampaolo Rodola
More than 130 check plugins for Icinga and other Nagios-compatible monitoring applications. Each plugin is a standalone command line tool (written in Python) that provides a specific type of check.

Python-based Monitoring Check Plugins Collection This Enterprise Class Check Plugin Collection offers a package of more than 130 Python-based, Nagios-

Linuxfabrik 119 Dec 27, 2022
A cron monitoring tool written in Python & Django

Healthchecks Healthchecks is a cron job monitoring service. It listens for HTTP requests and email messages ("pings") from your cron jobs and schedule

Healthchecks 5.8k Jan 2, 2023
Flexible and scalable monitoring framework

Presentation of the Shinken project Welcome to the Shinken project. Shinken is a modern, Nagios compatible monitoring framework, written in Python. It

Gabès Jean 1.1k Dec 18, 2022
Apache Airflow - A platform to programmatically author, schedule, and monitor workflows

Apache Airflow Apache Airflow (or simply Airflow) is a platform to programmatically author, schedule, and monitor workflows. When workflows are define

The Apache Software Foundation 28.6k Jan 1, 2023
Spinnaker is an open source, multi-cloud continuous delivery platform for releasing software changes with high velocity and confidence.

Welcome to the Spinnaker Project Spinnaker is an open-source continuous delivery platform for releasing software changes with high velocity and confid

null 8.8k Jan 7, 2023
Bitnami Docker Image for Python using snapshots for the system packages repositories

Python Snapshot packaged by Bitnami What is Python Snapshot? Python is a programming language that lets you work quickly and integrate systems more ef

Bitnami 1 Jan 13, 2022
Glances an Eye on your system. A top/htop alternative for GNU/Linux, BSD, Mac OS and Windows operating systems.

Glances - An eye on your system Summary Glances is a cross-platform monitoring tool which aims to present a large amount of monitoring information thr

Nicolas Hennion 22k Jan 8, 2023
Ralph is the CMDB / Asset Management system for data center and back office hardware.

Ralph Ralph is full-featured Asset Management, DCIM and CMDB system for data centers and back offices. Features: keep track of assets purchases and th

Allegro Tech 1.9k Jan 1, 2023
A honey token manager and alert system for AWS.

SpaceSiren SpaceSiren is a honey token manager and alert system for AWS. With this fully serverless application, you can create and manage honey token

null 287 Nov 9, 2022
DC/OS - The Datacenter Operating System

DC/OS - The Datacenter Operating System The easiest way to run microservices, big data, and containers in production. What is DC/OS? Like traditional

DC/OS 2.3k Jan 6, 2023
A system for managing CI data for Mozilla projects

Treeherder Description Treeherder is a reporting dashboard for Mozilla checkins. It allows users to see the results of automatic builds and their resp

Mozilla 235 Dec 22, 2022
Python IMDB Docker - A docker tutorial to containerize a python script.

Python_IMDB_Docker A docker tutorial to containerize a python script. Build the docker in the current directory: docker build -t python-imdb . Run the

Sarthak Babbar 1 Dec 30, 2021
Python utility function to communicate with a subprocess using iterables: for when data is too big to fit in memory and has to be streamed

iterable-subprocess Python utility function to communicate with a subprocess using iterables: for when data is too big to fit in memory and has to be

Department for International Trade 5 Jul 10, 2022
Build and Push docker image in Python (luigi + docker-py)

Docker build images workflow in Python Since docker hub stopped building images for free accounts, I've been looking for another way to do it. I could

Fabien D. 2 Dec 15, 2022
Jenkins-AWS-CICD - Implement Jenkins CI/CD with AWS CodeBuild and AWS CodeDeploy, build a python flask web application.

Jenkins-AWS-CICD - Implement Jenkins CI/CD with AWS CodeBuild and AWS CodeDeploy, build a python flask web application.

Ning 1 Jan 1, 2022
DAMPP (gui) is a Python based program to run simple webservers using MySQL, Php, Apache and PhpMyAdmin inside of Docker containers.

DAMPP (gui) is a Python based program to run simple webservers using MySQL, Php, Apache and PhpMyAdmin inside of Docker containers.

Sehan Weerasekara 1 Feb 19, 2022
A tool to convert AWS EC2 instances back and forth between On-Demand and Spot billing models.

ec2-spot-converter This tool converts existing AWS EC2 instances back and forth between On-Demand and 'persistent' Spot billing models while preservin

jcjorel 152 Dec 29, 2022
Iris is a highly configurable and flexible service for paging and messaging.

Iris Iris core, API, UI and sender service. For third-party integration support, see iris-relay, a stateless proxy designed to sit at the edge of a pr

LinkedIn 715 Dec 28, 2022