• monitor tomcat sctips


    #!/usr/bin/python
    #coding=utf-8  
    import datetime 
    from subprocess import Popen, PIPE
    import os
    import smtplib
    from email.mime.text import MIMEText
    from email.header import Header
    import commands
    
    class TomcatMonitor:
        def __init__(self):
            self.today = datetime.datetime.now().strftime("%Y-%m-%d")
            self.daybefyesday = (datetime.datetime.now()-datetime.timedelta(days =2)).strftime("%Y-%m-%d")
            self.tomcatLogPath = '/xebest/tomcat/logs/catalina.%s.out' % self.today
            self.mailList = '/opt/scripts/maillist/mail-%s.log' % self.today
            self.oldMailList = '/opt/scripts/maillist/mail-%s.log' % self.daybefyesday
            self.sender = '123123@qq.com'
            self.receiver =  [ '1231231231@qq.com', ]
            self.subject = '{{ id }} tomcat expect notify'
            self.smtpserver = 'smtp.qq.com'
            self.username = '1231231231@qq.com'
            self.password = '123123123'
            self.content = ''
        def collectTomcatInfo(self):
            
            self.ps = Popen("grep -n 'Exception:' %s" % self.tomcatLogPath, shell=True, stdout=PIPE, stderr=PIPE)
            self.output_lines = self.ps.stdout.readlines()
            
            #for line in output_lines:
            #    print ('|').join(line.strip().split(":",1))
        
        def contrastInfo(self):
            if os.path.exists(self.mailList):
                self.infoInFile = []
                with open(self.mailList) as f:
                    for l in f:
                        self.infoInFile.append(l.strip().split('|')[0])
                with open(self.mailList,'a') as f:
                    for line in self.output_lines:
                        if line.strip().split(":",1)[0] in self.infoInFile:
                            continue
                        else:
                            f.write(('|').join(line.strip().split(":",1))+"|"+self.tomcatLogPath+"|"+"0")
                            f.write('
    ') 
            else:
                with open(self.mailList,'w') as f:
                    for line in self.output_lines:
                        self.temp = ('|').join(line.strip().split(":",1))+"|"+self.tomcatLogPath+"|"+"0"
                        self.temp += '
    '
                        f.write(self.temp)
                    
        def useQqSendMail(self):
            msg = MIMEText( self.content.encode('utf8'), _charset = 'utf8')
            msg['From'] = self.sender
            msg['Subject'] = u'%s' % self.subject
            msg['To'] = ",".join( self.receiver )
    
            try:
                s = smtplib.SMTP_SSL( self.smtpserver, 465 )
                #s.set_debuglevel(1)
                #s.connect(mail_host)
                s.login(self.username, self.password)
                s.sendmail(self.username,self.receiver , msg.as_string())
                s.close()
                return 0
            except Exception as e:
                #print 'Exception: ', e
                return 1
    
    
                
        def sendMail(self):
            writeBackToFile = ''
            with open(self.mailList) as f:
                for l in f:
                    l = l.strip().split("|")
                    if l[3] == '0':
                        self.content = self.content + ('	').join(l) + '
    '
                        l[3] = '1'
                    writeBackToFile = writeBackToFile + ('|').join(l) + '
    '
                          
                    
            if len(self.content) != 0:
              #  cmd = "echo '%s' | /bin/mail -s '%s' %s" % (self.content,self.subject,self.receiver) 
               # status,result = commands.getstatusoutput(cmd)
                status = self.useQqSendMail()
                if status == 0:
                    with open(self.mailList,'w') as f:
                        f.write(writeBackToFile)
                
                        
            
           # self.ps = Popen("echo %s | mail -s %s %s" % (self.content,self.subject,self.receiver), shell=True, stdout=PIPE, stderr=PIPE)
        def cleanOldFile(self):
            if os.path.exists(self.oldMailList):
                os.remove(self.oldMailList)
    
         
        def run(self):
            self.collectTomcatInfo()
            self.contrastInfo()
            self.sendMail()
            self.cleanOldFile()
      
    
    if __name__ == '__main__':
        tomcat = TomcatMonitor()
        tomcat.run()
  • 相关阅读:
    分支与合并@基础
    创业者那些鲜为人知的事情
    centos7.5+nginx+php急速配置
    通过webhost扩展方式初始化EFCore数据库
    AspNetCore架构图
    Linux就该这样学--之常用linux命令及bash基础
    使用gitlab构建基于docker的持续集成(三)
    使用gitlab构建基于docker的持续集成(二)
    使用gitlab构建基于docker的持续集成(一)
    使用docker-compose配置mysql数据库并且初始化用户
  • 原文地址:https://www.cnblogs.com/zhaobin022/p/5057184.html
Copyright © 2020-2023  润新知