- 论坛徽章:
- 0
|
目的:
通过Python 调用ftp smtp 模块进行服务监控并对服务停止停止情况和恢复情况进行邮件通知。
目前遇到的故障:
如果登陆FTP 服务出现异常后,程序可以通过邮件完成对管理员的通知,但是在服务恢复后,程序并不能继续进行服务状况检测,仍判断服务处于故障状况。请各位仁兄帮忙看看,小弟不胜感激……
程序流程图见附件
代码
#!/usr/bin/env python
#coding=gbk
#purpos:To moitor service and need email notice,Notice the master who is the service`s monitor.
#version:1.0
#date:09/04/2008
#writer ayangjing
#mail:yangjing0617@gmail.com
import thread, time
from ftplib import FTP
import smtplib
import time
import os
import sys
server= '127.0.0.1'
server1= '127.0.0.1'
username='diandian'
loginpass='yangjing'
date=time.ctime()
log="c:\py\log.txt"
mail="c:\py\mail.txt"
flag = 0
while True:
i = 0 #服务检测是失败技术
j = 0 # 检查用计时单位
while j<4:
print "test j is %d" %j
try:
ftp=FTP(server)
ftp.login(username,loginpass)
ftp.quit()
time.sleep(5)
print "server %s ok" %time.ctime()
except:
i=i+1
print "failed i = %d " %i
time.sleep(5)
j = j+1
if i==4:
print "failed "
try:
ftp=FTP(server1)
ftp.login(username,loginpass)
ftp.quit()
print "chceking server"
i=0
flag=2
print "erroe checking flag is %d" %flag
print "erroe checking i is %d" %i
except:
pass
if flag==0:
flag = 1
print "failed and send email "
elif i==0:
print "success "
elif flag== 2:
flag = 3
print "recure and send email "
if flag == 1:
smtpServer = 'mx0.qq.com'
fromaddr = 'yangj@ibm.com'
toaddrs = 'admin@qq.com'
subject ='公司网络报警'
txt='%s 无法到达公司网络 ' %date
date=time.ctime()
msg = """FROM: %s\nTO: %s\nSubject: %s\ndate: %s\n\n
%s""" %(fromaddr,toaddrs,subject,date,txt)
server = smtplib.SMTP(smtpServer)
server.sendmail(fromaddr, toaddrs, msg)
server.quit( )
flag = 2
print "flag is %d" %flag
elif flag== 3 :
smtpServer = 'mx0.qq.com'
fromaddr = 'yangj@ibm.com'
toaddrs = 'admin@qq.com'
subject ='公司网络恢复'
txt='%s 已恢复网络 ' %date
date=time.ctime()
msg = """FROM: %s\nTO: %s\nSubject: %s\ndate: %s\n\n
%s""" %(fromaddr,toaddrs,subject,date,txt)
server = smtplib.SMTP(smtpServer)
server.sendmail(fromaddr, toaddrs, msg)
server.quit( )
flag = 0
|
[ 本帖最后由 stionmel 于 2008-9-24 14:28 编辑 ] |
|