Python reads Nginx logs into the database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.