Skip to content

Commit e254d30

Browse files
committed
refactor: use bash commands to get parent pid and children pids instead of psutil
1 parent d223cf2 commit e254d30

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

mamonsu/plugins/pgsql/plugin.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from mamonsu.lib.plugin import Plugin, PluginDisableException
22
from .pool import Pooler
3+
import subprocess
34

45

56
class PgsqlPlugin(Plugin):
@@ -36,19 +37,24 @@ def check(self, extension):
3637
@staticmethod
3738
def get_num_of_children_pids():
3839
result = Pooler.query("SELECT pg_backend_pid();")
39-
pid = result[0][0]
40-
with open('/proc/{pid}/status'.format(pid=int(pid)), 'r') as f:
41-
for line in f:
42-
data = line.split()
43-
if data[0] == "PPid:":
44-
ppid = data[1]
45-
import psutil
40+
child_pid = result[0][0]
4641
try:
47-
parent = psutil.Process(int(ppid))
48-
except psutil.NoSuchProcess:
49-
raise PluginDisableException("Unable to get parent process using psutil lib.")
50-
children = parent.children(recursive=True)
51-
return len(children)
42+
parent_pid = subprocess.check_output(['ps', '-oppid', '--no-headers', '--pid', str(child_pid)],
43+
stderr=subprocess.PIPE, encoding='utf8')
44+
except subprocess.CalledProcessError:
45+
raise PluginDisableException("Unable to get parent process for {0} pid.".format(child_pid))
46+
47+
parent_pid = int(parent_pid.split("\n")[0])
48+
try:
49+
child_pids = subprocess.check_output(['ps', '-opid', '--no-headers', '--ppid', str(parent_pid)],
50+
stderr=subprocess.PIPE, encoding='utf8')
51+
except subprocess.CalledProcessError:
52+
raise PluginDisableException("Unable to get children processes for {0} pid.".format(child_pid))
53+
# we want to return number of ALL PostgreSQL processes meaning parent pid + count(children pids)
54+
# len of the splitted result has extra blank line in the end so we return just the length of
55+
# splitted child pids output
56+
count_child_pids = len(child_pids.split("\n"))
57+
return count_child_pids
5258

5359
def disable_and_exit_if_extension_is_not_installed(self, ext, db=None):
5460
if not self.extension_installed(ext, db=db, silent=True):

0 commit comments

Comments
 (0)