import taos
import re
import time
def init_database_re(p_config,p_hostname, p_user='root', p_password='taosdata'):
'''
@parameter
taosd_host_name: TDengine服務(wù)器的hostname
user: TDengine服務(wù)器的登錄user
passwd: TDengine服務(wù)器登錄user對應(yīng)的password
@return
conn: 連接TDengine數(shù)據(jù)庫使用的connection句柄
c1: 操作TDengine數(shù)據(jù)庫使用的cursor句柄
pattern: 用于匹配fastlog中每行數(shù)據(jù)的正則表達(dá)式
@function
1. 如果不存在,為遠(yuǎn)端TDengine創(chuàng)建suricata數(shù)據(jù)庫
2. 如果不存在,為遠(yuǎn)端TDengine中的suricat數(shù)據(jù)庫創(chuàng)建表fastlog
3. 預(yù)先編譯正則表達(dá)式對象,用于匹配fast.log中的每行數(shù)據(jù)
'''
conn = taos.connect(host= p_hostname, user=p_user,password=p_password, config=p_config)
c1 = conn.cursor()
try:
c1.execute('create database if not exists suricata')
c1.execute('use suricata')
c1.execute('create table if not exists fastlog(ts timestamp, type binary(100), priority int, protocol binary(15), srcip binary(20), srcport int, dstip binary(20), dstport int)')
except Exception as err:
conn.close()
raise(err)
pattern = re.compile(r'(.*) (\[\*\*\]) (\[.*\]) (.*) (\[\*\*\]) (\[)(.*): (.*)(\]) (\[)(.*): (.*)(\]) (\{)(.*)(\}) (.*):(\d+) -> (.*):(\d+)')
return conn, c1, pattern
def insert(p_conn,p_c1,p_ts,p_type,p_priority,p_protocol,p_srcip,p_srcport,p_dstip,p_dstport):
'''
@param
p_conn: 連接TDengine數(shù)據(jù)庫的connection句柄
p_c1: 向suricata.fastlog插入數(shù)據(jù)使用的cursor句柄
p_ts: 告警產(chǎn)生的時間
p_type: 告警類型
p_priority: 告警優(yōu)先級
p_protocol: 告警packet的網(wǎng)絡(luò)協(xié)議
p_srcip: 告警packet的源IP
p_srcport: 告警packet的源端口
p_dstip: 告警packet的目的IP
p_dstport: 告警packet的目的端口
@function:
1. 根據(jù)參數(shù)構(gòu)建insert sql
2. 使用構(gòu)造的insert sql向TDengine中的suricata.fastlog插入數(shù)據(jù)
'''
sql_fixed = "insert into suricata.fastlog(ts, type, priority, protocol, srcip, srcport, dstip, dstport)"
sql_value = "values("+p_ts+",'"+p_type+"',"+p_priority+",'"+p_protocol+"','"+p_srcip+"',"+p_srcport+",'"+p_dstip+"',"+p_dstport+")"
try:
p_c1.execute(sql_fixed+sql_value)
print(sql_fixed+sql_value)
except Exception as err:
p_conn.close()
raise(err)
def upload(p_filename, p_conn, p_c1, p_pattern):
with open(p_filename) as f:
for line in f:
result = p_pattern.match(line)
ts = int(time.mktime(time.strptime(result.group(1)[:-8], '%m/%d/%Y-%H:%M:%S')))*1000 + int(result.group(1)[-6:-3])
insert(p_conn, p_c1, str(ts), result.group(8), result.group(12), result.group(15), result.group(17), result.group(18), result.group(19), result.group(20))
p_conn.close()
conn, c1, pattern = init_database_re(p_config="C:\\TDengine\\cfg",p_hostname="fmr-dev-centos7", p_user='root', p_password='taosdata')
upload(p_filename="./fast.log", p_conn=conn, p_c1=c1, p_pattern=pattern)
使用python腳本解析本地日志文件,并上傳至TDengine
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 開篇先講一下我遇到的需求:我是客戶給我了一個在快手上爬蟲下來的json文件,要求我根據(jù)文件的視頻鏈接和圖片鏈接上傳...
- 命令行獲取目標(biāo)事件ID的日志 現(xiàn)在我們嘗試將“應(yīng)用程序(Application)”路徑下,所有消息(Level=4...
- 處理流程 記錄accesslog日志 切割整理accesslog日志 入庫數(shù)據(jù) 數(shù)據(jù)抽取、分割 制作報表 適用場景...
- 項目的一個需求是解析nginx的日志文件。簡單的整理如下: 日志規(guī)則描述 首先要明確自己的Nginx的日志格式,這...