欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python脚本监控Tomcat服务器的方法

程序员文章站 2023-11-12 08:29:58
文章出处: 作者:朱培      id:sdksdk0     ----...

文章出处:

作者:朱培      id:sdksdk0    
--------------------------------------------------------------------------------------------

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

1、监控tomcat是否挂掉

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": "info@sogoucloud.cn", 
 "to": "zhupei@sogoucloud.cn", 
 "hostname": "smtp.exmail.qq.com", 
 "username": "info@sogoucloud.cn", 
 "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、监控日志的脚本

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": "info@sogoucloud.cn", 
 "to": "zhupei@sogoucloud.cn", 
 "hostname": "smtp.exmail.qq.com", 
 "username": "info@sogoucloud.cn", 
 "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

效果如下:

python脚本监控Tomcat服务器的方法

总结

以上所述是小编给大家介绍的python脚本监控tomcat服务器的方法,希望对大家有所帮助