mongo-healthcheck.sh 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/bin/bash
  2. containerstart=`stat -c %Z /proc/1`
  3. curtime=`date +%s`
  4. if (( curtime-containerstart < 180 )); then
  5. echo "0"
  6. exit 0
  7. fi
  8. if [[ -n $1 ]]; then
  9. myhost=$1
  10. else
  11. myhost=localhost:27017
  12. fi
  13. 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`
  14. if [[ $state == "PRIMARY" ]];then
  15. echo "0"
  16. exit 0
  17. fi
  18. if [[ $state == "SECONDARY" ]];then
  19. date=`mongo --host $myhost --eval 'rs.status().date;' |tail -1`
  20. dates=`date -d "$date" "+%s"`
  21. 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`
  22. if [[ -z "${lastHeartbeat}" ]]; then
  23. echo "Zero HartBeat - no PRIMARY !!!"
  24. exit 1
  25. fi
  26. datelh=`date -d "$lastHeartbeat" "+%s"`
  27. if [[ $(($dates-$datelh)) -le "60" ]]; then
  28. echo "0"
  29. exit 0
  30. fi
  31. fi
  32. echo "1"
  33. exit 1