MongoDB Log Switch Script
Today I want to talk about
log switching.
Having been managing MongoDB databases and MongoDB BI Connector for quite a while now. I can't put enough appreciation of how the logs have helped of making my life so much easier and they should be the one of the first things we check whenever we encounter any issue in regards to the aforementioned.
If you agree with me on how important the log is, please read on.
Log is definitely your friend, but I know I had made the mistake of leaving it alone for a long while and when I truly needed it, it became a monster that I had to cut and dissect into workable size.
There was one time we had a production issue and I had to look into the issue immediately and of course I went straight to my friend, the logs. I tried looking into the log and the response from my input was extremely sluggish to the point it was almost unworkable. I looked and realized it was because the log had grown to the size that was difficult to work with. After this incidence I decided to finally took some time and wrote a little bash script to always cut the log periodically via crontab.
I wrote the script based on our environment where our MongoDB instances and MongoDB BI Connectors are running inside of containers, so please feel free to modify it however you want.
I think it's pretty self explanatory, but I just want to mention I don't use the logswitch command offered by mongodb as I did run into issues when the log just don't get switched even when the command ran successfully. Instead I do
kill -SIGUSR1 $(ps -p $(pgrep mongosqld) -o pid=)
make sure you switch "mongosqld" to "mongod" when you are working with mongoDB instances.
logswitch.sh
#!/bin/bash
unset http_proxy
unset https_proxy
MONGO_DATA_DIR="/apps"
ds=$(du -s ${MONGO_DATA_DIR}/log/sqld.log )
set -- $ds
ds=$1
dx=$((${ds}/1024))
echo "Current sqld.log size is "${dx}"MB"
cd ${MONGO_DATA_DIR}/log
echo "sqld.log logrotate initiated"
kill -SIGUSR1 $(ps -p $(pgrep mongosqld) -o pid=)
echo "logrotate is completed, checking number of files"
nl=$(ls -altr ${MONGO_DATA_DIR}/log | grep sqld.log | wc -l)
if [ "${nl}" -gt 3 ]; then
cd ${MONGO_DATA_DIR}/log
ls -t ${MONGO_DATA_DIR}/log | grep sqld.log | tail -n +4 | xargs rm -fr
echo "number of sqld.log exceeded 3, anything older than the last 3 files are deleted"
else
echo "number of sqld.log has not exceeded 3"
fi
fName=$(ls -al ${MONGO_DATA_DIR}/log | tail -n 1 | awk '{print $9}')
tar -cvzf ${fName}.${HOSTNAME}.tar.gz ${fName}
rm -fr ${fName}
if [ "${AUTOARCHIVE}" ]; then
sshpass -p xxxxx scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${fName}.${HOSTNAME}.tar.gz xxxxxxxx@xxxxxxxxx:/path
fi
Hope this helps!
Feel free to ask me any question if any!
Comments
Post a Comment