#!/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