From 3ba23f7ab01d306ff9ed9bef790c64c6ead15a21 Mon Sep 17 00:00:00 2001 From: Hemachandra Reddy Date: Mon, 7 Oct 2019 21:22:41 +0000 Subject: [PATCH] Fix psutil inconsistencies Python psutil library has not been consistent in behavior a. gives trucated process names at times b. the truncated names sometimes contain path to Python instead of the program name Python runs Change-Id: I99b77a4c28761a2187e59be4e562d5893ef3caa9 --- neutron/templates/bin/_health-probe.py.tpl | 37 +++++++++++----------- nova/templates/bin/_health-probe.py.tpl | 37 +++++++++++----------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/neutron/templates/bin/_health-probe.py.tpl b/neutron/templates/bin/_health-probe.py.tpl index f242b8b3a6..d5cfbae8cf 100644 --- a/neutron/templates/bin/_health-probe.py.tpl +++ b/neutron/templates/bin/_health-probe.py.tpl @@ -148,25 +148,26 @@ def tcp_socket_state_check(agentq): rabbitmq_ports = get_rabbitmq_ports() - for pr in psutil.pids(): + for p in psutil.process_iter(): try: - p = psutil.Process(pr) - if p.name() == proc: - if parentId == 0: - parentId = p.pid - else: - if p.ppid() == parentId: - continue - pcon = p.connections() - for con in pcon: - try: - port = con.raddr[1] - status = con.status - except IndexError: - continue - if port in rabbitmq_ports and status == tcp_established: - rabbit_sock_count = rabbit_sock_count + 1 - except psutil.NoSuchProcess: + with p.oneshot(): + if proc in " ".join(p.cmdline()): + if parentId == 0: + parentId = p.pid + else: + if p.ppid() == parentId: + continue + pcon = p.connections() + for con in pcon: + try: + port = con.raddr[1] + status = con.status + except IndexError: + continue + if port in rabbitmq_ports and\ + status == tcp_established: + rabbit_sock_count = rabbit_sock_count + 1 + except psutil.Error: continue if rabbit_sock_count == 0: diff --git a/nova/templates/bin/_health-probe.py.tpl b/nova/templates/bin/_health-probe.py.tpl index b0c1095fe4..423326361f 100644 --- a/nova/templates/bin/_health-probe.py.tpl +++ b/nova/templates/bin/_health-probe.py.tpl @@ -90,25 +90,26 @@ def tcp_socket_status(process, ports): """Check the tcp socket status on a process""" sock_count = 0 parentId = 0 - for pr in psutil.pids(): + for p in psutil.process_iter(): try: - p = psutil.Process(pr) - if p.name() == process: - if parentId == 0: - parentId = p.pid - else: - if p.ppid() == parentId and not cfg.CONF.check_all_pids: - continue - pcon = p.connections() - for con in pcon: - try: - rport = con.raddr[1] - status = con.status - except IndexError: - continue - if rport in ports and status == tcp_established: - sock_count = sock_count + 1 - except psutil.NoSuchProcess: + with p.oneshot(): + if process in " ".join(p.cmdline()): + if parentId == 0: + parentId = p.pid + else: + if p.ppid() == parentId and \ + not cfg.CONF.check_all_pids: + continue + pcon = p.connections() + for con in pcon: + try: + rport = con.raddr[1] + status = con.status + except IndexError: + continue + if rport in ports and status == tcp_established: + sock_count = sock_count + 1 + except psutil.Error: continue if sock_count == 0: