#!/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 pymysql
import re
import datetime
import sys
import time
# DB variables
dbhost = "192.168.189.185"
dbport = 3306
dbuser = ‘root‘
dbpassword = ‘********‘
charset = ‘utf8‘
log_file = sys.argv[1]
# 连接数据库
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)
# monitor access log
pos = 0
while True:
fd = open(log_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 = 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)
# 插入数据SQL语句
insert_sql = "INSERT INTO nginx.accesslog(IP, TIME, Method, Status, bytesSent, Request_time, Host) values(‘%s‘,‘%s‘,‘%s‘,‘%d‘,‘%d‘, ‘%f‘,‘%s‘);" % (ip, Time, method, int(status), int(bytesSent), float(request_time), Host)
# 执行插入语句
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()
原文地址:http://blog.51cto.com/liangkai/2136517