#!/usr/bin/env python# coding:utf-8# auther:liangkai# date:2018/6/26 11:26# License: (C) Copyright 2013-2017, Node Supply Chain Manager Corporation limited.# describe:import pymysqlimport reimport datetime# DB variablesdbhost = "192.168.189.18 5 "Dbport = 3306dbuser = ' root ' Dbpassword = ' 123456 ' charset = ' UTF8 ' # connection database conn_db = Pymysql.connect (host=dbhost,port=dbpo Rt,user=dbuser,password=dbpassword,charset=charset) Cursor=conn_db.cursor () # CREATE DATABASE Cursor.execute ("Create db If not exists Nginx ") # creates table create_table = ' CREATE TABLE nginx.accesslog (ID int (4), IP char (), time timestamp, Method c Har (4), Status Int (4), Request_time float (4), X_forwarded_for char (8), Host char (), primary key (ID)); Cursor.execute (create_table) # Nginx Log Variableswith open ("Zabbix.eub-inc.com.access.log", ' R ') as F:list = F.readlines () for I in range (0,len (list)): Logdata = List[i].strip () matchobj = Re.search (R ' (. *)--\[(. *) \] \ "(. *) (\/.*) \ "(. *) (. *) (. *) \" (. *) \ "\" (. *) \ "\" (. *) \ "\" (. *) \ "'", Logdata) IF matchobj! = None:ip = Matchobj.group (1) time = Matchobj.group (2) [0:20] method = Matchobj.group (3) request = matchobj.gr OUP (4) status = Int (Matchobj.group (5)) bytessent = Int (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 as MySQL number Database supported formats format = '%d/%b/%y:%h:%m:%s ' time = datetime.datetime.strptime (time, format) print (time) # Insert data SQL statement sql = "Insert Into Nginx.accesslog (IP, Time, Method, Status, Request_time, Host) VALUES ('%s ', '%s ', '%s ', '%d ', '%f ', '%s '); "% (Ip,time, Method,int (status), float (request_time), Host) # Execute INSERT statement try:cursor.execute (SQL) Conn_db.commit () print ("Insert OK") Except:conn_db.rollback () print ("Insert Error!!!") Cursor.close () Conn_db.close ()
The
uses Python implementations to read the Nginx log and write the required information to the database.