Use Python to read the Nginx log and write the required information to the database.

Source: Internet
Author: User

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

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.