Python Gets the Nginx access log, writes to the database

Source: Internet
Author: User
Tags create database

#!/usr/bin/env python# coding:utf-8# auther:liangkai# date:2018/6/26 11:26# License: (C) Copyright 2013-2018, Node Supply Chain Manager Corporation limited.# describe:import pymysqlimport reimport datetimeimport sysimport time# DB variablesdbh ost = "192.168.189.185" dbport = 3306dbuser = ' root ' Dbpassword = ' ******** ' charset = ' utf8 ' log_file = sys.argv[1]# connection Database con n_db = Pymysql.connect (host=dbhost,port=dbport,user=dbuser,password=dbpassword,charset=charset) cursor=conn_ Db.cursor () # CREATE DATABASE # Cursor.execute ("Create db if not exists Nginx") # # CREATE TABLE # create_table = "" CREATE TABLE Nginx.acce Sslog (id int (4), IP char (+), # time timestamp, Method char (4), Status Int (4), Request_time float (4), # x_forwarded_for Cha R (8), Host char (a), primary key (ID)); # "# Cursor.execute (create_table) # monitor Access Logpos = 0while true:fd = open (l og_file) If pos! = 0:fd.seek (pos, 0) while true:line = Fd.readline () if Line.strip (): Logdata=line.strip () Matchobj = RE . Search (R ' (. *)--\[(. *) \] \ "(. *) (\. *) \ "(. *) (. *) (. *) \" (. *) \ "\" (. *) \ "\" (. *) \ "\" (. *) \ "', logdata) if matchobj! = None:ip = Matchobj.group (1) time = Mat Chobj.group (2) [0:20] method = Matchobj.group (3) Request = Matchobj.group (4) status = Int (Matchobj.group (5)) BytesSent = in T (Matchobj.group (6)) Request_time = float (matchobj.group (7)) refer = Matchobj.group (8) Agent = Matchobj.group (9) X_ Forwarded_for = Matchobj.group (Ten) Host = Matchobj.group (11) # Time formatted for MySQL database supported format = '%d/%b/%y:%h:%m:%s ' times = dat Etime.datetime.strptime (time, format) # Insert data SQL statement Insert_sql = "INSERT into Nginx.accesslog (IP, Time, Method, Status, byte SSent, Request_time, Host) VALUES ('%s ', '%s ', '%s ', '%d ', '%d ', '%f ', '%s '); "% (IP, time, method, int (status), int (bytessent ), float (request_time), Host) # Execute INSERT statement try:cursor.execute (INSERT_SQL) conn_db.commit () Except:conn_db.rollback () print ("Insert Error!!!") pos = pos + len (line) if not Line.strip (): Break Fd.close () time.sleep (1) cursor.close () Conn_db.close ()

Python Gets the Nginx access log, writes to the database

Related Article

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.