jugadengg
jugadengg

Reputation: 99

Detect if MongoDB is started or not, mail the admin if it's not

I have to write a script which will detect if Mongo DB is started or not. Then, if it has stopped, mail the administrator.
I'm very new to Mongo DB, it will be great help if some one can help me out.

Upvotes: 1

Views: 456

Answers (1)

jabaldonedo
jabaldonedo

Reputation: 26582

This script in Python will check if your MongoDB server is running and will send you an alert (you have to configure it):

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'] = '[email protected]'
    msg['To'] = '[email protected]'

    # 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()

Then you can simply add a cron entry that once per minute execute the script:

*/1 * * * *  /python_bin_root/python /your_script_root/script.py

Upvotes: 2

Related Questions