mongo-healthcheck.sh 804 B

123456789101112131415161718192021222324252627282930
  1. if [[ -n $1 ]]; then
  2. myhost=$1
  3. else
  4. myhost=localhost:27017
  5. fi
  6. EVAL="var i=0; rs.status().members.forEach( function(myDoc) { if (myDoc.name==\"$myhost\") { print(myDoc.stateStr); i++;} })"
  7. state=`echo $EVAL | mongo --host $myhost --quiet |tail -1`
  8. if [[ $state == "PRIMARY" ]];then
  9. echo "0"
  10. exit 0
  11. fi
  12. if [[ $state == "SECONDARY" ]];then
  13. date=`mongo --host $myhost --eval 'rs.status().date;' |tail -1`
  14. dates=`date -d "$date" "+%s"`
  15. 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`
  16. datelh=`date -d "$lastHeartbeat" "+%s"`
  17. if [[ $(($dates-$datelh)) -le "60" ]]; then
  18. echo "0"
  19. exit 0
  20. fi
  21. fi
  22. echo "1"
  23. exit 1