1. 程式人生 > >Python讀取NGINX日誌將其存入數據庫

Python讀取NGINX日誌將其存入數據庫

python pymysql datetime

日誌數據:

#/usr/bin/env python
#-*-coding:UTF-8 -*-
from  datetime  import  datetime

stat_days = []
import   pymysql
#print(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) #2018-05-25 22:23:44
#print(datetime.now().strftime("%d/%b-%Y %H:%M:%S")) #25/May-2018 22:23:44格式
#print(datetime.strptime('17/Jun/2017:12:11:16',"%d/%b/%Y:%H:%M:%S")) #格式轉換
connect=pymysql.connect(
   host='127.0.0.1',
   port=3306,
   user='root',
   password='',
   database='nginxlog',
   charset="utf8"

)
cur = connect.cursor()
sql= "insert into nginxlog(ip,time,methods,source,protocol,status) values(%s,%s,%s,%s,%s,%s)"

with open("portal_ssl.access.log","r")  as  ngfile:
   for line in ngfile:
      _nodes = line.split()
      IP= _nodes[0]
      Time= _nodes[3][1:-1].replace(":"," ",1) #將時間轉換為17/Jun/2017 12:43:4格式
      Time = datetime.strptime(Time,"%d/%b/%Y %H:%M:%S")#將時間格式化為2017-06-17 12:43:04
      Methods = _nodes[5][1:]
      Source = _nodes[6]
      Protocol = _nodes[7][:-1]
      Status = _nodes[8]
      print(IP,Time,Methods,Source,Protocol,Status)
      cur.execute(sql,(IP,Time,Methods,Source,Protocol,Status))
      connect.commit()
   connect.close()

SQL語句

create  database   if  not exists  nginxlog     default  charset  utf8;
create table nginxlog (
id int(10)  primary key auto_increment,
ip  varchar(30) not null  default '',
time  datetime,
methods  varchar(10) not null  default '',
source  varchar(200) not null  default '',
protocol varchar(10) not null  default '',
status varchar(10) not null  default ''
)

alter table nginxlog modify column source varchar(400);


Python讀取NGINX日誌將其存入數據庫