Skip to content

Commit 388e505

Browse files
committed
refactor: changed style formatting and version parameter assign for plugins
1 parent 0a32890 commit 388e505

File tree

14 files changed

+87
-193
lines changed

14 files changed

+87
-193
lines changed

mamonsu/plugins/pgsql/archive_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def triggers(self, template):
104104

105105
def keys_and_queries(self, template_zabbix):
106106
result = []
107-
if self.VersionPG['number'] < LooseVersion('10'):
107+
if LooseVersion(self.VersionPG) < LooseVersion('10'):
108108
xlog = 'xlog'
109109
else:
110110
xlog = 'wal'

mamonsu/plugins/pgsql/cfs.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,9 @@ def discovery_rules(self, template):
160160
rule = {
161161
'name': 'Compressed relations discovery',
162162
'key': 'pgsql.cfs.discovery_compressed_relations[]'
163-
# 'filter': '{#COMPRESSED_RELATION}:.*'
164163
}
165164
if Plugin.old_zabbix:
166-
rule['filter'] = '{#DATABASE}:.*'
165+
rule['filter'] = '{#COMPRESSED_RELATION}:.*'
167166
conditions = []
168167
else:
169168
conditions = [

mamonsu/plugins/pgsql/connections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def keys_and_queries(self, template_zabbix):
136136
result.append(
137137
'{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + item[1]), self.query_agent.format(item[1])))
138138
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format('.total'), self.query_agent_total))
139-
if self.VersionPG['number'] < LooseVersion('9.6'):
139+
if LooseVersion(self.VersionPG) < LooseVersion('9.6'):
140140
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format('.waiting'), self.query_agent_waiting_old_v))
141141
else:
142142
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format('.waiting'), self.query_agent_waiting_new_v))

mamonsu/plugins/pgsql/databases.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def run(self, zbx):
3333
datname, pg_database_size(datname::text), age(datfrozenxid) \
3434
from pg_catalog.pg_database where datistemplate = false')
3535
dbs = []
36+
bloat_count = []
3637
for info in result:
3738
dbs.append({'{#DATABASE}': info[0]})
3839
zbx.send('pgsql.database.size[{0}]'.format(
@@ -74,9 +75,9 @@ def discovery_rules(self, template):
7475
{
7576
'condition': [
7677
{'macro': '{#DATABASE}',
77-
'value': '.*',
78-
'operator': '',
79-
'formulaid': 'A'}
78+
'value': '.*',
79+
'operator': '',
80+
'formulaid': 'A'}
8081
]
8182
}
8283

@@ -124,13 +125,12 @@ def discovery_rules(self, template):
124125
return template.discovery_rule(rule=rule, conditions=conditions, items=items, graphs=graphs)
125126

126127
def keys_and_queries(self, template_zabbix):
127-
result = []
128-
result.append('{0},$2 $1 -c "{1}"'.format(self.key_autovacumm.format("[*]"), Pooler.SQL['count_autovacuum'][0]))
129-
result.append('{0},$2 $1 -c "{1}"'.format(self.key_db_discovery.format("[*]"), self.query_agent_discovery))
130-
result.append('{0},echo "{1}" | $3 $2 -v p1="$1"'.format(self.key_db_size.format("[*]"), self.query_size))
131-
result.append('{0},echo "{1}" | $3 $2 -v p1="$1"'.format(self.key_db_age.format("[*]"), self.query_age))
132-
result.append(
133-
'{0},$3 $2 -d "$1" -c "{1}"'.format(self.key_db_bloating_tables.format("[*]"),
134-
self.query_bloating_tables.format(self.plugin_config('bloat_scale'),
135-
self.plugin_config('min_rows'))))
128+
result = ['{0},$2 $1 -c "{1}"'.format(self.key_autovacumm.format("[*]"), Pooler.SQL['count_autovacuum'][0]),
129+
'{0},$2 $1 -c "{1}"'.format(self.key_db_discovery.format("[*]"), self.query_agent_discovery),
130+
'{0},echo "{1}" | $3 $2 -v p1="$1"'.format(self.key_db_size.format("[*]"), self.query_size),
131+
'{0},echo "{1}" | $3 $2 -v p1="$1"'.format(self.key_db_age.format("[*]"), self.query_age),
132+
'{0},$3 $2 -d "$1" -c "{1}"'.format(self.key_db_bloating_tables.format("[*]"),
133+
self.query_bloating_tables.format(
134+
self.plugin_config('bloat_scale'),
135+
self.plugin_config('min_rows')))]
136136
return template_zabbix.key_and_query(result)

mamonsu/plugins/pgsql/driver/connection.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
class ConnectionInfo(object):
1111

12-
def __init__(self, connection_hash={}):
12+
def __init__(self, connection_hash=None):
13+
if connection_hash is None:
14+
connection_hash = {}
1315
self.host = connection_hash.get('host') or os.environ.get('PGHOST')
1416
self.port = connection_hash.get('port') or int(os.environ.get('PGPORT') or 5432)
1517
self.user = connection_hash.get('user') or os.environ.get('PGUSER')
@@ -38,8 +40,10 @@ def get_hash(self):
3840

3941
class Connection(ConnectionInfo):
4042

41-
def __init__(self, info={}):
43+
def __init__(self, info=None):
4244
super(Connection, self).__init__(info)
45+
if info is None:
46+
info = {}
4347
self.lock = threading.Lock()
4448
self.conn = None
4549
self.connected = False

mamonsu/plugins/pgsql/driver/pool.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55

66
class Pool(object):
7-
87
ExcludeDBs = ['template0', 'template1']
98

109
SQL = {
@@ -40,7 +39,9 @@ class Pool(object):
4039
),
4140
}
4241

