run.py
#! /usr/bin/env python
# -*- coding: cp936 -*-
# -*- coding: gbk -*-
import os
import re
import string
import monitor
print '请输入您需要查找错误信息的URL (例:my.xxxx.com) \n'
target_url = raw_input('Enter target url:')
'请输入您需要查找模块,详细模块信息可见/data/xxxx/system_monitor/server/tomcat/(例:my)
\n'
module = raw_input('Enter target module:')
print '请输入您需要查找模块的时间(例:2008-11-24) \n'
err_time = raw_input('Enter target date:')
print '您输入的信息如下:\n'
print 'target_url: %s' %target_url
print 'module: %s' %module
print 'date: %s' %err_time
yes = raw_input('请检查您输入的信息,(y / n):')
if ( yes == 'y' or yes == 'y'.upper()):
monitor.monitor(target_url, module, err_time)
else:
'no'
target_url =
''
module =
''
err_time =
''
#! /usr/bin/env python
# -*- coding: cp936 -*-
# author: jonas
# Date: 2008-11-19
# funtion: get us_product error infomation
import err_time_ip
import time_map
import check_access
import err_search
import msgcheck
import log
import send
def monitor(value, module, err_date):
print 'It is
running....'
'Please wait a moment...'
value =
value
module =
module
err_date =
err_date
msg_check =
[]
result_list
= []
# attack
files
files
=[]
filename =
'/data/xxxx/system_monitor/exception_check/logs/'+log.getTimeYmdHM()+'.txt'
files.append(filename)
# get
time of error occus list_msgcheck = err_time_ip.search_err(value,err_date)
# get time
map of time
timemap =
time_map.getTime(list_msgcheck)
tag =
0
list_sample
= []
#
sampling data
for key in
list_msgcheck:
if(tag % 20 == 0):
list_sample.append(key)
tag += 1
#
ip<=>time
ip_time_list
= check_access.check(module, list_sample, timemap)
# get error
information
errmsg_list
= err_search.search(ip_time_list)
#delete
re error information : frist time
for key in
errmsg_list:
for line in key:
resul, msg_check = msgcheck.checklist(line, msg_check)
# if
numbers of ':' > 3, split it
for key in
msg_check:
num = key.count(':', 0, len(key))
str_temp = ''
if (num >= 3):
strs = key.split(':')
str_temp = strs[-3]+':'+strs[-2]+':'+strs[-1]
else:
str_temp = key
#delete re error infroamtion : second time
resul, result_list = msgcheck.checklist(str_temp,
result_list)
# save error
information
log.appendFile(filename, result_list)
# send
if
len(result_list):
send.send(value, err_date, result_list,files)
for key in
result_list:
print key
print 'It is
finished....'
if __name__=='__main__':
monitor('my.eachnet.com', 'my', '2008-11-24')
#! /usr/bin/env python
# -*- coding: cp936 -*-
# author: jonas
# Date: 2008-11-19
def msg():
title_value
= '错误信息的原因'
headmsg_value ='错误原因详细情况:'
dict =
{'title':title_value,
'headmsg':headmsg_value,
'endmsg':'提示:结果是由脚本生成,未删除脏数据(若没有错误信息,则不发邮件)!'
}
return
dict
if __name__=='__main__':
msg =
msg()
for key in
msg:
print key, msg[key]
#! /usr/bin/env python
# -*- coding: cp936 -*-
# -*- coding: gbk -*-
# check the product_ID whether exist in msg_list
# author: jonas
# date: 2008-11-19
# check the product_ID whether exist in msg_list
# if exist, return False
# else return True
def checklist(product_ID,msg_list):
result =
True
# check
product_ID wheter exist or not
for key in
msg_list:
if key == product_ID:
result = False
# if
product_ID is not exist, add product_ID into msg_list
if result ==
True:
msg_list.append(product_ID)
return
result, msg_list
if __name__=='__main__':
result,
msg_list = checklist('1234568',['123456','789456','456'])
result,"\n"
msg_list
#! /usr/bin/env python
# -*- coding: cp936 -*-
# -*- coding: gbk -*-
# author: jonas
# Date: 2008-11-19
# funtion: send mail
import string
import sendmail
import msg
def send(err_url, err_date, list_result, files):
toMails =
('jonas@xxxx.com', 'keith@xxxxx.com','felix@xxxx.com')
ccMails =
('jonas@xxxx.com',)
mailmsg =
msg.msg()
title =
mailmsg['title'].decode("gbk").encode("utf-8")
title =
err_url+' '+title+'['+ err_date +']'
endmsg =
mailmsg['endmsg'].decode("gbk").encode("utf-8")
headmsg =
mailmsg['headmsg'].decode("gbk").encode("utf-8")
message =
''
files =
files
list_result
=
list_result message +=
err_url+' '+err_date+' '+headmsg + '\r\n'
message +=
'\r\n\n'
tag =
1
for line in
list_result:
message += str(tag)+'. '+line + '\r\n\n'
tag += 1
message +=
'\r\n\n\n'
message +=
endmsg + '\r\n'
message +=
'\r\n\t\t\tPower By Python\r\t'
sendmail.SendMail(toMails, ccMails, title, message,
files).send()
# -*- coding: cp936 -*-
# author: jonas
# Date: 2008-11-19
# funtion: get us_product error infomation\
import os
import re
import string
def getTime(list):
timemap =
{}
for line in
list:
line = line.strip()
tag = line.find(' ', 0, len(line))
listtime = []
time = str(line[0:tag].strip())
listtime.append(str(line[0:4])) #year
listtime.append(str(line[4:6])) #month
listtime.append(str(line[6:8])) #day
listtime.append(str(line[8:10])) #hour
listtime.append(str(line[10:12])) #minute
listtime.append(str(line[12:len(time)])) #second
timemap[time] = listtime
return
timemap
if __name__=='__main__':
list
=['20081124031125 http://pic12xxxx.com/user_2007395670/product/8120/8120.jpg',
'20081124174109 http://pic13.xxxxxx.com/user_2007632518/product/others/��ϥ����%20Ԫ.jpg',
'20081124224032 http://syi.xxxx.com/jsp/prd/up?']
dict =
getTime(list)
for key in
dict:
print 'key = %s, value = %s' %(key,dict[key])
import os
import time
def getDateStr():
return time.strftime('%Y-%m-%d',
time.localtime())
def getTime():
return
time.strftime('%Y-%m-%d %H:%M', time.localtime())
def getTimeYmdHM():
return
time.strftime('%Y%m%d%H%M', time.localtime())
def outFile(path, msgList):
if not
os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
file = open(path, 'a')
file.write(getTime()+'\n')
for line in msgList:
file.write(line + '\r\n')
file.close
def appendFile(path, msgList):
if not
os.path.exists(os.path.dirname(path)):
os.makedirs(os.path.dirname(path))
file =
open(path, 'a')
for line in
msgList:
file.write(line + '\r\n')
file.close()
if __name__ == '__main__':
msgs = []
msgs.append('a')
msgs.append('b')
msgs.append('c')
path = 'e:\\python\\txt\\' + getDateStr() +
'.txt'
outFile(path, msgs)
grep $1 $2 | awk {' print $4" "$14 '} | sort -k 2
# -*- coding: cp936 -*-
# author: jonas
# Date: 2008-11-19
# funtion: get us_product error infomation
import os
import re
import string
from string import Template
def search_err(key_value,err_date):
cmdTemp =
Template('sh err_time_ip.sh ${value} ${path}')
value =
key_value
# err
information date
logdate =
err_date
# log file
path
path =
'/data/xxxx/logs/errorlog/'+logdate+'.error.log'
# set
value of ssh and grep
cmd =
cmdTemp.substitute(value = value, path = path)
list_msgcheck = []
for line in
os.popen(cmd):
line = line.strip()
list_msgcheck.append(line)
return
list_msgcheck
#! /usr/bin/env python
import os
import re
import string
from string import Template
import do_search
def search(ip_time_list):
errmsg_list
= [] #save time,ip
for line in
ip_time_list:
line = line.strip()
temp_list = do_search.do_search(line)
if (len(temp_list) > 0 ):
errmsg_list.append(temp_list)
return
errmsg_list
if __name__=='__main__':
list =
search_err('my.xxxx.com','2008-11-24')
for key in
list:
print key
#!
/usr/bin/env python
import os
import re
import string
from string import Template
def do_search(log_err_info):
sshinfo =
Template('${hostinfo} grep ${fultime}
/data/logs/tomcat/catalina.out.${errdate} -C 50 | grep
Exception')
strs
= log_err_info.split('|')
ip =
str(strs[2]).strip()
log_date =
str(strs[0]).strip()
key_value =
'\"'+str(strs[1]).strip()+'\"'
hostinfo =
'ssh '+ip
cmd =
sshinfo.substitute(hostinfo = hostinfo, fultime = key_value,
errdate = log_date)
errmsg_list
= []
for line in
os.popen(cmd):
line = line.strip()
if len(line):
errmsg_list.append(line)
return
errmsg_list
if __name__=='__main__':
msglist =
do_search('20081124|17:41:09|172.24.138.154')
for key in
msglist:
print key
# -*- coding: cp936 -*-
# author: jonas
# Date: 2008-11-19
import os
import re
import string
from string import Template
def check(module, list_msgcheck, timemap):
hostTemp =
Template('ssh ${user}@${ip}')
cmdTemp =
Template('${host} grep ${value} ${path}')
list_time_ip = []
# modult's
path
module =
module
listTomcat =
'/data/xxxx/system_monitor/server/tomcat/'+module+'.txt'
for line in
list_msgcheck:
# get time
line = line = line.strip()
tag = line.find(' ', 0, len(line))
time = str(line[0:tag].strip())
for key in timemap:
if time == key:
time_list = timemap[time]
# set time as : 2008:15:20:30
time_temp = time_list[0]+':'+time_list[3]+':'+ time_list[4]+':'+
time_list[5]
#set time as: 2008-11-24 12:00;20
time_check = time_list[3]+':'+ time_list[4]+':'+ time_list[5]
#set time as:20081124
err_log_time = time_list[0] + time_list[1] +
time_list[2]
value = time_temp
#logfile name
logdate = time_list[0] +'-'+ time_list[1] +'-'+time_list[2]
path = '/data/logs/localhost_access_log.'+logdate+'.txt'
f = open(listTomcat,'r')
for ip in f:
iptag = ip.index(' ')
ip = ip[:iptag]
hostinfo = hostTemp.substitute(user = 'web', ip = ip)
cmd = cmdTemp.substitute(host = hostinfo, value = value, path =
path )
info_time = os.popen(cmd).readlines()
if (len(info_time) >0):
list_time_ip.append(err_log_time+'|'+time_check+'|'+ip)
f.close()
return
list_time_ip