nginx将日志存入oracle,统计nginx日志中访客的IP和次数,并且实时写入redis中。

具体实现的代码如下:

#!/usr/bin/python

#coding=utf-8

#__author__ = 'chenhuachao'

#-------------------------------

#created by chenhuachao on 2015/7/15

#-------------------------------

import re

from time import sleep

import redis

#iipcount = []

def get_ip():

'''这个函数,主要是统计出日志里面的访问IP,和统计出IP出现的个数

'''

print "\033[31m程序每5秒正常运行一次中...\033[0m"

print "\033[32mUse Control-C to exit\033[0m"

iplist=[]

log_file=open(r"/usr/local/nginx/logs/access.log","r+")

for line in log_file:

#print line

#sleep (2)

ip = re.match(r"\d+\.\d+.\d+.\d+",line)

#print ip.group()

iplist.append(ip.group())

print iplist

ipcount=set(iplist)

#print ipcount

for countip in ipcount:

count_ip=iplist.count(countip)

print count_ip

conn = redis.StrictRedis(host='localhost',port=6379)

#print iplist.count(countip),"of",countip,"in list"

conn.hset('wiki.log-host','%s'%countip,'%s'%count_ip)

if __name__ == '__main__':

print "\033[31m程序每5秒正常运行一次中...\033[0m"

while True:

try:

sleep(5)

get_ip()

except KeyboardInterrupt:

print "\033[32m 退出了。。。\033[0m"

break

4b1795649c1d6425a31b050e73baaf02.png19dd56e9ac7fab2398c696d4a95b97f9.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值