标签:
#!/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(‘\n‘) 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 += ‘\n‘ 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 + (‘\t‘).join(l) + ‘\n‘ l[3] = ‘1‘ writeBackToFile = writeBackToFile + (‘|‘).join(l) + ‘\n‘ 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()
标签:
原文地址:http://www.cnblogs.com/zhaobin022/p/5057184.html