#!/usr/bin/python
import re
import os
import sys
import pycurl
def buffer_line(monitor_log):
buf = open("/data0/webinternal_monitor/%s"%monitor_log).read()
bytes=os.popen(‘wc -c /data0/logs/%s‘%monitor_log).read().split(" ")[0]
if int(bytes) < int(buf):
return 0
else:
return int(re.findall("^\d*", buf)[0])
def set_last_pos(pos,monitor_log):
open("/data0/webinternal_monitor/%s"%monitor_log, "w").write(str(pos))
def monitor_work(monitor_log):
fh=open(‘/data0/logs/%s‘%monitor_log,‘r‘)
fh.seek(buffer_line(monitor_log))
content=fh.read()
new_total_lines=len(content)+buffer_line(monitor_log)
set_last_pos(new_total_lines,monitor_log)
new_lines=content.split("\n")
return new_lines
def monitor_work_1(title,content):
cc = pycurl.Curl()
cc.setopt(cc.URL,‘http://alert.sae.sina.com.cn/new/?service=webinternal_monitor_time&checkpoint=averge_response_time&title=%s&content=%s&cluster=public&grade=2‘
cc.perform()
def monitor_work_2(title,content):
print ‘2‘
dd = pycurl.Curl()
dd.setopt(dd.URL,‘http://alert.sae.sina.com.cn/new/?service=webinternal_monitor&checkpoint=500_error&title=%s&content=%s&cluster=public&grade=2‘%(title,content)
print dd.perform()
if __name__ == ‘__main__‘:
log_list=[‘atc.sae.sina.com.cn-access_log‘,‘check.sae.sina.com.cn-access_log‘,‘deferredjob.sae.sina.com.cn-access_log‘,‘image.sae.sina.com.cn-access_log‘,‘internal.
for i in log_list:
appname={}
averge_list=[]
g=0
monitor_file=monitor_work(i)
for ii in monitor_file:
if ii:
b=re.search(r‘\] (\d.*?) \".*HTTP/1..\" (\d...*?)‘,ii)
try:
c=b.group(2)
except AttributeError, ee:
pass
try:
d=b.group(1)
averge_list.append(d)
except AttributeError, kk:
pass
if c not in appname:
appname[c]=1
else:
appname[c]+=1
for e in averge_list:
g+=int(e)
try:
f=g/len(averge_list)
appname[‘averge_response_time‘]=f
except ZeroDivisionError, l:
appname[‘averge_response_time‘]=0
print i,appname
if ‘500‘ in appname:
print ‘500‘
if appname[‘500‘] > 0:
error=i,‘ 500error‘
print appname[‘500‘]
monitor_work_2(i+‘_‘+str(appname[‘500‘])+‘_error‘,i+‘_‘+str(appname[‘500‘])+‘_error‘)
if appname[‘averge_response_time‘] >= 1000000:
print ‘ok‘
error_averge=appname[‘averge_response_time‘]
monitor_work_1(i+‘_‘+str(appname[‘averge_response_time‘])+‘_microsecond‘,i+‘_‘+str(appname[‘averge_response_time‘])+‘_microsecond‘)
原文地址:http://4249964.blog.51cto.com/4239964/1557266