gfdxy3322 发表于 2015-11-25 10:16:09

zabbix_sendmail.py

#!/usr/bin/python#coding:utf-8
import smtplibfrom email.mime.text import MIMETextimport osimport argparseimport loggingimport datetime mail_host = 'smtp.163.com'mail_user = 'monitor_itnihao'mail_pass = '123456'mail_postfix = '163.com' #mail_host='smtp.qq.com'#mail_user='itnihao'#mail_pass='123456'#mail_postfix='qq.com' def send_mail(mail_to,subject,content):    me = mail_user&#43;&quot;<&quot;&#43;mail_user&#43;&quot;@&quot;&#43;mail_postfix&#43;&quot;>&quot;    msg = MIMEText(content)    msg['Subject'] = subject    msg['From'] = me    msg['to'] = mail_to    global sendstatus    global senderr         try:      smtp = smtplib.SMTP()      smtp.connect(mail_host)      smtp.login(mail_user,mail_pass)      smtp.sendmail(me,mail_to,msg.as_string())      smtp.close()      print 'send ok'      sendstatus = True    except Exception,e:      senderr=str(e)      print senderr      sendstatus = False   def logwrite(sendstatus,mail_to,content):    logpath='/var/log/zabbix/alert'   if not sendstatus:      content = senderr   if not os.path.isdir(logpath):      os.makedirs(logpath)   t=datetime.datetime.now()    daytime=t.strftime('%Y-%m-%d')    daylogfile=logpath&#43;'/'&#43;str(daytime)&#43;'.log'    logging.basicConfig(filename=daylogfile,level=logging.DEBUG)    os.system('chown zabbix.zabbix {0}'.format(daylogfile))    logging.info('*'*130)    logging.debug(str(t)&#43;' mail send to {0},content is :\n {1}'.format(mail_to,content))if __name__ == &quot;__main__&quot;:    parser = argparse.ArgumentParser(description='Send mail to user for zabbix alerting')    parser.add_argument('mail_to',action=&quot;store&quot;, help='The address of the E-mail that send to user ')    parser.add_argument('subject',action=&quot;store&quot;, help='The subject of the E-mail')    parser.add_argument('content',action=&quot;store&quot;, help='The content of the E-mail')    args = parser.parse_args()    mail_to=args.mail_to    subject=args.subject    content=args.content   send_mail(mail_to,subject,content)    logwrite(sendstatus,mail_to,content)
页: [1]
查看完整版本: zabbix_sendmail.py