add filter status to ps module to get a list of processes according to their state

This commit is contained in:
Dernat Rémy 2022-01-05 16:09:16 +01:00 committed by Megan Wilhite
parent c258121e2c
commit 7662369074
2 changed files with 30 additions and 0 deletions

1
changelog/61420.added Normal file
View file

@ -0,0 +1 @@
add a filter function to ps module to get a list of processes on a minion according to their state.

View file

@ -772,3 +772,32 @@ def psaux(name):
ret = []
ret.extend([sanitize_name, found_infos, pid_count])
return ret
def status(filter):
"""
Returns a list of processes according to their states.
See https://psutil.readthedocs.io/en/latest/index.html\
?highlight=status#process-status-constants
CLI Example:
.. code-block:: bash
salt '*' ps.status [running, idle, stopped, sleeping, dead, \
zombie, ...]
"""
ret = []
if filter is not None and filter != "":
sanitize_name = str(filter)
try:
ret = [
proc.info
for proc in psutil.process_iter(["pid", "name", "status"])
if proc.info["status"] == sanitize_name
]
except psutil.AccessDenied:
pass
for i in ret:
i.pop("status")
return ret