#!/bin/bash containerstart=`stat -c %Z /proc/1` curtime=`date +%s` if (( curtime-containerstart < 180 )); then echo "0" exit 0 fi if [[ -n $1 ]]; then myhost=$1 else myhost=localhost:27017 fi state=`mongo --host $myhost --quiet --eval 'var i=0; rs.status().members.forEach( function(myDoc) { if (myDoc.self==true) { print(myDoc.stateStr); i++;} })' |tail -1` if [[ $state == "PRIMARY" ]];then echo "0" exit 0 fi if [[ $state == "SECONDARY" ]];then date=`mongo --host $myhost --eval 'rs.status().date;' |tail -1` dates=`date -d "$date" "+%s"` lastHeartbeat=`mongo --host $myhost --quiet --eval 'var i=0; rs.status().members.forEach( function(myDoc) { if (myDoc.stateStr=="PRIMARY") { print(myDoc.lastHeartbeat); i++;} })' |tail -1` if [[ -z "${lastHeartbeat}" ]]; then echo "Zero HartBeat - no PRIMARY !!!" exit 1 fi datelh=`date -d "$lastHeartbeat" "+%s"` if [[ $(($dates-$datelh)) -le "60" ]]; then echo "0" exit 0 fi fi echo "1" exit 1