Skip to content

Instantly share code, notes, and snippets.

@ramezrafla
Created December 19, 2018 16:15
Show Gist options
  • Save ramezrafla/3999d6920a7f2010dc72ee6e76049241 to your computer and use it in GitHub Desktop.
Save ramezrafla/3999d6920a7f2010dc72ee6e76049241 to your computer and use it in GitHub Desktop.
#!/bin/bash
function usage {
echo "$(basename $0) usage: "
echo " -u username Example: root"
echo " -p password Example: 1234"
echo " -h host Example: localhost"
echo " -d database Example: local"
echo " -q query Example: db.test.findOne()"
echo ""
exit 1
}
while [[ $# -gt 1 ]]
do
key="$1"
case $key in
-u)
USER="$2"
shift
;;
-p)
PASS="$2"
shift
;;
-h)
HOST="$2"
shift
;;
-d)
DATABASE="$2"
shift
;;
-q)
QUERY="$2"
shift
;;
*)
usage
shift
;;
esac
shift
done
[ ! -z ${HOST} ] && \
[ ! -z ${DATABASE} ] && \
[ ! -z ${QUERY} ] || usage
if [[ ${USER} == "" || ${PASS} == "" ]]
then
MONGO="$(which mongo) --host ${HOST} ${DATABASE}"
else
MONGO="$(which mongo) -u ${USER} -p ${PASS} --authenticationDatabase ${DATABASE} --host ${HOST} ${DATABASE} --ssl --sslAllowInvalidCertificates"
fi
MONGO_CHECK=$(echo "${QUERY}" | ${MONGO})
if [ $? -ne 0 ]
then
echo "CRITICAL - Failed to execute query ${QUERY} on MongoDB host ${HOST}"
exit 2
else
echo "OK - MongoDB"
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment