Skip to content

Commit cd9ca5e

Browse files
committed
feat: count number of PostgreSQL pid files
1 parent 839429f commit cd9ca5e

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

mamonsu/plugins/pgsql/connections.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,14 @@ class Connections(Plugin):
2323
'number of disabled',
2424
'00CCCC')
2525
]
26+
# ( key, name, graph)
27+
Item_ppid_children = [
28+
('pgsql.count_all_pids{0}',
29+
'Number of PostgreSQL parent pid children',
30+
('PostgreSQL: count children of PostgreSQL parent pid', 'BBB000', 0)),
31+
]
2632
Max_connections = None
33+
2734
query_agent = "select count(*) from pg_catalog.pg_stat_activity where state = '{0}';"
2835
query_agent_total = "select count(*) from pg_catalog.pg_stat_activity where state is not null ;"
2936
query_agent_waiting_new_v = "select count(*) from pg_catalog.pg_stat_activity where state is not null and " \
@@ -74,6 +81,11 @@ def run(self, zbx):
7481
self.Max_connections = result[0][0]
7582
zbx.send('pgsql.connections[max_connections]', int(self.Max_connections))
7683

84+
# get number of child pids of ppid
85+
num_of_children_pids = self.get_num_of_children_pids()
86+
key = self.Item_ppid_children[0][0].format('[]')
87+
zbx.send(key, num_of_children_pids+1)
88+
7789
def items(self, template):
7890
result = template.item({
7991
'name': 'PostgreSQL: number of total connections',
@@ -97,6 +109,11 @@ def items(self, template):
97109
'key': self.right_type(self.key, item[1]),
98110
'delay': self.plugin_config('interval')
99111
})
112+
result += template.item({
113+
'name': 'PostgreSQL: number of child pids',
114+
'key': self.right_type(self.Item_ppid_children[0][0]),
115+
'delay': self.plugin_config('interval')
116+
})
100117
return result
101118

102119
def graphs(self, template):
@@ -118,6 +135,10 @@ def graphs(self, template):
118135
'key': self.right_type(self.key, "max_connections"),
119136
'color': '00BB00'
120137
})
138+
items.append({
139+
'key': self.right_type(self.Item_ppid_children[0][0]),
140+
'color': '0BB000'
141+
})
121142
graph = {'name': 'PostgreSQL connections', 'items': items}
122143
return template.graph(graph)
123144

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)