Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Simple monit script for mongodb
check host mongodb with address localhost
start program = "/usr/bin/sudo /opt/database/mongo/bin/mongod"
stop program = "/usr/bin/sudo /usr/bin/pkill -f mongod"
if failed port 28017 protocol HTTP
request /
with timeout 10 seconds
then start
@ptecza

This comment has been minimized.

Show comment Hide comment
@ptecza

ptecza Mar 17, 2011

Very tricky way :) Thanks a lot for the inspiration! I'm going to use it for my Monit.

ptecza commented Mar 17, 2011

Very tricky way :) Thanks a lot for the inspiration! I'm going to use it for my Monit.

@ptecza

This comment has been minimized.

Show comment Hide comment
@ptecza

ptecza Mar 17, 2011

And this is my Monit config for MongoDB on Ubuntu box (it should work for Debian too):

check process mongodb with pidfile /db/path/mongod.lock
   group database
   start program = "/etc/init.d/mongodb start"
   stop  program = "/etc/init.d/mongodb stop"
   if failed host 127.0.0.1 port 28017 protocol http
     and request "/" with timeout 10 seconds then restart
   if 5 restarts within 5 cycles then timeout

The biggest difference is that I used also MongoDB PID file. I'm sharing it, because I hope it can be useful for other people as well.

ptecza commented Mar 17, 2011

And this is my Monit config for MongoDB on Ubuntu box (it should work for Debian too):

check process mongodb with pidfile /db/path/mongod.lock
   group database
   start program = "/etc/init.d/mongodb start"
   stop  program = "/etc/init.d/mongodb stop"
   if failed host 127.0.0.1 port 28017 protocol http
     and request "/" with timeout 10 seconds then restart
   if 5 restarts within 5 cycles then timeout

The biggest difference is that I used also MongoDB PID file. I'm sharing it, because I hope it can be useful for other people as well.

@antani

This comment has been minimized.

Show comment Hide comment
@antani

antani Jul 6, 2011

I cant get this above script working on Ubuntu VPS. My mongodb has been changed to start/stop as service and not as /etc/init.d - is there a way to fix this ?

antani commented Jul 6, 2011

I cant get this above script working on Ubuntu VPS. My mongodb has been changed to start/stop as service and not as /etc/init.d - is there a way to fix this ?

@ptecza

This comment has been minimized.

Show comment Hide comment
@ptecza

ptecza Jul 6, 2011

Sorry, but I'm not too experienced with Ubuntu Upstart, so probably I can't help you a lot...

I guess you can use something like below for starting/stopping your MongoDB:

start program = "/sbin/start mongodb"
stop  program = "/sbin/stop mongodb"

But I don't have an idea about MongoDB PID file in your scenario...

ptecza commented Jul 6, 2011

Sorry, but I'm not too experienced with Ubuntu Upstart, so probably I can't help you a lot...

I guess you can use something like below for starting/stopping your MongoDB:

start program = "/sbin/start mongodb"
stop  program = "/sbin/stop mongodb"

But I don't have an idea about MongoDB PID file in your scenario...

@lpiwowarek

This comment has been minimized.

Show comment Hide comment
@lpiwowarek

lpiwowarek Jan 18, 2012

I tried ptecza's way on my CentOS system but Monit claims Mongo is not running (even though it does). I'm shooting in the dark that Monit cannot check contents of /var/lib/mongo/mongo.lock because the file is executable by Mongo. Any ideas?

I tried ptecza's way on my CentOS system but Monit claims Mongo is not running (even though it does). I'm shooting in the dark that Monit cannot check contents of /var/lib/mongo/mongo.lock because the file is executable by Mongo. Any ideas?

@lpiwowarek

This comment has been minimized.

Show comment Hide comment
@lpiwowarek

lpiwowarek Jan 19, 2012

So after poking about I just added 3 lines to the start part of the MongoDB's init script:

sleep 5
cp $DBPATH/mongo.lock /var/run/mongo.pid
chmod -x /var/run/mongod.pid

Then pointed Monit at the new PID file. Also added line to remove the PID file when Mongo is stopped.

So after poking about I just added 3 lines to the start part of the MongoDB's init script:

