longqueries.py
author Radek Brich <brich.radek@ifortuna.cz>
Fri, 16 Aug 2013 13:19:26 +0200
changeset 86 b61b54aa9f96
parent 83 515fadd3d286
child 93 b72591087495
permissions -rwxr-xr-x
Fix: ConfigParser add_argument -> add_option.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     1
#!/usr/bin/env python3.2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     2
9
2fcc8ef0b97d Reorganize again :-) Add setup.py.
Radek Brich <radek.brich@devl.cz>
parents: 7
diff changeset
     3
from pgtoolkit import pgstats, toolbase
83
515fadd3d286 Add dependency on pycolib. Move common modules to pycolib. Add example table schema for meta DB.
Radek Brich <radek.brich@devl.cz>
parents: 73
diff changeset
     4
from pycolib.ansicolor import highlight, YELLOW, BOLD
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     5
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
     7
class LongQueriesTool(toolbase.SimpleTool):
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
    def __init__(self):
7
685b20d2d3ab Reorganize directories. PgDataDiff - reworked. PgManager - add fetchone_adapted, fetchall_adapted to cursor.
Radek Brich <radek.brich@devl.cz>
parents: 5
diff changeset
     9
        toolbase.SimpleTool.__init__(self, name='longqueries', desc='List long queries.')
73
390376b9e70f Update longqueries tool: add --age parameter.
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    10
        self.parser.add_argument('--age', default='1m', help='How long must be the query running to be listed.')
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
        self.init()
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
    def main(self):
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
        stats = pgstats.PgStats(self.pgm.get_conn('target'))
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
73
390376b9e70f Update longqueries tool: add --age parameter.
Radek Brich <radek.brich@devl.cz>
parents: 44
diff changeset
    16
        for ln in stats.list_long_queries(self.args.age):
44
4fe39c59c515 Update longqueries tool: add client IP, waiting attributes.
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    17
            print(highlight(1),
4fe39c59c515 Update longqueries tool: add client IP, waiting attributes.
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    18
                  'backend PID: ', ln['procpid'],
4fe39c59c515 Update longqueries tool: add client IP, waiting attributes.
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    19
                  ', query_start: ', ln['query_start'].strftime('%F %T'),
4fe39c59c515 Update longqueries tool: add client IP, waiting attributes.
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    20
                  ', client IP: ', ln['client_addr'],
4fe39c59c515 Update longqueries tool: add client IP, waiting attributes.
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    21
                  ln['waiting'] and ', ' + highlight(1, YELLOW|BOLD) + 'waiting' or '',
4fe39c59c515 Update longqueries tool: add client IP, waiting attributes.
Radek Brich <radek.brich@devl.cz>
parents: 39
diff changeset
    22
                  highlight(0), sep='')
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
            print(ln['query'])
5
57cf8fdff5ed Clean up tools.
Radek Brich <radek.brich@devl.cz>
parents: 2
diff changeset
    24
            print()
2
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
tool = LongQueriesTool()
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
tool.main()
efee419b7a2d Fix pgmanager exceptions.
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29