生活随笔
收集整理的這篇文章主要介紹了
zabbix邮件报警配合logging模块排错的python脚本
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
代碼如下:記錄排錯日志:
#?!/usr/bin/env?python
#?-*-?coding=utf-8?-*-
import?smtplib
from?email.mime.text?import?MIMEText
import?sys
import?logging
import?timesender?=?'shiyiguo@yijiaoyuan.net'
smtp_server?=?'smtp.exmail.qq.com'
username?=?sender[:]
password?=?'Yjy@yunwei123'
log_format?=?time.strftime("%Y%m%d")
time_format?=?time.strftime("%Y-%m-%d?%H:%M:%S")if?__name__?==?'__main__':logging.basicConfig(level=logging.DEBUG,?filename?=?'/tmp/smail'?+?log_format?+?'.log')logging.info(time_format?+?':?starting?sendmail?record')try:receiver?=?sys.argv[1]except?Exception,?e:print?efinally:logging.debug('receiver:\n\r\t'?+?receiver)try:subject?=?sys.argv[2]except?Exception,?e:print?efinally:logging.debug('subject:\n\r\t'?+?subject)try:content?=?sys.argv[3]except?Exception,?e:print?efinally:logging.debug('content:\n\r\t'?+?content)msg?=?MIMEText(content,?'plain',?'utf-8')smtp?=?smtplib.SMTP()smtp.connect(smtp_server)msg['Subject']?=?subjecttry:smtp.login(username,?password)try:print?receiver,sender,subject,msgsmtp.sendmail(sender,?receiver?,?msg.as_string())except?Exception,?e:print?elogging.debug(time_format?+?":\t"?+?str(e))except?Exception,?e:print?elogging.info(time_format?+?'\terror?message:\t'?+?str(e))logging.debug("\rending?logging?record.......\r\n")smtp.quit()
轉載于:https://blog.51cto.com/shiyiguo/1853808
總結
以上是生活随笔為你收集整理的zabbix邮件报警配合logging模块排错的python脚本的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。