sleep 5
cp $DBPATH/mongo.lock /var/run/mongo.pid
chmod -x /var/run/mongod.pid

Then pointed Monit at the new PID file. Also added line to remove the PID file when Mongo is stopped.

@virajp

This comment has been minimized.

Show comment Hide comment
@virajp

virajp Apr 29, 2013

Does this work even if the MongoDB requires authentication ?

virajp commented Apr 29, 2013

Does this work even if the MongoDB requires authentication ?

@dzamosenchuk

This comment has been minimized.

Show comment Hide comment
@dzamosenchuk

dzamosenchuk Jan 23, 2014

I have problem. I try to use this config for monitoring my mongo service. But my mongo server has authentication and monit is failed. I've tried to use next variant:
if failed url http://admin:password123@mongo.server.com:28017/ for 1 cycles then alert
But it didn't work.
Do You have any ideas for fix it?

I have problem. I try to use this config for monitoring my mongo service. But my mongo server has authentication and monit is failed. I've tried to use next variant:
if failed url http://admin:password123@mongo.server.com:28017/ for 1 cycles then alert
But it didn't work.
Do You have any ideas for fix it?

@artdokxxx

This comment has been minimized.

Show comment Hide comment
@artdokxxx

artdokxxx Feb 12, 2015

Check port 27017 ; ) Ex.: if failed host x.x.x.x port 27017

Check port 27017 ; ) Ex.: if failed host x.x.x.x port 27017

@rseward

This comment has been minimized.

Show comment Hide comment
@rseward

rseward Aug 30, 2016

Centos 7 - mongod monit check snippit.

check process mongodb with pidfile /var/lib/mongo/mongod.lock
group database
start program = "/bin/systemctl start mongod"
stop program = "/bin/systemctl stop mongod"
if failed host 127.0.0.1 port 28017 protocol http
and request "/" with timeout 10 seconds then restart
if 5 restarts within 5 cycles then timeout

rseward commented Aug 30, 2016

Centos 7 - mongod monit check snippit.

check process mongodb with pidfile /var/lib/mongo/mongod.lock
group database
start program = "/bin/systemctl start mongod"
stop program = "/bin/systemctl stop mongod"
if failed host 127.0.0.1 port 28017 protocol http
and request "/" with timeout 10 seconds then restart
if 5 restarts within 5 cycles then timeout

@madhavkaneriya

This comment has been minimized.

Show comment Hide comment
@madhavkaneriya

madhavkaneriya Mar 13, 2018

My mongodb has been authenticated and monit is giving "failed to connect" error.
I am using following config,

check process mongodb matching "mongodb"
start program = "/usr/bin/sudo /bin/systemctl start mongodb"
stop program = "/usr/bin/sudo /bin/systemctl stop mongodb"
if failed url http://username:password@127.0.0.1:27017/authDb with timeout 10 seconds then restart
if 5 restarts within 5 cycles then timeout

@dzamosenchuk @virajp , Did any of you find any solution, Please let me know I am having same kind of issue.

My mongodb has been authenticated and monit is giving "failed to connect" error.
I am using following config,

check process mongodb matching "mongodb"
start program = "/usr/bin/sudo /bin/systemctl start mongodb"
stop program = "/usr/bin/sudo /bin/systemctl stop mongodb"
if failed url http://username:password@127.0.0.1:27017/authDb with timeout 10 seconds then restart
if 5 restarts within 5 cycles then timeout

@dzamosenchuk @virajp , Did any of you find any solution, Please let me know I am having same kind of issue.

@saiakash

This comment has been minimized.

Show comment Hide comment
@saiakash

saiakash May 10, 2018

I succesfully configured my monit and it works fine.I think it will help someone.

check process mongodb with pidfile /var/lib/mongodb/mongod.lock

group database

start program = "/bin/systemctl start mongodb"
stop program = "/bin/systemctl stop mongodb"

if failed host localhost port 27017 then restart

I succesfully configured my monit and it works fine.I think it will help someone.

check process mongodb with pidfile /var/lib/mongodb/mongod.lock

group database

start program = "/bin/systemctl start mongodb"
stop program = "/bin/systemctl stop mongodb"

if failed host localhost port 27017 then restart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment