Chinaunix首页 | 论坛 | 博客
  • 博客访问: 266085
  • 博文数量: 53
  • 博客积分: 2580
  • 博客等级: 少校
  • 技术积分: 509
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-29 10:02
文章分类

全部博文(53)

文章存档

2014年(1)

2013年(1)

2011年(14)

2010年(37)

我的朋友

分类: LINUX

2010-06-17 18:37:33

    日志是很重要的东西,我们可以分析日志得出很多规律和结论。而cacti又是很强大的画图工具,可以通过它把数据以图表的方式表现出来。
 
    下面我要做的事情,就是用脚本对日志做实时分析,把结果输出到文本中,然后自定义OID,获取其中的值,而cacti通过自定义图表,用snmp取值,用rrdtool画图。
 
    关键在于对日志做实时分析,这个说出来其实很简单,使用一个tail -f 读日志文件,通过管道,调用自己写的脚本来分析。
 
将24小时分成5分钟一段,每条日志的三个数据(结束时间,耗时,字节数)读出来,根据结束时间(注意日志里的时间是结束时间,不是开始时间)计算出开始时间,然后看这个时间落在哪几个5分钟的段里面,将它的字节大小乘以1.07, 按秒平均后放进去。这样就可以算出之前的流量。
 
乘以1.07是国外通行的做法,因为网络开销比这个日志里的字节数要大(包头啊等等)。
 
由于在实时计算的时候,可能某些请求还没有结束(一个请求完成后才会写进日志),因此在20分钟内,可能算出来的流量图会变化,基本上20分钟后出来的流量数据就准确了。
 
nginx日志格式如下:
 

log_format log '$remote_addr - $remote_user [$time_local] "$request" '
                              '$status $body_bytes_sent "$http_referer" '
                              '"$http_user_agent" $http_x_forwarded_for $request_time $status $http_range ';

 
 
nginx日志分析脚本如下:
 

[root@test ~]# cat /home/test/tools/videoServerLogAnalog.py
#!/usr/bin/python
import IPInfo
import re
import time
import sys
from os import popen
patt = re.compile('([\d\\.]{7,}).+?\[(.+?)\s+.+?\].+?GET\s+\/(.+?\.mp4)\?segno.+?\s+.+?\s+.+?\s+(\d+).+\s+([\d\\.]+)\s+([\d]{3})')
timeformat="%d/%b/%Y:%H:%M:%S"
outformat="%Y%m%d_%H%M"
ip = IPInfo.IPInfo("/home/test/tools/myIPFile.txt")
speedRecord={}
curRange=0
def getsysInfo():
    temp=popen('/bin/netstat -ano|/bin/grep :80|/bin/grep ESTABLISHED|/usr/bin/wc -l').readlines()
    for line in temp:
        return line.rstrip().replace('\r','')
getsysInfo()
for line in sys.stdin:
    m=patt.search(line)
    if patt.match(line)!= None :
        if(float(m.group(5)) > 0.0):
            startsecs = time.mktime(time.strptime(m.group(2),timeformat)) - float(m.group(5))
            range=int(startsecs/300)*300
            sysRang = int((time.time())/300)*300
            #if range < (sysRang - 300 *2) :
            # continue
            if( curRange):
                #do record write and change curRange
                pre2Time = curRange-300*2
                if(speedRecord.has_key(pre2Time)):
                    print time.strftime(outformat,time.gmtime(time.time()+8*60*60))+' do write files'
                    myTimeRange=speedRecord.pop(pre2Time)
                    f1 = open('/tmp/speed.txt','w')
                    f1.write(str(int(long(myTimeRange[0])/(float(myTimeRange[1])*1000))))
                    f1.close()
                    f2 = open('/tmp/conn.txt','w')
                    f2.write(getsysInfo())
                    f2.close()
                    f3 = open('/tmp/block.txt','w')
                    f3.write(str(int(long(myTimeRange[0])/(int(myTimeRange[2])*1000))))
                    f3.close()
                curRange=sysRang
                
            #record current data to memory
            if speedRecord.has_key(range):
                speedRecord[range]=[speedRecord[range][0]+long(m.group(4)),speedRecord[range][1]+float(m.group(5)),speedRecord[range][2]+1]
            else:
                speedRecord[range]=[long(m.group(4)),float(m.group(5)),1]

 
在snmp自定义了3个OID,如下:(直接追加到/etc/snmp/snmpd.conf下,然后重启snmpd)
exec .1.3.6.1.4.1.15.6.13  speed  /bin/cat /tmp/speed.txt
exec .1.3.6.1.4.1.15.6.14  conn  /bin/cat /tmp/conn.txt
exec .1.3.6.1.4.1.15.6.15  block  /bin/cat /tmp/block.txt
 

[root@test ~]# cat /home/test/tools/speedRotate.sh

#!/bin/sh
#program:
# This program is used to kill the previous traffic speed scripts and restart a new scripts
#export the environment
JAVA_HOME=/usr/local/java/
export JAVA_HOME
export LANG=zh_CN.GB18030
#clear two days ago files
twoDays=`date -d "2 days ago" +%Y%m%d`
#twoDays=`date +%Y%m%d`
if [ -f /home/test/web/$twoDays'_0000'.txt ];then
echo $twoDays
for file in `ls /home/test/web/$twoDays*.txt`
do
        /bin/rm $file
        echo $file
done
fi
#kill previous scripts
/usr/bin/pkill -9 python
#start new scripts
/usr/bin/tail -f /home/nginx/logs/access.log | /usr/bin/python /home/test/tools/videoServerLogAnalog.py &


然后把这个shell脚本写到crond里面去,如下:

0 0 * * *  /home/test/tools/speedRotate.sh  > /dev/null 2>&1

这样就可以取值并且写到/tmp下的几个文件中了。

接下来就是在cacti中自定义图表,并添加到常用机器模板中了


阅读(3732) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-06-17 22:37:54

用tail -f 然后管道到分析脚本 真是好方法!谢谢share!