Log rotation for tendermint and BigchainDB using Monit

This commit is contained in:
Ahmed Muawia Khan 2018-09-10 10:29:30 +02:00
parent dd84d4eb6f
commit d8f5d72127

View File

@ -4,7 +4,7 @@ set -o nounset
# Check if directory for monit logs exists # Check if directory for monit logs exists
if [ ! -d "$HOME/.bigchaindb-monit" ]; then if [ ! -d "$HOME/.bigchaindb-monit" ]; then
mkdir -p "$HOME/.bigchaindb-monit" mkdir -p "$HOME/.bigchaindb-monit"
fi fi
monit_pid_path=${MONIT_PID_PATH:=$HOME/.bigchaindb-monit/monit_processes} monit_pid_path=${MONIT_PID_PATH:=$HOME/.bigchaindb-monit/monit_processes}
@ -13,7 +13,7 @@ monit_log_path=${MONIT_LOG_PATH:=$HOME/.bigchaindb-monit/logs}
monitrc_path=${MONITRC_PATH:=$HOME/.monitrc} monitrc_path=${MONITRC_PATH:=$HOME/.monitrc}
function usage() { function usage() {
cat <<EOM cat <<EOM
Usage: ${0##*/} [-h] Usage: ${0##*/} [-h]
@ -45,45 +45,45 @@ EOM
} }
while [[ $# -gt 0 ]]; do while [[ $# -gt 0 ]]; do
arg="$1" arg="$1"
case $arg in case $arg in
--monit-pid-path) --monit-pid-path)
monit_pid_path="$2" monit_pid_path="$2"
shift shift
;; ;;
--monit-script-path) --monit-script-path)
monit_script_path="$2" monit_script_path="$2"
shift shift
;; ;;
--monit-log-path) --monit-log-path)
monit_log_path="$2" monit_log_path="$2"
shift shift
;; ;;
--monitrc-path) --monitrc-path)
monitrc_path="$2" monitrc_path="$2"
shift shift
;; ;;
-h|--help) -h | --help)
usage usage
exit exit
;; ;;
*) *)
echo "Unknown option: $1" echo "Unknown option: $1"
usage usage
exit 1 exit 1
;; ;;
esac esac
shift shift
done done
# Check if directory for monit logs exists # Check if directory for monit logs exists
if [ ! -d "$monit_log_path" ]; then if [ ! -d "$monit_log_path" ]; then
mkdir -p "$monit_log_path" mkdir -p "$monit_log_path"
fi fi
# Check if directory for monit pid files exists # Check if directory for monit pid files exists
if [ ! -d "$monit_pid_path" ]; then if [ ! -d "$monit_pid_path" ]; then
mkdir -p "$monit_pid_path" mkdir -p "$monit_pid_path"
fi fi
cat >${monit_script_path} <<EOF cat >${monit_script_path} <<EOF
@ -93,7 +93,7 @@ case \$1 in
start_bigchaindb) start_bigchaindb)
pushd \$4 pushd \$4
nohup bigchaindb -l DEBUG start >> \$3/bigchaindb.out.log 2>> \$3/bigchaindb.err.log & nohup bigchaindb -l DEBUG start > /dev/null 2>&1 &
echo \$! > \$2 echo \$! > \$2
popd popd
@ -110,6 +110,7 @@ case \$1 in
start_tendermint) start_tendermint)
pushd \$4 pushd \$4
nohup tendermint node --consensus.create_empty_blocks=false >> \$3/tendermint.out.log 2>> \$3/tendermint.err.log & nohup tendermint node --consensus.create_empty_blocks=false >> \$3/tendermint.out.log 2>> \$3/tendermint.err.log &
echo \$! > \$2 echo \$! > \$2
@ -129,21 +130,37 @@ exit 0
EOF EOF
chmod +x ${monit_script_path} chmod +x ${monit_script_path}
cat >${monit_script_path}_logrotate <<EOF
#!/bin/bash
case \$1 in
rotate_tendermint_logs)
/bin/mv \$2 \$2.\$(date +%y-%m-%d)
/bin/tar -cvf \$2.\$(date +%Y%m%d_%H%M%S).tar.gz \$2.\$(date +%y-%m-%d)
/bin/rm \$2.\$(date +%y-%m-%d)
kill -2 \$(cat \$3)
;;
esac
exit 0
EOF
chmod +x ${monit_script_path}_logrotate
# Handling overwriting of control file interactively # Handling overwriting of control file interactively
if [ -f "$monitrc_path" ]; then if [ -f "$monitrc_path" ]; then
echo "$monitrc_path already exists." echo "$monitrc_path already exists."
read -p "Overwrite[Y]? " -n 1 -r read -p "Overwrite[Y]? " -n 1 -r
echo echo
if [[ $REPLY =~ ^[Yy]$ ]]; then if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Overriding $monitrc_path/.monitrc" echo "Overriding $monitrc_path"
else else
read -p "Enter absolute path to store Monit control file: " monitrc_path read -p "Enter absolute path to store Monit control file: " monitrc_path
eval monitrc_path="$monitrc_path" eval monitrc_path="$monitrc_path"
if [ ! -d "$(dirname $monitrc_path)" ]; then if [ ! -d "$(dirname $monitrc_path)" ]; then
echo "Failed to save monit control file '$monitrc_path': No such file or directory." echo "Failed to save monit control file '$monitrc_path': No such file or directory."
exit 1 exit 1
fi fi
fi fi
fi fi
# configure monitrc # configure monitrc
@ -161,9 +178,17 @@ check process bigchaindb
check process tendermint check process tendermint
with pidfile ${monit_pid_path}/tendermint.pid with pidfile ${monit_pid_path}/tendermint.pid
start program "${monit_script_path} start_tendermint ${monit_pid_path}/tendermint.pid ${monit_log_path} ${monit_log_path}" start program "${monit_script_path} start_tendermint ${monit_pid_path}/tendermint.pid ${monit_log_path} ${monit_log_path}"
restart program "${monit_script_path} start_bigchaindb ${monit_pid_path}/bigchaindb.pid ${monit_log_path} ${monit_log_path}" restart program "${monit_script_path} start_tendermint ${monit_pid_path}/tendermint.pid ${monit_log_path} ${monit_log_path}"
stop program "${monit_script_path} stop_tendermint ${monit_pid_path}/tendermint.pid ${monit_log_path} ${monit_log_path}" stop program "${monit_script_path} stop_tendermint ${monit_pid_path}/tendermint.pid ${monit_log_path} ${monit_log_path}"
depends on bigchaindb depends on bigchaindb
check file tendermint.out.log with path ${monit_log_path}/tendermint.out.log
if size > 100 KB then
exec "${monit_script_path}_logrotate rotate_tendermint_logs ${monit_log_path}/tendermint.out.log $monit_pid_path/tendermint.pid"
check file tendermint.err.log with path ${monit_log_path}/tendermint.err.log
if size > 100 KB then
exec "${monit_script_path}_logrotate rotate_tendermint_logs ${monit_log_path}/tendermint.err.log $monit_pid_path/tendermint.pid"
EOF EOF
# Setting permissions for control file # Setting permissions for control file