Skip to content

Commit 80bd6d2

Browse files
committed
add helpers, add 3d-party plugins to readme, add license file
1 parent c8679be commit 80bd6d2

File tree

4 files changed

+96
-30
lines changed

4 files changed

+96
-30
lines changed

LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2016, vadv
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
* Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,12 @@ See the `examples <https://github.com/postgrespro/mamonsu/tree/master/examples>`
149149

150150
After add new plugin, you must to reexport template and import this file to zabbix.
151151

152+
=========
153+
3rd-party
154+
=========
155+
156+
* `repo mamonsu-plugins <https://github.com/tarabanton/mamonsu-plugins>`_ for skytools, nginx, rabbitmq, uwsgi, gdnsd.
157+
152158
====
153159
Run
154160
====

mamonsu/plugins/pgsql/driver/pool.py

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ class Pool(ConnectionInfo):
4141
def __init__(self):
4242
super(Pool, self).__init__()
4343
self.all_connections = {}
44-
self._server_version = {}
45-
self._mamonsu_bootstrap = {}
46-
self._mamonsu_bootstrap_cache = 0
47-
self._in_recovery = {}
48-
self._in_recovery_cache = 0
44+
self._cache = {
45+
'server_version': {'storage': {}},
46+
'bootstrap': {'storage': {}, 'counter': 0, 'cache': 10},
47+
'recovery': {'storage': {}, 'counter': 0, 'cache': 10},
48+
'pgpro': {'storage': {}},
49+
'pgproee': {'storage': {}}
50+
}
4951

5052
def connection_string(self, db=None):
5153
self._init_conn_(db)
@@ -58,46 +60,69 @@ def query(self, query, db=None):
5860
return self.all_connections[db].query(query)
5961

6062
def server_version(self, db=None):
61-
if db in self._server_version:
62-
return self._server_version[db]
63+
if db in self._cache['server_version']['storage']:
64+
return self._cache['server_version']['storage'][db]
6365
if platform.PY2:
6466
result = self.query('show server_version', db)[0][0]
6567
elif platform.PY3:
6668
result = bytes(
6769
self.query('show server_version', db)[0][0], 'utf-8')
68-
self._server_version[db] = "{0}".format(result.decode('ascii'))
69-
return self._server_version[db]
70-
71-
def in_recovery(self, db=None):
72-
if (db in self._in_recovery) and (self._in_recovery_cache < 10):
73-
self._in_recovery_cache += 1
74-
return self._in_recovery[db]
75-
self._in_recovery_cache = 0
76-
self._in_recovery[db] = self.query(
77-
"select pg_catalog.pg_is_in_recovery()")[0][0]
78-
return self._in_recovery[db]
70+
self._cache['server_version']['storage'][db] = '{0}'.format(
71+
result.decode('ascii'))
72+
return self.self._cache['server_version']['storage'][db]
7973

8074
def server_version_greater(self, version, db=None):
8175
return self.server_version(db) >= LooseVersion(version)
8276

8377
def server_version_less(self, version, db=None):
8478
return self.server_version(db) <= LooseVersion(version)
8579

86-
def mamonsu_bootstrap(self, db=None):
87-
if (db in self._mamonsu_bootstrap) and (self._mamonsu_bootstrap_cache < 10):
88-
self._mamonsu_bootstrap_cache += 1
89-
return self._mamonsu_bootstrap[db]
90-
self._mamonsu_bootstrap_cache = 0
80+
def in_recovery(self, db=None):
81+
if db in self._cache['recovery']['storage']:
82+
if self._cache['recovery']['counter'] < self._cache['recovery']['cache']:
83+
self._cache['recovery']['counter'] += 1
84+
return self._cache['recovery']['storage'][db]
85+
self._cache['recovery']['counter'] = 0
86+
self._cache['recovery']['storage'][db] = self.query(
87+
"select pg_catalog.pg_is_in_recovery()")[0][0]
88+
return self._cache['recovery']['storage'][db]
89+
90+
def is_bootstraped(self, db=None):
91+
if db in self._cache['bootstrap']['storage']:
92+
if self.cache['bootstrap']['counter'] < self._cache['bootstrap']['cache']:
93+
self.cache['bootstrap']['counter'] += 1
94+
return self._cache['bootstrap']['storage'][db]
95+
self._cache['bootstrap']['counter'] = 0
9196
sql = """select count(*) from pg_catalog.pg_class
9297
where relname = 'mamonsu_config'"""
9398
result = int(self.query(sql, db)[0][0])
94-
self._mamonsu_bootstrap[db] = (result == 1)
95-
if self._mamonsu_bootstrap[db]:
96-
self.all_connections[db].log.info("Found mamonsu bootstrap")
99+
self._cache['bootstrap']['storage'][db] = (result == 1)
100+
if self._cache['bootstrap']['storage'][db]:
101+
self.all_connections[db].log.info('Found mamonsu bootstrap')
97102
else:
98-
self.all_connections[db].log.info("Can't found mamonsu bootstrap")
99-
self.all_connections[db].log.info("hint: run `mamonsu bootstrap` if you want to run without superuser rights")
100-
return self._mamonsu_bootstrap[db]
103+
self.all_connections[db].log.info('Can\'t found mamonsu bootstrap')
104+
self.all_connections[db].log.info('hint: run `mamonsu bootstrap` if you want to run without superuser rights')
105+
return self._cache['bootstrap']['storage'][db]
106+
107+
def is_pgpro(self, db=None):
108+
if db in self._cache['pgpro']:
109+
return self._cache['pgpro'][db]
110+
try:
111+
self.query('select pgpro_version()')
112+
self._cache['pgpro'][db] = True
113+
except:
114+
self._cache['pgpro'][db] = False
115+
return self._cache['pgpro'][db]
116+
117+
def is_pgpro_ee(self, db=None):
118+
if not self.is_pgpro(self, db):
119+
return False
120+
if db in self._cache['pgproee']:
121+
return self._cache['pgproee'][db]
122+
self._cache['pgproee'][db] = (
123+
self.query('select pgpro_edition()')[0][0].lower() == 'enterprise'
124+
)
125+
return self._cache['pgproee'][db]
101126

102127
def extension_installed(self, ext, db=None):
103128
result = self.query('select count(*) from pg_catalog.pg_extension\
@@ -116,7 +141,7 @@ def get_sql(self, typ, db=None):
116141
if typ not in self.SQL:
117142
raise LookupError("Unknown SQL type: '{0}'".format(typ))
118143
result = self.SQL[typ]
119-
if self.mamonsu_bootstrap(db):
144+
if self.is_bootstraped(db):
120145
return result[1]
121146
else:
122147
return result[0]

mamonsu/plugins/pgsql/plugin.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,9 @@ def disable_and_exit_if_extension_is_not_installed(self, ext, db=None):
4040
raise PluginDisableException("""Disable plugin and exit, because '{0}' \
4141
extension is not installed. Enable it in PostgreSQL instance: '{1}', \
4242
if needed and restart.""".format(ext, Pooler.connection_string(db)))
43+
44+
def disable_and_exit_if_not_pgpro_ee(self, db=None):
45+
if not Pooler.is_pgpro_ee(db):
46+
raise PluginDisableException("""Disable plugin and exit, because \
47+
PostgresPro Enterprise Edition is not detected [instance: '{1}']
48+
""".format(Pooler.connection_string(db)))

0 commit comments

Comments
 (0)