forked from Yelp/ybinlogp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathybinlog.py
executable file
·48 lines (39 loc) · 1.69 KB
/
ybinlog.py
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
#!/opt/local/bin/python2.6
import os
import sys
import datetime
import ybinlogp
if len(sys.argv) == 1:
print ('Usage: %s <binlog>' % (sys.argv[0]))
os._exit(1)
log = open(sys.argv[1])
# When calling from Python, you're responsible for seeking the fd to
# the right offset, aka 4 bytes into the file to skip the magic bytes.
log.seek(4)
# Don't pass anything that doesn't have a fileno...
binlog = ybinlogp.binlog(log)
# Binlog is iterable...
for entry in binlog:
# All entries have member event, and the type_code indicates the
# type of event. Eg. entry.event.type_code == 2 means it's a
# query. Then you can access entry.query. For a list of type
# codes, see ybinlogp.cc(enum e_event_types).
# You can also switch-case by checking entry.query, entry.rotate
# and see which is non-null.
# For a list of entry-entries, see ybinlogp.cc
print ('event type %d at %s' % (entry.event.type_code, datetime.datetime.fromtimestamp(entry.event.timestamp)))
if entry.query:
print ('query %s, %s' % (entry.query.database, entry.query.statement))
elif entry.rotate:
print ('rotate %d, %s' % (entry.rotate.next_position, entry.rotate.next_file))
elif entry.xid:
print ('xid %d' % (entry.xid.id))
elif entry.rand:
print ('rand %d %d' % (entry.rand.seed_1, entry.rand.seed_2))
elif entry.format_description:
print ('description %d %d %s' % (entry.format_description.format_version, entry.format_description.create_timestamp, entry.format_description.server_version))
elif entry.intvar:
print ('intvar %d = %d' % (entry.intvar.type, entry.intvar.value))
# For a list of attributes on on the various entries, see
# ybinlogp.cc. Patch for adding docstring entries to the
# boost.python bindings welcome.