#!/usr/bin/env python #coding:utf8 import os import time import smtplib from email.mime.text import MIMEText from email import Utils import socket def sendmail(to,subject,content): msg = MIMEText(content) lable_pwd="password" msg['from'] = '****@sina.com' msg['to'] = to msg['subject'] = subject msg['date'] = Utils.formatdate(localtime=1) msg['message-id'] = Utils.make_msgid() try: smtpObj = smtplib.SMTP() smtpObj.connect("smtp.sina.com:25") try: smtpObj.login("****@sina.com",lable_pwd) me = "****@sina.com" smtpObj.sendmail(me,to,msg.as_string()) print "Congratulations !Your mail have been sended Success !" except smtplib.SMTPAuthenticationError,smtplib.SMTPException: print "Login failed ,Please check the username/password." finally: try: smtpObj.close() except smtplib.SMTPException: pass except smtplib.SMTPException,e: print "Error: unable to send email %s" % e def check_server(f): try: fo = open(f) for line in fo: now = str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) line = line.rstrip('\n') print '正在检测%s的80端口是否正常 %s:......' % (line,now) try: s.connect((line,80)) print "地址%s检测正常" % line except socket.error,e: print "地址%s检测挂啦" % line sc = 'The web server %s is down '% line cc = 'The web server %s is down at %s' % (line,now) sendmail('****@****.com',sc,cc) finally: s.close() except IOError,e: print "error: %s" % e finally: try: fo.close() except IOError,e: print "error : %s" % e if __name__=='__main__': check_server('D:/workspace/studd/company_list')
用python实现监控网站状态,并发送告警邮件
精选 转载上一篇:用python检测qq在线状态
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Grafana 告警配置并发送邮件
一.安装并启动sendmailyum install -y sendmailsystemctl start s
grafana 发邮件 编辑页面 配置文件 -
Python3 实现简易ping监控并发送报警邮件
Python3 实现简易ping监控并发送报警邮件
监控 邮件 python