#!/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 pymysql
import re
import datetime
# DB variables
dbhost = "192.168.189.185"
dbport = 3306
dbuser = ‘root‘
dbpassword = ‘123456‘
charset = ‘utf8‘
# 连接数据库
conn_db = pymysql.connect(host=dbhost,port=dbport,user=dbuser,password=dbpassword,charset=charset)
cursor=conn_db.cursor()
# 创建数据库
cursor.execute("create database if not exists nginx")
# 创建表
create_table = ‘‘‘create table nginx.accesslog( id int(4), IP char(20), time timestamp, Method char(4), Status int(4),Request_time float(4), X_Forwarded_for char(8),Host char(20),primary key(id) );
‘‘‘
cursor.execute(create_table)
# Nginx log variables
with 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.group(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(10)
Host = matchObj.group(11)
# 时间格式化为mysql数据库支持的格式
format = ‘%d/%b/%Y:%H:%M:%S‘
time = datetime.datetime.strptime(time, format)
print(time)
# 插入数据SQL语句
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)
# 执行插入语句
try:
cursor.execute(sql)
conn_db.commit()
print("insert Ok")
except:
conn_db.rollback()
print("insert Error !!!")
cursor.close()
conn_db.close()
原文地址:http://blog.51cto.com/liangkai/2135634