Follow me

Saturday, March 24, 2012

Mongo Monitor Script: Health Check

Description


The Script will search for any working server listed in the mongo.properties. It executes the rs.status() command and check the health of all the server in replication


Step to configure mongo.properties


In mongo.properties we need to set the below properties


<!--[if !supportLists]-->· <!--[endif]-->USERNAME [mongo database user's username]


<!--[if !supportLists]-->· <!--[endif]-->PASSWORD [Password]


<!--[if !supportLists]-->· <!--[endif]-->MONGO_INSTALL_BIN_DIR [Mongo installed bin directory]


<!--[if !supportLists]-->· <!--[endif]-->HOSTNAME [List of all servers (get it by execute rs.status() command on mongo client) separated with ;]


<!--[if !supportLists]-->· <!--[endif]-->DEFAULT_HOST_PORT [Default server setting for health check]


USERNAME=username


PASSWORD=password


MONGO_INSTALL_BIN_DIR=/usr/local/mongo/bin/


DEFAULT_HOST_PORT=hostname:27017


HOSTNAME="host1:27017;host2:27017;host3:27017"


Step to configure mongo-health-chk.sh


Open the mongo-health-chk.sh and correct the path for the mongo.properties


For e.g. . /usr/local/mongo.properties


Setting the CRON job


To execute the script as cron job, run below command on shell prompt


#crontab -e


This will open the cron job file. Make an entry of below cron expression [update the script location path]. This will run the script in every 5 min. you need to pass the log file name/path as parameter while running this script


*/5 0 * * * /path/to/script/mongo-health-chk.sh mongo_health.log




Below is the script







#!/bin/bash


. /home/mongo/mongo.properties


exec >> $1


#################################################################


# CHECK THE HEALTH OF ALL SERVER IN REPLICATION #


# version:- 1.0 #


# Author:- Chandrashekhar Dehankar #


# THIS SCRIPT WILL SEARCH OUT FOR ANY WORKING SERVER LISTED #


# IN mongo.properties. EXECUTES THE rs.status() COMMAND AND #


# */5 * * * * /home/mongo/mongo-health-chk.sh health.log #


# CRON EXPRESSION EVERY 5MIN IS BELOW #


#################################################################


#DATABASE HOST-PORT DEFAULT SETTING


HOST_PORT=$DEFAULT_HOST_PORT


#METHOD TO CHECK SERVER HEALTH


function _do_health_check {


#FIND ANY WORKING SERVER


DATABASE_HOST_SERVER=$HOSTNAME


DATABASE_HOST_SERVER=${DATABASE_HOST_SERVER//;/$'\n'}


for host in $DATABASE_HOST_SERVER



do


#CHECK WEATHER ITS IS UP OR NOT WHAT, NO MATTERS PRIMARY OR SECONDARY


IS_SERVER_UP=$($MONGO_INSTALL_BIN_DIR/mongo $host/admin -u $USERNAME -p $PASSWORD --eval "printjson(db.isMaster().secondary)" )



#FILTER THE RESULT


IS_SERVER_UP=`echo $IS_SERVER_UP grep -o '[^ ]*$'`




if [ "$IS_SERVER_UP" == "true" ] [ "$IS_SERVER_UP" == "false" ]; then




HOST_PORT=$host


break


fi


done



count=0


CURRENT_DATE=`date +%Y%m%d_%H-%M-%S`


echo "--------HEALTH TEST START[$CURRENT_DATE] ----------"



while [ $count -ne -1 ]


do




HOST_NAME=$($MONGO_INSTALL_BIN_DIR/mongo $HOST_PORT/admin -u $USERNAME -p $PASSWORD --eval "printjson(rs.status().members[$count].name)" )


HOST_NAME=`echo $HOST_NAME grep -o '[^ ]*$'`


CHECK_HEALTH=$($MONGO_INSTALL_BIN_DIR/mongo $HOST_PORT/admin -u $USERNAME -p $PASSWORD --eval "printjson(rs.status().members[$count].health)" )


CHECK_HEALTH=`echo $CHECK_HEALTH grep -o '[^ ]*$'`




count=$(( $count + 1 ))


#CHECK ALL THE NODES IN REPLICATION


if [ "$CHECK_HEALTH" != "1" ] && [ "$CHECK_HEALTH" != "0" ]; then


echo "..........All SERVER HEALTH TEST DONE.........."


count="-1"


fi


if [ $count -ne -1 ]; then


echo "----------------------------------------------------------"


if [ "$CHECK_HEALTH" == "1" ]; then


echo $HOST_NAME IS WORKING FINE


else



echo $HOST_NAME IS DOWN


fi


echo "----------------------------------------------------------"


fi


done


}


## check the health


_do_health_check






No comments:

Post a Comment