Log data:
#/usr/bin/env python#-*-coding:utf-8 -*-from datetime import Datetimestat_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 format #print (datetime.strptime (' 17/jun/2017:12:11:16 ', "%d/%b/%y:%h:%m:%s")) # Format Conversion 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 () &nbsP; ip= _nodes[0] time= _nodes[3][1:-1].replace (" : "," ", 1) #将时间转换为17/jun/2017 12:43:4 format 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 statements
Create database if not EXISTS nginxlog default charset utf8;create table Nginxlog (id int (TEN) primary key auto _increment,ip varchar (2) NOT null default ' ', Time datetime,methods varchar (TEN) NOT null default ' ', source varchar (A-a), ' ",", " ) NOT null default ' ', Protocol varchar (TEN) NOT null default ' ', Status varchar (TEN) NOT NULL default ') ALTER TABLE NGI Nxlog Modify column source varchar (400);
Python reads Nginx logs into the database