利用python脚本US现金盘源码出售监控Tomcat服务器

对于最近的US现金盘源码出售论坛:haozbbs.com Q1446595067开发环境,偶尔会有挂掉的现象发生,然而并没有及时发现,下载需要添加一个监控功能,当服务挂掉的时候需要有邮件提醒,同时我们的系统每天晚上会跑定时任务,想知道有没有异常发生,所以添加了两个python监本监控,因为本身系统不大,所以没必要去配置kafka+storm这种日志监控了,只用了很简单的方式来处理了。

1、监控tomcat是否挂掉

[python] view plain copy
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.header import Header
from os.path import getsize
from sys import exit
from re import compile, IGNORECASE
import sys, time
import os

#定义主机 帐号 密码 收件人 邮件主题

#定义主机 帐号 密码 收件人 邮件主题
mail_info = {
"from": "[email protected]",
"to": "[email protected]",
"hostname": "smtp.exmail.qq.com",
"username": "[email protected]",
"password": "123456",
"mail_subject": "qybd服务器异常",
"mail_text": "hello, tomcat服务器出现异常了!,请及时处理",
"mail_encoding": "utf-8"
}

#发送邮件函数
def send_mail(error):

#定义邮件的头部信息  
#连接SMTP服务器,然后发送信息  
smtp = SMTP_SSL(mail_info["hostname"])  
smtp.set_debuglevel(1)  
smtp.ehlo(mail_info["hostname"])  
smtp.login(mail_info["username"], mail_info["password"])  
msg = MIMEText(error, "plain", mail_info["mail_encoding"])  
msg["Subject"] = Header(mail_info["mail_subject"], mail_info["mail_encoding"])  
msg["from"] = mail_info["from"]  
msg["to"] = mail_info["to"]  
smtp.sendmail(mail_info["from"], mail_info["to"], msg.as_string())  
smtp.quit()  

def isRunning(process_name):
try:
process = len(os.popen('ps aux | grep "' + process_name + '" | grep -v grep').readlines())
if process >= 1:
return True
else:
return False
except:
print("Check process ERROR!!!")
return False

#调用发送邮件函数发送邮件
if name == 'main':
process_name = "qybd"
isrunning = isRunning(process_name)
print(isrunning)
if isrunning == False:
send_mail("老铁!qybd服务器挂了!")
2、添加crontab定时任务:

/3 * python /usr/tools/qybd/cmd/sendEmail.py >> /usr/tools/qybd/cmd/tomcatlife.py.log 2>&1

3、使用crontab -u root -l 命令查看当前运行的定时任务

4、监控日志的脚本

[python] view plain copy
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.header import Header
from os.path import getsize
from sys import exit
from re import compile, IGNORECASE

#定义主机 帐号 密码 收件人 邮件主题

#定义主机 帐号 密码 收件人 邮件主题
mail_info = {
"from": "[email protected]",
"to": "[email protected]",
"hostname": "smtp.exmail.qq.com",
"username": "[email protected]",
"password": "123456",
"mail_subject": "qybd服务器异常",
"mail_text": "hello, tomcat服务器出现异常了!,请及时处理",
"mail_encoding": "utf-8"
}

#定义tomcat日志文件位置
tomcat_log = '/usr/tools/qybd/tomcat/logs/catalina.out'

#该文件是用于记录上次读取日志文件的位置,执行脚本的用户要有创建该文件的权限
last_position_logfile = '/usr/tools/qybd/tomcat/logs/last_position.txt'

#匹配的错误信息关键字的正则表达式
pattern = compile(r'Exception|^\t+\bat\b',IGNORECASE)

#发送邮件函数
def send_mail(error):

#定义邮件的头部信息  
#连接SMTP服务器,然后发送信息  
smtp = SMTP_SSL(mail_info["hostname"])  
smtp.set_debuglevel(1)  
smtp.ehlo(mail_info["hostname"])  
smtp.login(mail_info["username"], mail_info["password"])  
msg = MIMEText(error, "plain", mail_info["mail_encoding"])  
msg["Subject"] = Header(mail_info["mail_subject"], mail_info["mail_encoding"])  
msg["from"] = mail_info["from"]  
msg["to"] = mail_info["to"]  
smtp.sendmail(mail_info["from"], mail_info["to"], msg.as_string())  
smtp.quit()  

#读取上一次日志文件的读取位置
def get_last_position(file):
try:
data = open(file,'r')
last_position = data.readline()
if last_position:
last_position = int(last_position)
else:
last_position = 0
except:
last_position = 0

return last_position  

#写入本次日志文件的本次位置
def write_this_position(file,last_positon):
try:
data = open(file,'w')
data.write(str(last_positon))
data.write('\n' + "Don't Delete This File,It is Very important for Looking Tomcat Error Log !! \n")
data.close()
except:
print "Can't Create File !" + file
exit()

#分析文件找出异常的行
def analysis_log(file):

error_list = []                                         #定义一个列表,用于存放错误信息.  
try:  
    data = open(file,'r')  
except:  
    exit()  
last_position = get_last_position(last_position_logfile) #得到上一次文件指针在日志文件中的位置  
this_postion = getsize(tomcat_log)                      #得到现在文件的大小,相当于得到了文件指针在末尾的位置  
if this_postion < last_position:                        #如果这次的位置 小于 上次的位置说明 日志文件轮换过了,那么就从头开始  
    data.seek(0)  
elif this_postion == last_position:                     #如果这次的位置 等于 上次的位置 说明 还没有新的日志产生  
    exit()  
elif this_postion > last_position:                      #如果是大于上一次的位置,就移动文件指针到上次的位置  
    data.seek(last_position)  

for line in data:  
    if pattern.search(line):  
        error_list.append(line)  
write_this_position(last_position_logfile,data.tell())  #写入本次读取的位置  
data.close()  

return ''.join(error_list)                              #形成一个字符串  

#调用发送邮件函数发送邮件
error_info = analysis_log(tomcat_log)
if error_info:
send_mail(error_info)
5、添加crontab定时任务:
/10 * python /usr/tools/qybd/cmd/tomcat_log_error_analysis.py >> /usr/tools/qybd/cmd/crontest.py.log 2>&1

效果如下:

猜你喜欢

转载自blog.51cto.com/13855417/2136960