~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/python2.4
"""
Watch live PostgreSQL logs for interesting stuff
"""

import re, sys
from optparse import OptionParser
import subprocess

def get_options(args=None):
    parser = OptionParser()
    parser.add_option("-l", "--logfile", dest="logfile",
            default="/var/log/postgresql/postgres.log",
            metavar="LOG", help="Monitor LOG instead of the default"
            )
    parser.add_option("--slow", dest="slow",
            type="float", default=100.0, metavar="TIME",
            help="Report slow queries taking over TIME seconds",
            )
    (options, args) = parser.parse_args(args)
    return options

def generate_loglines(logfile):
    """Generator returning the next line in the logfile (blocking)"""
    cmd = subprocess.Popen(
            ['tail', '-f', logfile],
            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    while cmd.poll() is None:
        yield cmd.stdout.readline()
    if cmd.returncode != 0:
        print >> sys.stderr, cmd.stderr.read()
        raise RuntimeError("tail returned %d" % cmd.returncode)


class Process(object):
    statement = None
    duration = None
    connection = None
    auth = None

    def __init__(self, pid):
        self.pid = pid


class Watcher(object):
    _line_re = re.compile("""
        ^\d{4}-\d\d-\d\d \s \d\d:\d\d:\d\d \s 
        \[(?P<pid>\d+)\] \s (?P<type>LOG|ERROR|DETAIL): \s+ (?P<rest>.*)$
        """, re.X)

    _statement_re = re.compile("""
        ^statement: \s (?P<statement>.*)$
        """, re.X)

    _duration_re = re.compile("""
        ^duration: \s (?P<duration>\d+\.\d+) \s ms$
        """, re.X)

    _connection_received_re = re.compile("""
        ^connection \s received: \s+ (?P<connection>.*)$
        """, re.X)

    _connection_authorized_re = re.compile("""
        ^connection \s authorized: \s+ (?P<auth>.*)$
        """, re.X)

    _ignored_rest_re = re.compile("""
        ^(received \s | ERROR: \s | unexpected \s EOF \s) .*$
        """, re.X)

    _ignored_statements_re = re.compile("""
        ^(BEGIN.*|END)$
        """, re.X)

    def __init__(self, options):
        self.processes = {}
        self.options = options
        self.previous_process = None

    def run(self):
        lines = generate_loglines(options.logfile)
        for line in lines:
            self.feed(line)

    def feed(self, line):

        # Handle continuations of previous statement
        if line.startswith('\t'):
            if self.previous_process is not None:
                self.previous_process.statement += '\n%s' % line[1:-1]
            return

        match = self._line_re.search(line)
        if match is None:
            raise ValueError('Badly formatted line %r' % (line,))

        t = match.group('type')
        if t in ['ERROR', 'DETAIL']:
            return
        if t != 'LOG':
            raise ValueError('Unknown line type %s (%r)' % (t, line))

        pid = int(match.group('pid'))
        rest = match.group('rest')
        
        process = self.processes.get(pid, None)
        if process is None:
            process = Process(pid)
            self.processes[pid] = process
        self.previous_process = process
        
        match = self._statement_re.search(rest)
        if match is not None:
            statement = match.group('statement')
            if process.statement:
                process.statement += '\n%s' % statement
            else:
                process.statement = statement
            return

        match = self._duration_re.search(rest)
        if match is not None:
            process.duration = float(match.group('duration'))
            self.reportDuration(process)
            self.previous_process = None
            del self.processes[process.pid]
            return

        match = self._connection_received_re.search(rest)
        if match is not None:
            process.connection = match.group('connection')
            return

        match = self._connection_authorized_re.search(rest)
        if match is not None:
            process.auth = match.group('auth')
            return

        match = self._ignored_rest_re.search(rest)
        if match is not None:
            return

        raise ValueError('Unknown entry: %r' % (rest,))

    def reportDuration(self, process):
        """Report a slow statement if it is above a threshold"""
        if self.options.slow is None or process.statement is None:
            return

        match = self._ignored_statements_re.search(process.statement)
        if match is not None:
            return

        if process.duration > options.slow:
            print '[%5d] %s' % (process.pid, process.statement)
            print '        Duration: %0.3f' % (process.duration,)

if __name__ == '__main__':
    options = get_options()

    watcher = Watcher(options)
    try:
        watcher.run()
    except KeyboardInterrupt:
        pass