postgres module: TypeError fix in query_stats_
lgz committed
Apr 10, 2018 at 22:04 UTC
e23e6fbb7b8a15a995521a61a196eb8719986277
1 file changed
+31
-18
python.d/postgres.chart.py
+31
-18
@@ -71,9 +71,10 @@ METRICS = dict(
71
'vacuum_freeze',
72
'brin_summarize'],
73
STANDBY_DELTA=['sent_delta',
74
- 'write_delta',
75
- 'flush_delta',
76
- 'replay_delta'],
74
+ 'write_delta',
75
+ 'flush_delta',
76
+ 'replay_delta'
77
+ ],
78
REPSLOT_FILES=['replslot_wal_keep',
79
'replslot_files']
80
@@ -244,9 +245,9 @@ QUERY_STATS = {
245
}
246
247
ORDER = ['db_stat_temp_files', 'db_stat_temp_bytes', 'db_stat_blks', 'db_stat_tuple_returned', 'db_stat_tuple_write',
247
- 'db_stat_transactions','db_stat_connections', 'database_size', 'backend_process', 'index_count', 'index_size',
248
+ 'db_stat_transactions', 'db_stat_connections', 'database_size', 'backend_process', 'index_count', 'index_size',
249
'table_count', 'table_size', 'wal', 'wal_writes', 'archive_wal', 'checkpointer', 'stat_bgwriter_alloc', 'stat_bgwriter_checkpoint',
249
- 'stat_bgwriter_backend', 'stat_bgwriter_backend_fsync' , 'stat_bgwriter_bgwriter', 'stat_bgwriter_maxwritten',
250
+ 'stat_bgwriter_backend', 'stat_bgwriter_backend_fsync', 'stat_bgwriter_bgwriter', 'stat_bgwriter_maxwritten',
251
'replication_slot', 'standby_delta', 'autovacuum']
252
253
CHARTS = {
@@ -397,7 +398,7 @@ CHARTS = {
398
['flush_delta', 'flush delta', 'absolute', 1, 1024],
399
['replay_delta', 'replay delta', 'absolute', 1, 1024]
400
]},
400
- 'replication_slot': {
401
+ 'replication_slot': {
402
'options': [None, 'Replication slot files', 'files', 'replication slot', 'postgres.replication_slot', 'line'],
403
'lines': [
404
['replslot_wal_keep', 'wal keeped', 'absolute'],
@@ -482,8 +483,8 @@ class Service(SimpleService):
483
wal = 'xlog'
484
lsn = 'location'
485
self.queries[QUERIES['BGWRITER']] = METRICS['BGWRITER']
485
- self.queries[QUERIES['DIFF_LSN'].format(wal,lsn)] = METRICS['WAL_WRITES']
486
- self.queries[QUERIES['STANDBY_DELTA'].format(wal,lsn)] = METRICS['STANDBY_DELTA']
486
+ self.queries[QUERIES['DIFF_LSN'].format(wal, lsn)] = METRICS['WAL_WRITES']
487
+ self.queries[QUERIES['STANDBY_DELTA'].format(wal, lsn)] = METRICS['STANDBY_DELTA']
488
489
if self.index_stats:
490
self.queries[QUERIES['INDEX_STATS']] = METRICS['INDEX_STATS']
@@ -492,7 +493,7 @@ class Service(SimpleService):
493
if is_superuser:
494
self.queries[QUERIES['ARCHIVE'].format(wal)] = METRICS['ARCHIVE']
495
if self.server_version >= 90400:
495
- self.queries[QUERIES['WAL'].format(wal,lsn)] = METRICS['WAL']
496
+ self.queries[QUERIES['WAL'].format(wal, lsn)] = METRICS['WAL']
497
if self.server_version >= 100000:
498
self.queries[QUERIES['REPSLOT_FILES']] = METRICS['REPSLOT_FILES']
499
if self.server_version >= 90400:
@@ -501,8 +502,8 @@ class Service(SimpleService):
502
def create_dynamic_charts_(self):
503
504
for database_name in self.databases[::-1]:
504
- self.definitions['database_size']['lines'].append([database_name + '_size',
505
- database_name, 'absolute', 1, 1024 * 1024])
505
+ self.definitions['database_size']['lines'].append(
506
+ [database_name + '_size', database_name, 'absolute', 1, 1024 * 1024])
507
for chart_name in [name for name in self.order if name.startswith('db_stat')]:
508
add_database_stat_chart_(order=self.order, definitions=self.definitions,
509
name=chart_name, database_name=database_name)
@@ -510,14 +511,18 @@ class Service(SimpleService):
511
add_database_lock_chart_(order=self.order, definitions=self.definitions, database_name=database_name)
512
513
for application_name in self.secondaries[::-1]:
513
- add_replication_delta_chart_(order=self.order, definitions=self.definitions,
514
- name='standby_delta', application_name=application_name)
514
+ add_replication_delta_chart_(
515
+ order=self.order,
516
+ definitions=self.definitions,
517
+ name='standby_delta',
518
+ application_name=application_name)
519
520
for slot_name in self.replication_slots[::-1]:
517
- add_replication_slot_chart_(order=self.order, definitions=self.definitions,
518
- name='replication_slot', slot_name=slot_name)
519
-
520
-
521
+ add_replication_slot_chart_(
522
+ order=self.order,
523
+ definitions=self.definitions,
524
+ name='replication_slot',
525
+ slot_name=slot_name)
526
527
def _get_data(self):
528
result, error = self._connect()
@@ -551,7 +556,8 @@ class Service(SimpleService):
556
else:
557
dimension_id = metric
558
if metric in row:
554
- self.data[dimension_id] = int(row[metric])
559
+ if row[metric] is not None:
560
+ self.data[dimension_id] = int(row[metric])
561
elif 'locks_count' in row:
562
self.data[dimension_id] = row['locks_count'] if metric == row['mode'] else 0
563
@@ -564,6 +570,7 @@ def discover_databases_(cursor, query):
570
result.append(db)
571
return result
572
573
+
574
def discover_secondaries_(cursor, query):
575
cursor.execute(query)
576
result = list()
@@ -572,6 +579,7 @@ def discover_secondaries_(cursor, query):
579
result.append(sc)
580
return result
581
582
+
583
def discover_replication_slots_(cursor, query):
584
cursor.execute(query)
585
result = list()
@@ -580,14 +588,17 @@ def discover_replication_slots_(cursor, query):
588
result.append(slot)
589
return result
590
591
+
592
def check_if_superuser_(cursor, query):
593
cursor.execute(query)
594
return cursor.fetchone()[0]
595
596
+
597
def detect_server_version(cursor, query):
598
cursor.execute(query)
599
return int(cursor.fetchone()[0])
600
601
+
602
def populate_lock_types(databases):
603
result = dict()
604
for database in databases:
@@ -631,6 +642,7 @@ def add_database_stat_chart_(order, definitions, name, database_name):
642
'options': [name, title + ': ' + database_name, units, 'db ' + database_name, context, chart_type],
643
'lines': create_lines(database_name, chart_template['lines'])}
644
645
+
646
def add_replication_delta_chart_(order, definitions, name, application_name):
647
def create_lines(standby, lines):
648
result = list()
@@ -648,6 +660,7 @@ def add_replication_delta_chart_(order, definitions, name, application_name):
660
'options': [name, title + ': ' + application_name, units, 'replication delta', context, chart_type],
661
'lines': create_lines(application_name, chart_template['lines'])}
662
663
+
664
def add_replication_slot_chart_(order, definitions, name, slot_name):
665
def create_lines(slot, lines):
666
result = list()