I have a bash script to check a MongoDB database and send an email if certain conditions are met.
Mongo give you the --eval option to return a value. But instead to have something like:
ALERT=true|false
I have:
ALERT= MongoDB shell version: 2.6.1
#!/bin/bash
echo "WatchDog Jerry"
ALERT=$(mongo ob --eval 'var now = new Date().getTime(), alert = false; db.sess.find().forEach(function(sess){ var delay = 1 * 60 * 1000; var ts = sess.ts.toNumber(); if((now - ts) > delay) alert = true;}); print(alert);')
echo "alert: $ALERT"
if [ "$ALERT" == "true" ]; then
mail -s "ALARM - WatchDog Jerry" [email protected] < /root/watchdog/email
fi
Can someone help me? What I need is to assign the js variable 'alarm' to the bash variable ALARM
ALERT=$(mongo ...). But first, get just themongo db --eval ...to return the info you want to see when running from the cmd line. Post separate question with mongoDB error msgs if you can't figure it out by looking at mongo doc or some googling around. Good luck.