|
1 | 1 | from mamonsu.lib.plugin import Plugin, PluginDisableException
|
2 | 2 | from .pool import Pooler
|
| 3 | +import subprocess |
3 | 4 |
|
4 | 5 |
|
5 | 6 | class PgsqlPlugin(Plugin):
|
@@ -36,19 +37,24 @@ def check(self, extension):
|
36 | 37 | @staticmethod
|
37 | 38 | def get_num_of_children_pids():
|
38 | 39 | 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] |
46 | 41 | 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 |
52 | 58 |
|
53 | 59 | def disable_and_exit_if_extension_is_not_installed(self, ext, db=None):
|
54 | 60 | if not self.extension_installed(ext, db=db, silent=True):
|
|
0 commit comments