mongo-healthcheck.sh 796 B

12345678910111213141516171819202122232425262728
  1. #!/bin/bash
  2. if [[ -n $1 ]]; then
  3. myhost=$1
  4. else
  5. myhost=localhost:27017
  6. fi
  7. 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`
  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