Skip to content

Commit 62dd230

Browse files
committed
Merge branch 'PGPRO-4610' into dev
2 parents 35efed7 + 420b4da commit 62dd230

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

mamonsu/plugins/pgsql/connections.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from mamonsu.plugins.pgsql.plugin import PgsqlPlugin as Plugin
44
from distutils.version import LooseVersion
5+
import mamonsu.lib.platform as platform
56
from .pool import Pooler
67

78

@@ -23,7 +24,14 @@ class Connections(Plugin):
2324
'number of disabled',
2425
'00CCCC')
2526
]
27+
# ( key, name, graph)
28+
Item_ppid_children = [
29+
('pgsql.count_all_pids{0}',
30+
'Number of PostgreSQL parent pid children',
31+
('PostgreSQL: count children of PostgreSQL parent pid', 'BBB000', 0)),
32+
]
2633
Max_connections = None
34+
2735
query_agent = "select count(*) from pg_catalog.pg_stat_activity where state = '{0}';"
2836
query_agent_total = "select count(*) from pg_catalog.pg_stat_activity where state is not null ;"
2937
query_agent_waiting_new_v = "select count(*) from pg_catalog.pg_stat_activity where state is not null and " \
@@ -74,6 +82,12 @@ def run(self, zbx):
7482
self.Max_connections = result[0][0]
7583
zbx.send('pgsql.connections[max_connections]', int(self.Max_connections))
7684

85+
# get number of child pids of ppid
86+
if platform.LINUX:
87+
num_of_children_pids = self.get_num_of_children_pids()
88+
key = self.Item_ppid_children[0][0].format('[]')
89+
zbx.send(key, num_of_children_pids+1)
90+
7791
def items(self, template):
7892
result = template.item({
7993
'name': 'PostgreSQL: number of total connections',
@@ -97,6 +111,11 @@ def items(self, template):
97111
'key': self.right_type(self.key, item[1]),
98112
'delay': self.plugin_config('interval')
99113
})
114+
result += template.item({
115+
'name': 'PostgreSQL: number of child pids',
116+
'key': self.right_type(self.Item_ppid_children[0][0]),
117+
'delay': self.plugin_config('interval')
118+
})
100119
return result
101120

102121
def graphs(self, template):
@@ -118,6 +137,10 @@ def graphs(self, template):
118137
'key': self.right_type(self.key, "max_connections"),
119138
'color': '00BB00'
120139
})
140+
items.append({
141+
'key': self.right_type(self.Item_ppid_children[0][0]),
142+
'color': '0BB000'
143+
})
121144
graph = {'name': 'PostgreSQL connections', 'items': items}
122145
return template.graph(graph)
123146

mamonsu/plugins/pgsql/plugin.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import psutil
12
from mamonsu.lib.plugin import Plugin, PluginDisableException
23
from .pool import Pooler
34

@@ -33,6 +34,22 @@ def check(self, extension):
3334

3435
return self._ext_installed
3536

37+
@staticmethod
38+
def get_num_of_children_pids():
39+
result = Pooler.query("SELECT pg_backend_pid();")
40+
pid = result[0][0]
41+
with open('/proc/{pid}/status'.format(pid=int(pid)), 'r') as f:
42+
for line in f:
43+
data = line.split()
44+
if data[0] == "PPid:":
45+
ppid = data[1]
46+
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)
52+
3653
def disable_and_exit_if_extension_is_not_installed(self, ext, db=None):
3754
if not self.extension_installed(ext, db=db, silent=True):
3855
self.disable()

0 commit comments

Comments
 (0)