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
This commit is contained in:
Hemachandra Reddy 2019-10-07 21:22:41 +00:00
parent 21ad4a9fdb
commit 3ba23f7ab0
2 changed files with 38 additions and 36 deletions

View File

@ -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:

View File

@ -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: