Python实现对Tomcat的监控
短信预约 -IT技能 免费直播动态提醒
Python实现对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>'
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>'
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341