This repository has been archived by the owner on Mar 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchful_snake.py
executable file
·242 lines (190 loc) · 7.13 KB
/
watchful_snake.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env python3
# Watchful Snake is a MySQL BinLog to Redis Streams Data Pipeline.
# Watchful Snake is licensed under the
# GNU General Public License Version 3.
# Dimitar D. Mitov 2021
# https://github.com/ddmitov/watchful-snake
# Python Standard Library modules:
import json
# PyPI modules:
import pymysql
import redis
from pymysqlreplication import BinLogStreamReader
from pymysqlreplication.row_event import (
DeleteRowsEvent,
UpdateRowsEvent,
WriteRowsEvent,
)
# GLOBAL CONSTANTS: #
#####################
# Generated by the 'uuidgen' utility:
MYSQL_BINLOG_SLAVE_UUID = 'e7d14803-b12f-45eb-9c33-abd2e5e9ac77'
REDIS_HOST = '127.0.0.1'
REDIS_PORT = 6379
REDIS_DB = 0
REDIS_PASSWORD = ''
PREFIX = 'watchful_snake:mysql'
def redis_stream_appender(redis_server, schema, table, action, data):
return redis_server.xadd(
PREFIX + ':' + schema + ':' + table + ':' + action,
data
)
def mysql_binlog_watcher():
# Redis connection:
redis_server = redis.Redis(
host=REDIS_HOST,
port=REDIS_PORT,
db=REDIS_DB,
password=REDIS_PASSWORD
)
try:
# MySQL settings in Redis:
mysql_settings = redis_server.get(PREFIX + ':settings')
if mysql_settings is None:
print('MySQL settings are not found!')
exit(1)
mysql_settings_json = json.loads(mysql_settings)
mysql_connection_settings = mysql_settings_json['connection']
mysql_schemas = mysql_settings_json['schemas']
mysql_tables = mysql_settings_json['tables']
# Last MySQL binary log file recorded in Redis:
saved_binlog_file_bytes = redis_server.get(
PREFIX + ':last_binlog_file'
)
saved_binlog_file = ''
if saved_binlog_file_bytes is not None:
saved_binlog_file = saved_binlog_file_bytes.decode('utf-8')
# Last MySQL binary log position recorded in Redis:
saved_binlog_position_bytes = redis_server.get(
PREFIX + ':last_binlog_position'
)
saved_binlog_position = 0
if saved_binlog_position_bytes is not None:
saved_binlog_position = int(saved_binlog_position_bytes)
except (ConnectionRefusedError, redis.ConnectionError):
print('No Redis!')
exit(1)
mysql_binlog_stream = None
# MySQL connection:
try:
mysql_binlog_stream = BinLogStreamReader(
connection_settings=mysql_connection_settings,
server_id=1,
blocking=True,
only_schemas=mysql_schemas,
only_tables=mysql_tables,
only_events=[
DeleteRowsEvent,
WriteRowsEvent,
UpdateRowsEvent
],
slave_uuid=MYSQL_BINLOG_SLAVE_UUID,
)
except (ConnectionRefusedError, ConnectionError):
print('No MySQL!')
exit(1)
try:
for binlogevent in mysql_binlog_stream:
binlog_file = mysql_binlog_stream.log_file
binlog_position = int(mysql_binlog_stream.log_pos)
replicate = False
binlog_file_number = binlog_file.split('.')[-1]
binlog_file_number = binlog_file_number.lstrip('0')
binlog_file_number = int(binlog_file_number)
saved_binlog_file_number = 0
if len(saved_binlog_file) > 0:
saved_binlog_file_number = saved_binlog_file.split('.')[-1]
saved_binlog_file_number = (
saved_binlog_file_number.lstrip('0')
)
saved_binlog_file_number = int(saved_binlog_file_number)
if binlog_file == saved_binlog_file:
if binlog_position >= saved_binlog_position:
replicate = True
if binlog_file != saved_binlog_file:
if binlog_file_number >= saved_binlog_file_number:
replicate = True
if replicate is True:
print(binlog_file)
print(binlog_position)
schema = binlogevent.schema
table = binlogevent.table
print(schema + '.' + table)
for row in binlogevent.rows:
# Row deletion:
if isinstance(binlogevent, DeleteRowsEvent):
values = row['values']
print('delete')
print(values)
redis_stream_appender(
redis_server,
schema,
table,
'delete',
values
)
# Row update:
elif isinstance(binlogevent, UpdateRowsEvent):
combined_values = {
'before_values': {
row['before_values']
},
'after_values': {
row['after_values']
}
}
combined_values_json = json.dumps(
combined_values
)
print('update')
print(old_values)
print(new_values)
redis_stream_appender(
redis_server,
schema,
table,
'update',
combined_values_json
)
# Row insertion:
elif isinstance(binlogevent, WriteRowsEvent):
values = row['values']
print('insert')
print(values)
redis_stream_appender(
redis_server,
schema,
table,
'insert',
values
)
print('==============================')
# Last MySQL binary log position is
# saved only after the last database update is
# stored in Redis:
if binlog_file == saved_binlog_file:
if binlog_position >= saved_binlog_position:
redis_server.set(
PREFIX + ':last_binlog_position',
binlog_position
)
# Last MySQL binary log file is
# saved only after the last database update is
# stored in Redis:
if binlog_file != saved_binlog_file:
if binlog_file_number >= saved_binlog_file_number:
redis_server.set(
PREFIX + ':last_binlog_file',
binlog_file
)
except (pymysql.err.OperationalError):
print('No MySQL!')
exit(1)
def main():
try:
mysql_binlog_watcher()
except (KeyboardInterrupt):
print('')
exit(0)
if __name__ == '__main__':
main()