#!/usr/bin/python
import re
import os
import urllib
try:
import simplejson
except ImportError:
os.popen(‘yum install python-simplejson -y‘)
try:
import pycurl
except ImportError:
os.popen(‘yum install python-pycurl -y‘)
def buffer_line(monitor_log):
buf = open("/etc/sae/rdc_buffer.txt").read()
bytes=os.popen(‘wc -c /data0/logs/rdcmonitor/%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):
open("/etc/sae/rdc_buffer.txt", "w").write(str(pos))
def monitor_work(monitor_log):
fh=open(‘/data0/logs/rdcmonitor/%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)
new_lines=content.split("\n")
return new_lines
def monitor_work_1(content):
dict_1={‘service‘:‘rdc_monitor‘,‘checkpoint‘:‘rdc_monitor_log‘,‘title‘:content,‘content‘:content,‘cluster‘:‘public‘,‘grade‘:‘2‘}
params = urllib.urlencode(dict_1)
urllib.urlopen(‘http://alert.sae.sina.com.cn/new/‘,params)
#cc = pycurl.Curl()
#cc.setopt(cc.URL,‘http://alert.sae.sina.com.cn/new/?service=rdc_monitor&checkpoint=rdc_monitor_log&title=%s&content=%s&cluster=public&grade=2‘%(title,content)
)
#cc.perform()
if __name__ == ‘__main__‘:
log=os.popen("ls -lrt /data0/logs/rdcmonitor/|awk ‘END{print}‘").read()
print log
now_log=log.split(‘\n‘)[0].split()[-1]
value=monitor_work(now_log)
list_1=[]
host_ip=os.popen(‘hostname‘).read().split(‘\n‘)[0]
for i in value:
print i
if i.find(‘error‘) >= 0:
print i
list_1.append(i)
print list_1
if len(list_1) > 0:
date_json=simplejson.dumps(list_1)
print ‘error‘
list_len=len(list_1)
monitor_work_1(host_ip+‘_rdc_log_error:‘+date_json)