|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from mamonsu.plugins.pgsql.plugin import PgsqlPlugin as Plugin |
| 4 | +from distutils.version import LooseVersion |
| 5 | +from .pool import Pooler |
| 6 | + |
| 7 | + |
| 8 | +class PreparedTransaction(Plugin): |
| 9 | + Interval = 6 |
| 10 | + key_count = { |
| 11 | + 'state': 'count_prepared', |
| 12 | + 'key': 'pgsql.prepared.count', |
| 13 | + 'name': 'PostgreSQL: numder of prepared transactions', |
| 14 | + 'color': '00BB00', |
| 15 | + 'yaxisside': 0, |
| 16 | + } |
| 17 | + key_prepared = { |
| 18 | + 'state': 'oldest_prepared', |
| 19 | + 'key': 'pgsql.prepared.oldest', |
| 20 | + 'name': 'PostgreSQL: oldest prepared transaction running time in sec', |
| 21 | + 'color': '0000BB', |
| 22 | + 'yaxisside': 1, |
| 23 | + } |
| 24 | + query_prepared = "SELECT COUNT(*) as count_prepared, " \ |
| 25 | + "coalesce (ROUND(MAX(EXTRACT (EPOCH FROM (now() - prepared)))),0)::bigint " \ |
| 26 | + "AS oldest_prepared FROM pg_catalog.pg_prepared_xacts" |
| 27 | + query_prepared_bootstraped = "SELECT * FROM public.mamonsu_prepared_transaction()" |
| 28 | + |
| 29 | + DEFAULT_CONFIG = { |
| 30 | + 'max_prepared_transaction_time': str(5 * 60 * 60) |
| 31 | + } |
| 32 | + |
| 33 | + def run(self, zbx): |
| 34 | + if Pooler.is_bootstraped(): |
| 35 | + result = Pooler.query(self.query_prepared_bootstraped) |
| 36 | + else: |
| 37 | + result = Pooler.query(self.query_prepared) |
| 38 | + |
| 39 | + for count_prepared, oldest_prepared in result: |
| 40 | + zbx.send(self.key_count['key'], count_prepared) |
| 41 | + zbx.send(self.key_prepared['key'], oldest_prepared) |
| 42 | + |
| 43 | + def items(self, template): |
| 44 | + result = template.item({ |
| 45 | + 'name': self.key_count['name'], |
| 46 | + 'key': self.key_count['key'], |
| 47 | + }) |
| 48 | + result += template.item({ |
| 49 | + 'name': self.key_prepared['name'], |
| 50 | + 'key': self.key_prepared['key'], |
| 51 | + 'delay': self.plugin_config('interval') |
| 52 | + }) |
| 53 | + |
| 54 | + return result |
| 55 | + |
| 56 | + def graphs(self, template): |
| 57 | + result = template.graph({ |
| 58 | + 'name': 'PostgreSQL prepared transaction', |
| 59 | + 'items': [{ |
| 60 | + 'key': self.key_count['key'], |
| 61 | + 'color': self.key_count['color'], |
| 62 | + 'yaxisside': self.key_count['yaxisside'], |
| 63 | + }, |
| 64 | + { |
| 65 | + 'key': self.key_prepared['key'], |
| 66 | + 'color': self.key_prepared['color'], |
| 67 | + 'yaxisside': self.key_prepared['yaxisside'], |
| 68 | + }, |
| 69 | + ] |
| 70 | + }) |
| 71 | + return result |
| 72 | + |
| 73 | + def triggers(self, template): |
| 74 | + result = template.trigger({ |
| 75 | + 'name': 'PostgreSQL prepared transaction running is too old on {HOSTNAME}', |
| 76 | + 'expression': '{#TEMPLATE:' + self.key_prepared['key'] + |
| 77 | + '.last()}>' + self.plugin_config('max_prepared_transaction_time') |
| 78 | + }) |
| 79 | + return result |
| 80 | + |
0 commit comments