Initial commit

This commit is contained in:
hebo
2019-08-08 15:20:56 +08:00
parent 16b067ec89
commit 0f4a202c60
29 changed files with 1482 additions and 2 deletions

53
scripts/check_kafka.py Normal file
View File

@@ -0,0 +1,53 @@
# import json
from kafka import KafkaConsumer, KafkaProducer
kafka_server = '192.168.XX.XX:9091'
group_id = 'sniffer'
topic = 'ddl_sql_collector'
def check_consume():
conf = {
'bootstrap_servers': kafka_server,
'client_id': group_id,
'group_id': group_id,
'auto_offset_reset': 'earliest',
'session_timeout_ms': 60000,
'api_version': (0, 9, 0, 1)
}
consumer = KafkaConsumer(topic, **conf)
print('ready to consume')
for msg in consumer:
# event = json.loads(bytes.decode(msg.value))
print(msg)
def check_produce():
conf = {
'bootstrap_servers': kafka_server,
'client_id': group_id
}
# 'api_version': (0, 9, 0, 1)
producer = KafkaProducer(**conf)
try:
future = producer.send(topic, 'haha')
result = future.get(timeout=3)
print('send OK')
print(result)
except BaseException as e:
print('send failed')
# 发送失败时,用户需根据业务逻辑做异常处理,否则消息可能会丢失
print(str(e))
def _real_main():
check_produce()
# check_consume()
if __name__ == '__main__':
_real_main()

75
scripts/check_prepare.py Normal file
View File

@@ -0,0 +1,75 @@
import time
import mysql.connector
from mysql.connector.cursor import MySQLCursorPrepared
config = {
'host': '192.168.XX.XX',
'port': 3358,
'database': 'sniffer',
'user': 'root',
'password': '',
'charset': 'utf8',
'use_unicode': True,
'get_warnings': True,
}
def _real_main(config):
while True:
_once_check()
def _once_check():
output = []
conn = mysql.connector.Connect(**config)
curprep = conn.cursor(cursor_class=MySQLCursorPrepared)
cur = conn.cursor()
# Drop table if exists, and create it new
stmt_drop = "DROP TABLE IF EXISTS names"
cur.execute(stmt_drop)
stmt_create = (
"CREATE TABLE names ("
"id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT, "
"name VARCHAR(30) DEFAULT '' NOT NULL, "
"cnt TINYINT UNSIGNED DEFAULT 0, "
"PRIMARY KEY (id))"
)
cur.execute(stmt_create)
# Connector/Python also allows ? as placeholders for MySQL Prepared
# statements.
prepstmt = "INSERT INTO names (name) VALUES (%s)"
# Preparing the statement is done only once. It can be done before
# without data, or later with data.
curprep.execute(prepstmt)
# Insert 3 records
names = (
'Geert', 'Jan', 'Michel', 'wang', 'Jan', 'Michel', 'wang',
'Jan', 'Michel', 'wang', 'Jan', 'Michel', 'wang', 'Jan', 'Michel',
'wang', 'Jan', 'Michel', 'wang', 'Jan', 'Michel', 'wang', 'Jan',
'Jan', 'Michel', 'wang')
for name in names:
curprep.execute(prepstmt, (name,))
conn.commit()
time.sleep(0.1)
# We use a normal cursor issue a SELECT
output.append("Inserted data")
cur.execute("SELECT id, name FROM names")
for row in cur:
output.append("{0} | {1}".format(*row))
# Cleaning up, dropping the table again
cur.execute(stmt_drop)
conn.close()
print(output)
if __name__ == '__main__':
_real_main(config)

View File

@@ -0,0 +1,31 @@
#!/usr/bin/env bash
function execute_real(){
mysql_host=192.168.XX.XX
mysql_port=3358
user_name=user
passwd=123456
mysql -h$mysql_host -P$mysql_port -u$user_name -p$passwd jmms -e "select 1"
sleep 1
mysql -h$mysql_host -P$mysql_port -u$user_name -p$passwd jmms -e "use sniffer;show tables;create table haha(id int, name text)"
sleep 1
mysql -h$mysql_host -P$mysql_port -u$user_name -p$passwd jmms -e ""
sleep 1
mysql -h$mysql_host -P$mysql_port -u$user_name -p$passwd jmms -e ""
sleep 1
insert_cmd="insert into unibase.haha(id, name) values(10, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')"
insert_cmd="$insert_cmd,(10, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')"
insert_cmd="$insert_cmd,(10, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')"
insert_cmd="$insert_cmd,(10, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')"
insert_cmd="$insert_cmd,(10, 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')"
mysql -h$mysql_host -P$mysql_port -u$user_name -p$passwd jmms -e "$insert_cmd"
sleep 1
mysql -h$mysql_host -P$mysql_port -u$user_name -p$passwd jmms -e "use unibase; select * from haha; drop table haha"
sleep 1
}
while true
do
execute_real
done