Python实现对Tomcat的监控

发布时间:2019-08-28 09:12:54编辑:auto阅读(1682)

    Python实现对Tomcat的监控


       
    最近发现tomcat经常会僵死。而PS查看进程的时候进程还在。但不提供服务了。程序的功能:定期对tomcat服务器发送指命,如果得到响应,则服务器正常,否则异常,同时发邮件给相关人员。

    #! /usr/bin/env python
    # -*- coding: gb2312 -*-
    import socket,time
    import re #正则包
    import ElementTree as ET
    import ConfigParser
    import base64,time,smtplib
    from email.MIMEText import MIMEText
    from email.MIMEMultipart import MIMEMultipart

    #import xml.etree.ElementTree as ET
    def WriteLog(filename,tmpstr):
     
       time_str =time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
        logstr =str(tmpstr) + '\n'
        f =open(filename,"a")
       f.write(logstr)
       f.close()
    def ReWriteLog(filename,tmpstr): #用写模式找开文件
        time_str =time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
        logstr =str(tmpstr) + '\n'
        f =open(filename,"w")
       f.write(logstr)
       f.close()
    def getMailConfig(configFile):
       coms=[]
        Config =ConfigParser.ConfigParser()
       Config.read(configFile)
        host =Config.get("FROM","HOST")
        user =Config.get("FROM","USERNAME")
        subject =Config.get("FROM","SUBJECT")
        pwd =base64.b64decode(Config.get("FROM","PASSWORD"))
        commands =Config.items("TO")
        for com incommands:
           coms.append(com[1])
        returnhost,user,pwd,subject,coms
    def SendMail(configFile,sendfile,logFile):
       host,user,pwd,subject,coms=getMailConfig(configFile)
       _tos=""
        for _to incoms:
           _tos = _tos + _to + ";"
        msg =MIMEMultipart()
          # att = MIMEText("test mail")
        att =MIMEText(open(sendfile,'rb').read(),'base64','gb2312')
    #   att["Content-Type"] ='application/octet-stream'
       att["Content-Disposition"]='attachment;filename="'+sendfile+'"'
       #   msg.attach(att)
       #   msg['to']=coms
       msg['from']=user
       msg['to']=_tos
       msg['subject']=subject+" "+time.strftime("%Y-%m-%d",time.localtime())
       msg.attach(att)
        try:
           smtp_svr=smtplib.SMTP()
           smtp_svr.connect(host,"25")
        except(smtplib.SMTPConnectError,socket.error):
           print "connect error"
           WriteLog(logFile,"connect error")
        try:
           smtp_svr.login(user,pwd)
        exceptsmtplib.SMTPAuthenticationError:
           print "authentication error\n"
           WriteLog(logFile,"authentication error")
        try:
           smtp_svr.sendmail(user,coms,msg.as_string())
        except(smtplib.SMTPRecipientsRefused,smtplib.SMTPDataError,smtplib.SMTPServerDisconnected):
           print "send mail error"
           WriteLog(logFile,"send mail error")
       smtp_svr.close
           
    defSendMailForHtml(configFile,tableContent,mailtitle,logFile):
       host,user,pwd,subject,coms=getMailConfig(configFile)
       _tos=""
        for _to incoms:
           _tos = _tos + _to + ";"
        #
    #    smtp_server= smtplib.SMTP(smtp_host)  
        msg =MIMEMultipart()
       html='<html><body>'+tableContent+'</body></html>' 

关键字