我必须编写一个脚本来检测Mongo DB是否已启动。然后,如果它已停止,请邮寄管理员 我对Mongo DB很新,如果有人可以帮助我,那将会很有帮助。
答案 0 :(得分:2)
Python 中的此脚本将检查您的MongoDB服务器是否正在运行并向您发送警报(您必须对其进行配置):
import smtplib
from email.mime.text import MIMEText
from pymongo import MongoClient
client = MongoClient()
try:
client = MongoClient('localhost', 27017)
except Exception as err:
msg = MIMEText("Mongo is down\nError:%s" % err)
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = 'admin@domain.com'
msg['To'] = 'sysadminguy@domain.com'
# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
然后您只需添加一个 cron 条目,每分钟执行一次脚本:
*/1 * * * * /python_bin_root/python /your_script_root/script.py