用Python写的一个MySQL数据库监测系统
2010-01-05 23:36
309 查看
原理很简单,定期连接数据库,如果出现错误,发送邮件到指定邮箱中。
import MySQLdb
import sys
import os
import time
import smtplib
#该程序用于检测数据库服务器是否允许正常,服务器当机的话会往目的邮箱发送一封邮件
#使用说明:
#1:在服务器数据库上建立数据库test1(可以不含任何表)
#2:使用python monitorDB.py 用户名 密码调用该程序,如果用户名为root,密码为sa,则为python monitorDB.py root sa
def conn(user,pwd):
try:
conn=MySQLdb.connect(host='localhost',user=user,passwd=pwd,db='test1')
return 1
except:
return 0
def send():
mail_server='smtp.gmail.com' #邮件服务器,这里使用gmail
try:
s=smtplib.SMTP(mail_server)
s.starttls()
s.login('XXXXX','XXXXX')#用户名和密码,如果test@gmail.com密码为111.则为s.login('test','111')
s.sendmail('XXXX@gmail.com','XXXXX@gmail.com','DB server crashes!')#参数为发送者,接受者,消息
s.quit
print "mail have sended!"
except:
print "mail send error!"
def monitor(user,pwd):
havesend=0
while True:
time.sleep(60)#每隔60秒检查一次,可以通过括号内的值来改变时间
if conn(user,pwd)==1:
havesend=0
print "server is normally working! "
continue
else:
print "server crashes "
if havesend==0:
havesend=1
send()
continue
elif havesend==1:
continue
if __name__=="__main__":
if not len(sys.argv)==3:
print "username and password"
sys.exit(1)
user=sys.argv[1]
pwd=sys.argv[2]
os.system('cls')
print monitor(user,pwd)
import MySQLdb
import sys
import os
import time
import smtplib
#该程序用于检测数据库服务器是否允许正常,服务器当机的话会往目的邮箱发送一封邮件
#使用说明:
#1:在服务器数据库上建立数据库test1(可以不含任何表)
#2:使用python monitorDB.py 用户名 密码调用该程序,如果用户名为root,密码为sa,则为python monitorDB.py root sa
def conn(user,pwd):
try:
conn=MySQLdb.connect(host='localhost',user=user,passwd=pwd,db='test1')
return 1
except:
return 0
def send():
mail_server='smtp.gmail.com' #邮件服务器,这里使用gmail
try:
s=smtplib.SMTP(mail_server)
s.starttls()
s.login('XXXXX','XXXXX')#用户名和密码,如果test@gmail.com密码为111.则为s.login('test','111')
s.sendmail('XXXX@gmail.com','XXXXX@gmail.com','DB server crashes!')#参数为发送者,接受者,消息
s.quit
print "mail have sended!"
except:
print "mail send error!"
def monitor(user,pwd):
havesend=0
while True:
time.sleep(60)#每隔60秒检查一次,可以通过括号内的值来改变时间
if conn(user,pwd)==1:
havesend=0
print "server is normally working! "
continue
else:
print "server crashes "
if havesend==0:
havesend=1
send()
continue
elif havesend==1:
continue
if __name__=="__main__":
if not len(sys.argv)==3:
print "username and password"
sys.exit(1)
user=sys.argv[1]
pwd=sys.argv[2]
os.system('cls')
print monitor(user,pwd)
相关文章推荐
- 用Python写的一个MySQL数据库监测系统
- Python:通过自定义系统级快捷键来控制程序开始或停止记录日志(使用小技巧解决一个貌似无解的问题)
- 一个基于树莓派和Python的无人机视觉跟踪系统
- 用python 10min手写一个简易的实时内存监控系统
- 使用Python写一个量化股票提醒系统
- 【原创】用python写的一个监测本地进程CPU占用的程序
- Linux下利用 Python连接mysql数据库一个很好的例子
- 初学Python-搞了一个linux用户登录监测小工具
- Python计算一个目录下的所有文件的md5值,在Linux系统下面
- 用python写一个员工管理系统 牛刀小试
- 推荐一个web在线的mysql数据库管理系统
- 从零开始学Python(九):搭建一个基于SMTP的简单邮件预警系统
- Python:通过自定义系统级快捷键来控制程序开始或停止记录日志(使用小技巧解决一个貌似无解的问题)
- Python 实现一个名片管理系统
- 用python写一个简单的推荐系统
- 【python入门】学习字典的随笔,附使用字典做一个学生管理系统
- Cola:一个分布式爬虫框架 - 系统架构 - Python4cn(news, jobs)
- 搭建一个开发Predix软件的Wi dc5f ndows系统(6)配置Python
- window平台上基于python搭建一个堡垒级系统