43-
def __init__(self, params={}):
42+
def __init__(self, params=None):
43+
if params is None:
44+
params = {}
4445
self._params = params
4546
self._primary_connection_hash = None
4647
self._connections = {}
@@ -123,7 +124,7 @@ def is_bootstraped(self, db=None):
123124
return self._cache['bootstrap']['storage'][db]
124125

125126
def is_superuser(self, db=None):
126-
db = self._normalize_db(db)
127+
_ = self._normalize_db(db)
127128
if self.query("select current_setting('is_superuser')")[0][0] == 'on':
128129
return True
129130
else:
@@ -208,14 +209,14 @@ def _init_connection(self, db):
208209
# create new connection
209210
self._connections[db] = Connection(self._build_connection_hash(db))
210211

211-
def get_sys_param (self, param, db=None):
212+
def get_sys_param(self, param, db=None):
212213
if param == '':
213-
#todo
214+
# todo
214215
pass
215216
db = self._normalize_db(db)
216217
if self.is_bootstraped() and self.bootstrap_version_greater('2.3.4'):
217218
result = self.query("""select * from mamonsu_get_sys_param(\'{0}\')""".format(param))[0][0]
218219
else:
219220
result = self.query(
220221
'select setting from pg_catalog.pg_settings where name = \'{0}\''.format(param), db)[0][0]
221-
return result
222+
return result

mamonsu/plugins/pgsql/health.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def items(self, template):
3434
delay = self.plugin_config('interval')
3535
value_type = Plugin.VALUE_TYPE.numeric_unsigned
3636
else:
37-
delay = 5 #TODO check delay
37+
delay = 5 # TODO check delay
3838
value_type = Plugin.VALUE_TYPE.numeric_float
3939
result += template.item({
4040
'name': 'PostgreSQL: ping',
@@ -44,7 +44,7 @@ def items(self, template):
4444
'delay': delay
4545
}) + template.item({
4646
'name': 'PostgreSQL: cache hit ratio',
47-
'key': self.right_type(self.key_cache, "hit"),
47+
'key': self.right_type(self.key_cache, "hit"),
4848
'value_type': value_type,
4949
'delay': self.plugin_config('interval'),
5050
'units': Plugin.UNITS.percent
@@ -68,12 +68,12 @@ def graphs(self, template):
6868
def triggers(self, template):
6969
result = template.trigger({
7070
'name': 'PostgreSQL service was restarted on '
71-
'{HOSTNAME} (uptime={ITEM.LASTVALUE})',
71+
'{HOSTNAME} (uptime={ITEM.LASTVALUE})',
7272
'expression': '{#TEMPLATE:' + self.right_type(self.key_uptime) + '.last()}&lt;' +
7373
str(self.plugin_config('uptime'))
7474
}) + template.trigger({
7575
'name': 'PostgreSQL cache hit ratio too low on '
76-
'{HOSTNAME} ({ITEM.LASTVALUE})',
76+
'{HOSTNAME} ({ITEM.LASTVALUE})',
7777
'expression': '{#TEMPLATE:' + self.right_type(self.key_cache, "hit") + '.last()}&lt;' +
7878
str(self.plugin_config('cache'))
7979
}) + template.trigger({
@@ -84,8 +84,7 @@ def triggers(self, template):
8484
return result
8585

8686
def keys_and_queries(self, template_zabbix):
87-
result = []
88-
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key_ping.format(''), self.query_health))
89-
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key_uptime.format(''), self.query_uptime))
90-
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key_cache.format('.hit'), self.query_cache))
91-
return template_zabbix.key_and_query(result)
87+
result = ['{0}[*],$2 $1 -c "{1}"'.format(self.key_ping.format(''), self.query_health),
88+
'{0}[*],$2 $1 -c "{1}"'.format(self.key_uptime.format(''), self.query_uptime),
89+
'{0}[*],$2 $1 -c "{1}"'.format(self.key_cache.format('.hit'), self.query_cache)]
90+
return template_zabbix.key_and_query(result)

mamonsu/plugins/pgsql/oldest.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
class Oldest(Plugin):
88
key = 'pgsql.oldest{0}'
99
AgentPluginType = 'pg'
10-
OldestXidSql = "select greatest(max(age(backend_xmin)), max(age(backend_xid))) from pg_catalog.pg_stat_activity;"
10+
OldestXidSql = "SELECT greatest(max(age(backend_xmin)), max(age(backend_xid))) FROM pg_catalog.pg_stat_activity;"
1111

1212
OldestXidSql_bootstrap = "select public.mamonsu_get_oldest_xid();"
1313

14-
# OldestQuery = " SELECT query FROM pg_catalog.pg_stat_activity WHERE extract(epoch FROM (now() - query_start))=(SELECT " \
15-
# "extract(epoch from max(now() - query_start)) FROM pg_catalog.pg_stat_activity) and pid not " \
16-
# "in (select pid from pg_stat_replication) AND pid <> pg_backend_pid() AND query not ilike '%%VACUUM%%';"
17-
OldestTransactionSql = "SELECT CASE WHEN extract(epoch from max(now() - xact_start)) IS NOT NULL AND extract(epoch" \
18-
" from max(now() - xact_start))>0 THEN extract(epoch from max(now() - xact_start)) ELSE 0 END FROM " \
19-
"pg_catalog.pg_stat_activity WHERE pid NOT IN (SELECT pid FROM pg_stat_replication) AND " \
20-
"pid <> pg_backend_pid(); "
14+
OldestTransactionSql = "SELECT " \
15+
"CASE WHEN extract(epoch from max(now() - xact_start)) IS NOT NULL " \
16+
"AND extract(epoch FROM max(now() - xact_start))>0 " \
17+
"THEN extract(epoch from max(now() - xact_start)) " \
18+
"ELSE 0 END " \
19+
"FROM pg_catalog.pg_stat_activity " \
20+
"WHERE pid NOT IN (SELECT pid FROM pg_stat_replication) " \
21+
"AND pid <> pg_backend_pid(); "
22+
2123
OldestTransactionSql_bootstrap = """
2224
select public.mamonsu_get_oldest_transaction();
2325
"""
@@ -81,7 +83,6 @@ def triggers(self, template):
8183
})
8284

8385
def keys_and_queries(self, template_zabbix):
84-
result = []
85-
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format('.xid_age'), self.OldestXidSql))
86-
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format('.query_time'), self.OldestTransactionSql))
86+
result = ['{0}[*],$2 $1 -c "{1}"'.format(self.key.format('.xid_age'), self.OldestXidSql),
87+
'{0}[*],$2 $1 -c "{1}"'.format(self.key.format('.transaction_time'), self.OldestTransactionSql)]
8788
return template_zabbix.key_and_query(result)

mamonsu/plugins/pgsql/pg_buffercache.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@
77
class PgBufferCache(Plugin):
88
AgentPluginType = 'pg'
99
key = 'pgsql.buffers{0}'
10-
query_agent_size = "select sum(1) * (current_setting('block_size')::int8) as size from public.pg_buffercache;" # for zabbix
11-
query_agent_twice_used = "select sum(case when usagecount > 1 then 1 else 0 end) * (current_setting('block_size')::int8) as twice_used " \
12-
"from public.pg_buffercache;" # for zabbix
13-
query_agent_dirty = "select sum(case isdirty when true then 1 else 0 end) * (current_setting('block_size')::int8) as dirty " \
14-
"from public.pg_buffercache;" # for zabbix
10+
query_agent_size = "select sum(1) * (current_setting('block_size')::int8) as size from public.pg_buffercache;"
11+
# for zabbix
12+
query_agent_twice_used = "select " \
13+
"sum(case when usagecount > 1 then 1 else 0 end) * (current_setting('block_size')::int8) as twice_used " \
14+
"from public.pg_buffercache;" # for zabbix
15+
query_agent_dirty = "select " \
16+
"sum(case isdirty when true then 1 else 0 end) * (current_setting('block_size')::int8) as dirty " \
17+
"from public.pg_buffercache;" # for zabbix
1518
query = [query_agent_size, query_agent_twice_used, query_agent_dirty]
1619
Items = [
1720
# key, name, color
@@ -31,7 +34,7 @@ def items(self, template):
3134
result = ''
3235
for item in self.Items:
3336
result += template.item({
34-
'key': self.right_type(self.key, item[0]), #'pgsql.buffers[{0}]'.format(item[0]),
37+
'key': self.right_type(self.key, item[0]), # 'pgsql.buffers[{0}]'.format(item[0]),
3538
'name': item[1],
3639
'delay': self.plugin_config('interval'),
3740
'units': Plugin.UNITS.bytes
@@ -51,6 +54,7 @@ def graphs(self, template):
5154
def keys_and_queries(self, template_zabbix):
5255
result = []
5356
for i, item in enumerate(self.Items):
54-
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format('.'+item[0]), self.query[i].format(item[0])))
57+
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format('.'+item[0]),
58+
self.query[i].format(item[0])))
5559
return template_zabbix.key_and_query(result)
5660

mamonsu/plugins/pgsql/pg_locks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ def keys_and_queries(self, template_zabbix):
7474
for item in self.Items:
7575
result.append('{0}[*],$2 $1 -c "{1}"'.format(self.key.format("." + item[0]),
7676
self.query_agent.format('{0}lock'.format(item[0]))))
77-
return template_zabbix.key_and_query(result)
77+
return template_zabbix.key_and_query(result)

0 commit comments

Comments
 (0)