#!/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 #writerayangjing #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 |
warmning.png (41.34 KB, 下载次数: 108)
原帖由 moonranger 于 2008-9-24 14:29 发表
下面两段代码:
ftp=FTP(server1)
ftp.login(username,loginpass)
ftp.quit()
还有
smtpServer = 'mx0.qq.com'
froma ...
原帖由 luffy.deng 于 2008-9-24 14:33 发表
if i==4:
print "failed "
try:
ftp=FTP(server1)
ftp.login(username,loginpass)
ftp.quit()
...
原帖由 luffy.deng 于 2008-9-24 14:43 发表
我说的是这种情况
if i==4:-----------成立
print "failed "
try: ---------------- 此处失败接跳到except
............................
...
原帖由 stionmel 于 2008-9-23 19:14 发表
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().
原帖由 stionmel 于 2008-9-25 08:14 发表
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().
这部分服务检测程序,在系统服务恢复后不再执行try部分……。
原帖由 stionmel 于 2008-9-24 14:53 发表
关于邮件发送状态标志做了一个新的定义
0 初始
1 启用故障邮件发送程序
2 故障邮件已发送
3 进入恢复发送程序
4 恢复邮件已发送
原帖由 luffy.deng 于 2008-9-27 19:34 发表
这个定义还真是复杂。下面是我的想法仅供参考
def test():
...
flag=0
while True:
if 0 == flag:
if test():
flag=0
else:
flag=1
elif 1== ...
原帖由 yhmin 于 2009-6-16 14:26 发表
偷偷传授你一个小技巧,注册一个139手机邮箱,开通免费邮件短信提醒。。。再把告警邮件发到这个邮箱。。。
我试过了,效果不错,用来做监控非常好。
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |