mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Support older version of psutil (RHEL7 and RHEL6) (#2808)
* Support older version of psutil (RHEL7 and RHEL6) The psutil python module is a true mess, they changed the API twice. The function arguments, as well as the objects that are returned. The documentation does not make it clear which version supports what so the safest implementation is this waterfall approach. A better approach would be to inspect the returned information, rather than trust a version, but that would not be any more efficient. In the end it is better to have something that at least works out-of-the-box on all platforms than something that requires custom updates to system packages before it works as expected. Especially for something as basic as `pids`. * A little bit more concise * Apply suggestions from code review * Add changelog fragment. Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
1705335ba7
commit
b5d6457611
2 changed files with 16 additions and 5 deletions
2
changelogs/fragments/2808-pids-older-psutil.yml
Normal file
2
changelogs/fragments/2808-pids-older-psutil.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- "pids - avoid crashes for older ``psutil`` versions, like on RHEL6 and RHEL7 (https://github.com/ansible-collections/community.general/pull/2808)."
|
|
@ -79,11 +79,20 @@ def compare_lower(a, b):
|
||||||
def get_pid(name):
|
def get_pid(name):
|
||||||
pids = []
|
pids = []
|
||||||
|
|
||||||
|
try:
|
||||||
for proc in psutil.process_iter(attrs=['name', 'cmdline']):
|
for proc in psutil.process_iter(attrs=['name', 'cmdline']):
|
||||||
if compare_lower(proc.info['name'], name) or \
|
if compare_lower(proc.info['name'], name) or \
|
||||||
proc.info['cmdline'] and compare_lower(proc.info['cmdline'][0], name):
|
proc.info['cmdline'] and compare_lower(proc.info['cmdline'][0], name):
|
||||||
pids.append(proc.pid)
|
pids.append(proc.pid)
|
||||||
|
except TypeError: # EL6, EL7: process_iter() takes no arguments (1 given)
|
||||||
|
for proc in psutil.process_iter():
|
||||||
|
try: # EL7
|
||||||
|
proc_name, proc_cmdline = proc.name(), proc.cmdline()
|
||||||
|
except TypeError: # EL6: 'str' object is not callable
|
||||||
|
proc_name, proc_cmdline = proc.name, proc.cmdline
|
||||||
|
if compare_lower(proc_name, name) or \
|
||||||
|
proc_cmdline and compare_lower(proc_cmdline[0], name):
|
||||||
|
pids.append(proc.pid)
|
||||||
return pids
|
return pids
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue